include: Add ID2D1PathGeometry1 definition.
[wine.git] / dlls / wined3d / glsl_shader.c
blob82c022e4ba7b8fd623ccb4b775a374868021a8b2
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 static const char *get_info_log_line(const char **ptr)
489 const char *p, *q;
491 p = *ptr;
492 if (!(q = strstr(p, "\n")))
494 if (!*p) return NULL;
495 *ptr += strlen(p);
496 return p;
498 *ptr = q + 1;
500 return p;
503 /* Context activation is done by the caller. */
504 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
506 int length = 0;
507 char *log;
509 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
510 return;
512 if (program)
513 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
514 else
515 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
517 /* A size of 1 is just a null-terminated string, so the log should be bigger than
518 * that if there are errors. */
519 if (length > 1)
521 const char *ptr, *line;
523 log = heap_alloc(length);
524 /* The info log is supposed to be zero-terminated, but at least some
525 * versions of fglrx don't terminate the string properly. The reported
526 * length does include the terminator, so explicitly set it to zero
527 * here. */
528 log[length - 1] = 0;
529 if (program)
530 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
531 else
532 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
534 ptr = log;
535 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
537 WARN("Info log received from GLSL shader #%u:\n", id);
538 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
540 else
542 FIXME("Info log received from GLSL shader #%u:\n", id);
543 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
545 heap_free(log);
549 /* Context activation is done by the caller. */
550 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
552 const char *ptr, *line;
554 TRACE("Compiling shader object %u.\n", shader);
556 if (TRACE_ON(d3d_shader))
558 ptr = src;
559 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
562 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
563 checkGLcall("glShaderSource");
564 GL_EXTCALL(glCompileShader(shader));
565 checkGLcall("glCompileShader");
566 print_glsl_info_log(gl_info, shader, FALSE);
569 /* Context activation is done by the caller. */
570 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
572 GLint i, shader_count, source_size = -1;
573 GLuint *shaders;
574 char *source = NULL;
576 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
577 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
579 ERR("Failed to allocate shader array memory.\n");
580 return;
583 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
584 for (i = 0; i < shader_count; ++i)
586 const char *ptr, *line;
587 GLint tmp;
589 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
591 if (source_size < tmp)
593 heap_free(source);
595 if (!(source = heap_alloc_zero(tmp)))
597 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
598 heap_free(shaders);
599 return;
601 source_size = tmp;
604 FIXME("Shader %u:\n", shaders[i]);
605 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
606 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
607 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
608 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
609 FIXME("\n");
611 ptr = source;
612 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
613 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
614 FIXME("\n");
617 heap_free(source);
618 heap_free(shaders);
621 /* Context activation is done by the caller. */
622 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
624 GLint tmp;
626 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
627 return;
629 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
630 if (!tmp)
632 FIXME("Program %u link status invalid.\n", program);
633 shader_glsl_dump_program_source(gl_info, program);
636 print_glsl_info_log(gl_info, program, TRUE);
639 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
641 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
642 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
643 * versions. */
644 return shader_glsl_get_version(gl_info) >= 140;
647 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
649 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
652 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
653 struct shader_glsl_priv *priv, GLuint program_id,
654 const struct wined3d_shader_reg_maps *reg_maps)
656 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
657 struct wined3d_string_buffer *name;
658 unsigned int i, base, count;
659 GLuint block_idx;
661 if (shader_glsl_use_layout_binding_qualifier(gl_info))
662 return;
664 name = string_buffer_get(&priv->string_buffers);
665 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
666 for (i = 0; i < count; ++i)
668 if (!reg_maps->cb_sizes[i])
669 continue;
671 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
672 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
673 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
675 checkGLcall("glUniformBlockBinding");
676 string_buffer_release(&priv->string_buffers, name);
679 /* Context activation is done by the caller. */
680 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
681 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
682 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
684 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
685 unsigned int i, mapped_unit;
686 GLint name_loc;
688 for (i = 0; i < count; ++i)
690 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
691 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
692 if (name_loc == -1)
693 continue;
695 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
696 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
698 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
699 continue;
702 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
703 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
705 checkGLcall("Load sampler bindings");
706 string_buffer_release(&priv->string_buffers, sampler_name);
709 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
710 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
712 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
713 const unsigned int *tex_unit_map;
714 unsigned int base, count;
716 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
717 if (sampler_idx >= count)
718 return WINED3D_UNMAPPED_STAGE;
719 if (!tex_unit_map)
720 return base + sampler_idx;
721 return tex_unit_map[base + sampler_idx];
724 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
725 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
726 unsigned int sampler_idx)
728 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
729 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
730 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
731 else
732 ERR("Unmapped sampler %u.\n", sampler_idx);
735 /* Context activation is done by the caller. */
736 static void shader_glsl_load_samplers(const struct wined3d_context *context,
737 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
739 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
740 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
741 const struct wined3d_shader_version *shader_version;
742 const unsigned int *tex_unit_map;
743 unsigned int base, count;
744 const char *prefix;
746 if (shader_glsl_use_layout_binding_qualifier(gl_info))
747 return;
749 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
750 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
751 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
752 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
755 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
756 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
758 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
760 if (icb)
762 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
763 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
764 GLint icb_location;
766 string_buffer_sprintf(icb_name, "%s_icb", prefix);
767 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
768 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
769 checkGLcall("Load immediate constant buffer");
771 string_buffer_release(&priv->string_buffers, icb_name);
775 /* Context activation is done by the caller. */
776 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
777 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
779 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
780 struct wined3d_string_buffer *name;
781 GLint location;
782 unsigned int i;
784 if (shader_glsl_use_layout_binding_qualifier(gl_info))
785 return;
787 name = string_buffer_get(&priv->string_buffers);
788 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
790 if (!reg_maps->uav_resource_info[i].type)
791 continue;
793 string_buffer_sprintf(name, "%s_image%u", prefix, i);
794 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
795 if (location == -1)
796 continue;
798 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
799 GL_EXTCALL(glUniform1i(location, i));
801 checkGLcall("Load image bindings");
802 string_buffer_release(&priv->string_buffers, name);
805 /* Context activation is done by the caller. */
806 static void shader_glsl_load_program_resources(const struct wined3d_context_gl *context_gl,
807 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
809 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
811 shader_glsl_init_uniform_block_bindings(context_gl->gl_info, priv, program_id, reg_maps);
812 shader_glsl_load_icb(context_gl->gl_info, priv, program_id, reg_maps);
813 /* Texture unit mapping is set up to be the same each time the shader
814 * program is used so we can hardcode the sampler uniform values. */
815 shader_glsl_load_samplers(&context_gl->c, priv, program_id, reg_maps);
818 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
819 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
821 if (varyings && *strings)
823 char *ptr = *strings;
825 varyings[*varying_count] = ptr;
827 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
828 ptr += buffer->content_size + 1;
830 *strings = ptr;
833 *strings_length += buffer->content_size + 1;
834 ++(*varying_count);
837 static void append_transform_feedback_skip_components(const char **varyings,
838 unsigned int *varying_count, char **strings, unsigned int *strings_length,
839 struct wined3d_string_buffer *buffer, unsigned int component_count)
841 unsigned int j;
843 for (j = 0; j < component_count / 4; ++j)
845 string_buffer_sprintf(buffer, "gl_SkipComponents4");
846 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
848 if (component_count % 4)
850 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
851 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
855 static BOOL shader_glsl_generate_transform_feedback_varyings(struct wined3d_string_buffer *buffer,
856 const char **varyings, unsigned int *varying_count, char *strings, unsigned int *strings_length,
857 GLenum buffer_mode, struct wined3d_shader *shader)
859 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
860 unsigned int buffer_idx, count, length, highest_output_slot, stride;
861 unsigned int i, register_idx, component_idx;
862 BOOL have_varyings_to_record = FALSE;
864 count = length = 0;
865 highest_output_slot = 0;
866 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
868 stride = 0;
870 for (i = 0; i < so_desc->element_count; ++i)
872 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
874 highest_output_slot = max(highest_output_slot, e->output_slot);
875 if (e->output_slot != buffer_idx)
876 continue;
878 if (e->stream_idx)
880 FIXME("Unhandled stream %u.\n", e->stream_idx);
881 continue;
884 stride += e->component_count;
886 if (!e->semantic_name)
888 append_transform_feedback_skip_components(varyings, &count,
889 &strings, &length, buffer, e->component_count);
890 continue;
893 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
894 continue;
896 if (component_idx || e->component_count != 4)
898 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
900 FIXME("Unsupported component range %u-%u.\n", component_idx, e->component_count);
901 append_transform_feedback_skip_components(varyings, &count,
902 &strings, &length, buffer, e->component_count);
903 continue;
906 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
907 register_idx, component_idx, component_idx + e->component_count - 1);
908 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
910 else
912 string_buffer_sprintf(buffer, "shader_in_out.reg%u", register_idx);
913 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
916 have_varyings_to_record = TRUE;
919 if (buffer_idx < so_desc->buffer_stride_count
920 && stride < so_desc->buffer_strides[buffer_idx] / 4)
922 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
923 append_transform_feedback_skip_components(varyings, &count,
924 &strings, &length, buffer, component_count);
927 if (highest_output_slot <= buffer_idx)
928 break;
930 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
932 string_buffer_sprintf(buffer, "gl_NextBuffer");
933 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
937 if (varying_count)
938 *varying_count = count;
939 if (strings_length)
940 *strings_length = length;
942 return have_varyings_to_record;
945 static void shader_glsl_init_transform_feedback(const struct wined3d_context_gl *context_gl,
946 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
948 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
949 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
950 struct wined3d_string_buffer *buffer;
951 unsigned int i, count, length;
952 const char **varyings;
953 char *strings;
954 GLenum mode;
956 if (!so_desc)
957 return;
959 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
961 mode = GL_INTERLEAVED_ATTRIBS;
963 else
965 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
967 for (i = 0; i < so_desc->element_count; ++i)
969 if (!so_desc->elements[i].semantic_name)
971 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
972 return;
974 ++element_count[so_desc->elements[i].output_slot];
977 if (element_count[0] == so_desc->element_count)
979 mode = GL_INTERLEAVED_ATTRIBS;
981 else
983 mode = GL_SEPARATE_ATTRIBS;
984 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
986 if (element_count[i] != 1)
987 break;
989 for (; i < ARRAY_SIZE(element_count); ++i)
991 if (element_count[i])
993 FIXME("Only single element per buffer is allowed in separate mode.\n");
994 return;
1000 buffer = string_buffer_get(&priv->string_buffers);
1002 if (!shader_glsl_generate_transform_feedback_varyings(buffer, NULL, &count, NULL, &length, mode, shader))
1004 FIXME("No varyings to record, disabling transform feedback.\n");
1005 shader->u.gs.so_desc = NULL;
1006 string_buffer_release(&priv->string_buffers, buffer);
1007 return;
1010 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
1012 ERR("Out of memory.\n");
1013 string_buffer_release(&priv->string_buffers, buffer);
1014 return;
1016 if (!(strings = heap_calloc(length, sizeof(*strings))))
1018 ERR("Out of memory.\n");
1019 heap_free(varyings);
1020 string_buffer_release(&priv->string_buffers, buffer);
1021 return;
1024 shader_glsl_generate_transform_feedback_varyings(buffer, varyings, NULL, strings, NULL, mode, shader);
1025 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
1026 checkGLcall("glTransformFeedbackVaryings");
1028 heap_free(varyings);
1029 heap_free(strings);
1030 string_buffer_release(&priv->string_buffers, buffer);
1033 /* Context activation is done by the caller. */
1034 static void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
1035 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
1037 unsigned int start = ~0U, end = 0;
1038 int stack_idx = 0;
1039 unsigned int heap_idx = 1;
1040 unsigned int idx;
1042 if (heap->entries[heap_idx].version <= version) return;
1044 idx = heap->entries[heap_idx].idx;
1045 if (constant_locations[idx] != -1)
1046 start = end = idx;
1047 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1049 while (stack_idx >= 0)
1051 /* Note that we fall through to the next case statement. */
1052 switch(stack[stack_idx])
1054 case HEAP_NODE_TRAVERSE_LEFT:
1056 unsigned int left_idx = heap_idx << 1;
1057 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1059 heap_idx = left_idx;
1060 idx = heap->entries[heap_idx].idx;
1061 if (constant_locations[idx] != -1)
1063 if (start > idx)
1064 start = idx;
1065 if (end < idx)
1066 end = idx;
1069 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1070 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1071 break;
1075 case HEAP_NODE_TRAVERSE_RIGHT:
1077 unsigned int right_idx = (heap_idx << 1) + 1;
1078 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1080 heap_idx = right_idx;
1081 idx = heap->entries[heap_idx].idx;
1082 if (constant_locations[idx] != -1)
1084 if (start > idx)
1085 start = idx;
1086 if (end < idx)
1087 end = idx;
1090 stack[stack_idx++] = HEAP_NODE_POP;
1091 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1092 break;
1096 case HEAP_NODE_POP:
1097 heap_idx >>= 1;
1098 --stack_idx;
1099 break;
1102 if (start <= end)
1103 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1104 checkGLcall("walk_constant_heap()");
1107 /* Context activation is done by the caller. */
1108 static void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1109 GLint location, const struct wined3d_vec4 *data)
1111 GLfloat clamped_constant[4];
1113 if (location == -1) return;
1115 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1116 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1117 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1118 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1120 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1123 /* Context activation is done by the caller. */
1124 static void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1125 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1126 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1128 int stack_idx = 0;
1129 unsigned int heap_idx = 1;
1130 unsigned int idx;
1132 if (heap->entries[heap_idx].version <= version) return;
1134 idx = heap->entries[heap_idx].idx;
1135 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1136 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1138 while (stack_idx >= 0)
1140 /* Note that we fall through to the next case statement. */
1141 switch(stack[stack_idx])
1143 case HEAP_NODE_TRAVERSE_LEFT:
1145 unsigned int left_idx = heap_idx << 1;
1146 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1148 heap_idx = left_idx;
1149 idx = heap->entries[heap_idx].idx;
1150 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1152 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1153 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1154 break;
1158 case HEAP_NODE_TRAVERSE_RIGHT:
1160 unsigned int right_idx = (heap_idx << 1) + 1;
1161 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1163 heap_idx = right_idx;
1164 idx = heap->entries[heap_idx].idx;
1165 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1167 stack[stack_idx++] = HEAP_NODE_POP;
1168 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1169 break;
1173 case HEAP_NODE_POP:
1174 heap_idx >>= 1;
1175 --stack_idx;
1176 break;
1179 checkGLcall("walk_constant_heap_clamped()");
1182 /* Context activation is done by the caller. */
1183 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1184 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1185 unsigned char *stack, unsigned int version)
1187 const struct wined3d_shader_lconst *lconst;
1189 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1190 if (shader->reg_maps.shader_version.major == 1
1191 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1192 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1193 else
1194 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1196 if (!shader->load_local_constsF)
1198 TRACE("No need to load local float constants for this shader.\n");
1199 return;
1202 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1203 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1205 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1207 checkGLcall("glUniform4fv()");
1210 /* Context activation is done by the caller. */
1211 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1212 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], uint32_t constants_set)
1214 unsigned int i;
1215 struct list* ptr;
1217 while (constants_set)
1219 /* We found this uniform name in the program - go ahead and send the data */
1220 i = wined3d_bit_scan(&constants_set);
1221 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1224 /* Load immediate constants */
1225 ptr = list_head(&shader->constantsI);
1226 while (ptr)
1228 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1229 unsigned int idx = lconst->idx;
1230 const GLint *values = (const GLint *)lconst->value;
1232 /* We found this uniform name in the program - go ahead and send the data */
1233 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1234 ptr = list_next(&shader->constantsI, ptr);
1236 checkGLcall("glUniform4iv()");
1239 /* Context activation is done by the caller. */
1240 static void shader_glsl_load_constants_b(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1241 const BOOL *constants, const GLint locations[WINED3D_MAX_CONSTS_B], uint32_t constants_set)
1243 unsigned int i;
1244 struct list* ptr;
1246 while (constants_set)
1248 i = wined3d_bit_scan(&constants_set);
1249 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1252 /* Load immediate constants */
1253 ptr = list_head(&shader->constantsB);
1254 while (ptr)
1256 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1257 unsigned int idx = lconst->idx;
1258 const GLint *values = (const GLint *)lconst->value;
1260 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1261 ptr = list_next(&shader->constantsB, ptr);
1263 checkGLcall("glUniform1iv()");
1266 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1268 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1271 /* Context activation is done by the caller (state handler). */
1272 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1273 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1275 struct
1277 float sx, sy;
1279 np2fixup_constants[WINED3D_MAX_FRAGMENT_SAMPLERS];
1280 UINT fixup = ps->np2_fixup_info->active;
1281 UINT i;
1283 for (i = 0; fixup; fixup >>= 1, ++i)
1285 const struct wined3d_texture *tex = state->textures[i];
1286 unsigned char idx = ps->np2_fixup_info->idx[i];
1288 if (!tex)
1290 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1291 continue;
1294 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1295 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1298 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1301 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1303 struct wined3d_matrix temp;
1304 unsigned int i, j;
1306 for (i = 0; i < 4; ++i)
1307 for (j = 0; j < 4; ++j)
1308 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1310 *out = temp;
1313 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context_gl *context_gl,
1314 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1316 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1317 struct wined3d_matrix mv;
1318 float mat[3 * 3];
1320 if (prog->vs.normal_matrix_location == -1)
1321 return;
1323 get_modelview_matrix(&context_gl->c, state, 0, &mv);
1324 compute_normal_matrix(mat, context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING, &mv);
1326 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1327 checkGLcall("glUniformMatrix3fv");
1330 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context_gl *context_gl,
1331 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1333 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1334 struct wined3d_matrix mat;
1336 if (tex >= WINED3D_MAX_TEXTURES)
1337 return;
1338 if (prog->vs.texture_matrix_location[tex] == -1)
1339 return;
1341 get_texture_matrix(&context_gl->c, state, tex, &mat);
1342 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1343 checkGLcall("glUniformMatrix4fv");
1346 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context_gl *context_gl,
1347 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1349 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1351 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1353 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1354 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1356 else
1358 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1360 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1362 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1363 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1364 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1365 checkGLcall("setting FFP material uniforms");
1368 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context_gl *context_gl,
1369 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1371 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1372 struct wined3d_color color;
1374 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1375 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1376 checkGLcall("glUniform3fv");
1379 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context_gl *context_gl,
1380 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1381 struct glsl_shader_prog_link *prog)
1383 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1384 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1385 struct wined3d_vec4 vec4;
1387 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1388 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1389 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1391 switch (light_info->OriginalParms.type)
1393 case WINED3D_LIGHT_POINT:
1394 wined3d_vec4_transform(&vec4, &light_info->position, view);
1395 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1396 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1397 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1398 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1399 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1400 break;
1402 case WINED3D_LIGHT_SPOT:
1403 wined3d_vec4_transform(&vec4, &light_info->position, view);
1404 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1406 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1407 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1409 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1410 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1411 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1412 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1413 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1414 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1415 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1416 break;
1418 case WINED3D_LIGHT_DIRECTIONAL:
1419 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1420 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1421 break;
1423 case WINED3D_LIGHT_PARALLELPOINT:
1424 wined3d_vec4_transform(&vec4, &light_info->position, view);
1425 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1426 break;
1428 default:
1429 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1431 checkGLcall("setting FFP lights uniforms");
1434 static void shader_glsl_pointsize_uniform(const struct wined3d_context_gl *context_gl,
1435 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1437 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1438 float size, att[3];
1439 float min, max;
1441 get_pointsize_minmax(&context_gl->c, state, &min, &max);
1443 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1444 checkGLcall("glUniform1f");
1445 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1446 checkGLcall("glUniform1f");
1448 get_pointsize(&context_gl->c, state, &size, att);
1450 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1451 checkGLcall("glUniform1f");
1452 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1453 checkGLcall("glUniform1f");
1454 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1455 checkGLcall("glUniform1f");
1456 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1457 checkGLcall("glUniform1f");
1460 static void shader_glsl_load_fog_uniform(const struct wined3d_context_gl *context_gl,
1461 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1463 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1464 struct wined3d_color color;
1465 float start, end, scale;
1466 union
1468 DWORD d;
1469 float f;
1470 } tmpvalue;
1472 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1473 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1474 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1475 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1476 get_fog_start_end(&context_gl->c, state, &start, &end);
1477 scale = 1.0f / (end - start);
1478 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1479 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1480 checkGLcall("fog emulation uniforms");
1483 static void shader_glsl_clip_plane_uniform(const struct wined3d_context_gl *context_gl,
1484 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1486 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1487 struct wined3d_matrix matrix;
1488 struct wined3d_vec4 plane;
1490 plane = state->clip_planes[index];
1492 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1493 if (!use_vs(state))
1495 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1496 transpose_matrix(&matrix, &matrix);
1497 wined3d_vec4_transform(&plane, &plane, &matrix);
1500 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1503 /* Context activation is done by the caller (state handler). */
1504 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1505 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1507 struct wined3d_color float_key[2];
1508 const struct wined3d_texture *texture = state->textures[0];
1510 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1511 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1514 /* Context activation is done by the caller (state handler). */
1515 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1516 const struct wined3d_state *state)
1518 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1519 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1520 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1521 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1522 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1523 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1524 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1525 struct shader_glsl_priv *priv = shader_priv;
1526 unsigned int constant_version;
1527 DWORD update_mask;
1528 int i;
1530 /* No GLSL program set - nothing to do. */
1531 if (!prog)
1532 return;
1534 constant_version = prog->constant_version;
1535 update_mask = context->constant_update_mask & prog->constant_update_mask;
1537 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1538 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1539 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1541 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1542 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1543 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1545 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1546 shader_glsl_load_constants_b(vshader, gl_info, state->vs_consts_b,
1547 prog->vs.uniform_b_locations, vshader->reg_maps.boolean_constants);
1549 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1551 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1552 shader_glsl_clip_plane_uniform(context_gl, state, i, prog);
1555 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1556 shader_glsl_pointsize_uniform(context_gl, state, prog);
1558 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1560 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1561 max(state->viewport_count, 1) : 1;
1562 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1563 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1564 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1565 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1566 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1567 else
1568 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1569 checkGLcall("glUniform4fv");
1572 if (update_mask & WINED3D_SHADER_CONST_BASE_VERTEX_ID)
1574 GL_EXTCALL(glUniform1i(prog->vs.base_vertex_id_location, state->base_vertex_index));
1575 checkGLcall("base vertex id");
1578 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1580 struct wined3d_matrix mat;
1582 get_modelview_matrix(context, state, 0, &mat);
1583 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1584 checkGLcall("glUniformMatrix4fv");
1586 shader_glsl_ffp_vertex_normalmatrix_uniform(context_gl, state, prog);
1589 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1591 struct wined3d_matrix mat;
1593 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1595 if (prog->vs.modelview_matrix_location[i] == -1)
1596 break;
1598 get_modelview_matrix(context, state, i, &mat);
1599 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1600 checkGLcall("glUniformMatrix4fv");
1604 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1606 struct wined3d_matrix projection;
1608 get_projection_matrix(context, state, &projection);
1609 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1610 checkGLcall("glUniformMatrix4fv");
1613 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1615 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1616 shader_glsl_ffp_vertex_texmatrix_uniform(context_gl, state, i, prog);
1619 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1620 shader_glsl_ffp_vertex_material_uniform(context_gl, state, prog);
1622 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1624 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1625 DWORD point_count = 0;
1626 DWORD spot_count = 0;
1627 DWORD directional_count = 0;
1628 DWORD parallel_point_count = 0;
1630 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1632 if (!state->light_state.lights[i])
1633 continue;
1635 switch (state->light_state.lights[i]->OriginalParms.type)
1637 case WINED3D_LIGHT_POINT:
1638 ++point_count;
1639 break;
1640 case WINED3D_LIGHT_SPOT:
1641 ++spot_count;
1642 break;
1643 case WINED3D_LIGHT_DIRECTIONAL:
1644 ++directional_count;
1645 break;
1646 case WINED3D_LIGHT_PARALLELPOINT:
1647 ++parallel_point_count;
1648 break;
1649 default:
1650 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
1651 break;
1654 point_idx = 0;
1655 spot_idx = point_idx + point_count;
1656 directional_idx = spot_idx + spot_count;
1657 parallel_point_idx = directional_idx + directional_count;
1659 shader_glsl_ffp_vertex_lightambient_uniform(context_gl, state, prog);
1660 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1662 const struct wined3d_light_info *light_info = state->light_state.lights[i];
1663 unsigned int idx;
1665 if (!light_info)
1666 continue;
1668 switch (light_info->OriginalParms.type)
1670 case WINED3D_LIGHT_POINT:
1671 idx = point_idx++;
1672 break;
1673 case WINED3D_LIGHT_SPOT:
1674 idx = spot_idx++;
1675 break;
1676 case WINED3D_LIGHT_DIRECTIONAL:
1677 idx = directional_idx++;
1678 break;
1679 case WINED3D_LIGHT_PARALLELPOINT:
1680 idx = parallel_point_idx++;
1681 break;
1682 default:
1683 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1684 continue;
1686 shader_glsl_ffp_vertex_light_uniform(context_gl, state, idx, light_info, prog);
1690 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1691 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1692 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1694 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1695 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1696 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1698 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1699 shader_glsl_load_constants_b(pshader, gl_info, state->ps_consts_b,
1700 prog->ps.uniform_b_locations, pshader->reg_maps.boolean_constants);
1702 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1704 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1706 if (prog->ps.bumpenv_mat_location[i] == -1)
1707 continue;
1709 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1710 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1712 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1714 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1715 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1716 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1717 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1721 checkGLcall("bump env uniforms");
1724 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1726 const struct wined3d_vec4 correction_params =
1728 /* Position is relative to the framebuffer, not the viewport. */
1729 context->render_offscreen ? 0.0f : (float)state->fb.render_targets[0]->height,
1730 context->render_offscreen ? 1.0f : -1.0f,
1731 0.0f,
1732 0.0f,
1735 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1738 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1739 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1740 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1741 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1743 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1745 struct wined3d_color color;
1747 if (prog->ps.tex_factor_location != -1)
1749 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1750 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1753 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1754 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1755 else
1756 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1758 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1760 if (prog->ps.tss_constant_location[i] == -1)
1761 continue;
1763 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1764 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1767 checkGLcall("fixed function uniforms");
1770 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1771 shader_glsl_load_fog_uniform(context_gl, state, prog);
1773 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1775 float ref = wined3d_alpha_ref(state);
1777 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1778 checkGLcall("alpha test emulation uniform");
1781 if (priv->next_constant_version == UINT_MAX)
1783 TRACE("Max constant version reached, resetting to 0.\n");
1784 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1785 priv->next_constant_version = 1;
1787 else
1789 prog->constant_version = priv->next_constant_version++;
1793 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1795 struct constant_entry *entries = heap->entries;
1796 unsigned int *positions = heap->positions;
1797 unsigned int heap_idx, parent_idx;
1799 if (!heap->contained[idx])
1801 heap_idx = heap->size++;
1802 heap->contained[idx] = TRUE;
1804 else
1806 heap_idx = positions[idx];
1809 while (heap_idx > 1)
1811 parent_idx = heap_idx >> 1;
1813 if (new_version <= entries[parent_idx].version) break;
1815 entries[heap_idx] = entries[parent_idx];
1816 positions[entries[parent_idx].idx] = heap_idx;
1817 heap_idx = parent_idx;
1820 entries[heap_idx].version = new_version;
1821 entries[heap_idx].idx = idx;
1822 positions[idx] = heap_idx;
1825 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1827 struct shader_glsl_priv *priv = device->shader_priv;
1828 struct constant_heap *heap = &priv->vconst_heap;
1829 UINT i;
1831 for (i = start; i < count + start; ++i)
1833 update_heap_entry(heap, i, priv->next_constant_version);
1837 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1839 struct shader_glsl_priv *priv = device->shader_priv;
1840 struct constant_heap *heap = &priv->pconst_heap;
1841 UINT i;
1843 for (i = start; i < count + start; ++i)
1845 update_heap_entry(heap, i, priv->next_constant_version);
1849 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1851 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1852 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1853 if(shader_major > 3) return ret;
1855 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1856 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1857 return ret;
1860 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1862 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
1865 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1867 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
1868 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
1871 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
1873 return shader_glsl_get_version(gl_info) >= 150;
1876 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1878 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1881 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1882 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1884 va_list args;
1885 int ret;
1887 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1888 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1889 for (;;)
1891 va_start(args, format);
1892 ret = shader_vaddline(buffer, format, args);
1893 va_end(args);
1894 if (!ret)
1895 return;
1896 if (!string_buffer_resize(buffer, ret))
1897 return;
1901 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1902 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1904 va_list args;
1905 int ret;
1907 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1908 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1909 for (;;)
1911 va_start(args, format);
1912 ret = shader_vaddline(buffer, format, args);
1913 va_end(args);
1914 if (!ret)
1915 return;
1916 if (!string_buffer_resize(buffer, ret))
1917 return;
1921 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
1923 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
1926 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
1928 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
1931 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
1933 switch (mode)
1935 case WINED3DSIM_CONSTANT:
1936 return "flat ";
1937 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
1938 return "noperspective ";
1939 default:
1940 FIXME("Unhandled interpolation mode %#x.\n", mode);
1941 case WINED3DSIM_NONE:
1942 case WINED3DSIM_LINEAR:
1943 return "";
1947 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
1948 const DWORD *packed_interpolation_mode, unsigned int register_idx)
1950 return wined3d_extract_bits(packed_interpolation_mode,
1951 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
1954 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
1955 struct wined3d_string_buffer *buffer, unsigned int element_count,
1956 const DWORD *interpolation_mode, BOOL unroll)
1958 enum wined3d_shader_interpolation_mode mode;
1959 unsigned int i;
1961 if (shader_glsl_use_interface_blocks(gl_info))
1963 if (unroll)
1965 shader_addline(buffer, "in shader_in_out {\n");
1966 for (i = 0; i < element_count; ++i)
1968 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
1969 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
1971 shader_addline(buffer, "} shader_in;\n");
1973 else
1975 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
1978 else
1980 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
1984 static BOOL needs_interpolation_qualifiers_for_shader_outputs(const struct wined3d_gl_info *gl_info)
1986 /* In GLSL 4.40+ it is fine to specify interpolation qualifiers only in
1987 * fragment shaders. In older GLSL versions interpolation qualifiers must
1988 * match between shader stages. */
1989 return gl_info->glsl_version < MAKEDWORD_VERSION(4, 40);
1992 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
1993 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
1994 const DWORD *interpolation_mode)
1996 enum wined3d_shader_interpolation_mode mode;
1997 unsigned int i;
1999 if (shader_glsl_use_interface_blocks(gl_info))
2001 if (rasterizer_setup)
2003 shader_addline(buffer, "out shader_in_out {\n");
2004 for (i = 0; i < element_count; ++i)
2006 const char *interpolation_qualifiers = "";
2007 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2009 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2010 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2012 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2014 shader_addline(buffer, "} shader_out;\n");
2016 else
2018 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2021 else
2023 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2027 static BOOL use_legacy_fragment_output(const struct wined3d_gl_info *gl_info)
2029 /* Technically 1.30 does support user-defined fragment shader outputs but
2030 * we might not have glBindFragDataLocation() available (i.e. GL version
2031 * might be < 3.0). */
2032 return gl_info->glsl_version <= MAKEDWORD_VERSION(1, 30);
2035 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2037 return use_legacy_fragment_output(gl_info) ? "gl_FragData" : "ps_out";
2040 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2042 switch (primitive_type)
2044 case WINED3D_PT_POINTLIST:
2045 return "points";
2047 case WINED3D_PT_LINELIST:
2048 return "lines";
2050 case WINED3D_PT_LINESTRIP:
2051 return "line_strip";
2053 case WINED3D_PT_TRIANGLELIST:
2054 return "triangles";
2056 case WINED3D_PT_TRIANGLESTRIP:
2057 return "triangle_strip";
2059 case WINED3D_PT_LINELIST_ADJ:
2060 return "lines_adjacency";
2062 case WINED3D_PT_TRIANGLELIST_ADJ:
2063 return "triangles_adjacency";
2065 default:
2066 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2067 return "";
2071 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2073 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2074 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2075 DWORD input_reg_used = shader->u.ps.input_reg_used;
2076 unsigned int i;
2078 if (reg_maps->shader_version.major < 3)
2079 return input_reg_used & (1u << idx);
2081 for (i = 0; i < input_signature->element_count; ++i)
2083 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2085 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2086 continue;
2088 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2089 && input->semantic_idx == idx)
2090 return input_reg_used & (1u << input->register_idx);
2092 return FALSE;
2095 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2096 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2098 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2100 if (version->major >= 4)
2101 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2102 else
2103 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2106 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2107 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2108 unsigned int index)
2110 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2111 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2112 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2113 index, scalar_type, scalar_type, index);
2116 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2117 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2119 unsigned int index = e->register_idx;
2120 enum wined3d_component_type type;
2122 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2124 shader_addline(buffer, "uniform int base_vertex_id;\n");
2125 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID - base_vertex_id), 0.0, 0.0, 0.0);\n", index);
2126 return;
2128 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2130 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2131 index);
2132 return;
2134 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2135 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2137 if (shader_glsl_use_explicit_attrib_location(gl_info))
2138 shader_addline(buffer, "layout(location = %u) ", index);
2140 type = e->component_type;
2141 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2143 FIXME("Unhandled type %#x.\n", type);
2144 type = WINED3D_TYPE_FLOAT;
2146 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2147 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2148 else
2149 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2150 component_type_info[type].glsl_vector_type,
2151 component_type_info[type].glsl_scalar_type, index);
2154 /** Generate the variable & register declarations for the GLSL output target */
2155 static void shader_generate_glsl_declarations(const struct wined3d_context_gl *context_gl,
2156 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2157 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2159 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2160 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2161 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2162 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2163 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2164 unsigned int uniform_block_base, uniform_block_count;
2165 enum wined3d_shader_resource_type resource_type;
2166 const struct wined3d_shader_lconst *lconst;
2167 const char *prefix;
2168 unsigned int i;
2169 uint32_t map;
2171 if (wined3d_settings.strict_shader_math)
2172 shader_addline(buffer, "#pragma optionNV(fastmath off)\n");
2174 prefix = shader_glsl_get_prefix(version->type);
2176 /* Prototype the subroutines */
2177 map = reg_maps->labels;
2178 while (map)
2180 i = wined3d_bit_scan(&map);
2181 shader_addline(buffer, "void subroutine%u();\n", i);
2184 if (version->type != WINED3D_SHADER_TYPE_PIXEL && version->type != WINED3D_SHADER_TYPE_COMPUTE)
2186 if (version->type == WINED3D_SHADER_TYPE_HULL)
2187 shader_addline(buffer, "out gl_PerVertex { invariant vec4 gl_Position; } gl_out[];\n");
2188 else
2189 shader_addline(buffer, "invariant gl_Position;\n");
2192 /* Declare the constants (aka uniforms) */
2193 if (shader->limits->constant_float > 0)
2195 unsigned max_constantsF;
2197 /* Unless the shader uses indirect addressing, always declare the
2198 * maximum array size and ignore that we need some uniforms privately.
2199 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2200 * and immediate values, still declare VC[256]. If the shader needs
2201 * more uniforms than we have it won't work in any case. If it uses
2202 * less, the compiler will figure out which uniforms are really used
2203 * and strip them out. This allows a shader to use c255 on a dx9 card,
2204 * as long as it doesn't also use all the other constants.
2206 * If the shader uses indirect addressing the compiler must assume
2207 * that all declared uniforms are used. In this case, declare only the
2208 * amount that we're assured to have.
2210 * Thus we run into problems in these two cases:
2211 * 1) The shader really uses more uniforms than supported.
2212 * 2) The shader uses indirect addressing, less constants than
2213 * supported, but uses a constant index > #supported consts. */
2214 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2216 /* No indirect addressing here. */
2217 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2219 else
2221 if (reg_maps->usesrelconstF)
2223 /* Subtract the other potential uniforms from the max
2224 * available (bools, ints, and 1 row of projection matrix).
2225 * Subtract another uniform for immediate values, which have
2226 * to be loaded via uniform by the driver as well. The shader
2227 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2228 * shader code, so one vec4 should be enough. (Unfortunately
2229 * the Nvidia driver doesn't store 128 and -128 in one float).
2231 * Writing gl_ClipVertex requires one uniform for each
2232 * clipplane as well. */
2233 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2234 if (vs_args->clip_enabled)
2235 max_constantsF -= gl_info->limits.user_clip_distances;
2236 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2237 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2238 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2239 * for now take this into account when calculating the number of available constants
2241 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2242 /* Set by driver quirks in directx.c */
2243 max_constantsF -= gl_info->reserved_glsl_constants;
2245 if (max_constantsF < shader->limits->constant_float)
2247 static unsigned int once;
2249 if (!once++)
2250 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2251 " it may not render correctly.\n");
2252 else
2253 WARN("The hardware does not support enough uniform components to run this shader.\n");
2256 else
2258 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2261 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2262 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2265 /* Always declare the full set of constants, the compiler can remove the
2266 * unused ones because d3d doesn't (yet) support indirect int and bool
2267 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2268 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2269 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2271 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2272 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2274 /* Declare immediate constant buffer */
2275 if (reg_maps->icb)
2276 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2278 /* Declare constant buffers */
2279 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2280 &uniform_block_base, &uniform_block_count);
2281 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2283 if (reg_maps->cb_sizes[i])
2285 shader_addline(buffer, "layout(std140");
2286 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2287 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2288 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2289 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2293 /* Declare texture samplers */
2294 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2296 struct wined3d_shader_sampler_map_entry *entry;
2297 const char *sampler_type_prefix, *sampler_type;
2298 BOOL shadow_sampler, tex_rect;
2300 entry = &reg_maps->sampler_map.entries[i];
2302 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2304 ERR("Invalid resource index %u.\n", entry->resource_idx);
2305 continue;
2308 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2310 case WINED3D_DATA_FLOAT:
2311 case WINED3D_DATA_UNORM:
2312 case WINED3D_DATA_SNORM:
2313 sampler_type_prefix = "";
2314 break;
2316 case WINED3D_DATA_INT:
2317 sampler_type_prefix = "i";
2318 break;
2320 case WINED3D_DATA_UINT:
2321 sampler_type_prefix = "u";
2322 break;
2324 default:
2325 sampler_type_prefix = "";
2326 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2327 break;
2330 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2331 resource_type = version->type == WINED3D_SHADER_TYPE_PIXEL
2332 ? pixelshader_get_resource_type(reg_maps, entry->resource_idx, ps_args->tex_types)
2333 : reg_maps->resource_info[entry->resource_idx].type;
2335 switch (resource_type)
2337 case WINED3D_SHADER_RESOURCE_BUFFER:
2338 sampler_type = "samplerBuffer";
2339 break;
2341 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2342 if (shadow_sampler)
2343 sampler_type = "sampler1DShadow";
2344 else
2345 sampler_type = "sampler1D";
2346 break;
2348 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2349 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2350 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2351 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2352 if (shadow_sampler)
2354 if (tex_rect)
2355 sampler_type = "sampler2DRectShadow";
2356 else
2357 sampler_type = "sampler2DShadow";
2359 else
2361 if (tex_rect)
2362 sampler_type = "sampler2DRect";
2363 else
2364 sampler_type = "sampler2D";
2366 break;
2368 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2369 if (shadow_sampler)
2370 FIXME("Unsupported 3D shadow sampler.\n");
2371 sampler_type = "sampler3D";
2372 break;
2374 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2375 if (shadow_sampler)
2376 sampler_type = "samplerCubeShadow";
2377 else
2378 sampler_type = "samplerCube";
2379 break;
2381 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2382 if (shadow_sampler)
2383 sampler_type = "sampler1DArrayShadow";
2384 else
2385 sampler_type = "sampler1DArray";
2386 break;
2388 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2389 if (shadow_sampler)
2390 sampler_type = "sampler2DArrayShadow";
2391 else
2392 sampler_type = "sampler2DArray";
2393 break;
2395 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2396 if (shadow_sampler)
2397 sampler_type = "samplerCubeArrayShadow";
2398 else
2399 sampler_type = "samplerCubeArray";
2400 break;
2402 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2403 sampler_type = "sampler2DMS";
2404 break;
2406 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2407 sampler_type = "sampler2DMSArray";
2408 break;
2410 default:
2411 sampler_type = "unsupported_sampler";
2412 FIXME("Unhandled resource type %#x.\n", resource_type);
2413 break;
2416 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2417 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
2418 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2419 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2422 /* Declare images */
2423 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2425 const char *image_type_prefix, *image_type, *read_format;
2427 if (!reg_maps->uav_resource_info[i].type)
2428 continue;
2430 switch (reg_maps->uav_resource_info[i].data_type)
2432 case WINED3D_DATA_FLOAT:
2433 case WINED3D_DATA_UNORM:
2434 case WINED3D_DATA_SNORM:
2435 image_type_prefix = "";
2436 read_format = "r32f";
2437 break;
2439 case WINED3D_DATA_INT:
2440 image_type_prefix = "i";
2441 read_format = "r32i";
2442 break;
2444 case WINED3D_DATA_UINT:
2445 image_type_prefix = "u";
2446 read_format = "r32ui";
2447 break;
2449 default:
2450 image_type_prefix = "";
2451 read_format = "";
2452 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2453 break;
2456 switch (reg_maps->uav_resource_info[i].type)
2458 case WINED3D_SHADER_RESOURCE_BUFFER:
2459 image_type = "imageBuffer";
2460 break;
2462 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2463 image_type = "image1D";
2464 break;
2466 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2467 image_type = "image2D";
2468 break;
2470 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2471 image_type = "image3D";
2472 break;
2474 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2475 image_type = "image1DArray";
2476 break;
2478 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2479 image_type = "image2DArray";
2480 break;
2482 default:
2483 image_type = "unsupported_image";
2484 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2485 break;
2488 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2489 shader_addline(buffer, "layout(binding = %u)\n", i);
2490 if (reg_maps->uav_read_mask & (1u << i))
2491 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2492 read_format, image_type_prefix, image_type, prefix, i);
2493 else
2494 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2495 image_type_prefix, image_type, prefix, i);
2497 if (reg_maps->uav_counter_mask & (1u << i))
2498 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2499 i, prefix, i);
2502 /* Declare address variables */
2503 map = reg_maps->address;
2504 while (map)
2506 i = wined3d_bit_scan(&map);
2507 shader_addline(buffer, "ivec4 A%u;\n", i);
2510 /* Declare output register temporaries */
2511 if (shader->limits->packed_output)
2512 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2514 /* Declare temporary variables */
2515 if (reg_maps->temporary_count)
2517 for (i = 0; i < reg_maps->temporary_count; ++i)
2518 shader_addline(buffer, "vec4 R%u;\n", i);
2520 else if (version->major < 4)
2522 map = reg_maps->temporary;
2523 while (map)
2525 i = wined3d_bit_scan(&map);
2526 shader_addline(buffer, "vec4 R%u;\n", i);
2530 /* Declare indexable temporary variables */
2531 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2533 if (idx_temp_reg->component_count != 4)
2534 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2535 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2538 /* Declare loop registers aLx */
2539 if (version->major < 4)
2541 for (i = 0; i < reg_maps->loop_depth; ++i)
2543 shader_addline(buffer, "int aL%u;\n", i);
2544 shader_addline(buffer, "int tmpInt%u;\n", i);
2548 /* Temporary variables for matrix operations */
2549 shader_addline(buffer, "vec4 tmp0;\n");
2550 shader_addline(buffer, "vec4 tmp1;\n");
2551 if (gl_info->supported[ARB_GPU_SHADER5])
2552 shader_addline(buffer, "precise vec4 tmp_precise[2];\n");
2553 else
2554 shader_addline(buffer, "/* precise */ vec4 tmp_precise[2];\n");
2556 if (!shader->load_local_constsF)
2558 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2560 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2561 shader_glsl_append_imm_vec(buffer, (const float *)lconst->value, ARRAY_SIZE(lconst->value), gl_info);
2562 shader_addline(buffer, ";\n");
2567 /* Prototypes */
2568 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2569 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2570 enum wined3d_data_type data_type);
2572 /** Used for opcode modifiers - They multiply the result by the specified amount */
2573 static const char * const shift_glsl_tab[] = {
2574 "", /* 0 (none) */
2575 "2.0 * ", /* 1 (x2) */
2576 "4.0 * ", /* 2 (x4) */
2577 "8.0 * ", /* 3 (x8) */
2578 "16.0 * ", /* 4 (x16) */
2579 "32.0 * ", /* 5 (x32) */
2580 "", /* 6 (x64) */
2581 "", /* 7 (x128) */
2582 "", /* 8 (d256) */
2583 "", /* 9 (d128) */
2584 "", /* 10 (d64) */
2585 "", /* 11 (d32) */
2586 "0.0625 * ", /* 12 (d16) */
2587 "0.125 * ", /* 13 (d8) */
2588 "0.25 * ", /* 14 (d4) */
2589 "0.5 * " /* 15 (d2) */
2592 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2593 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2594 const char *in_reg, const char *in_regswizzle, char *out_str)
2596 switch (src_modifier)
2598 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2599 case WINED3DSPSM_DW:
2600 case WINED3DSPSM_NONE:
2601 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2602 break;
2603 case WINED3DSPSM_NEG:
2604 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2605 break;
2606 case WINED3DSPSM_NOT:
2607 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2608 break;
2609 case WINED3DSPSM_BIAS:
2610 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2611 break;
2612 case WINED3DSPSM_BIASNEG:
2613 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2614 break;
2615 case WINED3DSPSM_SIGN:
2616 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2617 break;
2618 case WINED3DSPSM_SIGNNEG:
2619 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2620 break;
2621 case WINED3DSPSM_COMP:
2622 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2623 break;
2624 case WINED3DSPSM_X2:
2625 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2626 break;
2627 case WINED3DSPSM_X2NEG:
2628 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2629 break;
2630 case WINED3DSPSM_ABS:
2631 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2632 break;
2633 case WINED3DSPSM_ABSNEG:
2634 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2635 break;
2636 default:
2637 FIXME("Unhandled modifier %u\n", src_modifier);
2638 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2642 static void shader_glsl_fixup_scalar_register_variable(struct wined3d_string_buffer *register_name,
2643 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2645 /* The ARB_shading_language_420pack extension allows swizzle operations on
2646 * scalars. */
2647 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2648 string_buffer_sprintf(register_name, "%s", glsl_variable);
2649 else
2650 string_buffer_sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2653 /** Writes the GLSL variable name that corresponds to the register that the
2654 * DX opcode parameter is trying to access */
2655 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2656 enum wined3d_data_type data_type, struct wined3d_string_buffer *register_name,
2657 BOOL *is_swizzled, const struct wined3d_shader_context *ctx)
2659 /* oPos, oFog and oPts in D3D */
2660 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2662 const struct wined3d_shader *shader = ctx->shader;
2663 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2664 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2665 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2666 const char *prefix = shader_glsl_get_prefix(version->type);
2667 const struct wined3d_gl_info *gl_info = priv->gl_info;
2668 struct glsl_src_param rel_param0, rel_param1;
2670 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2671 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2672 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2673 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2674 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2675 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2676 if (is_swizzled)
2677 *is_swizzled = FALSE;
2679 switch (reg->type)
2681 case WINED3DSPR_TEMP:
2682 string_buffer_sprintf(register_name, "R%u", reg->idx[0].offset);
2683 break;
2685 case WINED3DSPR_INPUT:
2686 case WINED3DSPR_INCONTROLPOINT:
2687 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2689 if (reg->idx[0].rel_addr)
2690 FIXME("VS3 input registers relative addressing.\n");
2691 if (is_swizzled && priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2692 *is_swizzled = TRUE;
2693 if (reg->idx[0].rel_addr)
2695 string_buffer_sprintf(register_name, "%s_in[%s + %u]",
2696 prefix, rel_param0.param_str, reg->idx[0].offset);
2698 else
2700 string_buffer_sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2702 break;
2705 if (version->type == WINED3D_SHADER_TYPE_HULL
2706 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2707 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2709 if (reg->idx[0].rel_addr)
2711 if (reg->idx[1].rel_addr)
2712 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2713 rel_param0.param_str, reg->idx[0].offset,
2714 rel_param1.param_str, reg->idx[1].offset);
2715 else
2716 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2717 rel_param0.param_str, reg->idx[0].offset,
2718 reg->idx[1].offset);
2720 else if (reg->idx[1].rel_addr)
2721 string_buffer_sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2722 rel_param1.param_str, reg->idx[1].offset);
2723 else
2724 string_buffer_sprintf(register_name, "shader_in[%u].reg[%u]",
2725 reg->idx[0].offset, reg->idx[1].offset);
2726 break;
2729 /* pixel shaders >= 3.0 */
2730 if (version->major >= 3)
2732 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2733 unsigned int in_count = vec4_varyings(version->major, gl_info);
2735 if (reg->idx[0].rel_addr)
2737 /* Removing a + 0 would be an obvious optimization, but
2738 * OS X doesn't see the NOP operation there. */
2739 if (idx)
2741 if (needs_legacy_glsl_syntax(gl_info)
2742 && shader->u.ps.declared_in_count > in_count)
2744 string_buffer_sprintf(register_name,
2745 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2746 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2747 prefix, rel_param0.param_str, idx);
2749 else
2751 string_buffer_sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2754 else
2756 if (needs_legacy_glsl_syntax(gl_info)
2757 && shader->u.ps.declared_in_count > in_count)
2759 string_buffer_sprintf(register_name,
2760 "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2761 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2762 prefix, rel_param0.param_str);
2764 else
2766 string_buffer_sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2770 else
2772 if (idx == in_count)
2773 string_buffer_sprintf(register_name, "gl_Color");
2774 else if (idx == in_count + 1)
2775 string_buffer_sprintf(register_name, "gl_SecondaryColor");
2776 else
2777 string_buffer_sprintf(register_name, "%s_in[%u]", prefix, idx);
2780 else
2782 if (!reg->idx[0].offset)
2783 string_buffer_sprintf(register_name, "ffp_varying_diffuse");
2784 else
2785 string_buffer_sprintf(register_name, "ffp_varying_specular");
2786 break;
2788 break;
2790 case WINED3DSPR_CONST:
2792 /* Relative addressing */
2793 if (reg->idx[0].rel_addr)
2795 if (wined3d_settings.check_float_constants)
2796 string_buffer_sprintf(register_name,
2797 "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2798 rel_param0.param_str, reg->idx[0].offset,
2799 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2800 prefix, rel_param0.param_str, reg->idx[0].offset);
2801 else if (reg->idx[0].offset)
2802 string_buffer_sprintf(register_name, "%s_c[%s + %u]",
2803 prefix, rel_param0.param_str, reg->idx[0].offset);
2804 else
2805 string_buffer_sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2807 else
2809 if (shader_constant_is_local(shader, reg->idx[0].offset))
2810 string_buffer_sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2811 else
2812 string_buffer_sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2815 break;
2817 case WINED3DSPR_CONSTINT:
2818 string_buffer_sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2819 break;
2821 case WINED3DSPR_CONSTBOOL:
2822 string_buffer_sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2823 break;
2825 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2826 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2827 string_buffer_sprintf(register_name, "T%u", reg->idx[0].offset);
2828 else
2829 string_buffer_sprintf(register_name, "A%u", reg->idx[0].offset);
2830 break;
2832 case WINED3DSPR_LOOP:
2833 string_buffer_sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
2834 break;
2836 case WINED3DSPR_SAMPLER:
2837 string_buffer_sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2838 break;
2840 case WINED3DSPR_COLOROUT:
2841 if (reg->idx[0].offset >= gl_info->limits.buffers)
2842 WARN("Write to render target %u, only %d supported.\n",
2843 reg->idx[0].offset, gl_info->limits.buffers);
2845 string_buffer_sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2846 break;
2848 case WINED3DSPR_RASTOUT:
2849 string_buffer_sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2850 break;
2852 case WINED3DSPR_DEPTHOUT:
2853 case WINED3DSPR_DEPTHOUTGE:
2854 case WINED3DSPR_DEPTHOUTLE:
2855 string_buffer_sprintf(register_name, "gl_FragDepth");
2856 break;
2858 case WINED3DSPR_ATTROUT:
2859 if (!reg->idx[0].offset)
2860 string_buffer_sprintf(register_name, "%s_out[8]", prefix);
2861 else
2862 string_buffer_sprintf(register_name, "%s_out[9]", prefix);
2863 break;
2865 case WINED3DSPR_TEXCRDOUT:
2866 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2867 if (reg->idx[0].rel_addr)
2868 string_buffer_sprintf(register_name, "%s_out[%s + %u]",
2869 prefix, rel_param0.param_str, reg->idx[0].offset);
2870 else
2871 string_buffer_sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2872 break;
2874 case WINED3DSPR_MISCTYPE:
2875 if (!reg->idx[0].offset)
2877 /* vPos */
2878 string_buffer_sprintf(register_name, "vpos");
2880 else if (reg->idx[0].offset == 1)
2882 /* Note that gl_FrontFacing is a bool, while vFace is
2883 * a float for which the sign determines front/back */
2884 string_buffer_sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2886 else
2888 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2889 string_buffer_sprintf(register_name, "unrecognized_register");
2891 break;
2893 case WINED3DSPR_IMMCONST:
2894 switch (reg->immconst_type)
2896 case WINED3D_IMMCONST_SCALAR:
2897 switch (data_type)
2899 case WINED3D_DATA_UNORM:
2900 case WINED3D_DATA_SNORM:
2901 case WINED3D_DATA_FLOAT:
2902 string_buffer_clear(register_name);
2903 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 1, gl_info);
2904 break;
2905 case WINED3D_DATA_INT:
2906 string_buffer_sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2907 break;
2908 case WINED3D_DATA_RESOURCE:
2909 case WINED3D_DATA_SAMPLER:
2910 case WINED3D_DATA_UINT:
2911 string_buffer_sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2912 break;
2913 default:
2914 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2915 break;
2917 break;
2919 case WINED3D_IMMCONST_VEC4:
2920 switch (data_type)
2922 case WINED3D_DATA_UNORM:
2923 case WINED3D_DATA_SNORM:
2924 case WINED3D_DATA_FLOAT:
2925 string_buffer_clear(register_name);
2926 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 4, gl_info);
2927 break;
2928 case WINED3D_DATA_INT:
2929 string_buffer_sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2930 reg->u.immconst_data[0], reg->u.immconst_data[1],
2931 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2932 break;
2933 case WINED3D_DATA_RESOURCE:
2934 case WINED3D_DATA_SAMPLER:
2935 case WINED3D_DATA_UINT:
2936 string_buffer_sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2937 reg->u.immconst_data[0], reg->u.immconst_data[1],
2938 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2939 break;
2940 default:
2941 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2942 break;
2944 break;
2946 default:
2947 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2948 string_buffer_sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2950 break;
2952 case WINED3DSPR_CONSTBUFFER:
2953 if (reg->idx[1].rel_addr)
2954 string_buffer_sprintf(register_name, "%s_cb%u[%s + %u]",
2955 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2956 else
2957 string_buffer_sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2958 break;
2960 case WINED3DSPR_IMMCONSTBUFFER:
2961 if (reg->idx[0].rel_addr)
2962 string_buffer_sprintf(register_name, "%s_icb[%s + %u]",
2963 prefix, rel_param0.param_str, reg->idx[0].offset);
2964 else
2965 string_buffer_sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2966 break;
2968 case WINED3DSPR_PRIMID:
2969 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2970 string_buffer_sprintf(register_name, "gl_PrimitiveIDIn");
2971 else
2972 string_buffer_sprintf(register_name, "gl_PrimitiveID");
2973 break;
2975 case WINED3DSPR_IDXTEMP:
2976 if (reg->idx[1].rel_addr)
2977 string_buffer_sprintf(register_name, "X%u[%s + %u]",
2978 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2979 else
2980 string_buffer_sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2981 break;
2983 case WINED3DSPR_LOCALTHREADINDEX:
2984 shader_glsl_fixup_scalar_register_variable(register_name,
2985 "int(gl_LocalInvocationIndex)", gl_info);
2986 break;
2988 case WINED3DSPR_GSINSTID:
2989 case WINED3DSPR_OUTPOINTID:
2990 shader_glsl_fixup_scalar_register_variable(register_name,
2991 "gl_InvocationID", gl_info);
2992 break;
2994 case WINED3DSPR_THREADID:
2995 string_buffer_sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2996 break;
2998 case WINED3DSPR_THREADGROUPID:
2999 string_buffer_sprintf(register_name, "ivec3(gl_WorkGroupID)");
3000 break;
3002 case WINED3DSPR_LOCALTHREADID:
3003 string_buffer_sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3004 break;
3006 case WINED3DSPR_FORKINSTID:
3007 case WINED3DSPR_JOININSTID:
3008 shader_glsl_fixup_scalar_register_variable(register_name,
3009 "phase_instance_id", gl_info);
3010 break;
3012 case WINED3DSPR_TESSCOORD:
3013 string_buffer_sprintf(register_name, "gl_TessCoord");
3014 break;
3016 case WINED3DSPR_OUTCONTROLPOINT:
3017 if (reg->idx[0].rel_addr)
3019 if (reg->idx[1].rel_addr)
3020 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3021 rel_param0.param_str, reg->idx[0].offset,
3022 rel_param1.param_str, reg->idx[1].offset);
3023 else
3024 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3025 rel_param0.param_str, reg->idx[0].offset,
3026 reg->idx[1].offset);
3028 else if (reg->idx[1].rel_addr)
3030 string_buffer_sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3031 reg->idx[0].offset, rel_param1.param_str,
3032 reg->idx[1].offset);
3034 else
3036 string_buffer_sprintf(register_name, "shader_out[%u].reg[%u]",
3037 reg->idx[0].offset, reg->idx[1].offset);
3039 break;
3041 case WINED3DSPR_PATCHCONST:
3042 if (version->type == WINED3D_SHADER_TYPE_HULL)
3043 string_buffer_sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3044 else
3045 string_buffer_sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3046 break;
3048 case WINED3DSPR_COVERAGE:
3049 string_buffer_sprintf(register_name, "gl_SampleMaskIn[0]");
3050 break;
3052 case WINED3DSPR_SAMPLEMASK:
3053 string_buffer_sprintf(register_name, "sample_mask");
3054 break;
3056 default:
3057 FIXME("Unhandled register type %#x.\n", reg->type);
3058 string_buffer_sprintf(register_name, "unrecognised_register");
3059 break;
3063 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3065 *str++ = '.';
3066 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3067 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3068 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3069 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3070 *str = '\0';
3073 /* Get the GLSL write mask for the destination register */
3074 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3076 DWORD mask = param->write_mask;
3078 if (shader_is_scalar(&param->reg))
3080 mask = WINED3DSP_WRITEMASK_0;
3081 *write_mask = '\0';
3083 else
3085 shader_glsl_write_mask_to_str(mask, write_mask);
3088 return mask;
3091 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3093 unsigned int size = 0;
3095 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3096 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3097 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3098 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3100 return size;
3103 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3104 unsigned int component_idx)
3106 /* swizzle bits fields: wwzzyyxx */
3107 return (swizzle >> (2 * component_idx)) & 0x3;
3110 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3112 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3113 * but addressed as "rgba". To fix this we need to swap the register's x
3114 * and z components. */
3115 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3116 unsigned int i;
3118 *str++ = '.';
3119 for (i = 0; i < 4; ++i)
3121 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3122 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3124 *str = '\0';
3127 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3128 BOOL fixup, DWORD mask, char *swizzle_str)
3130 if (shader_is_scalar(&param->reg))
3131 *swizzle_str = '\0';
3132 else
3133 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3136 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3137 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type, unsigned int size)
3139 if (dst_data_type == WINED3D_DATA_UNORM || dst_data_type == WINED3D_DATA_SNORM)
3140 dst_data_type = WINED3D_DATA_FLOAT;
3141 if (src_data_type == WINED3D_DATA_UNORM || src_data_type == WINED3D_DATA_SNORM)
3142 src_data_type = WINED3D_DATA_FLOAT;
3144 if (dst_data_type == src_data_type)
3146 string_buffer_sprintf(dst_param, "%s", src_param);
3147 return;
3150 if (src_data_type == WINED3D_DATA_FLOAT)
3152 switch (dst_data_type)
3154 case WINED3D_DATA_INT:
3155 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3156 return;
3157 case WINED3D_DATA_RESOURCE:
3158 case WINED3D_DATA_SAMPLER:
3159 case WINED3D_DATA_UINT:
3160 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3161 return;
3162 default:
3163 break;
3167 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3169 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3170 return;
3173 if (src_data_type == WINED3D_DATA_INT)
3175 switch (dst_data_type)
3177 case WINED3D_DATA_FLOAT:
3178 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3179 return;
3180 case WINED3D_DATA_UINT:
3181 if (size == 1)
3182 string_buffer_sprintf(dst_param, "uint(%s)", src_param);
3183 else
3184 string_buffer_sprintf(dst_param, "uvec%u(%s)", size, src_param);
3185 return;
3186 default:
3187 break;
3191 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3192 string_buffer_sprintf(dst_param, "%s", src_param);
3195 /* From a given parameter token, generate the corresponding GLSL string.
3196 * Also, return the actual register name and swizzle in case the
3197 * caller needs this information as well. */
3198 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3199 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3200 enum wined3d_data_type data_type)
3202 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3203 struct wined3d_string_buffer *param_str = string_buffer_get(priv->string_buffers);
3204 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3205 enum wined3d_data_type param_data_type;
3206 BOOL is_color = FALSE;
3207 char swizzle_str[6];
3208 unsigned int size;
3210 glsl_src->param_str[0] = '\0';
3211 swizzle_str[0] = '\0';
3213 shader_glsl_get_register_name(&wined3d_src->reg, data_type, reg_name, &is_color, ctx);
3214 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3216 switch (wined3d_src->reg.type)
3218 case WINED3DSPR_IMMCONST:
3219 param_data_type = data_type;
3220 size = wined3d_src->reg.immconst_type == WINED3D_IMMCONST_SCALAR ? 1 : 4;
3221 break;
3222 case WINED3DSPR_FORKINSTID:
3223 case WINED3DSPR_GSINSTID:
3224 case WINED3DSPR_JOININSTID:
3225 case WINED3DSPR_LOCALTHREADINDEX:
3226 case WINED3DSPR_OUTPOINTID:
3227 case WINED3DSPR_PRIMID:
3228 param_data_type = WINED3D_DATA_INT;
3229 size = 1;
3230 break;
3231 case WINED3DSPR_LOCALTHREADID:
3232 case WINED3DSPR_THREADGROUPID:
3233 case WINED3DSPR_THREADID:
3234 param_data_type = WINED3D_DATA_INT;
3235 size = 3;
3236 break;
3237 default:
3238 param_data_type = WINED3D_DATA_FLOAT;
3239 size = 4;
3240 break;
3243 shader_glsl_sprintf_cast(param_str, reg_name->buffer, data_type, param_data_type, size);
3244 shader_glsl_gen_modifier(wined3d_src->modifiers, param_str->buffer, swizzle_str, glsl_src->param_str);
3246 string_buffer_release(priv->string_buffers, reg_name);
3247 string_buffer_release(priv->string_buffers, param_str);
3250 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3251 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3253 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3256 /* From a given parameter token, generate the corresponding GLSL string.
3257 * Also, return the actual register name and swizzle in case the
3258 * caller needs this information as well. */
3259 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3260 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3262 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3263 struct wined3d_string_buffer *reg_name;
3264 size_t len;
3266 glsl_dst->mask_str[0] = '\0';
3268 reg_name = string_buffer_get(priv->string_buffers);
3269 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type, reg_name, NULL, ins->ctx);
3270 len = min(reg_name->content_size, ARRAY_SIZE(glsl_dst->reg_name) - 1);
3271 memcpy(glsl_dst->reg_name, reg_name->buffer, len);
3272 glsl_dst->reg_name[len] = '\0';
3273 string_buffer_release(priv->string_buffers, reg_name);
3275 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3278 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3279 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3280 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3281 unsigned int dst_idx, enum wined3d_data_type data_type)
3283 struct glsl_dst_param glsl_dst;
3284 DWORD mask;
3286 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3288 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3289 sprintf(glsl_dst.reg_name, "tmp_precise[%u]", dst_idx);
3291 switch (data_type)
3293 case WINED3D_DATA_FLOAT:
3294 case WINED3D_DATA_UNORM:
3295 case WINED3D_DATA_SNORM:
3296 shader_addline(buffer, "%s%s = %s(",
3297 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3298 break;
3299 case WINED3D_DATA_INT:
3300 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3301 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3302 break;
3303 case WINED3D_DATA_RESOURCE:
3304 case WINED3D_DATA_SAMPLER:
3305 case WINED3D_DATA_UINT:
3306 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3307 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3308 break;
3309 default:
3310 FIXME("Unhandled data type %#x.\n", data_type);
3311 shader_addline(buffer, "%s%s = %s(",
3312 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3313 break;
3317 return mask;
3320 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3321 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3323 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3326 /** Process GLSL instruction modifiers */
3327 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3329 const struct wined3d_shader_dst_param *dst;
3330 struct glsl_dst_param dst_param;
3331 DWORD modifiers;
3332 unsigned int i;
3334 for (i = 0; i < ins->dst_count; ++i)
3336 if ((dst = &ins->dst[i])->reg.type == WINED3DSPR_NULL)
3337 continue;
3339 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3341 shader_glsl_add_dst_param(ins, dst, &dst_param);
3342 shader_addline(ins->ctx->buffer, "%s%s = tmp_precise[%u]%s;\n",
3343 dst_param.reg_name, dst_param.mask_str, i, dst_param.mask_str);
3346 if (!(modifiers = dst->modifiers))
3347 continue;
3349 shader_glsl_add_dst_param(ins, dst, &dst_param);
3351 if (modifiers & WINED3DSPDM_SATURATE)
3353 /* _SAT means to clamp the value of the register to between 0 and 1 */
3354 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3355 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3358 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3360 FIXME("_centroid modifier not handled\n");
3363 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3365 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3370 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3372 switch (op)
3374 case WINED3D_SHADER_REL_OP_GT: return ">";
3375 case WINED3D_SHADER_REL_OP_EQ: return "==";
3376 case WINED3D_SHADER_REL_OP_GE: return ">=";
3377 case WINED3D_SHADER_REL_OP_LT: return "<";
3378 case WINED3D_SHADER_REL_OP_NE: return "!=";
3379 case WINED3D_SHADER_REL_OP_LE: return "<=";
3380 default:
3381 FIXME("Unrecognized operator %#x.\n", op);
3382 return "(\?\?)";
3386 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3388 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3391 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3392 unsigned int *coord_size, unsigned int *deriv_size)
3394 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3395 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3397 *coord_size = resource_type_info[resource_type].coord_size;
3398 *deriv_size = *coord_size;
3399 if (is_array)
3400 --(*deriv_size);
3403 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3404 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3406 enum wined3d_shader_resource_type resource_type;
3407 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3408 const struct wined3d_gl_info *gl_info = priv->gl_info;
3409 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3410 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3411 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3412 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3413 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3414 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3415 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3416 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3417 const char *base = "texture", *type_part = "", *suffix = "";
3418 unsigned int coord_size, deriv_size;
3420 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3421 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3422 : ctx->reg_maps->resource_info[resource_idx].type;
3424 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3426 if (resource_type >= ARRAY_SIZE(resource_type_info))
3428 ERR("Unexpected resource type %#x.\n", resource_type);
3429 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3432 /* Note that there's no such thing as a projected cube texture. */
3433 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3434 projected = FALSE;
3436 if (needs_legacy_glsl_syntax(gl_info))
3438 if (shadow)
3439 base = "shadow";
3441 type_part = resource_type_info[resource_type].type_part;
3442 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3443 type_part = "2DRect";
3444 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3445 FIXME("Unhandled resource type %#x.\n", resource_type);
3447 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3449 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3450 suffix = "ARB";
3451 else
3452 FIXME("Unsupported grad function.\n");
3456 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3458 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3459 if (flags & ~texel_fetch_flags)
3460 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3462 base = "texelFetch";
3463 type_part = "";
3466 sample_function->name = string_buffer_get(priv->string_buffers);
3467 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3468 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3470 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3471 if (shadow)
3472 ++coord_size;
3473 sample_function->offset_size = offset ? deriv_size : 0;
3474 sample_function->coord_mask = wined3d_mask_from_size(coord_size);
3475 sample_function->deriv_mask = wined3d_mask_from_size(deriv_size);
3476 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3479 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3480 struct glsl_sample_function *sample_function)
3482 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3484 string_buffer_release(priv->string_buffers, sample_function->name);
3487 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3488 BOOL sign_fixup, enum fixup_channel_source channel_source)
3490 switch(channel_source)
3492 case CHANNEL_SOURCE_ZERO:
3493 strcat(arguments, "0.0");
3494 break;
3496 case CHANNEL_SOURCE_ONE:
3497 strcat(arguments, "1.0");
3498 break;
3500 case CHANNEL_SOURCE_X:
3501 strcat(arguments, reg_name);
3502 strcat(arguments, ".x");
3503 break;
3505 case CHANNEL_SOURCE_Y:
3506 strcat(arguments, reg_name);
3507 strcat(arguments, ".y");
3508 break;
3510 case CHANNEL_SOURCE_Z:
3511 strcat(arguments, reg_name);
3512 strcat(arguments, ".z");
3513 break;
3515 case CHANNEL_SOURCE_W:
3516 strcat(arguments, reg_name);
3517 strcat(arguments, ".w");
3518 break;
3520 default:
3521 FIXME("Unhandled channel source %#x\n", channel_source);
3522 strcat(arguments, "undefined");
3523 break;
3526 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3529 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3530 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3532 unsigned int mask_size, remaining;
3533 DWORD fixup_mask = 0;
3534 char arguments[256];
3535 char mask_str[6];
3537 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3538 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3539 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3540 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3541 if (!(mask &= fixup_mask))
3542 return;
3544 if (is_complex_fixup(fixup))
3546 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3547 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3548 return;
3551 shader_glsl_write_mask_to_str(mask, mask_str);
3552 mask_size = shader_glsl_get_write_mask_size(mask);
3554 arguments[0] = '\0';
3555 remaining = mask_size;
3556 if (mask & WINED3DSP_WRITEMASK_0)
3558 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3559 if (--remaining) strcat(arguments, ", ");
3561 if (mask & WINED3DSP_WRITEMASK_1)
3563 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3564 if (--remaining) strcat(arguments, ", ");
3566 if (mask & WINED3DSP_WRITEMASK_2)
3568 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3569 if (--remaining) strcat(arguments, ", ");
3571 if (mask & WINED3DSP_WRITEMASK_3)
3573 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3574 if (--remaining) strcat(arguments, ", ");
3577 if (mask_size > 1)
3578 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3579 else
3580 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3583 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3585 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3586 struct wined3d_string_buffer *reg_name;
3588 reg_name = string_buffer_get(priv->string_buffers);
3589 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3590 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3591 string_buffer_release(priv->string_buffers, reg_name);
3594 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3595 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3596 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3597 const char *coord_reg_fmt, ...)
3599 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3600 char dst_swizzle[6];
3601 struct color_fixup_desc fixup;
3602 BOOL np2_fixup = FALSE;
3603 va_list args;
3604 int ret;
3606 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3608 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3609 * We actually rely on it for vertex shaders and SM4+. */
3610 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3612 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3613 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3615 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3616 np2_fixup = TRUE;
3618 else
3620 fixup = COLOR_FIXUP_IDENTITY;
3623 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, sample_function->data_type);
3625 if (sample_function->output_single_component)
3626 shader_addline(ins->ctx->buffer, "vec4(");
3628 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3629 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3631 for (;;)
3633 va_start(args, coord_reg_fmt);
3634 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3635 va_end(args);
3636 if (!ret)
3637 break;
3638 if (!string_buffer_resize(ins->ctx->buffer, ret))
3639 break;
3642 if (np2_fixup)
3644 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3645 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3647 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3649 case 1:
3650 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3651 idx >> 1, (idx % 2) ? "z" : "x");
3652 break;
3653 case 2:
3654 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3655 idx >> 1, (idx % 2) ? "zw" : "xy");
3656 break;
3657 case 3:
3658 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3659 idx >> 1, (idx % 2) ? "zw" : "xy");
3660 break;
3661 case 4:
3662 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3663 idx >> 1, (idx % 2) ? "zw" : "xy");
3664 break;
3667 if (dx && dy)
3668 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3669 else if (bias)
3670 shader_addline(ins->ctx->buffer, ", %s", bias);
3671 if (sample_function->offset_size)
3673 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3674 shader_addline(ins->ctx->buffer, ", ");
3675 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3677 shader_addline(ins->ctx->buffer, ")");
3679 if (sample_function->output_single_component)
3680 shader_addline(ins->ctx->buffer, ")");
3682 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3684 if (!is_identity_fixup(fixup))
3685 shader_glsl_color_correction(ins, fixup);
3688 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3690 /* Write the final position.
3692 * OpenGL coordinates specify the center of the pixel while D3D coords
3693 * specify the corner. The offsets are stored in z and w in
3694 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3695 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3696 * a MAD. */
3697 if (use_viewport_index)
3699 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3700 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3702 else
3704 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3705 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3708 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3709 * in utils.c
3711 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3712 * shaders are run before the homogeneous divide, so we have to take the w
3713 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3714 * z = z * 2 - w. */
3715 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3718 /*****************************************************************************
3719 * Begin processing individual instruction opcodes
3720 ****************************************************************************/
3722 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3724 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3725 struct glsl_src_param src0_param;
3726 struct glsl_src_param src1_param;
3727 DWORD write_mask;
3728 const char *op;
3730 /* Determine the GLSL operator to use based on the opcode */
3731 switch (ins->handler_idx)
3733 case WINED3DSIH_ADD: op = "+"; break;
3734 case WINED3DSIH_AND: op = "&"; break;
3735 case WINED3DSIH_DIV: op = "/"; break;
3736 case WINED3DSIH_IADD: op = "+"; break;
3737 case WINED3DSIH_ISHL: op = "<<"; break;
3738 case WINED3DSIH_ISHR: op = ">>"; break;
3739 case WINED3DSIH_MUL: op = "*"; break;
3740 case WINED3DSIH_OR: op = "|"; break;
3741 case WINED3DSIH_SUB: op = "-"; break;
3742 case WINED3DSIH_USHR: op = ">>"; break;
3743 case WINED3DSIH_XOR: op = "^"; break;
3744 default:
3745 op = "<unhandled operator>";
3746 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3747 break;
3750 write_mask = shader_glsl_append_dst(buffer, ins);
3751 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3752 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3753 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3756 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3758 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3759 struct glsl_src_param src0_param;
3760 struct glsl_src_param src1_param;
3761 unsigned int mask_size;
3762 DWORD write_mask;
3763 const char *op;
3765 write_mask = shader_glsl_append_dst(buffer, ins);
3766 mask_size = shader_glsl_get_write_mask_size(write_mask);
3767 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3768 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3770 if (mask_size > 1)
3772 switch (ins->handler_idx)
3774 case WINED3DSIH_EQ: op = "equal"; break;
3775 case WINED3DSIH_IEQ: op = "equal"; break;
3776 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3777 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3778 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3779 case WINED3DSIH_LT: op = "lessThan"; break;
3780 case WINED3DSIH_ILT: op = "lessThan"; break;
3781 case WINED3DSIH_ULT: op = "lessThan"; break;
3782 case WINED3DSIH_NE: op = "notEqual"; break;
3783 case WINED3DSIH_INE: op = "notEqual"; break;
3784 default:
3785 op = "<unhandled operator>";
3786 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3787 break;
3790 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3791 mask_size, op, src0_param.param_str, src1_param.param_str);
3793 else
3795 switch (ins->handler_idx)
3797 case WINED3DSIH_EQ: op = "=="; break;
3798 case WINED3DSIH_IEQ: op = "=="; break;
3799 case WINED3DSIH_GE: op = ">="; break;
3800 case WINED3DSIH_IGE: op = ">="; break;
3801 case WINED3DSIH_UGE: op = ">="; break;
3802 case WINED3DSIH_LT: op = "<"; break;
3803 case WINED3DSIH_ILT: op = "<"; break;
3804 case WINED3DSIH_ULT: op = "<"; break;
3805 case WINED3DSIH_NE: op = "!="; break;
3806 case WINED3DSIH_INE: op = "!="; break;
3807 default:
3808 op = "<unhandled operator>";
3809 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3810 break;
3813 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3814 src0_param.param_str, op, src1_param.param_str);
3818 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3820 struct glsl_src_param src_param;
3821 DWORD write_mask;
3822 const char *op;
3824 switch (ins->handler_idx)
3826 case WINED3DSIH_INEG: op = "-"; break;
3827 case WINED3DSIH_NOT: op = "~"; break;
3828 default:
3829 op = "<unhandled operator>";
3830 ERR("Unhandled opcode %s.\n",
3831 debug_d3dshaderinstructionhandler(ins->handler_idx));
3832 break;
3835 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3836 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3837 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3840 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3842 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3843 struct glsl_src_param src0_param;
3844 struct glsl_src_param src1_param;
3845 DWORD write_mask;
3847 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3848 * If not, we can emulate it. */
3849 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3850 FIXME("64-bit integer multiplies not implemented.\n");
3852 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3854 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3855 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3856 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3858 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3859 src0_param.param_str, src1_param.param_str);
3863 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3865 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3866 struct glsl_src_param src0_param, src1_param;
3867 DWORD write_mask;
3869 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3871 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3873 char dst_mask[6];
3875 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3876 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3877 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3878 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3879 dst_mask, src0_param.param_str, src1_param.param_str);
3881 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3882 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3883 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3884 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3886 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
3887 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3889 else
3891 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3892 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3893 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3894 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3897 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3899 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3900 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3901 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3902 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3906 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3907 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3909 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3910 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3911 const struct wined3d_gl_info *gl_info = priv->gl_info;
3912 struct glsl_src_param src0_param;
3913 DWORD write_mask;
3915 write_mask = shader_glsl_append_dst(buffer, ins);
3916 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3918 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3919 * shader versions WINED3DSIO_MOVA is used for this. */
3920 if (ins->ctx->reg_maps->shader_version.major == 1
3921 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3922 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3924 /* This is a simple floor() */
3925 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3926 if (mask_size > 1) {
3927 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3928 } else {
3929 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3932 else if (ins->handler_idx == WINED3DSIH_MOVA)
3934 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3936 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3938 if (mask_size > 1)
3939 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3940 else
3941 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3943 else
3945 if (mask_size > 1)
3946 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3947 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3948 else
3949 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3950 src0_param.param_str, src0_param.param_str);
3953 else
3955 shader_addline(buffer, "%s);\n", src0_param.param_str);
3959 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3960 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3962 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3963 struct glsl_src_param src0_param;
3964 struct glsl_src_param src1_param;
3965 DWORD dst_write_mask, src_write_mask;
3966 unsigned int dst_size;
3968 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3969 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3971 /* dp4 works on vec4, dp3 on vec3, etc. */
3972 if (ins->handler_idx == WINED3DSIH_DP4)
3973 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3974 else if (ins->handler_idx == WINED3DSIH_DP3)
3975 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3976 else
3977 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3979 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3980 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3982 if (dst_size > 1) {
3983 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3984 } else {
3985 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3989 /* Note that this instruction has some restrictions. The destination write mask
3990 * can't contain the w component, and the source swizzles have to be .xyzw */
3991 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3993 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3994 struct glsl_src_param src0_param;
3995 struct glsl_src_param src1_param;
3996 char dst_mask[6];
3998 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3999 shader_glsl_append_dst(ins->ctx->buffer, ins);
4000 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4001 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4002 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4005 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4007 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4009 if (!stream)
4010 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4011 else
4012 FIXME("Unhandled primitive stream %u.\n", stream);
4015 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4016 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4017 * GLSL uses the value as-is. */
4018 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4020 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4021 struct glsl_src_param src0_param;
4022 struct glsl_src_param src1_param;
4023 DWORD dst_write_mask;
4024 unsigned int dst_size;
4026 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4027 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4029 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4030 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4032 if (dst_size > 1)
4034 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4035 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4037 else
4039 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4040 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4044 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4045 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4047 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4048 bool y_correction = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4049 ? priv->cur_ps_args->y_correction : false;
4050 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4051 struct glsl_src_param src_param;
4052 const char *instruction;
4053 DWORD write_mask;
4054 unsigned i;
4056 /* Determine the GLSL function to use based on the opcode */
4057 /* TODO: Possibly make this a table for faster lookups */
4058 switch (ins->handler_idx)
4060 case WINED3DSIH_ABS: instruction = "abs"; break;
4061 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4062 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4063 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4064 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4065 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4066 case WINED3DSIH_DSY: instruction = y_correction ? "ycorrection.y * dFdy" : "dFdy"; break;
4067 case WINED3DSIH_DSY_COARSE: instruction = y_correction ? "ycorrection.y * dFdyCoarse" : "dFdyCoarse"; break;
4068 case WINED3DSIH_DSY_FINE: instruction = y_correction ? "ycorrection.y * dFdyFine" : "dFdyFine"; break;
4069 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4070 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4071 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4072 case WINED3DSIH_FRC: instruction = "fract"; break;
4073 case WINED3DSIH_IMAX: instruction = "max"; break;
4074 case WINED3DSIH_IMIN: instruction = "min"; break;
4075 case WINED3DSIH_MAX: instruction = "max"; break;
4076 case WINED3DSIH_MIN: instruction = "min"; break;
4077 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4078 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4079 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4080 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4081 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4082 case WINED3DSIH_UMAX: instruction = "max"; break;
4083 case WINED3DSIH_UMIN: instruction = "min"; break;
4084 default: instruction = "";
4085 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4086 break;
4089 write_mask = shader_glsl_append_dst(buffer, ins);
4091 /* In D3D bits are numbered from the most significant bit. */
4092 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4093 shader_addline(buffer, "31 - ");
4094 shader_addline(buffer, "%s(", instruction);
4096 if (ins->src_count)
4098 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4099 shader_addline(buffer, "%s", src_param.param_str);
4100 for (i = 1; i < ins->src_count; ++i)
4102 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4103 shader_addline(buffer, ", %s", src_param.param_str);
4107 shader_addline(buffer, "));\n");
4110 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4112 struct wined3d_shader_dst_param dst;
4113 struct glsl_src_param src;
4114 DWORD write_mask;
4115 const char *fmt;
4116 unsigned int i;
4118 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4119 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4121 dst = ins->dst[0];
4122 for (i = 0; i < 4; ++i)
4124 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4125 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4126 &dst, 0, dst.reg.data_type)))
4127 continue;
4129 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4130 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4134 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4136 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4137 struct wined3d_shader_dst_param dst;
4138 struct glsl_src_param src[4];
4139 const char *instruction;
4140 BOOL tmp_dst = FALSE;
4141 char mask_char[6];
4142 unsigned int i, j;
4143 DWORD write_mask;
4145 switch (ins->handler_idx)
4147 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4148 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4149 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4150 default:
4151 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4152 return;
4155 for (i = 0; i < ins->src_count; ++i)
4157 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4158 && ins->dst[0].reg.type == ins->src[i].reg.type)
4159 tmp_dst = TRUE;
4162 dst = ins->dst[0];
4163 for (i = 0; i < 4; ++i)
4165 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4166 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4167 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4168 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4169 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, 0, dst.reg.data_type)))
4170 continue;
4172 for (j = 0; j < ins->src_count; ++j)
4173 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4174 shader_addline(buffer, "%s(", instruction);
4175 for (j = 0; j < ins->src_count - 2; ++j)
4176 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4177 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4180 if (tmp_dst)
4182 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
4183 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4184 shader_addline(buffer, "tmp0%s);\n", mask_char);
4188 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4190 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4192 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4193 struct glsl_src_param src_param;
4194 unsigned int mask_size;
4195 DWORD write_mask;
4196 char dst_mask[6];
4198 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4199 mask_size = shader_glsl_get_write_mask_size(write_mask);
4200 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4202 if (mask_size > 3)
4203 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4204 src_param.param_str, src_param.param_str);
4205 else
4206 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4207 src_param.param_str, src_param.param_str);
4208 shader_glsl_append_dst(buffer, ins);
4210 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4211 src_param.param_str, src_param.param_str);
4214 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4216 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4217 ins->ctx->reg_maps->shader_version.minor);
4218 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4219 struct glsl_src_param src0_param;
4220 const char *prefix, *suffix;
4221 unsigned int dst_size;
4222 DWORD dst_write_mask;
4224 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4225 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4227 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4228 dst_write_mask = WINED3DSP_WRITEMASK_3;
4230 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4232 switch (ins->handler_idx)
4234 case WINED3DSIH_EXP:
4235 case WINED3DSIH_EXPP:
4236 prefix = "exp2(";
4237 suffix = ")";
4238 break;
4240 case WINED3DSIH_LOG:
4241 case WINED3DSIH_LOGP:
4242 prefix = "log2(abs(";
4243 suffix = "))";
4244 break;
4246 case WINED3DSIH_RCP:
4247 prefix = "1.0 / ";
4248 suffix = "";
4249 break;
4251 case WINED3DSIH_RSQ:
4252 prefix = "inversesqrt(abs(";
4253 suffix = "))";
4254 break;
4256 default:
4257 prefix = "";
4258 suffix = "";
4259 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4260 break;
4263 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4264 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4265 else
4266 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4269 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4270 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4271 * dst.x = 2^(floor(src))
4272 * dst.y = src - floor(src)
4273 * dst.z = 2^src (partial precision is allowed, but optional)
4274 * dst.w = 1.0;
4275 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4276 * dst = 2^src; (partial precision is allowed, but optional)
4278 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4280 if (ins->ctx->reg_maps->shader_version.major < 2)
4282 struct glsl_src_param src_param;
4283 char dst_mask[6];
4285 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4287 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4288 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4289 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4290 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4292 shader_glsl_append_dst(ins->ctx->buffer, ins);
4293 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4294 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4295 return;
4298 shader_glsl_scalar_op(ins);
4301 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4302 const char *vector_constructor, const char *scalar_constructor)
4304 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4305 struct glsl_src_param src_param;
4306 unsigned int mask_size;
4307 DWORD write_mask;
4309 write_mask = shader_glsl_append_dst(buffer, ins);
4310 mask_size = shader_glsl_get_write_mask_size(write_mask);
4311 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4313 if (mask_size > 1)
4314 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4315 else
4316 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4319 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4321 shader_glsl_cast(ins, "ivec", "int");
4324 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4326 shader_glsl_cast(ins, "uvec", "uint");
4329 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4331 shader_glsl_cast(ins, "vec", "float");
4334 /** Process signed comparison opcodes in GLSL. */
4335 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4337 struct glsl_src_param src0_param;
4338 struct glsl_src_param src1_param;
4339 DWORD write_mask;
4340 unsigned int mask_size;
4342 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4343 mask_size = shader_glsl_get_write_mask_size(write_mask);
4344 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4345 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4347 if (mask_size > 1) {
4348 const char *compare;
4350 switch(ins->handler_idx)
4352 case WINED3DSIH_SLT: compare = "lessThan"; break;
4353 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4354 default: compare = "";
4355 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4358 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4359 src0_param.param_str, src1_param.param_str);
4360 } else {
4361 switch(ins->handler_idx)
4363 case WINED3DSIH_SLT:
4364 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4365 * to return 0.0 but step returns 1.0 because step is not < x
4366 * An alternative is a bvec compare padded with an unused second component.
4367 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4368 * issue. Playing with not() is not possible either because not() does not accept
4369 * a scalar.
4371 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4372 src0_param.param_str, src1_param.param_str);
4373 break;
4374 case WINED3DSIH_SGE:
4375 /* Here we can use the step() function and safe a conditional */
4376 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4377 break;
4378 default:
4379 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4385 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4387 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4388 struct wined3d_shader_dst_param dst[2];
4389 struct glsl_src_param src[3];
4390 unsigned int i, j, k;
4391 char mask_char[6];
4392 DWORD write_mask;
4393 BOOL tmp_dst[2];
4395 for (i = 0; i < ins->dst_count; ++i)
4397 tmp_dst[i] = FALSE;
4398 for (j = 0; j < ins->src_count; ++j)
4400 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4401 && ins->dst[i].reg.type == ins->src[j].reg.type)
4402 tmp_dst[i] = TRUE;
4406 dst[0] = ins->dst[0];
4407 dst[1] = ins->dst[1];
4408 for (i = 0; i < 4; ++i)
4410 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4412 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4413 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4414 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4415 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], j, dst[j].reg.data_type)))
4416 continue;
4418 for (k = 0; k < ARRAY_SIZE(src); ++k)
4419 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4421 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4422 src[0].param_str, src[1].param_str, src[2].param_str);
4426 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4428 if (tmp_dst[i])
4430 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4431 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], i, ins->dst[i].reg.data_type);
4432 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4437 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4439 const char *condition_prefix, *condition_suffix;
4440 struct wined3d_shader_dst_param dst;
4441 struct glsl_src_param src0_param;
4442 struct glsl_src_param src1_param;
4443 struct glsl_src_param src2_param;
4444 BOOL temp_destination = FALSE;
4445 DWORD cmp_channel = 0;
4446 unsigned int i, j;
4447 char mask_char[6];
4448 DWORD write_mask;
4450 switch (ins->handler_idx)
4452 case WINED3DSIH_CMP:
4453 condition_prefix = "";
4454 condition_suffix = " >= 0.0";
4455 break;
4457 case WINED3DSIH_CND:
4458 condition_prefix = "";
4459 condition_suffix = " > 0.5";
4460 break;
4462 case WINED3DSIH_MOVC:
4463 condition_prefix = "bool(";
4464 condition_suffix = ")";
4465 break;
4467 default:
4468 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4469 condition_prefix = "<unhandled prefix>";
4470 condition_suffix = "<unhandled suffix>";
4471 break;
4474 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4476 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4477 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4478 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4479 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4481 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4482 condition_prefix, src0_param.param_str, condition_suffix,
4483 src1_param.param_str, src2_param.param_str);
4484 return;
4487 dst = ins->dst[0];
4489 /* Splitting the instruction up in multiple lines imposes a problem:
4490 * The first lines may overwrite source parameters of the following lines.
4491 * Deal with that by using a temporary destination register if needed. */
4492 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4493 && ins->src[0].reg.type == dst.reg.type)
4494 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4495 && ins->src[1].reg.type == dst.reg.type)
4496 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4497 && ins->src[2].reg.type == dst.reg.type))
4498 temp_destination = TRUE;
4500 /* Cycle through all source0 channels. */
4501 for (i = 0; i < 4; ++i)
4503 write_mask = 0;
4504 /* Find the destination channels which use the current source0 channel. */
4505 for (j = 0; j < 4; ++j)
4507 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4509 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4510 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4513 dst.write_mask = ins->dst[0].write_mask & write_mask;
4515 if (temp_destination)
4517 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4518 continue;
4519 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4521 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type)))
4522 continue;
4524 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4525 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4526 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4528 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4529 condition_prefix, src0_param.param_str, condition_suffix,
4530 src1_param.param_str, src2_param.param_str);
4533 if (temp_destination)
4535 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4536 shader_glsl_append_dst(ins->ctx->buffer, ins);
4537 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4541 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4542 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4543 * the compare is done per component of src0. */
4544 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4546 struct glsl_src_param src0_param;
4547 struct glsl_src_param src1_param;
4548 struct glsl_src_param src2_param;
4549 DWORD write_mask;
4550 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4551 ins->ctx->reg_maps->shader_version.minor);
4553 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4555 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4556 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4557 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4558 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4560 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4561 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4562 else
4563 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4564 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4565 return;
4568 shader_glsl_conditional_move(ins);
4571 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4572 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4574 struct glsl_src_param src0_param;
4575 struct glsl_src_param src1_param;
4576 struct glsl_src_param src2_param;
4577 DWORD write_mask;
4579 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4580 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4581 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4582 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4583 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4584 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4587 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4588 Vertex shaders to GLSL codes */
4589 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4591 int i;
4592 int nComponents = 0;
4593 struct wined3d_shader_dst_param tmp_dst = {{0}};
4594 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4595 struct wined3d_shader_instruction tmp_ins;
4597 memset(&tmp_ins, 0, sizeof(tmp_ins));
4599 /* Set constants for the temporary argument */
4600 tmp_ins.ctx = ins->ctx;
4601 tmp_ins.dst_count = 1;
4602 tmp_ins.dst = &tmp_dst;
4603 tmp_ins.src_count = 2;
4604 tmp_ins.src = tmp_src;
4606 switch(ins->handler_idx)
4608 case WINED3DSIH_M4x4:
4609 nComponents = 4;
4610 tmp_ins.handler_idx = WINED3DSIH_DP4;
4611 break;
4612 case WINED3DSIH_M4x3:
4613 nComponents = 3;
4614 tmp_ins.handler_idx = WINED3DSIH_DP4;
4615 break;
4616 case WINED3DSIH_M3x4:
4617 nComponents = 4;
4618 tmp_ins.handler_idx = WINED3DSIH_DP3;
4619 break;
4620 case WINED3DSIH_M3x3:
4621 nComponents = 3;
4622 tmp_ins.handler_idx = WINED3DSIH_DP3;
4623 break;
4624 case WINED3DSIH_M3x2:
4625 nComponents = 2;
4626 tmp_ins.handler_idx = WINED3DSIH_DP3;
4627 break;
4628 default:
4629 break;
4632 tmp_dst = ins->dst[0];
4633 tmp_src[0] = ins->src[0];
4634 tmp_src[1] = ins->src[1];
4635 for (i = 0; i < nComponents; ++i)
4637 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4638 shader_glsl_dot(&tmp_ins);
4639 ++tmp_src[1].reg.idx[0].offset;
4644 The LRP instruction performs a component-wise linear interpolation
4645 between the second and third operands using the first operand as the
4646 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4647 This is equivalent to mix(src2, src1, src0);
4649 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4651 struct glsl_src_param src0_param;
4652 struct glsl_src_param src1_param;
4653 struct glsl_src_param src2_param;
4654 DWORD write_mask;
4656 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4658 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4659 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4660 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4662 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4663 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4666 /** Process the WINED3DSIO_LIT instruction in GLSL:
4667 * dst.x = dst.w = 1.0
4668 * dst.y = (src0.x > 0) ? src0.x
4669 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4670 * where src.w is clamped at +- 128
4672 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4674 struct glsl_src_param src0_param;
4675 struct glsl_src_param src1_param;
4676 struct glsl_src_param src3_param;
4677 char dst_mask[6];
4679 shader_glsl_append_dst(ins->ctx->buffer, ins);
4680 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4682 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4683 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4684 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4686 /* The sdk specifies the instruction like this
4687 * dst.x = 1.0;
4688 * if(src.x > 0.0) dst.y = src.x
4689 * else dst.y = 0.0.
4690 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4691 * else dst.z = 0.0;
4692 * dst.w = 1.0;
4693 * (where power = src.w clamped between -128 and 128)
4695 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4696 * dst.x = 1.0 ... No further explanation needed
4697 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4698 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4699 * dst.w = 1.0. ... Nothing fancy.
4701 * So we still have one conditional in there. So do this:
4702 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4704 * 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),
4705 * 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.
4706 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4708 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4709 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4710 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4712 shader_addline(ins->ctx->buffer,
4713 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4714 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4715 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4716 src0_param.param_str, src3_param.param_str, dst_mask);
4719 /** Process the WINED3DSIO_DST instruction in GLSL:
4720 * dst.x = 1.0
4721 * dst.y = src0.x * src0.y
4722 * dst.z = src0.z
4723 * dst.w = src1.w
4725 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4727 struct glsl_src_param src0y_param;
4728 struct glsl_src_param src0z_param;
4729 struct glsl_src_param src1y_param;
4730 struct glsl_src_param src1w_param;
4731 char dst_mask[6];
4733 shader_glsl_append_dst(ins->ctx->buffer, ins);
4734 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4736 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4737 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4738 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4739 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4741 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4742 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4745 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4746 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4747 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4749 * dst.x = cos(src0.?)
4750 * dst.y = sin(src0.?)
4751 * dst.z = dst.z
4752 * dst.w = dst.w
4754 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4756 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4757 struct glsl_src_param src0_param;
4758 DWORD write_mask;
4760 if (ins->ctx->reg_maps->shader_version.major < 4)
4762 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4764 write_mask = shader_glsl_append_dst(buffer, ins);
4765 switch (write_mask)
4767 case WINED3DSP_WRITEMASK_0:
4768 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4769 break;
4771 case WINED3DSP_WRITEMASK_1:
4772 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4773 break;
4775 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4776 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4777 src0_param.param_str, src0_param.param_str);
4778 break;
4780 default:
4781 ERR("Write mask should be .x, .y or .xy\n");
4782 break;
4785 return;
4788 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4791 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4793 char dst_mask[6];
4795 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4796 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4797 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4799 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4800 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4801 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4803 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4804 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4806 else
4808 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4809 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4810 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4813 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4815 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4816 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4817 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4821 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4822 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4823 * generate invalid code
4825 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4827 struct glsl_src_param src0_param;
4828 DWORD write_mask;
4830 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4831 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4833 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4836 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4837 * Start a for() loop where src1.y is the initial value of aL,
4838 * increment aL by src1.z for a total of src1.x iterations.
4839 * Need to use a temporary variable for this operation.
4841 /* FIXME: I don't think nested loops will work correctly this way. */
4842 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4844 struct wined3d_shader_parser_state *state = ins->ctx->state;
4845 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4846 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4847 const struct wined3d_shader *shader = ins->ctx->shader;
4848 const struct wined3d_shader_lconst *constant;
4849 struct wined3d_string_buffer *reg_name;
4850 const DWORD *control_values = NULL;
4852 if (ins->ctx->reg_maps->shader_version.major < 4)
4854 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4855 * class hardware doesn't support real varying indexing, but Microsoft
4856 * designed this feature for Shader model 2.x+. If the loop control is
4857 * known at compile time, the GLSL compiler can unroll the loop, and
4858 * replace indirect addressing with direct addressing. */
4859 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4861 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4863 if (constant->idx == ins->src[1].reg.idx[0].offset)
4865 control_values = constant->value;
4866 break;
4871 if (control_values)
4873 struct wined3d_shader_loop_control loop_control;
4874 loop_control.count = control_values[0];
4875 loop_control.start = control_values[1];
4876 loop_control.step = (int)control_values[2];
4878 if (loop_control.step > 0)
4880 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4881 state->current_loop_depth, loop_control.start,
4882 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4883 state->current_loop_depth, loop_control.step);
4885 else if (loop_control.step < 0)
4887 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4888 state->current_loop_depth, loop_control.start,
4889 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4890 state->current_loop_depth, loop_control.step);
4892 else
4894 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4895 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4896 state->current_loop_depth, loop_control.count,
4897 state->current_loop_depth);
4900 else
4902 reg_name = string_buffer_get(priv->string_buffers);
4903 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
4905 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4906 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
4907 state->current_loop_depth, reg_name->buffer,
4908 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
4910 string_buffer_release(priv->string_buffers, reg_name);
4914 ++state->current_loop_reg;
4916 else
4918 shader_addline(buffer, "for (;;)\n{\n");
4921 ++state->current_loop_depth;
4924 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4926 struct wined3d_shader_parser_state *state = ins->ctx->state;
4928 shader_addline(ins->ctx->buffer, "}\n");
4930 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4932 --state->current_loop_depth;
4933 --state->current_loop_reg;
4936 if (ins->handler_idx == WINED3DSIH_ENDREP)
4938 --state->current_loop_depth;
4942 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4944 struct wined3d_shader_parser_state *state = ins->ctx->state;
4945 const struct wined3d_shader *shader = ins->ctx->shader;
4946 const struct wined3d_shader_lconst *constant;
4947 struct glsl_src_param src0_param;
4948 const DWORD *control_values = NULL;
4950 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4951 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4953 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4955 if (constant->idx == ins->src[0].reg.idx[0].offset)
4957 control_values = constant->value;
4958 break;
4963 if (control_values)
4965 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4966 state->current_loop_depth, state->current_loop_depth,
4967 control_values[0], state->current_loop_depth);
4969 else
4971 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4972 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4973 state->current_loop_depth, state->current_loop_depth,
4974 src0_param.param_str, state->current_loop_depth);
4977 ++state->current_loop_depth;
4980 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4982 struct glsl_src_param src0_param;
4984 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4985 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4988 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4990 struct glsl_src_param src0_param;
4992 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4993 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4996 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4998 shader_addline(ins->ctx->buffer, "default:\n");
5001 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
5003 struct glsl_src_param src_param;
5004 const char *condition;
5006 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5007 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5008 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5011 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5013 shader_glsl_generate_condition(ins);
5014 shader_addline(ins->ctx->buffer, "{\n");
5017 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5019 struct glsl_src_param src0_param;
5020 struct glsl_src_param src1_param;
5022 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5023 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5025 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5026 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5029 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5031 shader_addline(ins->ctx->buffer, "} else {\n");
5034 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5036 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5037 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5038 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5040 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5041 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5042 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5044 if (!stream)
5045 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5046 else
5047 FIXME("Unhandled primitive stream %u.\n", stream);
5050 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5052 shader_addline(ins->ctx->buffer, "break;\n");
5055 /* FIXME: According to MSDN the compare is done per component. */
5056 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5058 struct glsl_src_param src0_param;
5059 struct glsl_src_param src1_param;
5061 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5062 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5064 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5065 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5068 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5070 const char *op;
5072 switch (ins->handler_idx)
5074 case WINED3DSIH_BREAKP:
5075 op = "break;";
5076 break;
5077 case WINED3DSIH_CONTINUEP:
5078 op = "continue;";
5079 break;
5080 case WINED3DSIH_RETP:
5081 op = "return;";
5082 break;
5083 default:
5084 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5085 return;
5088 shader_glsl_generate_condition(ins);
5089 if (ins->handler_idx == WINED3DSIH_RETP)
5091 shader_addline(ins->ctx->buffer, "{\n");
5092 shader_glsl_generate_shader_epilogue(ins->ctx);
5094 shader_addline(ins->ctx->buffer, " %s\n", op);
5095 if (ins->handler_idx == WINED3DSIH_RETP)
5096 shader_addline(ins->ctx->buffer, "}\n");
5099 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5101 shader_addline(ins->ctx->buffer, "continue;\n");
5104 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5106 shader_addline(ins->ctx->buffer, "}\n");
5107 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5109 /* Subroutines appear at the end of the shader. */
5110 ins->ctx->state->in_subroutine = TRUE;
5113 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5115 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5118 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5120 struct glsl_src_param src1_param;
5122 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5123 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5124 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5127 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5129 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5131 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5133 shader_glsl_generate_shader_epilogue(ins->ctx);
5134 shader_addline(ins->ctx->buffer, "return;\n");
5138 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5140 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5141 ins->ctx->reg_maps->shader_version.minor);
5142 struct glsl_sample_function sample_function;
5143 DWORD sample_flags = 0;
5144 DWORD resource_idx;
5145 DWORD mask = 0, swizzle;
5146 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5147 enum wined3d_shader_resource_type resource_type;
5149 /* 1.0-1.4: Use destination register as sampler source.
5150 * 2.0+: Use provided sampler source. */
5151 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5152 resource_idx = ins->dst[0].reg.idx[0].offset;
5153 else
5154 resource_idx = ins->src[1].reg.idx[0].offset;
5156 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5157 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5158 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5160 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5162 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5163 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5165 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5166 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5168 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5169 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5171 case WINED3D_TTFF_COUNT1:
5172 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5173 break;
5174 case WINED3D_TTFF_COUNT2:
5175 mask = WINED3DSP_WRITEMASK_1;
5176 break;
5177 case WINED3D_TTFF_COUNT3:
5178 mask = WINED3DSP_WRITEMASK_2;
5179 break;
5180 case WINED3D_TTFF_COUNT4:
5181 case WINED3D_TTFF_DISABLE:
5182 mask = WINED3DSP_WRITEMASK_3;
5183 break;
5187 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5189 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5191 if (src_mod == WINED3DSPSM_DZ) {
5192 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5193 mask = WINED3DSP_WRITEMASK_2;
5194 } else if (src_mod == WINED3DSPSM_DW) {
5195 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5196 mask = WINED3DSP_WRITEMASK_3;
5199 else
5201 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5202 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5204 /* ps 2.0 texldp instruction always divides by the fourth component. */
5205 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5206 mask = WINED3DSP_WRITEMASK_3;
5210 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5211 mask |= sample_function.coord_mask;
5212 sample_function.coord_mask = mask;
5214 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5215 else swizzle = ins->src[1].swizzle;
5217 /* 1.0-1.3: Use destination register as coordinate source.
5218 1.4+: Use provided coordinate source register. */
5219 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5221 char coord_mask[6];
5222 shader_glsl_write_mask_to_str(mask, coord_mask);
5223 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5224 "T%u%s", resource_idx, coord_mask);
5226 else
5228 struct glsl_src_param coord_param;
5229 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5230 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5232 struct glsl_src_param bias;
5233 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5234 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5235 NULL, "%s", coord_param.param_str);
5236 } else {
5237 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5238 "%s", coord_param.param_str);
5241 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5244 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5246 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5247 const struct wined3d_gl_info *gl_info = priv->gl_info;
5248 struct glsl_src_param coord_param, dx_param, dy_param;
5249 struct glsl_sample_function sample_function;
5250 DWORD sampler_idx;
5251 DWORD swizzle = ins->src[1].swizzle;
5253 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5255 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5256 shader_glsl_tex(ins);
5257 return;
5260 sampler_idx = ins->src[1].reg.idx[0].offset;
5262 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5263 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5264 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5265 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5267 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5268 NULL, NULL, "%s", coord_param.param_str);
5269 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5272 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5274 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5275 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5276 const struct wined3d_gl_info *gl_info = priv->gl_info;
5277 struct glsl_src_param coord_param, lod_param;
5278 struct glsl_sample_function sample_function;
5279 DWORD swizzle = ins->src[1].swizzle;
5280 DWORD sampler_idx;
5282 sampler_idx = ins->src[1].reg.idx[0].offset;
5284 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5285 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5287 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5289 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5290 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5292 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5293 * However, the NVIDIA drivers allow them in fragment shaders as well,
5294 * even without the appropriate extension. */
5295 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5297 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5298 "%s", coord_param.param_str);
5299 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5302 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5303 unsigned int resource_idx, unsigned int sampler_idx)
5305 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5306 unsigned int i;
5308 for (i = 0; i < sampler_map->count; ++i)
5310 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5311 return entries[i].bind_idx;
5314 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5316 return ~0u;
5319 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5321 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5322 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5323 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5324 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5325 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5326 struct glsl_src_param structure_idx, offset, data, data2;
5327 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5328 enum wined3d_shader_resource_type resource_type;
5329 struct wined3d_string_buffer *address;
5330 enum wined3d_data_type data_type;
5331 unsigned int resource_idx, stride;
5332 const char *op, *resource;
5333 DWORD coord_mask;
5334 BOOL is_tgsm;
5336 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5337 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5338 if (is_tgsm)
5340 if (resource_idx >= reg_maps->tgsm_count)
5342 ERR("Invalid TGSM index %u.\n", resource_idx);
5343 return;
5345 resource = "g";
5346 data_type = WINED3D_DATA_UINT;
5347 coord_mask = 1;
5348 stride = reg_maps->tgsm[resource_idx].stride;
5350 else
5352 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5354 ERR("Invalid UAV index %u.\n", resource_idx);
5355 return;
5357 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5358 if (resource_type >= ARRAY_SIZE(resource_type_info))
5360 ERR("Unexpected resource type %#x.\n", resource_type);
5361 return;
5363 resource = "image";
5364 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5365 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5366 stride = reg_maps->uav_resource_info[resource_idx].stride;
5369 switch (ins->handler_idx)
5371 case WINED3DSIH_ATOMIC_AND:
5372 case WINED3DSIH_IMM_ATOMIC_AND:
5373 if (is_tgsm)
5374 op = "atomicAnd";
5375 else
5376 op = "imageAtomicAnd";
5377 break;
5378 case WINED3DSIH_ATOMIC_CMP_STORE:
5379 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5380 if (is_tgsm)
5381 op = "atomicCompSwap";
5382 else
5383 op = "imageAtomicCompSwap";
5384 break;
5385 case WINED3DSIH_ATOMIC_IADD:
5386 case WINED3DSIH_IMM_ATOMIC_IADD:
5387 if (is_tgsm)
5388 op = "atomicAdd";
5389 else
5390 op = "imageAtomicAdd";
5391 break;
5392 case WINED3DSIH_ATOMIC_IMAX:
5393 case WINED3DSIH_IMM_ATOMIC_IMAX:
5394 if (is_tgsm)
5395 op = "atomicMax";
5396 else
5397 op = "imageAtomicMax";
5398 if (data_type != WINED3D_DATA_INT)
5400 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5401 return;
5403 break;
5404 case WINED3DSIH_ATOMIC_IMIN:
5405 case WINED3DSIH_IMM_ATOMIC_IMIN:
5406 if (is_tgsm)
5407 op = "atomicMin";
5408 else
5409 op = "imageAtomicMin";
5410 if (data_type != WINED3D_DATA_INT)
5412 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5413 return;
5415 break;
5416 case WINED3DSIH_ATOMIC_OR:
5417 case WINED3DSIH_IMM_ATOMIC_OR:
5418 if (is_tgsm)
5419 op = "atomicOr";
5420 else
5421 op = "imageAtomicOr";
5422 break;
5423 case WINED3DSIH_ATOMIC_UMAX:
5424 case WINED3DSIH_IMM_ATOMIC_UMAX:
5425 if (is_tgsm)
5426 op = "atomicMax";
5427 else
5428 op = "imageAtomicMax";
5429 if (data_type != WINED3D_DATA_UINT)
5431 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5432 return;
5434 break;
5435 case WINED3DSIH_ATOMIC_UMIN:
5436 case WINED3DSIH_IMM_ATOMIC_UMIN:
5437 if (is_tgsm)
5438 op = "atomicMin";
5439 else
5440 op = "imageAtomicMin";
5441 if (data_type != WINED3D_DATA_UINT)
5443 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5444 return;
5446 break;
5447 case WINED3DSIH_ATOMIC_XOR:
5448 case WINED3DSIH_IMM_ATOMIC_XOR:
5449 if (is_tgsm)
5450 op = "atomicXor";
5451 else
5452 op = "imageAtomicXor";
5453 break;
5454 case WINED3DSIH_IMM_ATOMIC_EXCH:
5455 if (is_tgsm)
5456 op = "atomicExchange";
5457 else
5458 op = "imageAtomicExchange";
5459 break;
5460 default:
5461 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5462 return;
5465 address = string_buffer_get(priv->string_buffers);
5466 if (stride)
5468 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5469 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5470 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5472 else
5474 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5475 string_buffer_sprintf(address, "%s", offset.param_str);
5476 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5477 shader_addline(address, "/ 4");
5480 if (is_imm_instruction)
5481 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5483 if (is_tgsm)
5484 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5485 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5486 else
5487 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5488 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5490 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5491 shader_addline(buffer, "%s", data.param_str);
5492 if (ins->src_count >= 3)
5494 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5495 shader_addline(buffer, ", %s", data2.param_str);
5498 if (is_imm_instruction)
5499 shader_addline(buffer, ")");
5500 shader_addline(buffer, ");\n");
5502 string_buffer_release(priv->string_buffers, address);
5505 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5507 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5508 const char *op;
5510 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5511 op = "atomicCounterIncrement";
5512 else
5513 op = "atomicCounterDecrement";
5515 shader_glsl_append_dst(ins->ctx->buffer, ins);
5516 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5519 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5521 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5522 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5523 enum wined3d_shader_resource_type resource_type;
5524 struct glsl_src_param image_coord_param;
5525 enum wined3d_data_type data_type;
5526 DWORD coord_mask, write_mask;
5527 unsigned int uav_idx;
5528 char dst_swizzle[6];
5530 uav_idx = ins->src[1].reg.idx[0].offset;
5531 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5533 ERR("Invalid UAV index %u.\n", uav_idx);
5534 return;
5536 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5537 if (resource_type >= ARRAY_SIZE(resource_type_info))
5539 ERR("Unexpected resource type %#x.\n", resource_type);
5540 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5542 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5543 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5545 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5546 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5548 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5549 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5550 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5553 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5555 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5556 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5557 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5558 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5559 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5560 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5561 struct glsl_src_param structure_idx, offset;
5562 struct wined3d_string_buffer *address;
5563 struct wined3d_shader_dst_param dst;
5564 const char *function, *resource;
5566 resource_idx = src->reg.idx[0].offset;
5567 if (src->reg.type == WINED3DSPR_RESOURCE)
5569 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5571 ERR("Invalid resource index %u.\n", resource_idx);
5572 return;
5574 stride = reg_maps->resource_info[resource_idx].stride;
5575 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5576 function = "texelFetch";
5577 resource = "sampler";
5579 else if (src->reg.type == WINED3DSPR_UAV)
5581 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5583 ERR("Invalid UAV index %u.\n", resource_idx);
5584 return;
5586 stride = reg_maps->uav_resource_info[resource_idx].stride;
5587 bind_idx = resource_idx;
5588 function = "imageLoad";
5589 resource = "image";
5591 else
5593 if (resource_idx >= reg_maps->tgsm_count)
5595 ERR("Invalid TGSM index %u.\n", resource_idx);
5596 return;
5598 stride = reg_maps->tgsm[resource_idx].stride;
5599 bind_idx = resource_idx;
5600 function = NULL;
5601 resource = "g";
5604 address = string_buffer_get(priv->string_buffers);
5605 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5607 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5608 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5610 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5611 shader_addline(address, "%s / 4", offset.param_str);
5613 dst = ins->dst[0];
5614 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5616 /* The instruction is split into multiple lines. The first lines may
5617 * overwrite source parameters of the following lines. */
5618 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5619 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5622 for (i = 0; i < 4; ++i)
5624 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5625 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type))
5626 continue;
5628 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5629 if (function)
5630 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5631 function, prefix, resource, bind_idx, address->buffer, swizzle);
5632 else
5633 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5634 prefix, resource, bind_idx, address->buffer, swizzle);
5637 string_buffer_release(priv->string_buffers, address);
5640 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5642 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5643 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5644 struct glsl_src_param image_coord_param, image_data_param;
5645 enum wined3d_shader_resource_type resource_type;
5646 enum wined3d_data_type data_type;
5647 unsigned int uav_idx;
5648 DWORD coord_mask;
5650 uav_idx = ins->dst[0].reg.idx[0].offset;
5651 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5653 ERR("Invalid UAV index %u.\n", uav_idx);
5654 return;
5656 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5657 if (resource_type >= ARRAY_SIZE(resource_type_info))
5659 ERR("Unexpected resource type %#x.\n", resource_type);
5660 return;
5662 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5663 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5665 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5666 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5667 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5668 shader_glsl_get_prefix(version->type), uav_idx,
5669 image_coord_param.param_str, image_data_param.param_str);
5672 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5674 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5675 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5676 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5677 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5678 struct glsl_src_param structure_idx, offset, data;
5679 unsigned int i, resource_idx, stride, src_idx = 0;
5680 struct wined3d_string_buffer *address;
5681 DWORD write_mask;
5682 BOOL is_tgsm;
5684 resource_idx = ins->dst[0].reg.idx[0].offset;
5685 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5686 if (is_tgsm)
5688 if (resource_idx >= reg_maps->tgsm_count)
5690 ERR("Invalid TGSM index %u.\n", resource_idx);
5691 return;
5693 stride = reg_maps->tgsm[resource_idx].stride;
5695 else
5697 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5699 ERR("Invalid UAV index %u.\n", resource_idx);
5700 return;
5702 stride = reg_maps->uav_resource_info[resource_idx].stride;
5705 address = string_buffer_get(priv->string_buffers);
5706 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5708 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5709 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5711 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5712 shader_addline(address, "%s / 4", offset.param_str);
5714 for (i = 0; i < 4; ++i)
5716 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5717 continue;
5719 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5721 if (is_tgsm)
5722 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5723 prefix, resource_idx, address->buffer, i, data.param_str);
5724 else
5725 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5726 prefix, resource_idx, address->buffer, i, data.param_str);
5729 string_buffer_release(priv->string_buffers, address);
5732 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5734 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5735 unsigned int sync_flags = ins->flags;
5737 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5739 shader_addline(buffer, "barrier();\n");
5740 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5743 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5745 shader_addline(buffer, "memoryBarrierShared();\n");
5746 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5749 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
5751 shader_addline(buffer, "memoryBarrier();\n");
5752 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
5755 if (sync_flags)
5756 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5759 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5760 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5762 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5763 unsigned int idx = reg->idx[0].offset;
5765 if (reg->type == WINED3DSPR_RESOURCE)
5767 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5769 ERR("Invalid resource index %u.\n", idx);
5770 return NULL;
5772 return &reg_maps->resource_info[idx];
5775 if (reg->type == WINED3DSPR_UAV)
5777 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5779 ERR("Invalid UAV index %u.\n", idx);
5780 return NULL;
5782 return &reg_maps->uav_resource_info[idx];
5785 FIXME("Unhandled register type %#x.\n", reg->type);
5786 return NULL;
5789 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5791 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5792 const struct wined3d_shader_resource_info *resource_info;
5793 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5794 unsigned int resource_idx;
5795 char dst_swizzle[6];
5796 DWORD write_mask;
5798 write_mask = shader_glsl_append_dst(buffer, ins);
5799 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5801 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5802 return;
5803 resource_idx = ins->src[0].reg.idx[0].offset;
5805 shader_addline(buffer, "ivec2(");
5806 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5808 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5809 resource_idx, WINED3D_SAMPLER_DEFAULT);
5810 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5812 else
5814 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5816 if (resource_info->stride)
5817 shader_addline(buffer, " / %u", resource_info->stride);
5818 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5819 shader_addline(buffer, " * 4");
5820 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5823 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5825 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5826 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5829 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5831 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5834 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5836 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5837 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5838 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5839 const struct wined3d_gl_info *gl_info = priv->gl_info;
5840 enum wined3d_shader_resource_type resource_type;
5841 enum wined3d_shader_register_type reg_type;
5842 unsigned int resource_idx, bind_idx, i;
5843 enum wined3d_data_type dst_data_type;
5844 struct glsl_src_param lod_param;
5845 BOOL supports_mipmaps;
5846 char dst_swizzle[6];
5847 DWORD write_mask;
5849 dst_data_type = ins->dst[0].reg.data_type;
5850 if (ins->flags == WINED3DSI_RESINFO_UINT)
5851 dst_data_type = WINED3D_DATA_UINT;
5852 else if (ins->flags)
5853 FIXME("Unhandled flags %#x.\n", ins->flags);
5855 reg_type = ins->src[1].reg.type;
5856 resource_idx = ins->src[1].reg.idx[0].offset;
5857 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5858 if (reg_type == WINED3DSPR_RESOURCE)
5860 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5861 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5862 resource_idx, WINED3D_SAMPLER_DEFAULT);
5864 else
5866 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5867 bind_idx = resource_idx;
5870 if (resource_type >= ARRAY_SIZE(resource_type_info))
5872 ERR("Unexpected resource type %#x.\n", resource_type);
5873 return;
5876 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, dst_data_type);
5877 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5879 if (dst_data_type == WINED3D_DATA_UINT)
5880 shader_addline(buffer, "uvec4(");
5881 else
5882 shader_addline(buffer, "vec4(");
5884 if (reg_type == WINED3DSPR_RESOURCE)
5886 shader_addline(buffer, "textureSize(%s_sampler%u",
5887 shader_glsl_get_prefix(version->type), bind_idx);
5889 else
5891 shader_addline(buffer, "imageSize(%s_image%u",
5892 shader_glsl_get_prefix(version->type), bind_idx);
5895 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5896 if (supports_mipmaps)
5897 shader_addline(buffer, ", %s", lod_param.param_str);
5898 shader_addline(buffer, "), ");
5900 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5901 shader_addline(buffer, "0, ");
5903 if (supports_mipmaps)
5905 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5907 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5908 shader_glsl_get_prefix(version->type), bind_idx);
5910 else
5912 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5913 shader_addline(buffer, "1");
5916 else
5918 shader_addline(buffer, "1");
5921 shader_addline(buffer, ")%s);\n", dst_swizzle);
5924 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
5926 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5927 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5928 const struct wined3d_gl_info *gl_info = priv->gl_info;
5929 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5930 const struct wined3d_shader_dst_param *dst = ins->dst;
5931 const struct wined3d_shader_src_param *src = ins->src;
5932 enum wined3d_shader_resource_type resource_type;
5933 enum wined3d_data_type dst_data_type;
5934 unsigned int resource_idx, bind_idx;
5935 char dst_swizzle[6];
5936 DWORD write_mask;
5938 dst_data_type = dst->reg.data_type;
5939 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
5940 dst_data_type = WINED3D_DATA_UINT;
5941 else if (ins->flags)
5942 FIXME("Unhandled flags %#x.\n", ins->flags);
5944 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, 0, dst_data_type);
5945 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
5947 if (dst_data_type == WINED3D_DATA_UINT)
5948 shader_addline(buffer, "uvec4(");
5949 else
5950 shader_addline(buffer, "vec4(");
5952 if (src->reg.type == WINED3DSPR_RASTERIZER)
5954 if (gl_info->supported[ARB_SAMPLE_SHADING])
5956 shader_addline(buffer, "gl_NumSamples");
5958 else
5960 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
5961 shader_addline(buffer, "1");
5964 else
5966 resource_idx = src->reg.idx[0].offset;
5967 resource_type = reg_maps->resource_info[resource_idx].type;
5968 if (resource_type >= ARRAY_SIZE(resource_type_info))
5970 ERR("Unexpected resource type %#x.\n", resource_type);
5971 return;
5973 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5975 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
5977 shader_addline(buffer, "textureSamples(%s_sampler%u)",
5978 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
5980 else
5982 FIXME("textureSamples() is not supported.\n");
5983 shader_addline(buffer, "1");
5987 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
5990 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
5992 const struct wined3d_shader_src_param *input = &ins->src[0];
5993 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5994 struct glsl_src_param sample_param;
5995 char dst_swizzle[6];
5996 DWORD write_mask;
5998 write_mask = shader_glsl_append_dst(buffer, ins);
5999 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
6001 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
6002 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
6003 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
6006 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6008 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6009 struct glsl_src_param coord_param, lod_param, sample_param;
6010 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6011 struct glsl_sample_function sample_function;
6012 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6013 BOOL has_lod_param;
6015 if (wined3d_shader_instruction_has_texel_offset(ins))
6016 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6018 resource_idx = ins->src[1].reg.idx[0].offset;
6019 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6021 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6023 ERR("Invalid resource index %u.\n", resource_idx);
6024 return;
6026 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6028 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6029 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6030 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6031 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6032 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6034 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6035 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6036 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6038 else
6040 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6041 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6042 "%s", coord_param.param_str);
6044 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6047 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6049 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6050 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6051 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6052 struct glsl_sample_function sample_function;
6053 DWORD flags = 0;
6055 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6056 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6057 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6058 flags |= WINED3D_GLSL_SAMPLE_LOD;
6059 if (wined3d_shader_instruction_has_texel_offset(ins))
6060 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6062 resource_idx = ins->src[1].reg.idx[0].offset;
6063 sampler_idx = ins->src[2].reg.idx[0].offset;
6065 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6066 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6068 switch (ins->handler_idx)
6070 case WINED3DSIH_SAMPLE:
6071 break;
6072 case WINED3DSIH_SAMPLE_B:
6073 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6074 lod_param_str = lod_param.param_str;
6075 break;
6076 case WINED3DSIH_SAMPLE_GRAD:
6077 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6078 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6079 dx_param_str = dx_param.param_str;
6080 dy_param_str = dy_param.param_str;
6081 break;
6082 case WINED3DSIH_SAMPLE_LOD:
6083 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6084 lod_param_str = lod_param.param_str;
6085 break;
6086 default:
6087 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6088 break;
6091 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6092 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6093 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6094 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6097 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6098 * to sample from level zero with depth comparison for array textures and cube
6099 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6100 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6101 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6102 unsigned int coord_size, const char *coord_param, const char *ref_param)
6104 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6105 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6106 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6107 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6108 char dst_swizzle[6];
6110 WARN("Emitting textureGrad() for sample_c_lz.\n");
6112 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6113 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, sample_function->data_type);
6114 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6115 sample_function->offset_size ? "Offset" : "",
6116 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6117 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6118 if (sample_function->offset_size)
6120 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6121 shader_addline(buffer, ", ");
6122 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6124 shader_addline(buffer, "))%s);\n", dst_swizzle);
6127 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6129 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6130 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6131 const struct wined3d_shader_resource_info *resource_info;
6132 const struct wined3d_gl_info *gl_info = priv->gl_info;
6133 struct glsl_src_param coord_param, compare_param;
6134 struct glsl_sample_function sample_function;
6135 const char *lod_param = NULL;
6136 unsigned int coord_size;
6137 DWORD flags = 0;
6139 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6141 lod_param = "0";
6142 flags |= WINED3D_GLSL_SAMPLE_LOD;
6145 if (wined3d_shader_instruction_has_texel_offset(ins))
6146 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6148 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6149 return;
6150 resource_idx = ins->src[1].reg.idx[0].offset;
6151 sampler_idx = ins->src[2].reg.idx[0].offset;
6153 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6154 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6155 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6156 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6157 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6158 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6159 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6160 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6161 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6163 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6164 coord_size, coord_param.param_str, compare_param.param_str);
6166 else
6168 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6169 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6170 coord_size, coord_param.param_str, compare_param.param_str);
6172 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6175 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6177 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6178 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6179 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6180 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6181 struct glsl_src_param coord_param, compare_param, offset_param;
6182 const struct wined3d_gl_info *gl_info = priv->gl_info;
6183 const struct wined3d_shader_resource_info *resource_info;
6184 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6185 unsigned int coord_size, offset_size;
6186 char dst_swizzle[6];
6187 BOOL has_offset;
6189 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6191 FIXME("OpenGL implementation does not support textureGather.\n");
6192 return;
6195 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6196 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6197 || wined3d_shader_instruction_has_texel_offset(ins);
6199 resource_param_idx =
6200 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6201 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6202 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6203 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6204 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6206 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6207 return;
6209 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6211 ERR("Unexpected resource type %#x.\n", resource_info->type);
6212 return;
6214 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6216 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6217 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, resource_info->data_type);
6219 shader_glsl_add_src_param(ins, &ins->src[0], wined3d_mask_from_size(coord_size), &coord_param);
6221 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6222 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6223 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6225 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6226 shader_addline(buffer, ", %s", compare_param.param_str);
6228 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6230 shader_glsl_add_src_param(ins, &ins->src[1], wined3d_mask_from_size(offset_size), &offset_param);
6231 shader_addline(buffer, ", %s", offset_param.param_str);
6233 else if (has_offset)
6235 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6236 shader_addline(buffer, ", ");
6237 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6239 if (component_idx)
6240 shader_addline(buffer, ", %u", component_idx);
6242 shader_addline(buffer, ")%s);\n", dst_swizzle);
6245 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6247 /* FIXME: Make this work for more than just 2D textures */
6248 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6249 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6251 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6253 char dst_mask[6];
6255 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6256 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6257 ins->dst[0].reg.idx[0].offset, dst_mask);
6259 else
6261 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6262 DWORD reg = ins->src[0].reg.idx[0].offset;
6263 char dst_swizzle[6];
6265 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6267 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6269 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6270 struct glsl_src_param div_param;
6271 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6273 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6275 if (mask_size > 1)
6276 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6277 else
6278 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6280 else
6282 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6287 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6288 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6289 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6290 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6292 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6293 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6294 struct glsl_sample_function sample_function;
6295 struct glsl_src_param src0_param;
6296 UINT mask_size;
6298 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6300 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6301 * scalar, and projected sampling would require 4.
6303 * It is a dependent read - not valid with conditional NP2 textures
6305 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6306 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6308 switch(mask_size)
6310 case 1:
6311 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6312 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6313 break;
6315 case 2:
6316 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6317 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6318 break;
6320 case 3:
6321 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6322 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6323 break;
6325 default:
6326 FIXME("Unexpected mask size %u\n", mask_size);
6327 break;
6329 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6332 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6333 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6334 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6336 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6337 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6338 struct glsl_src_param src0_param;
6339 DWORD dst_mask;
6340 unsigned int mask_size;
6342 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6343 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6344 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6346 if (mask_size > 1) {
6347 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6348 } else {
6349 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6353 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6354 * Calculate the depth as dst.x / dst.y */
6355 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6357 struct glsl_dst_param dst_param;
6359 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6361 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6362 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6363 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6364 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6365 * >= 1.0 or < 0.0
6367 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6368 dst_param.reg_name, dst_param.reg_name);
6371 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6372 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6373 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6374 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6376 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6378 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6379 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6380 struct glsl_src_param src0_param;
6382 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6384 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6385 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6388 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6389 * Calculate the 1st of a 2-row matrix multiplication. */
6390 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6392 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6393 DWORD reg = ins->dst[0].reg.idx[0].offset;
6394 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6395 struct glsl_src_param src0_param;
6397 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6398 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6401 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6402 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6403 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6405 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6406 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6407 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6408 DWORD reg = ins->dst[0].reg.idx[0].offset;
6409 struct glsl_src_param src0_param;
6411 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6412 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6413 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6416 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6418 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6419 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6420 struct glsl_sample_function sample_function;
6421 DWORD reg = ins->dst[0].reg.idx[0].offset;
6422 struct glsl_src_param src0_param;
6424 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6425 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6427 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6429 /* Sample the texture using the calculated coordinates */
6430 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6431 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6434 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6435 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6436 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6438 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6439 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6440 struct glsl_sample_function sample_function;
6441 DWORD reg = ins->dst[0].reg.idx[0].offset;
6442 struct glsl_src_param src0_param;
6444 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6445 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6447 /* Dependent read, not valid with conditional NP2 */
6448 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6450 /* Sample the texture using the calculated coordinates */
6451 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6452 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6454 tex_mx->current_row = 0;
6457 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6458 * Perform the 3rd row of a 3x3 matrix multiply */
6459 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6461 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6462 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6463 DWORD reg = ins->dst[0].reg.idx[0].offset;
6464 struct glsl_src_param src0_param;
6465 char dst_mask[6];
6467 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6469 shader_glsl_append_dst(ins->ctx->buffer, ins);
6470 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6471 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6473 tex_mx->current_row = 0;
6476 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6477 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6478 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6480 struct glsl_src_param src0_param;
6481 struct glsl_src_param src1_param;
6482 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6483 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6484 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6485 struct glsl_sample_function sample_function;
6486 DWORD reg = ins->dst[0].reg.idx[0].offset;
6487 char coord_mask[6];
6489 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6490 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6492 /* Perform the last matrix multiply operation */
6493 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6494 /* Reflection calculation */
6495 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6497 /* Dependent read, not valid with conditional NP2 */
6498 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6499 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6501 /* Sample the texture */
6502 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6503 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6504 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6506 tex_mx->current_row = 0;
6509 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6510 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6511 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6513 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6514 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6515 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6516 struct glsl_sample_function sample_function;
6517 DWORD reg = ins->dst[0].reg.idx[0].offset;
6518 struct glsl_src_param src0_param;
6519 char coord_mask[6];
6521 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6523 /* Perform the last matrix multiply operation */
6524 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6526 /* Construct the eye-ray vector from w coordinates */
6527 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6528 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6529 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6531 /* Dependent read, not valid with conditional NP2 */
6532 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6533 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6535 /* Sample the texture using the calculated coordinates */
6536 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6537 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6538 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6540 tex_mx->current_row = 0;
6543 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6544 * Apply a fake bump map transform.
6545 * texbem is pshader <= 1.3 only, this saves a few version checks
6547 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6549 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6550 struct glsl_sample_function sample_function;
6551 struct glsl_src_param coord_param;
6552 DWORD sampler_idx;
6553 DWORD mask;
6554 DWORD flags;
6555 char coord_mask[6];
6557 sampler_idx = ins->dst[0].reg.idx[0].offset;
6558 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6559 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6561 /* Dependent read, not valid with conditional NP2 */
6562 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6563 mask = sample_function.coord_mask;
6565 shader_glsl_write_mask_to_str(mask, coord_mask);
6567 /* With projected textures, texbem only divides the static texture coord,
6568 * not the displacement, so we can't let GL handle this. */
6569 if (flags & WINED3D_PSARGS_PROJECTED)
6571 DWORD div_mask=0;
6572 char coord_div_mask[3];
6573 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6575 case WINED3D_TTFF_COUNT1:
6576 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6577 break;
6578 case WINED3D_TTFF_COUNT2:
6579 div_mask = WINED3DSP_WRITEMASK_1;
6580 break;
6581 case WINED3D_TTFF_COUNT3:
6582 div_mask = WINED3DSP_WRITEMASK_2;
6583 break;
6584 case WINED3D_TTFF_COUNT4:
6585 case WINED3D_TTFF_DISABLE:
6586 div_mask = WINED3DSP_WRITEMASK_3;
6587 break;
6589 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6590 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6593 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6595 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6596 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6597 coord_param.param_str, coord_mask);
6599 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6601 struct glsl_src_param luminance_param;
6602 struct glsl_dst_param dst_param;
6604 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6605 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6607 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6608 dst_param.reg_name, dst_param.mask_str,
6609 luminance_param.param_str, sampler_idx, sampler_idx);
6611 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6614 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6616 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6617 struct glsl_src_param src0_param, src1_param;
6619 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6620 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6622 shader_glsl_append_dst(ins->ctx->buffer, ins);
6623 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6624 src0_param.param_str, sampler_idx, src1_param.param_str);
6627 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6628 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6629 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6631 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6632 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6633 struct glsl_sample_function sample_function;
6634 struct wined3d_string_buffer *reg_name;
6636 reg_name = string_buffer_get(priv->string_buffers);
6637 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6639 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6640 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6641 "%s.wx", reg_name->buffer);
6642 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6644 string_buffer_release(priv->string_buffers, reg_name);
6647 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6648 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6649 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6651 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6652 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6653 struct glsl_sample_function sample_function;
6654 struct wined3d_string_buffer *reg_name;
6656 reg_name = string_buffer_get(priv->string_buffers);
6657 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6659 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6660 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6661 "%s.yz", reg_name->buffer);
6662 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6664 string_buffer_release(priv->string_buffers, reg_name);
6667 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6668 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6669 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6671 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6672 struct glsl_sample_function sample_function;
6673 struct glsl_src_param src0_param;
6675 /* Dependent read, not valid with conditional NP2 */
6676 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6677 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6679 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6680 "%s", src0_param.param_str);
6681 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6684 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6685 * If any of the first 3 components are < 0, discard this pixel */
6686 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6688 if (ins->ctx->reg_maps->shader_version.major >= 4)
6690 shader_glsl_generate_condition(ins);
6691 shader_addline(ins->ctx->buffer, " discard;\n");
6693 else
6695 struct glsl_dst_param dst_param;
6697 /* The argument is a destination parameter, and no writemasks are allowed */
6698 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6700 /* 2.0 shaders compare all 4 components in texkill. */
6701 if (ins->ctx->reg_maps->shader_version.major >= 2)
6702 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6703 /* 1.x shaders only compare the first 3 components, probably due to
6704 * the nature of the texkill instruction as a tex* instruction, and
6705 * phase, which kills all .w components. Even if all 4 components are
6706 * defined, only the first 3 are used. */
6707 else
6708 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6712 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6713 * dst = dot2(src0, src1) + src2 */
6714 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6716 struct glsl_src_param src0_param;
6717 struct glsl_src_param src1_param;
6718 struct glsl_src_param src2_param;
6719 DWORD write_mask;
6720 unsigned int mask_size;
6722 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6723 mask_size = shader_glsl_get_write_mask_size(write_mask);
6725 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6726 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6727 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6729 if (mask_size > 1) {
6730 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6731 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6732 } else {
6733 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6734 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6738 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6739 const struct wined3d_shader_signature *input_signature,
6740 const struct wined3d_shader_reg_maps *reg_maps,
6741 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6743 unsigned int i;
6745 for (i = 0; i < input_signature->element_count; ++i)
6747 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6748 const char *semantic_name;
6749 UINT semantic_idx;
6750 char reg_mask[6];
6752 /* Unused */
6753 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6754 continue;
6756 semantic_name = input->semantic_name;
6757 semantic_idx = input->semantic_idx;
6758 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6760 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6762 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6764 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6765 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6767 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6769 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6771 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6773 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6774 input->register_idx, reg_mask);
6776 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6778 if (gl_info->supported[ARB_SAMPLE_SHADING])
6779 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6780 input->register_idx, reg_mask);
6781 else
6782 FIXME("ARB_sample_shading is not supported.\n");
6784 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6786 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6787 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6788 input->register_idx, reg_mask);
6789 else
6790 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6792 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6794 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6795 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6796 input->register_idx, reg_mask);
6797 else
6798 FIXME("ARB_viewport_array is not supported.\n");
6800 else
6802 if (input->sysval_semantic)
6803 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6804 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6805 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6806 shader_glsl_shader_input_name(gl_info),
6807 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6810 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6812 if (args->pointsprite)
6813 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6814 shader->u.ps.input_reg_map[input->register_idx]);
6815 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6816 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6817 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6818 needs_legacy_glsl_syntax(gl_info)
6819 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6820 else
6821 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6822 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6824 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6826 if (!semantic_idx)
6827 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6828 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6829 else if (semantic_idx == 1)
6830 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6831 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6832 else
6833 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6834 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6836 else
6838 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6839 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6844 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6846 struct glsl_program_key key;
6848 key.vs_id = entry->vs.id;
6849 key.hs_id = entry->hs.id;
6850 key.ds_id = entry->ds.id;
6851 key.gs_id = entry->gs.id;
6852 key.ps_id = entry->ps.id;
6853 key.cs_id = entry->cs.id;
6855 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6857 ERR("Failed to insert program entry.\n");
6861 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6862 const struct glsl_program_key *key)
6864 struct wine_rb_entry *entry;
6866 entry = wine_rb_get(&priv->program_lookup, key);
6867 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6870 /* Context activation is done by the caller. */
6871 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6872 struct glsl_shader_prog_link *entry)
6874 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6876 GL_EXTCALL(glDeleteProgram(entry->id));
6877 if (entry->vs.id)
6878 list_remove(&entry->vs.shader_entry);
6879 if (entry->hs.id)
6880 list_remove(&entry->hs.shader_entry);
6881 if (entry->ds.id)
6882 list_remove(&entry->ds.shader_entry);
6883 if (entry->gs.id)
6884 list_remove(&entry->gs.shader_entry);
6885 if (entry->ps.id)
6886 list_remove(&entry->ps.shader_entry);
6887 if (entry->cs.id)
6888 list_remove(&entry->cs.shader_entry);
6889 heap_free(entry);
6892 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6893 const struct wined3d_gl_info *gl_info, const DWORD *map,
6894 const struct wined3d_shader_signature *input_signature,
6895 const struct wined3d_shader_reg_maps *reg_maps_in,
6896 const struct wined3d_shader_signature *output_signature,
6897 const struct wined3d_shader_reg_maps *reg_maps_out)
6899 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6900 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6901 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6902 unsigned int in_count = vec4_varyings(3, gl_info);
6903 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6904 DWORD in_idx, *set = NULL;
6905 unsigned int i, j;
6906 char reg_mask[6];
6908 set = heap_calloc(max_varyings, sizeof(*set));
6910 for (i = 0; i < input_signature->element_count; ++i)
6912 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6914 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6915 continue;
6917 in_idx = map[input->register_idx];
6918 /* Declared, but not read register */
6919 if (in_idx == ~0u)
6920 continue;
6921 if (in_idx >= max_varyings)
6923 FIXME("More input varyings declared than supported, expect issues.\n");
6924 continue;
6927 if (in_idx == in_count)
6928 string_buffer_sprintf(destination, "gl_FrontColor");
6929 else if (in_idx == in_count + 1)
6930 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6931 else
6932 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6934 if (!set[in_idx])
6935 set[in_idx] = ~0u;
6937 for (j = 0; j < output_signature->element_count; ++j)
6939 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6940 DWORD mask;
6942 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6943 || input->semantic_idx != output->semantic_idx
6944 || strcmp(input->semantic_name, output->semantic_name)
6945 || !(mask = input->mask & output->mask))
6946 continue;
6948 if (set[in_idx] == ~0u)
6949 set[in_idx] = 0;
6950 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6951 shader_glsl_write_mask_to_str(mask, reg_mask);
6953 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6954 destination->buffer, reg_mask, output->register_idx, reg_mask);
6958 for (i = 0; i < max_varyings; ++i)
6960 unsigned int size;
6962 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6963 continue;
6965 if (set[i] == ~0u)
6966 set[i] = 0;
6968 size = 0;
6969 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6970 reg_mask[size++] = 'x';
6971 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6972 reg_mask[size++] = 'y';
6973 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6974 reg_mask[size++] = 'z';
6975 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6976 reg_mask[size++] = 'w';
6977 reg_mask[size] = '\0';
6979 if (i == in_count)
6980 string_buffer_sprintf(destination, "gl_FrontColor");
6981 else if (i == in_count + 1)
6982 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6983 else
6984 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6986 if (size == 1)
6987 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6988 else
6989 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6992 heap_free(set);
6993 string_buffer_release(&priv->string_buffers, destination);
6996 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6997 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6998 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6999 BOOL rasterizer_setup)
7001 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7002 char reg_mask[6];
7003 unsigned int i;
7005 for (i = 0; i < output_signature->element_count; ++i)
7007 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7009 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7010 continue;
7012 if (output->stream_idx)
7013 continue;
7015 if (output->register_idx >= input_count)
7016 continue;
7018 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7020 shader_addline(buffer,
7021 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7022 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7026 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7027 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7029 unsigned int i, clip_or_cull_index;
7030 const char *name;
7031 char reg_mask[6];
7033 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7034 /* Assign consecutive indices starting from 0. */
7035 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7036 for (i = 0; i < 4; ++i)
7038 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7039 continue;
7041 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7042 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7043 name, clip_or_cull_index, element->register_idx, reg_mask);
7044 ++clip_or_cull_index;
7048 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7049 const struct wined3d_gl_info *gl_info, const DWORD *map,
7050 const struct wined3d_shader_signature *input_signature,
7051 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7052 const struct wined3d_shader_signature *output_signature,
7053 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7055 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7056 const char *semantic_name;
7057 unsigned int semantic_idx;
7058 char reg_mask[6];
7059 unsigned int i;
7061 /* First, sort out position and point size system values. */
7062 for (i = 0; i < output_signature->element_count; ++i)
7064 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7066 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7067 continue;
7069 if (output->stream_idx)
7070 continue;
7072 semantic_name = output->semantic_name;
7073 semantic_idx = output->semantic_idx;
7074 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7076 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7078 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7079 reg_mask, output->register_idx, reg_mask);
7081 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7083 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7084 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7086 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7088 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7089 output->register_idx, reg_mask);
7091 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7093 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7094 output->register_idx, reg_mask);
7096 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7098 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7100 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7102 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7104 else if (output->sysval_semantic)
7106 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7110 /* Then, setup the pixel shader input. */
7111 if (reg_maps_out->shader_version.major < 4)
7112 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7113 output_signature, reg_maps_out);
7114 else
7115 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7118 /* Context activation is done by the caller. */
7119 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7120 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7121 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7123 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7124 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7125 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7126 const char *semantic_name;
7127 UINT semantic_idx;
7128 char reg_mask[6];
7129 unsigned int i;
7130 GLuint ret;
7132 string_buffer_clear(buffer);
7134 shader_glsl_add_version_declaration(buffer, gl_info);
7136 shader_addline(buffer, "invariant gl_Position;\n");
7138 if (per_vertex_point_size)
7140 shader_addline(buffer, "uniform struct\n{\n");
7141 shader_addline(buffer, " float size_min;\n");
7142 shader_addline(buffer, " float size_max;\n");
7143 shader_addline(buffer, "} ffp_point;\n");
7146 if (ps_major < 3)
7148 DWORD colors_written_mask[2] = {0};
7149 DWORD texcoords_written_mask[WINED3D_MAX_TEXTURES] = {0};
7151 if (!legacy_syntax)
7153 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7154 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7155 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7156 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7159 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7161 for (i = 0; i < vs->output_signature.element_count; ++i)
7163 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7164 DWORD write_mask;
7166 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7167 continue;
7169 semantic_name = output->semantic_name;
7170 semantic_idx = output->semantic_idx;
7171 write_mask = output->mask;
7172 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7174 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7176 if (legacy_syntax)
7177 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7178 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7179 else
7180 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7181 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7183 colors_written_mask[semantic_idx] = write_mask;
7185 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7187 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7188 reg_mask, output->register_idx, reg_mask);
7190 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7192 if (semantic_idx < WINED3D_MAX_TEXTURES)
7194 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7195 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7196 semantic_idx, reg_mask, output->register_idx, reg_mask);
7197 texcoords_written_mask[semantic_idx] = write_mask;
7200 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7202 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7203 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7205 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7207 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7208 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7209 output->register_idx, reg_mask[1]);
7213 for (i = 0; i < 2; ++i)
7215 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7217 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7218 if (!i)
7219 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7220 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7221 reg_mask, reg_mask);
7222 else
7223 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7224 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7225 reg_mask, reg_mask);
7228 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
7230 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7231 continue;
7233 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7235 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7236 continue;
7238 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7239 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7240 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7244 else
7246 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7248 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7249 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7250 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7251 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7254 shader_addline(buffer, "}\n");
7256 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7257 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7258 shader_glsl_compile(gl_info, ret, buffer->buffer);
7260 return ret;
7263 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7264 const struct wined3d_shader *shader)
7266 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7267 unsigned int i, register_idx, component_idx;
7269 shader_addline(buffer, "out shader_in_out\n{\n");
7270 for (i = 0; i < so_desc->element_count; ++i)
7272 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7274 if (e->stream_idx)
7276 FIXME("Unhandled stream %u.\n", e->stream_idx);
7277 continue;
7279 if (!e->semantic_name)
7280 continue;
7281 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7282 continue;
7284 if (component_idx || e->component_count != 4)
7286 if (e->component_count == 1)
7287 shader_addline(buffer, "float");
7288 else
7289 shader_addline(buffer, "vec%u", e->component_count);
7291 shader_addline(buffer, " reg%u_%u_%u;\n",
7292 register_idx, component_idx, component_idx + e->component_count - 1);
7294 else
7296 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7299 shader_addline(buffer, "} shader_out;\n");
7301 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7302 shader->limits->packed_output);
7303 for (i = 0; i < so_desc->element_count; ++i)
7305 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7307 if (e->stream_idx)
7309 FIXME("Unhandled stream %u.\n", e->stream_idx);
7310 continue;
7312 if (!e->semantic_name)
7313 continue;
7314 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7315 continue;
7317 if (component_idx || e->component_count != 4)
7319 DWORD write_mask;
7320 char str_mask[6];
7322 write_mask = wined3d_mask_from_size(e->component_count) << component_idx;
7323 shader_glsl_write_mask_to_str(write_mask, str_mask);
7324 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7325 register_idx, component_idx, component_idx + e->component_count - 1,
7326 register_idx, str_mask);
7328 else
7330 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7333 shader_addline(buffer, "}\n");
7336 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7337 const struct wined3d_shader *shader, unsigned int input_count,
7338 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7340 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7341 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7343 if (rasterizer_setup)
7344 input_count = min(vec4_varyings(4, gl_info), input_count);
7346 if (input_count)
7347 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7349 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7350 prefix, shader->limits->packed_output);
7352 if (rasterizer_setup)
7353 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7354 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7355 else
7356 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7357 &shader->reg_maps, "shader_out", rasterizer_setup);
7359 shader_addline(buffer, "}\n");
7362 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7363 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7364 const char *reg_mask)
7366 if (!constant->sysval_semantic)
7368 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7369 return;
7372 switch (constant->sysval_semantic)
7374 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7375 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7376 case WINED3D_SV_TESS_FACTOR_LINEDET:
7377 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7378 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7379 break;
7380 case WINED3D_SV_TESS_FACTOR_QUADINT:
7381 case WINED3D_SV_TESS_FACTOR_TRIINT:
7382 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7383 break;
7384 default:
7385 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7386 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7390 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7391 const struct wined3d_shader_signature *signature, BOOL input_setup)
7393 unsigned int i, register_count, user_constant_index, user_constant_count;
7395 register_count = user_constant_count = 0;
7396 for (i = 0; i < signature->element_count; ++i)
7398 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7399 register_count = max(constant->register_idx + 1, register_count);
7400 if (!constant->sysval_semantic)
7401 ++user_constant_count;
7404 if (user_constant_count)
7405 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7406 input_setup ? "in" : "out", user_constant_count);
7407 if (input_setup)
7408 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7410 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7411 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7413 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7414 char reg_mask[6];
7416 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7418 if (input_setup)
7419 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7420 else
7421 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7423 shader_addline(buffer, " = ");
7425 if (input_setup)
7426 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7427 else
7428 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7430 shader_addline(buffer, ";\n");
7432 shader_addline(buffer, "}\n");
7435 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7436 const struct wined3d_gl_info *gl_info)
7438 const char *output = get_fragment_output(gl_info);
7440 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7441 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7442 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7443 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7444 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7445 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7448 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7449 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7451 const char *output = get_fragment_output(gl_info);
7453 switch (mode)
7455 case WINED3D_FFP_PS_FOG_OFF:
7456 return;
7458 case WINED3D_FFP_PS_FOG_LINEAR:
7459 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7460 break;
7462 case WINED3D_FFP_PS_FOG_EXP:
7463 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7464 break;
7466 case WINED3D_FFP_PS_FOG_EXP2:
7467 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7468 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7469 break;
7471 default:
7472 ERR("Invalid fog mode %#x.\n", mode);
7473 return;
7476 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7477 output, output);
7480 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7481 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7483 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7484 * flipping all the operators here, just negate the comparison below. */
7485 static const char * const comparison_operator[] =
7487 "", /* WINED3D_CMP_NEVER */
7488 "<", /* WINED3D_CMP_LESS */
7489 "==", /* WINED3D_CMP_EQUAL */
7490 "<=", /* WINED3D_CMP_LESSEQUAL */
7491 ">", /* WINED3D_CMP_GREATER */
7492 "!=", /* WINED3D_CMP_NOTEQUAL */
7493 ">=", /* WINED3D_CMP_GREATEREQUAL */
7494 "" /* WINED3D_CMP_ALWAYS */
7497 if (alpha_func == WINED3D_CMP_ALWAYS)
7498 return;
7500 if (alpha_func != WINED3D_CMP_NEVER)
7501 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7502 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7503 shader_addline(buffer, " discard;\n");
7506 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7507 const char *colour, const char *colour_key_low, const char *colour_key_high)
7509 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7510 colour, colour_key_low, colour, colour_key_high);
7511 shader_addline(buffer, " discard;\n");
7514 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7515 const struct wined3d_gl_info *gl_info)
7517 if (gl_info->supported[ARB_CULL_DISTANCE])
7518 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7519 if (gl_info->supported[ARB_GPU_SHADER5])
7520 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7521 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7522 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7523 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7524 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7525 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7526 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7527 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7528 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7529 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7530 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7531 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7532 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7533 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7534 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7535 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7536 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7537 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7538 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7539 if (gl_info->supported[ARB_TEXTURE_GATHER])
7540 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7541 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7542 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7543 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7544 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7545 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7546 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7547 if (gl_info->supported[EXT_GPU_SHADER4])
7548 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7549 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7550 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7551 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7552 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7555 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7556 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7557 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7559 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7560 struct wined3d_string_buffer *src, *assignment;
7561 enum wined3d_data_type dst_data_type;
7562 const char *swizzle;
7563 unsigned int i;
7565 if (output_signature->element_count)
7567 src = string_buffer_get(string_buffers);
7568 assignment = string_buffer_get(string_buffers);
7569 for (i = 0; i < output_signature->element_count; ++i)
7571 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7573 /* register_idx is set to ~0u for non-color outputs. */
7574 if (output->register_idx == ~0u)
7575 continue;
7576 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7578 FIXME("Unhandled component type %#x.\n", output->component_type);
7579 continue;
7581 dst_data_type = component_type_info[output->component_type].data_type;
7582 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7583 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7584 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7585 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7586 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7588 string_buffer_release(string_buffers, src);
7589 string_buffer_release(string_buffers, assignment);
7591 else
7593 DWORD mask = shader->reg_maps.rt_mask;
7595 while (mask)
7597 i = wined3d_bit_scan(&mask);
7598 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7599 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7604 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7605 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7606 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7608 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7610 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7611 if (reg_maps->shader_version.major < 2)
7612 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7614 if (args->srgb_correction)
7615 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7617 /* SM < 3 does not replace the fog stage. */
7618 if (reg_maps->shader_version.major < 3)
7619 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7621 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7623 if (reg_maps->sample_mask)
7624 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7626 if (!use_legacy_fragment_output(gl_info))
7627 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7630 /* Context activation is done by the caller. */
7631 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7632 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7633 const struct wined3d_shader *shader, const struct ps_compile_args *args,
7634 struct ps_np2fixup_info *np2fixup_info)
7636 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7637 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7638 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7639 const char *prefix = shader_glsl_get_prefix(version->type);
7640 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7641 unsigned int i, extra_constants_needed = 0;
7642 struct shader_glsl_ctx_priv priv_ctx;
7643 GLuint shader_id;
7644 uint32_t map;
7646 memset(&priv_ctx, 0, sizeof(priv_ctx));
7647 priv_ctx.gl_info = gl_info;
7648 priv_ctx.cur_ps_args = args;
7649 priv_ctx.cur_np2fixup_info = np2fixup_info;
7650 priv_ctx.string_buffers = string_buffers;
7652 shader_glsl_add_version_declaration(buffer, gl_info);
7654 shader_glsl_enable_extensions(buffer, gl_info);
7655 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7656 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7657 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7658 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7659 if (shader_glsl_use_explicit_attrib_location(gl_info))
7660 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7661 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7662 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7663 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7664 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7665 if (gl_info->supported[ARB_SAMPLE_SHADING])
7666 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7667 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7668 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7669 /* The spec says that it doesn't have to be explicitly enabled, but the
7670 * nvidia drivers write a warning if we don't do so. */
7671 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7672 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7674 /* Base Declarations */
7675 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7677 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7679 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7680 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7681 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7682 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7685 /* Declare uniforms for NP2 texcoord fixup:
7686 * This is NOT done inside the loop that declares the texture samplers
7687 * since the NP2 fixup code is currently only used for the GeforceFX
7688 * series and when forcing the ARB_npot extension off. Modern cards just
7689 * skip the code anyway, so put it inside a separate loop. */
7690 if (args->np2_fixup)
7692 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7693 unsigned int cur = 0;
7695 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7696 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7697 * samplerNP2Fixup stores texture dimensions and is updated through
7698 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7700 for (i = 0; i < shader->limits->sampler; ++i)
7702 enum wined3d_shader_resource_type resource_type;
7704 resource_type = pixelshader_get_resource_type(reg_maps, i, args->tex_types);
7706 if (!resource_type || !(args->np2_fixup & (1u << i)))
7707 continue;
7709 if (resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7711 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7712 continue;
7715 fixup->idx[i] = cur++;
7718 fixup->num_consts = (cur + 1) >> 1;
7719 fixup->active = args->np2_fixup;
7720 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7723 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7725 shader_addline(buffer, "uniform struct\n{\n");
7726 shader_addline(buffer, " vec4 color;\n");
7727 shader_addline(buffer, " float density;\n");
7728 shader_addline(buffer, " float end;\n");
7729 shader_addline(buffer, " float scale;\n");
7730 shader_addline(buffer, "} ffp_fog;\n");
7732 if (legacy_syntax)
7734 if (glsl_is_color_reg_read(shader, 0))
7735 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7736 if (glsl_is_color_reg_read(shader, 1))
7737 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7738 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7739 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7741 else
7743 if (glsl_is_color_reg_read(shader, 0))
7744 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7745 if (glsl_is_color_reg_read(shader, 1))
7746 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7747 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7748 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7749 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7753 if (version->major >= 3)
7755 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7757 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7758 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7759 shader->u.ps.interpolation_mode, version->major >= 4);
7760 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7763 map = reg_maps->bumpmat;
7764 while (map)
7766 i = wined3d_bit_scan(&map);
7767 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7769 if (reg_maps->luminanceparams & (1u << i))
7771 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7772 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7773 extra_constants_needed++;
7776 extra_constants_needed++;
7779 if (args->srgb_correction)
7781 shader_addline(buffer, "const vec4 srgb_const0 = ");
7782 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7783 shader_addline(buffer, ";\n");
7784 shader_addline(buffer, "const vec4 srgb_const1 = ");
7785 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7786 shader_addline(buffer, ";\n");
7788 if ((reg_maps->usesdsy && wined3d_settings.offscreen_rendering_mode != ORM_FBO)
7789 || (reg_maps->vpos && !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]))
7791 ++extra_constants_needed;
7792 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7794 if (reg_maps->vpos)
7796 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7798 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7799 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7800 args->y_correction ? "origin_upper_left, " : "");
7801 else if (args->y_correction)
7802 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7804 shader_addline(buffer, "vec4 vpos;\n");
7807 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7808 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7810 if (!use_legacy_fragment_output(gl_info))
7812 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7814 if (args->dual_source_blend)
7815 shader_addline(buffer, "vec4 ps_out[2];\n");
7816 else
7817 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7818 if (output_signature->element_count)
7820 for (i = 0; i < output_signature->element_count; ++i)
7822 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7824 if (output->register_idx == ~0u)
7825 continue;
7826 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7828 FIXME("Unhandled component type %#x.\n", output->component_type);
7829 continue;
7831 if (shader_glsl_use_explicit_attrib_location(gl_info))
7833 if (args->dual_source_blend)
7834 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7835 else
7836 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7838 shader_addline(buffer, "out %s4 color_out%u;\n",
7839 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7842 else
7844 DWORD mask = reg_maps->rt_mask;
7846 while (mask)
7848 i = wined3d_bit_scan(&mask);
7849 if (shader_glsl_use_explicit_attrib_location(gl_info))
7851 if (args->dual_source_blend)
7852 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7853 else
7854 shader_addline(buffer, "layout(location = %u) ", i);
7856 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7861 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7862 FIXME("Insufficient uniforms to run this shader.\n");
7864 if (shader->u.ps.force_early_depth_stencil)
7865 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7867 shader_addline(buffer, "void main()\n{\n");
7869 if (reg_maps->sample_mask)
7870 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7872 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7873 * add approximately 0.5. This causes off-by-one problems as spotted by
7874 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7875 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7876 * causes precision troubles when we just subtract 0.5.
7878 * To deal with that, just floor() the position. This will eliminate the
7879 * fraction on all cards.
7881 * TODO: Test how this behaves with multisampling.
7883 * An advantage of floor is that it works even if the driver doesn't add
7884 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7885 * to return in gl_FragCoord, even though coordinates specify the pixel
7886 * centers instead of the pixel corners. This code will behave correctly
7887 * on drivers that returns integer values. */
7888 if (reg_maps->vpos)
7890 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7891 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7892 else if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7893 shader_addline(buffer,
7894 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7895 else
7896 shader_addline(buffer,
7897 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7900 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7902 unsigned int i;
7904 if (legacy_syntax)
7906 if (glsl_is_color_reg_read(shader, 0))
7907 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7908 if (glsl_is_color_reg_read(shader, 1))
7909 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7912 map = reg_maps->texcoord;
7913 while (map)
7915 i = wined3d_bit_scan(&map);
7916 if (args->pointsprite)
7917 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7918 else if (args->texcoords_initialized & (1u << i))
7919 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7920 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7921 else
7922 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7923 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7926 if (legacy_syntax)
7927 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7930 /* Pack 3.0 inputs */
7931 if (reg_maps->shader_version.major >= 3)
7932 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7933 reg_maps->shader_version.major >= 4);
7935 /* Base Shader Body */
7936 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7937 return 0;
7939 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7940 if (reg_maps->shader_version.major < 4)
7941 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
7943 shader_addline(buffer, "}\n");
7945 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7946 TRACE("Compiling shader object %u.\n", shader_id);
7947 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7949 return shader_id;
7952 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7953 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7954 const struct vs_compile_args *args)
7956 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7957 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7958 unsigned int i;
7960 /* Unpack outputs. */
7961 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7963 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7964 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7965 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7966 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7968 if (reg_maps->shader_version.major < 3)
7970 if (args->fog_src == VS_FOG_Z)
7971 shader_addline(buffer, "%s = gl_Position.z;\n",
7972 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7973 else if (!reg_maps->fog)
7974 shader_addline(buffer, "%s = 0.0;\n",
7975 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7978 /* We always store the clipplanes without y inversion. */
7979 if (args->clip_enabled)
7981 if (legacy_syntax)
7982 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7983 else
7984 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7985 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7988 if (args->point_size && !args->per_vertex_point_size)
7989 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7991 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7992 shader_glsl_fixup_position(buffer, FALSE);
7995 /* Context activation is done by the caller. */
7996 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
7997 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7999 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8000 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8001 const struct wined3d_shader_version *version = &reg_maps->shader_version;
8002 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8003 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8004 struct shader_glsl_ctx_priv priv_ctx;
8005 GLuint shader_id;
8006 unsigned int i;
8008 memset(&priv_ctx, 0, sizeof(priv_ctx));
8009 priv_ctx.gl_info = gl_info;
8010 priv_ctx.cur_vs_args = args;
8011 priv_ctx.string_buffers = string_buffers;
8013 shader_glsl_add_version_declaration(buffer, gl_info);
8015 shader_glsl_enable_extensions(buffer, gl_info);
8016 if (gl_info->supported[ARB_DRAW_INSTANCED])
8017 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8018 if (shader_glsl_use_explicit_attrib_location(gl_info))
8019 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8020 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
8021 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
8023 /* Base Declarations */
8024 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8026 for (i = 0; i < shader->input_signature.element_count; ++i)
8027 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8029 if (args->point_size && !args->per_vertex_point_size)
8031 shader_addline(buffer, "uniform struct\n{\n");
8032 shader_addline(buffer, " float size;\n");
8033 shader_addline(buffer, " float size_min;\n");
8034 shader_addline(buffer, " float size_max;\n");
8035 shader_addline(buffer, "} ffp_point;\n");
8038 if (!needs_legacy_glsl_syntax(gl_info))
8040 if (args->clip_enabled)
8041 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8043 if (version->major < 3)
8045 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8046 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8047 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
8048 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8052 if (version->major < 4)
8053 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8055 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8056 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8058 if (reg_maps->shader_version.major >= 4)
8059 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8060 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8062 shader_addline(buffer, "void main()\n{\n");
8064 if (reg_maps->input_rel_addressing)
8066 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8067 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8068 for (i = 0; i < shader->input_signature.element_count; ++i)
8070 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8071 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8075 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8076 return 0;
8078 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8079 if (reg_maps->shader_version.major < 4)
8080 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8082 shader_addline(buffer, "}\n");
8084 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8085 TRACE("Compiling shader object %u.\n", shader_id);
8086 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8088 return shader_id;
8091 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8092 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8094 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8095 char reg_mask[6];
8096 unsigned int i;
8098 for (i = 0; i < output_signature->element_count; ++i)
8100 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8102 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8103 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8104 output->register_idx, reg_mask, output->register_idx, reg_mask);
8108 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8109 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8110 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8111 const char *phase_name, unsigned phase_idx)
8113 unsigned int i;
8114 HRESULT hr;
8116 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8117 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8118 for (i = 0; i < phase->temporary_count; ++i)
8119 shader_addline(buffer, "vec4 R%u;\n", i);
8120 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8121 shader_addline(buffer, "}\n");
8122 return hr;
8125 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8126 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8128 if (phase->instance_count)
8130 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8131 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8132 shader_addline(buffer, "}\n");
8134 else
8136 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8140 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8141 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8143 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8144 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8145 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8146 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8147 const struct wined3d_hull_shader *hs = &shader->u.hs;
8148 const struct wined3d_shader_phase *phase;
8149 struct shader_glsl_ctx_priv priv_ctx;
8150 GLuint shader_id;
8151 unsigned int i;
8153 memset(&priv_ctx, 0, sizeof(priv_ctx));
8154 priv_ctx.gl_info = gl_info;
8155 priv_ctx.string_buffers = string_buffers;
8157 shader_glsl_add_version_declaration(buffer, gl_info);
8159 shader_glsl_enable_extensions(buffer, gl_info);
8160 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8162 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8164 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8166 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8167 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8169 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8171 if (hs->phases.control_point)
8173 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8174 shader->limits->packed_output);
8175 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8176 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8177 shader_addline(buffer, "}\n");
8180 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8181 if ((phase = hs->phases.control_point))
8183 for (i = 0; i < phase->temporary_count; ++i)
8184 shader_addline(buffer, "vec4 R%u;\n", i);
8185 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8186 return 0;
8187 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8189 else
8191 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8193 shader_addline(buffer, "}\n");
8195 for (i = 0; i < hs->phases.fork_count; ++i)
8197 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8198 &hs->phases.fork[i], "fork", i)))
8199 return 0;
8202 for (i = 0; i < hs->phases.join_count; ++i)
8204 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8205 &hs->phases.join[i], "join", i)))
8206 return 0;
8209 shader_addline(buffer, "void main()\n{\n");
8210 shader_addline(buffer, "hs_control_point_phase();\n");
8211 if (reg_maps->vocp)
8212 shader_addline(buffer, "barrier();\n");
8213 for (i = 0; i < hs->phases.fork_count; ++i)
8214 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8215 for (i = 0; i < hs->phases.join_count; ++i)
8216 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8217 shader_addline(buffer, "setup_patch_constant_output();\n");
8218 shader_addline(buffer, "}\n");
8220 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8221 TRACE("Compiling shader object %u.\n", shader_id);
8222 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8224 return shader_id;
8227 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8228 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8229 const struct ds_compile_args *args)
8231 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8233 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8234 shader_glsl_fixup_position(buffer, FALSE);
8237 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8238 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8240 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8241 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8242 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8243 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8244 struct shader_glsl_ctx_priv priv_ctx;
8245 GLuint shader_id;
8247 memset(&priv_ctx, 0, sizeof(priv_ctx));
8248 priv_ctx.gl_info = gl_info;
8249 priv_ctx.cur_ds_args = args;
8250 priv_ctx.string_buffers = string_buffers;
8252 shader_glsl_add_version_declaration(buffer, gl_info);
8254 shader_glsl_enable_extensions(buffer, gl_info);
8255 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8257 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8259 shader_addline(buffer, "layout(");
8260 switch (shader->u.ds.tessellator_domain)
8262 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8263 shader_addline(buffer, "isolines");
8264 break;
8265 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8266 shader_addline(buffer, "quads");
8267 break;
8268 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8269 shader_addline(buffer, "triangles");
8270 break;
8272 switch (args->tessellator_output_primitive)
8274 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8275 if (args->render_offscreen)
8276 shader_addline(buffer, ", ccw");
8277 else
8278 shader_addline(buffer, ", cw");
8279 break;
8280 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8281 if (args->render_offscreen)
8282 shader_addline(buffer, ", cw");
8283 else
8284 shader_addline(buffer, ", ccw");
8285 break;
8286 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8287 shader_addline(buffer, ", point_mode");
8288 break;
8289 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8290 break;
8292 switch (args->tessellator_partitioning)
8294 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8295 shader_addline(buffer, ", fractional_odd_spacing");
8296 break;
8297 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8298 shader_addline(buffer, ", fractional_even_spacing");
8299 break;
8300 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8301 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8302 shader_addline(buffer, ", equal_spacing");
8303 break;
8305 shader_addline(buffer, ") in;\n");
8307 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8309 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8310 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8312 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8313 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8314 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8316 shader_addline(buffer, "void main()\n{\n");
8317 shader_addline(buffer, "setup_patch_constant_input();\n");
8319 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8320 return 0;
8322 shader_addline(buffer, "}\n");
8324 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8325 TRACE("Compiling shader object %u.\n", shader_id);
8326 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8328 return shader_id;
8331 /* Context activation is done by the caller. */
8332 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8333 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8335 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8336 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8337 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8338 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8339 const struct wined3d_shader_signature_element *output;
8340 enum wined3d_primitive_type primitive_type;
8341 struct shader_glsl_ctx_priv priv_ctx;
8342 unsigned int max_vertices;
8343 unsigned int i, j;
8344 GLuint shader_id;
8346 memset(&priv_ctx, 0, sizeof(priv_ctx));
8347 priv_ctx.gl_info = gl_info;
8348 priv_ctx.string_buffers = string_buffers;
8350 shader_glsl_add_version_declaration(buffer, gl_info);
8352 shader_glsl_enable_extensions(buffer, gl_info);
8354 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8356 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8357 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8358 if (shader->u.gs.instance_count > 1)
8359 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8360 shader_addline(buffer, ") in;\n");
8362 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8363 if (!(max_vertices = shader->u.gs.vertices_out))
8365 switch (args->primitive_type)
8367 case WINED3D_PT_POINTLIST:
8368 max_vertices = 1;
8369 break;
8370 case WINED3D_PT_LINELIST:
8371 max_vertices = 2;
8372 break;
8373 case WINED3D_PT_TRIANGLELIST:
8374 max_vertices = 3;
8375 break;
8376 default:
8377 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8378 break;
8381 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8382 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8383 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8385 if (!gl_info->supported[ARB_CLIP_CONTROL])
8387 shader_addline(buffer, "uniform vec4 pos_fixup");
8388 if (reg_maps->viewport_array)
8389 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8390 shader_addline(buffer, ";\n");
8393 if (is_rasterization_disabled(shader))
8395 shader_glsl_generate_stream_output_setup(buffer, shader);
8397 else
8399 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8400 gl_info, TRUE, args->interpolation_mode);
8403 shader_addline(buffer, "void main()\n{\n");
8404 if (shader->function)
8406 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8407 return 0;
8409 else
8411 for (i = 0; i < max_vertices; ++i)
8413 for (j = 0; j < shader->output_signature.element_count; ++j)
8415 output = &shader->output_signature.elements[j];
8416 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8417 output->register_idx, i, output->register_idx);
8419 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8420 if (!gl_info->supported[ARB_CLIP_CONTROL])
8421 shader_glsl_fixup_position(buffer, FALSE);
8422 shader_addline(buffer, "EmitVertex();\n");
8425 shader_addline(buffer, "}\n");
8427 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8428 TRACE("Compiling shader object %u.\n", shader_id);
8429 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8431 return shader_id;
8434 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8436 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8437 const struct wined3d_gl_info *gl_info = priv->gl_info;
8438 struct wined3d_string_buffer *buffer = ctx->buffer;
8439 const struct wined3d_shader *shader = ctx->shader;
8441 switch (shader->reg_maps.shader_version.type)
8443 case WINED3D_SHADER_TYPE_PIXEL:
8444 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8445 break;
8446 case WINED3D_SHADER_TYPE_VERTEX:
8447 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8448 break;
8449 case WINED3D_SHADER_TYPE_DOMAIN:
8450 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8451 break;
8452 case WINED3D_SHADER_TYPE_GEOMETRY:
8453 case WINED3D_SHADER_TYPE_COMPUTE:
8454 break;
8455 default:
8456 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8457 break;
8461 /* Context activation is done by the caller. */
8462 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8463 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8464 const struct wined3d_shader *shader)
8466 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8467 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8468 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8469 struct shader_glsl_ctx_priv priv_ctx;
8470 GLuint shader_id;
8471 unsigned int i;
8473 memset(&priv_ctx, 0, sizeof(priv_ctx));
8474 priv_ctx.gl_info = gl_info;
8475 priv_ctx.string_buffers = string_buffers;
8477 shader_glsl_add_version_declaration(buffer, gl_info);
8479 shader_glsl_enable_extensions(buffer, gl_info);
8480 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8482 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8484 for (i = 0; i < reg_maps->tgsm_count; ++i)
8486 if (reg_maps->tgsm[i].size)
8487 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8490 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8491 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8493 shader_addline(buffer, "void main()\n{\n");
8494 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8495 shader_addline(buffer, "}\n");
8497 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8498 TRACE("Compiling shader object %u.\n", shader_id);
8499 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8501 return shader_id;
8504 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8505 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8506 struct wined3d_shader *shader,
8507 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8509 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8510 struct glsl_shader_private *shader_data;
8511 struct ps_np2fixup_info *np2fixup;
8512 UINT i;
8513 DWORD new_size;
8514 GLuint ret;
8516 if (!shader->backend_data)
8518 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8520 ERR("Failed to allocate backend data.\n");
8521 return 0;
8524 shader_data = shader->backend_data;
8525 gl_shaders = shader_data->gl_shaders.ps;
8527 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8528 * so a linear search is more performant than a hashmap or a binary search
8529 * (cache coherency etc)
8531 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8533 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8535 if (args->np2_fixup)
8536 *np2fixup_info = &gl_shaders[i].np2fixup;
8537 return gl_shaders[i].id;
8541 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8542 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8544 if (shader_data->num_gl_shaders)
8546 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8547 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8549 else
8551 new_array = heap_alloc(sizeof(*gl_shaders));
8552 new_size = 1;
8555 if(!new_array) {
8556 ERR("Out of memory\n");
8557 return 0;
8559 shader_data->gl_shaders.ps = new_array;
8560 shader_data->shader_array_size = new_size;
8561 gl_shaders = new_array;
8564 gl_shaders[shader_data->num_gl_shaders].args = *args;
8566 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8567 memset(np2fixup, 0, sizeof(*np2fixup));
8568 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8570 string_buffer_clear(buffer);
8571 ret = shader_glsl_generate_fragment_shader(context_gl, buffer, string_buffers, shader, args, np2fixup);
8572 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8574 return ret;
8577 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8578 const DWORD use_map)
8580 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8581 return FALSE;
8582 if ((stored->clip_enabled) != new->clip_enabled)
8583 return FALSE;
8584 if (stored->point_size != new->point_size)
8585 return FALSE;
8586 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8587 return FALSE;
8588 if (stored->flatshading != new->flatshading)
8589 return FALSE;
8590 if (stored->next_shader_type != new->next_shader_type)
8591 return FALSE;
8592 if (stored->next_shader_input_count != new->next_shader_input_count)
8593 return FALSE;
8594 if (stored->fog_src != new->fog_src)
8595 return FALSE;
8596 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8599 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8600 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8602 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8603 uint32_t use_map = context_gl->c.stream_info.use_map;
8604 struct glsl_shader_private *shader_data;
8605 unsigned int i, new_size;
8606 GLuint ret;
8608 if (!shader->backend_data)
8610 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8612 ERR("Failed to allocate backend data.\n");
8613 return 0;
8616 shader_data = shader->backend_data;
8617 gl_shaders = shader_data->gl_shaders.vs;
8619 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8620 * so a linear search is more performant than a hashmap or a binary search
8621 * (cache coherency etc)
8623 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8625 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8626 return gl_shaders[i].id;
8629 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8631 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8633 if (shader_data->num_gl_shaders)
8635 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8636 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8638 else
8640 new_array = heap_alloc(sizeof(*gl_shaders));
8641 new_size = 1;
8644 if(!new_array) {
8645 ERR("Out of memory\n");
8646 return 0;
8648 shader_data->gl_shaders.vs = new_array;
8649 shader_data->shader_array_size = new_size;
8650 gl_shaders = new_array;
8653 gl_shaders[shader_data->num_gl_shaders].args = *args;
8655 string_buffer_clear(&priv->shader_buffer);
8656 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8657 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8659 return ret;
8662 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8663 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8665 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8666 struct glsl_shader_private *shader_data;
8667 unsigned int new_size;
8668 GLuint ret;
8670 if (!shader->backend_data)
8672 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8674 ERR("Failed to allocate backend data.\n");
8675 return 0;
8678 shader_data = shader->backend_data;
8679 gl_shaders = shader_data->gl_shaders.hs;
8681 if (shader_data->num_gl_shaders > 0)
8683 assert(shader_data->num_gl_shaders == 1);
8684 return gl_shaders[0].id;
8687 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8689 assert(!shader_data->gl_shaders.hs);
8690 new_size = 1;
8691 if (!(new_array = heap_alloc(sizeof(*new_array))))
8693 ERR("Failed to allocate GL shaders array.\n");
8694 return 0;
8696 shader_data->gl_shaders.hs = new_array;
8697 shader_data->shader_array_size = new_size;
8698 gl_shaders = new_array;
8700 string_buffer_clear(&priv->shader_buffer);
8701 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8702 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8704 return ret;
8707 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8708 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8710 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8711 struct glsl_shader_private *shader_data;
8712 unsigned int i, new_size;
8713 GLuint ret;
8715 if (!shader->backend_data)
8717 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8719 ERR("Failed to allocate backend data.\n");
8720 return 0;
8723 shader_data = shader->backend_data;
8724 gl_shaders = shader_data->gl_shaders.ds;
8726 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8728 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8729 return gl_shaders[i].id;
8732 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8734 if (shader_data->num_gl_shaders)
8736 new_size = shader_data->shader_array_size + 1;
8737 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8739 else
8741 new_array = heap_alloc(sizeof(*new_array));
8742 new_size = 1;
8745 if (!new_array)
8747 ERR("Failed to allocate GL shaders array.\n");
8748 return 0;
8750 shader_data->gl_shaders.ds = new_array;
8751 shader_data->shader_array_size = new_size;
8752 gl_shaders = new_array;
8754 string_buffer_clear(&priv->shader_buffer);
8755 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8756 gl_shaders[shader_data->num_gl_shaders].args = *args;
8757 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8759 return ret;
8762 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8763 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8765 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8766 struct glsl_shader_private *shader_data;
8767 unsigned int i, new_size;
8768 GLuint ret;
8770 if (!shader->backend_data)
8772 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8774 ERR("Failed to allocate backend data.\n");
8775 return 0;
8778 shader_data = shader->backend_data;
8779 gl_shaders = shader_data->gl_shaders.gs;
8781 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8783 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8784 return gl_shaders[i].id;
8787 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8789 if (shader_data->num_gl_shaders)
8791 new_size = shader_data->shader_array_size + 1;
8792 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8794 else
8796 new_array = heap_alloc(sizeof(*new_array));
8797 new_size = 1;
8800 if (!new_array)
8802 ERR("Failed to allocate GL shaders array.\n");
8803 return 0;
8805 shader_data->gl_shaders.gs = new_array;
8806 shader_data->shader_array_size = new_size;
8807 gl_shaders = new_array;
8809 string_buffer_clear(&priv->shader_buffer);
8810 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8811 gl_shaders[shader_data->num_gl_shaders].args = *args;
8812 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8814 return ret;
8817 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8819 switch (mcs)
8821 case WINED3D_MCS_MATERIAL:
8822 return material;
8823 case WINED3D_MCS_COLOR1:
8824 return "ffp_attrib_diffuse";
8825 case WINED3D_MCS_COLOR2:
8826 return "ffp_attrib_specular";
8827 default:
8828 ERR("Invalid material color source %#x.\n", mcs);
8829 return "<invalid>";
8833 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8834 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8836 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8837 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8838 if (settings->localviewer)
8839 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8840 else
8841 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8842 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8843 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8844 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8847 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8848 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8850 const char *diffuse, *specular, *emissive, *ambient;
8851 unsigned int i, idx;
8853 if (!settings->lighting)
8855 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8856 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8857 return;
8860 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8861 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8862 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8863 shader_addline(buffer, "vec3 dir, dst;\n");
8864 shader_addline(buffer, "float att, t;\n");
8866 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8867 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8868 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8869 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8871 idx = 0;
8872 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8874 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8875 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8876 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8877 shader_addline(buffer, "dst.x = 1.0;\n");
8878 if (legacy_lighting)
8880 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8881 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8882 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8884 else
8886 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8888 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8889 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8890 if (!legacy_lighting)
8891 shader_addline(buffer, "att = 1.0 / att;\n");
8892 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8893 if (!settings->normal)
8895 shader_addline(buffer, "}\n");
8896 continue;
8898 shader_addline(buffer, "dir = normalize(dir);\n");
8899 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8900 shader_addline(buffer, "}\n");
8903 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8905 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8906 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8907 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8908 shader_addline(buffer, "dst.x = 1.0;\n");
8909 if (legacy_lighting)
8911 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8912 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8913 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8915 else
8917 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8919 shader_addline(buffer, "dir = normalize(dir);\n");
8920 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8921 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8922 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8923 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8924 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8925 idx, idx, idx, idx);
8926 if (legacy_lighting)
8927 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8928 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8929 idx, idx, idx);
8930 else
8931 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8932 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8933 idx, idx, idx);
8934 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8935 if (!settings->normal)
8937 shader_addline(buffer, "}\n");
8938 continue;
8940 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8941 shader_addline(buffer, "}\n");
8944 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8946 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8947 if (!settings->normal)
8948 continue;
8949 shader_addline(buffer, "att = 1.0;\n");
8950 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8951 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8954 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8956 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8957 if (!settings->normal)
8958 continue;
8959 shader_addline(buffer, "att = 1.0;\n");
8960 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8961 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8964 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8965 ambient, diffuse, emissive);
8966 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8967 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8970 /* Context activation is done by the caller. */
8971 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8972 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8974 static const struct attrib_info
8976 const char type[6];
8977 const char name[24];
8979 attrib_info[] =
8981 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8982 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8983 /* TODO: Indexed vertex blending */
8984 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8985 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8986 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8987 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8988 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8990 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8991 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8992 BOOL output_legacy_fogcoord = legacy_syntax;
8993 BOOL legacy_lighting = priv->legacy_lighting;
8994 GLuint shader_obj;
8995 unsigned int i;
8997 string_buffer_clear(buffer);
8999 shader_glsl_add_version_declaration(buffer, gl_info);
9001 if (shader_glsl_use_explicit_attrib_location(gl_info))
9002 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9004 shader_addline(buffer, "invariant gl_Position;\n");
9006 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
9008 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9010 if (shader_glsl_use_explicit_attrib_location(gl_info))
9011 shader_addline(buffer, "layout(location = %u) ", i);
9012 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9014 shader_addline(buffer, "\n");
9016 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9017 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9018 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9019 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_TEXTURES);
9021 shader_addline(buffer, "uniform struct\n{\n");
9022 shader_addline(buffer, " vec4 emissive;\n");
9023 shader_addline(buffer, " vec4 ambient;\n");
9024 shader_addline(buffer, " vec4 diffuse;\n");
9025 shader_addline(buffer, " vec4 specular;\n");
9026 shader_addline(buffer, " float shininess;\n");
9027 shader_addline(buffer, "} ffp_material;\n");
9029 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9030 shader_addline(buffer, "uniform struct\n{\n");
9031 shader_addline(buffer, " vec4 diffuse;\n");
9032 shader_addline(buffer, " vec4 specular;\n");
9033 shader_addline(buffer, " vec4 ambient;\n");
9034 shader_addline(buffer, " vec4 position;\n");
9035 shader_addline(buffer, " vec3 direction;\n");
9036 shader_addline(buffer, " float range;\n");
9037 shader_addline(buffer, " float falloff;\n");
9038 shader_addline(buffer, " float c_att;\n");
9039 shader_addline(buffer, " float l_att;\n");
9040 shader_addline(buffer, " float q_att;\n");
9041 shader_addline(buffer, " float cos_htheta;\n");
9042 shader_addline(buffer, " float cos_hphi;\n");
9043 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9045 if (settings->point_size)
9047 shader_addline(buffer, "uniform struct\n{\n");
9048 shader_addline(buffer, " float size;\n");
9049 shader_addline(buffer, " float size_min;\n");
9050 shader_addline(buffer, " float size_max;\n");
9051 shader_addline(buffer, " float c_att;\n");
9052 shader_addline(buffer, " float l_att;\n");
9053 shader_addline(buffer, " float q_att;\n");
9054 shader_addline(buffer, "} ffp_point;\n");
9057 if (legacy_syntax)
9059 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9060 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9061 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9062 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9064 else
9066 if (settings->clipping)
9067 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9069 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9070 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9071 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9072 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9075 shader_addline(buffer, "\nvoid main()\n{\n");
9076 shader_addline(buffer, "float m;\n");
9077 shader_addline(buffer, "vec3 r;\n");
9079 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9081 if (attrib_info[i].name[0])
9082 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9083 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9085 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9087 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9088 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9089 && settings->texcoords & (1u << i))
9090 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9093 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9095 if (settings->transformed)
9097 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9098 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9099 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9101 else
9103 for (i = 0; i < settings->vertexblends; ++i)
9104 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9106 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9107 for (i = 0; i < settings->vertexblends + 1; ++i)
9108 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9110 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9111 if (settings->clipping)
9113 if (legacy_syntax)
9114 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9115 else
9116 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9117 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9119 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9122 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9123 if (settings->normal)
9125 if (!settings->vertexblends)
9127 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9129 else
9131 for (i = 0; i < settings->vertexblends + 1; ++i)
9132 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9135 if (settings->normalize)
9136 shader_addline(buffer, "normal = normalize(normal);\n");
9139 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9140 if (legacy_syntax)
9142 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9143 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9145 else
9147 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9148 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9151 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9153 BOOL output_legacy_texcoord = legacy_syntax;
9155 switch (settings->texgen[i] & 0xffff0000)
9157 case WINED3DTSS_TCI_PASSTHRU:
9158 if (settings->texcoords & (1u << i))
9159 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9160 i, i, i);
9161 else if (shader_glsl_full_ffp_varyings(gl_info))
9162 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9163 else
9164 output_legacy_texcoord = FALSE;
9165 break;
9167 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9168 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9169 break;
9171 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9172 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9173 break;
9175 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9176 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9177 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9178 break;
9180 case WINED3DTSS_TCI_SPHEREMAP:
9181 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9182 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9183 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9184 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9185 break;
9187 default:
9188 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9189 break;
9191 if (output_legacy_texcoord)
9192 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9195 switch (settings->fog_mode)
9197 case WINED3D_FFP_VS_FOG_OFF:
9198 output_legacy_fogcoord = FALSE;
9199 break;
9201 case WINED3D_FFP_VS_FOG_FOGCOORD:
9202 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9203 break;
9205 case WINED3D_FFP_VS_FOG_RANGE:
9206 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9207 break;
9209 case WINED3D_FFP_VS_FOG_DEPTH:
9210 if (settings->ortho_fog)
9212 if (gl_info->supported[ARB_CLIP_CONTROL])
9213 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9214 else
9215 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9216 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9218 else if (settings->transformed)
9220 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9222 else
9224 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9226 break;
9228 default:
9229 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9230 break;
9232 if (output_legacy_fogcoord)
9233 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9235 if (settings->point_size)
9237 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9238 " + ffp_point.l_att * length(ec_pos.xyz)"
9239 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9240 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9241 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9244 shader_addline(buffer, "}\n");
9246 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9247 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9249 return shader_obj;
9252 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9253 DWORD argnum, unsigned int stage, DWORD arg)
9255 const char *ret;
9257 if (arg == ARG_UNUSED)
9258 return "<unused arg>";
9260 switch (arg & WINED3DTA_SELECTMASK)
9262 case WINED3DTA_DIFFUSE:
9263 ret = "ffp_varying_diffuse";
9264 break;
9266 case WINED3DTA_CURRENT:
9267 ret = "ret";
9268 break;
9270 case WINED3DTA_TEXTURE:
9271 switch (stage)
9273 case 0: ret = "tex0"; break;
9274 case 1: ret = "tex1"; break;
9275 case 2: ret = "tex2"; break;
9276 case 3: ret = "tex3"; break;
9277 case 4: ret = "tex4"; break;
9278 case 5: ret = "tex5"; break;
9279 case 6: ret = "tex6"; break;
9280 case 7: ret = "tex7"; break;
9281 default:
9282 ret = "<invalid texture>";
9283 break;
9285 break;
9287 case WINED3DTA_TFACTOR:
9288 ret = "tex_factor";
9289 break;
9291 case WINED3DTA_SPECULAR:
9292 ret = "ffp_varying_specular";
9293 break;
9295 case WINED3DTA_TEMP:
9296 ret = "temp_reg";
9297 break;
9299 case WINED3DTA_CONSTANT:
9300 switch (stage)
9302 case 0: ret = "tss_const0"; break;
9303 case 1: ret = "tss_const1"; break;
9304 case 2: ret = "tss_const2"; break;
9305 case 3: ret = "tss_const3"; break;
9306 case 4: ret = "tss_const4"; break;
9307 case 5: ret = "tss_const5"; break;
9308 case 6: ret = "tss_const6"; break;
9309 case 7: ret = "tss_const7"; break;
9310 default:
9311 ret = "<invalid constant>";
9312 break;
9314 break;
9316 default:
9317 return "<unhandled arg>";
9320 if (arg & WINED3DTA_COMPLEMENT)
9322 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9323 if (argnum == 0)
9324 ret = "arg0";
9325 else if (argnum == 1)
9326 ret = "arg1";
9327 else if (argnum == 2)
9328 ret = "arg2";
9331 if (arg & WINED3DTA_ALPHAREPLICATE)
9333 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9334 if (argnum == 0)
9335 ret = "arg0";
9336 else if (argnum == 1)
9337 ret = "arg1";
9338 else if (argnum == 2)
9339 ret = "arg2";
9342 return ret;
9345 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9346 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9348 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9350 if (color && alpha)
9351 dstmask = "";
9352 else if (color)
9353 dstmask = ".xyz";
9354 else
9355 dstmask = ".w";
9357 dstreg = tmp_dst ? "temp_reg" : "ret";
9359 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9360 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9361 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9363 switch (op)
9365 case WINED3D_TOP_DISABLE:
9366 break;
9368 case WINED3D_TOP_SELECT_ARG1:
9369 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9370 break;
9372 case WINED3D_TOP_SELECT_ARG2:
9373 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9374 break;
9376 case WINED3D_TOP_MODULATE:
9377 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9378 break;
9380 case WINED3D_TOP_MODULATE_4X:
9381 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9382 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9383 break;
9385 case WINED3D_TOP_MODULATE_2X:
9386 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9387 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9388 break;
9390 case WINED3D_TOP_ADD:
9391 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9392 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9393 break;
9395 case WINED3D_TOP_ADD_SIGNED:
9396 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9397 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9398 break;
9400 case WINED3D_TOP_ADD_SIGNED_2X:
9401 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9402 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9403 break;
9405 case WINED3D_TOP_SUBTRACT:
9406 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9407 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9408 break;
9410 case WINED3D_TOP_ADD_SMOOTH:
9411 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9412 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9413 break;
9415 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9416 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9417 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9418 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9419 break;
9421 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9422 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9423 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9424 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9425 break;
9427 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9428 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9429 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9430 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9431 break;
9433 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9434 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9435 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9436 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9437 break;
9439 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9440 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9441 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9442 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9443 break;
9445 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9446 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9447 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9448 break;
9450 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9451 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9452 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9453 break;
9455 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9456 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9457 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9458 break;
9459 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9460 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9461 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9462 break;
9464 case WINED3D_TOP_BUMPENVMAP:
9465 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9466 /* These are handled in the first pass, nothing to do. */
9467 break;
9469 case WINED3D_TOP_DOTPRODUCT3:
9470 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9471 dstreg, dstmask, arg1, arg2, dstmask);
9472 break;
9474 case WINED3D_TOP_MULTIPLY_ADD:
9475 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9476 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9477 break;
9479 case WINED3D_TOP_LERP:
9480 /* MSDN isn't quite right here. */
9481 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9482 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9483 break;
9485 default:
9486 FIXME("Unhandled operation %#x.\n", op);
9487 break;
9491 /* Context activation is done by the caller. */
9492 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9493 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9495 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9496 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9497 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9498 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9499 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9500 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9501 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9502 UINT lowest_disabled_stage;
9503 GLuint shader_id;
9504 DWORD arg0, arg1, arg2;
9505 unsigned int stage;
9507 string_buffer_clear(buffer);
9509 /* Find out which textures are read */
9510 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9512 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9513 break;
9515 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9516 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9517 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9519 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9520 || (stage == 0 && settings->color_key_enabled))
9521 tex_map |= 1u << stage;
9522 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9523 tfactor_used = TRUE;
9524 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9525 tempreg_used = TRUE;
9526 if (settings->op[stage].tmp_dst)
9527 tempreg_used = TRUE;
9528 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9529 tss_const_map |= 1u << stage;
9531 switch (settings->op[stage].cop)
9533 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9534 lum_map |= 1u << stage;
9535 /* fall through */
9536 case WINED3D_TOP_BUMPENVMAP:
9537 bump_map |= 1u << stage;
9538 /* fall through */
9539 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9540 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9541 tex_map |= 1u << stage;
9542 break;
9544 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9545 tfactor_used = TRUE;
9546 break;
9548 default:
9549 break;
9552 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9553 continue;
9555 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9556 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9557 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9559 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9560 tex_map |= 1u << stage;
9561 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9562 tfactor_used = TRUE;
9563 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9564 tempreg_used = TRUE;
9565 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9566 tss_const_map |= 1u << stage;
9568 lowest_disabled_stage = stage;
9570 shader_glsl_add_version_declaration(buffer, gl_info);
9572 if (shader_glsl_use_explicit_attrib_location(gl_info))
9573 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9574 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9575 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9576 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9577 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9579 if (!use_legacy_fragment_output(gl_info))
9581 shader_addline(buffer, "vec4 ps_out[1];\n");
9582 if (shader_glsl_use_explicit_attrib_location(gl_info))
9583 shader_addline(buffer, "layout(location = 0) ");
9584 shader_addline(buffer, "out vec4 color_out0;\n");
9587 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9588 shader_addline(buffer, "vec4 ret;\n");
9589 if (tempreg_used || settings->sRGB_write)
9590 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9591 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9593 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9595 const char *sampler_type;
9597 if (tss_const_map & (1u << stage))
9598 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9600 if (!(tex_map & (1u << stage)))
9601 continue;
9603 switch (settings->op[stage].tex_type)
9605 case WINED3D_GL_RES_TYPE_TEX_1D:
9606 sampler_type = "1D";
9607 break;
9608 case WINED3D_GL_RES_TYPE_TEX_2D:
9609 sampler_type = "2D";
9610 break;
9611 case WINED3D_GL_RES_TYPE_TEX_3D:
9612 sampler_type = "3D";
9613 break;
9614 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9615 sampler_type = "Cube";
9616 break;
9617 case WINED3D_GL_RES_TYPE_TEX_RECT:
9618 sampler_type = "2DRect";
9619 break;
9620 default:
9621 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9622 sampler_type = NULL;
9623 break;
9625 if (sampler_type)
9627 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9628 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9629 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9632 shader_addline(buffer, "vec4 tex%u;\n", stage);
9634 if (!(bump_map & (1u << stage)))
9635 continue;
9636 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9638 if (!(lum_map & (1u << stage)))
9639 continue;
9640 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9641 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9643 if (tfactor_used)
9644 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9645 if (settings->color_key_enabled)
9646 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9647 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9649 if (settings->sRGB_write)
9651 shader_addline(buffer, "const vec4 srgb_const0 = ");
9652 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9653 shader_addline(buffer, ";\n");
9654 shader_addline(buffer, "const vec4 srgb_const1 = ");
9655 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9656 shader_addline(buffer, ";\n");
9659 shader_addline(buffer, "uniform struct\n{\n");
9660 shader_addline(buffer, " vec4 color;\n");
9661 shader_addline(buffer, " float density;\n");
9662 shader_addline(buffer, " float end;\n");
9663 shader_addline(buffer, " float scale;\n");
9664 shader_addline(buffer, "} ffp_fog;\n");
9666 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9667 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9669 if (legacy_syntax)
9671 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9672 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9673 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9674 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9675 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9677 else
9679 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9680 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9681 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9682 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9683 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9686 shader_addline(buffer, "void main()\n{\n");
9688 if (legacy_syntax)
9690 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9691 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9694 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9696 if (tex_map & (1u << stage))
9698 if (settings->pointsprite)
9699 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9700 else if (settings->texcoords_initialized & (1u << stage))
9701 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9702 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9703 else
9704 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9708 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9709 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9711 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9712 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9714 /* Generate texture sampling instructions */
9715 for (stage = 0; stage < WINED3D_MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9717 const char *texture_function, *coord_mask;
9718 BOOL proj;
9720 if (!(tex_map & (1u << stage)))
9721 continue;
9723 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9725 proj = FALSE;
9727 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9728 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9730 proj = TRUE;
9732 else
9734 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9735 proj = TRUE;
9738 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9739 proj = FALSE;
9741 switch (settings->op[stage].tex_type)
9743 case WINED3D_GL_RES_TYPE_TEX_1D:
9744 texture_function = "texture1D";
9745 coord_mask = "x";
9746 break;
9747 case WINED3D_GL_RES_TYPE_TEX_2D:
9748 texture_function = "texture2D";
9749 coord_mask = "xy";
9750 break;
9751 case WINED3D_GL_RES_TYPE_TEX_3D:
9752 texture_function = "texture3D";
9753 coord_mask = "xyz";
9754 break;
9755 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9756 texture_function = "textureCube";
9757 coord_mask = "xyz";
9758 break;
9759 case WINED3D_GL_RES_TYPE_TEX_RECT:
9760 texture_function = "texture2DRect";
9761 coord_mask = "xy";
9762 break;
9763 default:
9764 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9765 texture_function = "";
9766 coord_mask = "xyzw";
9767 proj = FALSE;
9768 break;
9770 if (!legacy_syntax)
9771 texture_function = "texture";
9773 if (stage > 0
9774 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9775 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9777 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9779 /* With projective textures, texbem only divides the static
9780 * texture coordinate, not the displacement, so multiply the
9781 * displacement with the dividing parameter before passing it to
9782 * TXP. */
9783 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9785 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9787 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9788 stage, stage);
9789 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9791 else
9793 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9794 stage, stage);
9795 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9798 else
9800 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9803 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9804 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9806 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9807 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9808 stage, stage - 1, stage - 1, stage - 1);
9810 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9812 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9813 stage, texture_function, proj ? "Proj" : "", stage, stage);
9815 else
9817 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9818 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9821 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9822 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9823 settings->op[stage].color_fixup);
9826 if (settings->color_key_enabled)
9827 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9829 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9831 /* Generate the main shader */
9832 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9834 BOOL op_equal;
9836 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9837 break;
9839 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9840 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9841 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9842 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9843 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9844 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9845 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9846 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9847 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9848 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9849 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9850 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9851 else
9852 op_equal = settings->op[stage].aop == settings->op[stage].cop
9853 && settings->op[stage].carg0 == settings->op[stage].aarg0
9854 && settings->op[stage].carg1 == settings->op[stage].aarg1
9855 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9857 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9859 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9860 settings->op[stage].cop, settings->op[stage].carg0,
9861 settings->op[stage].carg1, settings->op[stage].carg2);
9863 else if (op_equal)
9865 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9866 settings->op[stage].cop, settings->op[stage].carg0,
9867 settings->op[stage].carg1, settings->op[stage].carg2);
9869 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9870 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9872 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9873 settings->op[stage].cop, settings->op[stage].carg0,
9874 settings->op[stage].carg1, settings->op[stage].carg2);
9875 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9876 settings->op[stage].aop, settings->op[stage].aarg0,
9877 settings->op[stage].aarg1, settings->op[stage].aarg2);
9881 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9882 get_fragment_output(gl_info));
9884 if (settings->sRGB_write)
9885 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9887 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9889 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9890 if (!use_legacy_fragment_output(gl_info))
9891 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9893 shader_addline(buffer, "}\n");
9895 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9896 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9898 string_buffer_release(&priv->string_buffers, tex_reg_name);
9899 return shader_id;
9902 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9903 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9905 struct glsl_ffp_vertex_shader *shader;
9906 const struct wine_rb_entry *entry;
9908 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9909 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9911 if (!(shader = heap_alloc(sizeof(*shader))))
9912 return NULL;
9914 shader->desc.settings = *settings;
9915 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9916 list_init(&shader->linked_programs);
9917 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9918 ERR("Failed to insert ffp vertex shader.\n");
9920 return shader;
9923 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9924 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9926 struct glsl_ffp_fragment_shader *glsl_desc;
9927 const struct ffp_frag_desc *desc;
9929 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9930 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9932 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9933 return NULL;
9935 glsl_desc->entry.settings = *args;
9936 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
9937 list_init(&glsl_desc->linked_programs);
9938 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9940 return glsl_desc;
9944 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9945 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9947 unsigned int i;
9948 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9950 for (i = 0; i < vs_c_count; ++i)
9952 string_buffer_sprintf(name, "vs_c[%u]", i);
9953 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9955 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9957 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9959 string_buffer_sprintf(name, "vs_i[%u]", i);
9960 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9963 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9965 string_buffer_sprintf(name, "vs_b[%u]", i);
9966 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9969 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9970 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
9972 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9974 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9975 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9977 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9978 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9979 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9981 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9982 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9984 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9985 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9986 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9987 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9988 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9989 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9990 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
9992 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9993 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9994 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9995 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9996 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9997 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9998 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9999 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10000 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
10001 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10002 string_buffer_sprintf(name, "ffp_light[%u].range", i);
10003 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10004 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
10005 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10006 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
10007 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10008 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10009 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10010 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10011 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10012 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10013 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10014 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10015 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10017 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10018 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10019 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10020 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10021 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10022 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10023 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10025 string_buffer_release(&priv->string_buffers, name);
10028 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10029 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10031 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10034 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10035 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10037 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10040 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10041 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10043 unsigned int i;
10044 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10046 for (i = 0; i < ps_c_count; ++i)
10048 string_buffer_sprintf(name, "ps_c[%u]", i);
10049 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10051 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10053 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10055 string_buffer_sprintf(name, "ps_i[%u]", i);
10056 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10059 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10061 string_buffer_sprintf(name, "ps_b[%u]", i);
10062 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10065 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10067 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10068 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10069 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10070 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10071 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10072 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10073 string_buffer_sprintf(name, "tss_const%u", i);
10074 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10077 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10078 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10080 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10081 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10082 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10083 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10085 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10087 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10088 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10089 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10091 string_buffer_release(&priv->string_buffers, name);
10094 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10095 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10097 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10098 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10099 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10100 struct glsl_cs_compiled_shader *gl_shaders;
10101 struct glsl_shader_private *shader_data;
10102 struct glsl_shader_prog_link *entry;
10103 GLuint shader_id, program_id;
10105 if (!(entry = heap_alloc(sizeof(*entry))))
10107 ERR("Out of memory.\n");
10108 return E_OUTOFMEMORY;
10111 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10113 ERR("Failed to allocate backend data.\n");
10114 heap_free(entry);
10115 return E_OUTOFMEMORY;
10117 shader_data = shader->backend_data;
10119 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10121 ERR("Failed to allocate GL shader array.\n");
10122 heap_free(entry);
10123 heap_free(shader->backend_data);
10124 shader->backend_data = NULL;
10125 return E_OUTOFMEMORY;
10127 shader_data->shader_array_size = 1;
10128 gl_shaders = shader_data->gl_shaders.cs;
10130 TRACE("Compiling compute shader %p.\n", shader);
10132 string_buffer_clear(buffer);
10133 shader_id = shader_glsl_generate_compute_shader(context_gl, buffer, &priv->string_buffers, shader);
10134 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10136 program_id = GL_EXTCALL(glCreateProgram());
10137 TRACE("Created new GLSL shader program %u.\n", program_id);
10139 entry->id = program_id;
10140 entry->vs.id = 0;
10141 entry->hs.id = 0;
10142 entry->ds.id = 0;
10143 entry->gs.id = 0;
10144 entry->ps.id = 0;
10145 entry->cs.id = shader_id;
10146 entry->constant_version = 0;
10147 entry->shader_controlled_clip_distances = 0;
10148 entry->ps.np2_fixup_info = NULL;
10149 add_glsl_program_entry(priv, entry);
10151 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10152 GL_EXTCALL(glAttachShader(program_id, shader_id));
10153 checkGLcall("glAttachShader");
10155 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10157 TRACE("Linking GLSL shader program %u.\n", program_id);
10158 GL_EXTCALL(glLinkProgram(program_id));
10159 shader_glsl_validate_link(gl_info, program_id);
10161 GL_EXTCALL(glUseProgram(program_id));
10162 checkGLcall("glUseProgram");
10163 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10164 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10166 entry->constant_update_mask = 0;
10168 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10169 checkGLcall("glUseProgram");
10170 return WINED3D_OK;
10173 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10174 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10176 struct glsl_shader_private *shader_data;
10178 if (!shader->backend_data)
10180 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10181 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10183 ERR("Failed to compile compute shader %p.\n", shader);
10184 return 0;
10187 shader_data = shader->backend_data;
10188 return shader_data->gl_shaders.cs[0].id;
10191 /* Context activation is done by the caller. */
10192 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10193 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10195 struct glsl_shader_prog_link *entry;
10196 struct wined3d_shader *shader;
10197 struct glsl_program_key key;
10198 GLuint cs_id;
10200 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10201 return;
10203 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10205 WARN("Compute shader is NULL.\n");
10206 ctx_data->glsl_program = NULL;
10207 return;
10210 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10211 memset(&key, 0, sizeof(key));
10212 key.cs_id = cs_id;
10213 if (!(entry = get_glsl_program_entry(priv, &key)))
10214 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10215 ctx_data->glsl_program = entry;
10218 /* Context activation is done by the caller. */
10219 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10220 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10222 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10223 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10224 const struct wined3d_shader *pre_rasterization_shader;
10225 const struct ps_np2fixup_info *np2fixup_info = NULL;
10226 struct wined3d_shader *hshader, *dshader, *gshader;
10227 struct glsl_shader_prog_link *entry = NULL;
10228 struct wined3d_shader *vshader = NULL;
10229 struct wined3d_shader *pshader = NULL;
10230 GLuint reorder_shader_id = 0;
10231 struct glsl_program_key key;
10232 uint32_t attribs_map;
10233 GLuint program_id;
10234 unsigned int i;
10235 GLuint vs_id = 0;
10236 GLuint hs_id = 0;
10237 GLuint ds_id = 0;
10238 GLuint gs_id = 0;
10239 GLuint ps_id = 0;
10240 struct list *ps_list, *vs_list;
10241 struct wined3d_string_buffer *tmp_name;
10243 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10245 vs_id = ctx_data->glsl_program->vs.id;
10246 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10248 if (use_vs(state))
10249 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10251 else if (use_vs(state))
10253 struct vs_compile_args vs_compile_args;
10255 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10257 find_vs_compile_args(state, vshader, &vs_compile_args, &context_gl->c);
10258 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10259 vs_list = &vshader->linked_programs;
10261 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10263 struct glsl_ffp_vertex_shader *ffp_shader;
10264 struct wined3d_ffp_vs_settings settings;
10266 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10267 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10268 vs_id = ffp_shader->id;
10269 vs_list = &ffp_shader->linked_programs;
10272 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10273 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10274 hs_id = ctx_data->glsl_program->hs.id;
10275 else if (hshader)
10276 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10278 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10279 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10281 ds_id = ctx_data->glsl_program->ds.id;
10283 else if (dshader)
10285 struct ds_compile_args args;
10287 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10288 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10291 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10292 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10294 gs_id = ctx_data->glsl_program->gs.id;
10296 else if (gshader)
10298 struct gs_compile_args args;
10300 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10301 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10304 /* A pixel shader is not used when rasterization is disabled. */
10305 if (is_rasterization_disabled(gshader))
10307 ps_id = 0;
10308 ps_list = NULL;
10310 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10312 ps_id = ctx_data->glsl_program->ps.id;
10313 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10315 if (use_ps(state))
10316 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10318 else if (use_ps(state))
10320 struct ps_compile_args ps_compile_args;
10321 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10322 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10323 &ps_compile_args, &context_gl->c);
10324 ps_id = find_glsl_fragment_shader(context_gl, &priv->shader_buffer, &priv->string_buffers,
10325 pshader, &ps_compile_args, &np2fixup_info);
10326 ps_list = &pshader->linked_programs;
10328 else if (priv->fragment_pipe == &glsl_fragment_pipe
10329 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10331 struct glsl_ffp_fragment_shader *ffp_shader;
10332 struct ffp_frag_settings settings;
10334 wined3d_ffp_get_fs_settings(&context_gl->c, state, &settings, FALSE);
10335 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10336 ps_id = ffp_shader->id;
10337 ps_list = &ffp_shader->linked_programs;
10340 key.vs_id = vs_id;
10341 key.hs_id = hs_id;
10342 key.ds_id = ds_id;
10343 key.gs_id = gs_id;
10344 key.ps_id = ps_id;
10345 key.cs_id = 0;
10346 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10348 ctx_data->glsl_program = entry;
10349 return;
10352 /* If we get to this point, then no matching program exists, so we create one */
10353 program_id = GL_EXTCALL(glCreateProgram());
10354 TRACE("Created new GLSL shader program %u.\n", program_id);
10356 /* Create the entry */
10357 entry = heap_alloc(sizeof(*entry));
10358 entry->id = program_id;
10359 entry->vs.id = vs_id;
10360 entry->hs.id = hs_id;
10361 entry->ds.id = ds_id;
10362 entry->gs.id = gs_id;
10363 entry->ps.id = ps_id;
10364 entry->cs.id = 0;
10365 entry->constant_version = 0;
10366 entry->shader_controlled_clip_distances = 0;
10367 entry->ps.np2_fixup_info = np2fixup_info;
10368 /* Add the hash table entry */
10369 add_glsl_program_entry(priv, entry);
10371 /* Set the current program */
10372 ctx_data->glsl_program = entry;
10374 /* Attach GLSL vshader */
10375 if (vs_id)
10377 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10378 GL_EXTCALL(glAttachShader(program_id, vs_id));
10379 checkGLcall("glAttachShader");
10381 list_add_head(vs_list, &entry->vs.shader_entry);
10384 if (vshader)
10386 attribs_map = vshader->reg_maps.input_registers;
10387 if (vshader->reg_maps.shader_version.major < 4)
10389 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10390 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10391 d3d_info->emulated_flatshading
10392 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10393 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10394 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10395 checkGLcall("glAttachShader");
10396 /* Flag the reorder function for deletion, it will be freed
10397 * automatically when the program is destroyed. */
10398 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10401 else
10403 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10406 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10408 /* Bind vertex attributes to a corresponding index number to match
10409 * the same index numbers as ARB_vertex_programs (makes loading
10410 * vertex attributes simpler). With this method, we can use the
10411 * exact same code to load the attributes later for both ARB and
10412 * GLSL shaders.
10414 * We have to do this here because we need to know the Program ID
10415 * in order to make the bindings work, and it has to be done prior
10416 * to linking the GLSL program. */
10417 tmp_name = string_buffer_get(&priv->string_buffers);
10418 while (attribs_map)
10420 i = wined3d_bit_scan(&attribs_map);
10421 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10422 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10423 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10425 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10426 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10427 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10428 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10431 checkGLcall("glBindAttribLocation");
10433 if (!use_legacy_fragment_output(gl_info))
10435 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10437 string_buffer_sprintf(tmp_name, "color_out%u", i);
10438 if (state->blend_state && state->blend_state->dual_source)
10439 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10440 else
10441 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10442 checkGLcall("glBindFragDataLocation");
10445 string_buffer_release(&priv->string_buffers, tmp_name);
10448 if (hshader)
10450 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10451 GL_EXTCALL(glAttachShader(program_id, hs_id));
10452 checkGLcall("glAttachShader");
10454 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10457 if (dshader)
10459 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10460 GL_EXTCALL(glAttachShader(program_id, ds_id));
10461 checkGLcall("glAttachShader");
10463 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10466 if (gshader)
10468 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10469 GL_EXTCALL(glAttachShader(program_id, gs_id));
10470 checkGLcall("glAttachShader");
10472 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10474 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10477 /* Attach GLSL pshader */
10478 if (ps_id)
10480 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10481 GL_EXTCALL(glAttachShader(program_id, ps_id));
10482 checkGLcall("glAttachShader");
10484 list_add_head(ps_list, &entry->ps.shader_entry);
10487 /* Link the program */
10488 TRACE("Linking GLSL shader program %u.\n", program_id);
10489 GL_EXTCALL(glLinkProgram(program_id));
10490 shader_glsl_validate_link(gl_info, program_id);
10492 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10493 vshader ? vshader->limits->constant_float : 0);
10494 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10495 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10496 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10497 pshader ? pshader->limits->constant_float : 0);
10498 checkGLcall("find glsl program uniform locations");
10500 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10501 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10503 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10504 entry->shader_controlled_clip_distances = 1;
10505 entry->clip_distance_mask = wined3d_mask_from_size(clip_distance_count);
10508 if (needs_legacy_glsl_syntax(gl_info))
10510 if (pshader && pshader->reg_maps.shader_version.major >= 3
10511 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10513 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10514 entry->vs.vertex_color_clamp = GL_FALSE;
10516 else
10518 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10521 else
10523 /* With core profile we never change vertex_color_clamp from
10524 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10525 * glClampColorARB(). */
10526 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10529 /* Set the shader to allow uniform loading on it */
10530 GL_EXTCALL(glUseProgram(program_id));
10531 checkGLcall("glUseProgram");
10533 entry->constant_update_mask = 0;
10534 if (vshader)
10536 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10537 if (vshader->reg_maps.integer_constants)
10538 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10539 if (vshader->reg_maps.boolean_constants)
10540 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10541 if (entry->vs.pos_fixup_location != -1)
10542 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10543 if (entry->vs.base_vertex_id_location != -1)
10544 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10546 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10548 else
10550 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10551 | WINED3D_SHADER_CONST_FFP_PROJ;
10553 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10555 if (entry->vs.modelview_matrix_location[i] != -1)
10557 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10558 break;
10562 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10564 if (entry->vs.texture_matrix_location[i] != -1)
10566 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10567 break;
10570 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10571 || entry->vs.material_specular_location != -1
10572 || entry->vs.material_emissive_location != -1
10573 || entry->vs.material_shininess_location != -1)
10574 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10575 if (entry->vs.light_ambient_location != -1)
10576 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10578 if (entry->vs.clip_planes_location != -1)
10579 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10580 if (entry->vs.pointsize_min_location != -1)
10581 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10583 if (hshader)
10584 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10586 if (dshader)
10588 if (entry->ds.pos_fixup_location != -1)
10589 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10591 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10594 if (gshader)
10596 if (entry->gs.pos_fixup_location != -1)
10597 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10599 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10602 if (ps_id)
10604 if (pshader)
10606 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10607 if (pshader->reg_maps.integer_constants)
10608 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10609 if (pshader->reg_maps.boolean_constants)
10610 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10611 if (entry->ps.ycorrection_location != -1)
10612 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10614 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10615 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10617 else
10619 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10621 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10624 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10626 if (entry->ps.bumpenv_mat_location[i] != -1)
10628 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10629 break;
10633 if (entry->ps.fog_color_location != -1)
10634 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10635 if (entry->ps.alpha_test_ref_location != -1)
10636 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10637 if (entry->ps.np2_fixup_location != -1)
10638 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10639 if (entry->ps.color_key_location != -1)
10640 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10644 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10646 struct wined3d_device *device = shader->device;
10647 struct wined3d_context *context;
10649 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10651 context = context_acquire(device, NULL, 0);
10652 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10653 context_release(context);
10657 /* Context activation is done by the caller. */
10658 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10659 const struct wined3d_state *state)
10661 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10662 struct glsl_context_data *ctx_data = context->shader_backend_data;
10663 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10664 struct shader_glsl_priv *priv = shader_priv;
10665 struct glsl_shader_prog_link *glsl_program;
10666 GLenum current_vertex_color_clamp;
10667 GLuint program_id, prev_id;
10669 priv->vertex_pipe->vp_enable(context, !use_vs(state));
10670 priv->fragment_pipe->fp_enable(context, !use_ps(state));
10672 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10673 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10674 glsl_program = ctx_data->glsl_program;
10676 if (glsl_program)
10678 program_id = glsl_program->id;
10679 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10680 if (glsl_program->shader_controlled_clip_distances)
10681 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10683 else
10685 program_id = 0;
10686 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10689 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10691 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10692 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10694 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10695 checkGLcall("glClampColorARB");
10697 else
10699 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10703 TRACE("Using GLSL program %u.\n", program_id);
10705 if (prev_id != program_id)
10707 GL_EXTCALL(glUseProgram(program_id));
10708 checkGLcall("glUseProgram");
10710 if (glsl_program)
10711 context->constant_update_mask |= glsl_program->constant_update_mask;
10714 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10717 /* Context activation is done by the caller. */
10718 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10719 const struct wined3d_state *state)
10721 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10722 struct glsl_context_data *ctx_data = context->shader_backend_data;
10723 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10724 struct shader_glsl_priv *priv = shader_priv;
10725 GLuint program_id, prev_id;
10727 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10728 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10729 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10731 TRACE("Using GLSL program %u.\n", program_id);
10733 if (prev_id != program_id)
10735 GL_EXTCALL(glUseProgram(program_id));
10736 checkGLcall("glUseProgram");
10739 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10740 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10741 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10742 | (1u << WINED3D_SHADER_TYPE_HULL)
10743 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10746 /* "context" is not necessarily the currently active context. */
10747 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10749 struct glsl_context_data *ctx_data = context->shader_backend_data;
10751 ctx_data->glsl_program = NULL;
10752 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10753 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10754 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10755 | (1u << WINED3D_SHADER_TYPE_HULL)
10756 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10757 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10760 /* Context activation is done by the caller. */
10761 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10763 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10764 struct glsl_context_data *ctx_data = context->shader_backend_data;
10765 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10766 struct shader_glsl_priv *priv = shader_priv;
10768 shader_glsl_invalidate_current_program(context);
10769 GL_EXTCALL(glUseProgram(0));
10770 checkGLcall("glUseProgram");
10772 priv->vertex_pipe->vp_enable(context, FALSE);
10773 priv->fragment_pipe->fp_enable(context, FALSE);
10775 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10777 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10778 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10779 checkGLcall("glClampColorARB");
10783 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10784 const struct glsl_shader_prog_link *program)
10786 const struct glsl_context_data *ctx_data;
10787 struct wined3d_context *context;
10788 unsigned int i;
10790 for (i = 0; i < device->context_count; ++i)
10792 context = device->contexts[i];
10793 ctx_data = context->shader_backend_data;
10795 if (ctx_data->glsl_program == program)
10796 shader_glsl_invalidate_current_program(context);
10800 static void shader_glsl_destroy(struct wined3d_shader *shader)
10802 struct glsl_shader_private *shader_data = shader->backend_data;
10803 struct wined3d_device *device = shader->device;
10804 struct shader_glsl_priv *priv = device->shader_priv;
10805 const struct wined3d_gl_info *gl_info;
10806 const struct list *linked_programs;
10807 struct wined3d_context *context;
10809 if (!shader_data || !shader_data->num_gl_shaders)
10811 heap_free(shader_data);
10812 shader->backend_data = NULL;
10813 return;
10816 context = context_acquire(device, NULL, 0);
10817 gl_info = wined3d_context_gl(context)->gl_info;
10819 TRACE("Deleting linked programs.\n");
10820 linked_programs = &shader->linked_programs;
10821 if (!list_empty(linked_programs))
10823 struct glsl_shader_prog_link *entry, *entry2;
10824 UINT i;
10826 switch (shader->reg_maps.shader_version.type)
10828 case WINED3D_SHADER_TYPE_PIXEL:
10830 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10832 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10834 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10835 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10836 checkGLcall("glDeleteShader");
10838 heap_free(shader_data->gl_shaders.ps);
10840 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10841 struct glsl_shader_prog_link, ps.shader_entry)
10843 shader_glsl_invalidate_contexts_program(device, entry);
10844 delete_glsl_program_entry(priv, gl_info, entry);
10847 break;
10850 case WINED3D_SHADER_TYPE_VERTEX:
10852 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10854 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10856 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10857 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10858 checkGLcall("glDeleteShader");
10860 heap_free(shader_data->gl_shaders.vs);
10862 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10863 struct glsl_shader_prog_link, vs.shader_entry)
10865 shader_glsl_invalidate_contexts_program(device, entry);
10866 delete_glsl_program_entry(priv, gl_info, entry);
10869 break;
10872 case WINED3D_SHADER_TYPE_HULL:
10874 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10876 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10878 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10879 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10880 checkGLcall("glDeleteShader");
10882 heap_free(shader_data->gl_shaders.hs);
10884 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10885 struct glsl_shader_prog_link, hs.shader_entry)
10887 shader_glsl_invalidate_contexts_program(device, entry);
10888 delete_glsl_program_entry(priv, gl_info, entry);
10891 break;
10894 case WINED3D_SHADER_TYPE_DOMAIN:
10896 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10898 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10900 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10901 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10902 checkGLcall("glDeleteShader");
10904 heap_free(shader_data->gl_shaders.ds);
10906 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10907 struct glsl_shader_prog_link, ds.shader_entry)
10909 shader_glsl_invalidate_contexts_program(device, entry);
10910 delete_glsl_program_entry(priv, gl_info, entry);
10913 break;
10916 case WINED3D_SHADER_TYPE_GEOMETRY:
10918 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10920 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10922 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10923 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10924 checkGLcall("glDeleteShader");
10926 heap_free(shader_data->gl_shaders.gs);
10928 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10929 struct glsl_shader_prog_link, gs.shader_entry)
10931 shader_glsl_invalidate_contexts_program(device, entry);
10932 delete_glsl_program_entry(priv, gl_info, entry);
10935 break;
10938 case WINED3D_SHADER_TYPE_COMPUTE:
10940 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10942 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10944 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10945 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10946 checkGLcall("glDeleteShader");
10948 heap_free(shader_data->gl_shaders.cs);
10950 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10951 struct glsl_shader_prog_link, cs.shader_entry)
10953 shader_glsl_invalidate_contexts_program(device, entry);
10954 delete_glsl_program_entry(priv, gl_info, entry);
10957 break;
10960 default:
10961 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10962 break;
10966 heap_free(shader->backend_data);
10967 shader->backend_data = NULL;
10969 context_release(context);
10972 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10974 const struct glsl_program_key *k = key;
10975 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10976 const struct glsl_shader_prog_link, program_lookup_entry);
10977 int ret;
10979 if ((ret = wined3d_uint32_compare(k->vs_id, prog->vs.id)))
10980 return ret;
10981 if ((ret = wined3d_uint32_compare(k->gs_id, prog->gs.id)))
10982 return ret;
10983 if ((ret = wined3d_uint32_compare(k->ps_id, prog->ps.id)))
10984 return ret;
10985 if ((ret = wined3d_uint32_compare(k->hs_id, prog->hs.id)))
10986 return ret;
10987 if ((ret = wined3d_uint32_compare(k->ds_id, prog->ds.id)))
10988 return ret;
10989 if ((ret = wined3d_uint32_compare(k->cs_id, prog->cs.id)))
10990 return ret;
10992 return 0;
10995 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10997 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10998 + constant_count * sizeof(*heap->contained)
10999 + constant_count * sizeof(*heap->positions);
11000 void *mem;
11002 if (!(mem = heap_alloc(size)))
11004 ERR("Failed to allocate memory\n");
11005 return FALSE;
11008 heap->entries = mem;
11009 heap->entries[1].version = 0;
11010 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11011 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11012 heap->positions = (unsigned int *)(heap->contained + constant_count);
11013 heap->size = 1;
11015 return TRUE;
11018 static void constant_heap_free(struct constant_heap *heap)
11020 heap_free(heap->entries);
11023 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11024 const struct wined3d_fragment_pipe_ops *fragment_pipe)
11026 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11027 struct fragment_caps fragment_caps;
11028 void *vertex_priv, *fragment_priv;
11029 struct shader_glsl_priv *priv;
11031 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11032 return E_OUTOFMEMORY;
11034 string_buffer_list_init(&priv->string_buffers);
11036 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11038 ERR("Failed to initialize vertex pipe.\n");
11039 heap_free(priv);
11040 return E_FAIL;
11043 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11045 ERR("Failed to initialize fragment pipe.\n");
11046 vertex_pipe->vp_free(device, NULL);
11047 heap_free(priv);
11048 return E_FAIL;
11051 if (!string_buffer_init(&priv->shader_buffer))
11053 ERR("Failed to initialize shader buffer.\n");
11054 goto fail;
11057 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11059 ERR("Failed to allocate memory.\n");
11060 goto fail;
11063 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11065 ERR("Failed to initialize vertex shader constant heap\n");
11066 goto fail;
11069 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11071 ERR("Failed to initialize pixel shader constant heap\n");
11072 goto fail;
11075 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11077 priv->next_constant_version = 1;
11078 priv->vertex_pipe = vertex_pipe;
11079 priv->fragment_pipe = fragment_pipe;
11080 fragment_pipe->get_caps(device->adapter, &fragment_caps);
11081 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11082 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11084 device->vertex_priv = vertex_priv;
11085 device->fragment_priv = fragment_priv;
11086 device->shader_priv = priv;
11088 return WINED3D_OK;
11090 fail:
11091 constant_heap_free(&priv->pconst_heap);
11092 constant_heap_free(&priv->vconst_heap);
11093 heap_free(priv->stack);
11094 string_buffer_free(&priv->shader_buffer);
11095 fragment_pipe->free_private(device, NULL);
11096 vertex_pipe->vp_free(device, NULL);
11097 heap_free(priv);
11098 return E_OUTOFMEMORY;
11101 /* Context activation is done by the caller. */
11102 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11104 struct shader_glsl_priv *priv = device->shader_priv;
11106 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11107 constant_heap_free(&priv->pconst_heap);
11108 constant_heap_free(&priv->vconst_heap);
11109 heap_free(priv->stack);
11110 string_buffer_list_cleanup(&priv->string_buffers);
11111 string_buffer_free(&priv->shader_buffer);
11112 priv->fragment_pipe->free_private(device, context);
11113 priv->vertex_pipe->vp_free(device, context);
11115 heap_free(device->shader_priv);
11116 device->shader_priv = NULL;
11119 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11121 struct glsl_context_data *ctx_data;
11123 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11124 return FALSE;
11125 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11126 context->shader_backend_data = ctx_data;
11127 return TRUE;
11130 static void shader_glsl_free_context_data(struct wined3d_context *context)
11132 heap_free(context->shader_backend_data);
11135 static void shader_glsl_init_context_state(struct wined3d_context *context)
11137 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11138 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11140 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11141 checkGLcall("GL_PROGRAM_POINT_SIZE");
11144 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11146 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11147 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11148 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11150 if (shader_model_4
11151 && gl_info->supported[ARB_COMPUTE_SHADER]
11152 && gl_info->supported[ARB_CULL_DISTANCE]
11153 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11154 && gl_info->supported[ARB_GPU_SHADER5]
11155 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11156 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11157 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11158 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11159 && gl_info->supported[ARB_TESSELLATION_SHADER]
11160 && gl_info->supported[ARB_TEXTURE_GATHER]
11161 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11162 return 5;
11164 if (shader_model_4)
11165 return 4;
11167 /* Support for texldd and texldl instructions in pixel shaders is required
11168 * for SM3. */
11169 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11170 return 3;
11172 return 2;
11175 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11177 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11178 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11180 TRACE("Shader model %u.\n", shader_model);
11182 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11183 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11184 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11185 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11186 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11187 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11189 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11190 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11192 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11193 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11194 caps->varying_count = gl_info->limits.glsl_varyings;
11196 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11197 * Direct3D minimum requirement.
11199 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11200 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11202 * The problem is that the refrast clamps temporary results in the shader to
11203 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11204 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11205 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11206 * offer a way to query this.
11208 if (shader_model >= 4)
11209 caps->ps_1x_max_value = FLT_MAX;
11210 else
11211 caps->ps_1x_max_value = 1024.0f;
11213 /* Ideally we'd only set caps like sRGB writes here if supported by both
11214 * the shader backend and the fragment pipe, but we can get called before
11215 * shader_glsl_alloc(). */
11216 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11217 | WINED3D_SHADER_CAP_SRGB_WRITE;
11218 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11219 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11220 if (shader_glsl_full_ffp_varyings(gl_info))
11221 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11224 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11226 /* We support everything except YUV conversions. */
11227 return !is_complex_fixup(fixup);
11230 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11232 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11233 /* WINED3DSIH_ADD */ shader_glsl_binop,
11234 /* WINED3DSIH_AND */ shader_glsl_binop,
11235 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11236 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11237 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11238 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11239 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11240 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11241 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11242 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11243 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11244 /* WINED3DSIH_BEM */ shader_glsl_bem,
11245 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11246 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11247 /* WINED3DSIH_BREAK */ shader_glsl_break,
11248 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11249 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11250 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11251 /* WINED3DSIH_CALL */ shader_glsl_call,
11252 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11253 /* WINED3DSIH_CASE */ shader_glsl_case,
11254 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11255 /* WINED3DSIH_CND */ shader_glsl_cnd,
11256 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11257 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11258 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11259 /* WINED3DSIH_CRS */ shader_glsl_cross,
11260 /* WINED3DSIH_CUT */ shader_glsl_cut,
11261 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11262 /* WINED3DSIH_DCL */ shader_glsl_nop,
11263 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11264 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11265 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11266 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11267 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11268 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11269 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11270 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11271 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11272 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11273 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11274 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11275 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11276 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11277 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11278 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11279 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11280 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11281 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11282 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11283 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11284 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11285 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11286 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11287 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11288 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11289 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11290 /* WINED3DSIH_DCL_STREAM */ NULL,
11291 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11292 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11293 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11294 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11295 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11296 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11297 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11298 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11299 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11300 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11301 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11302 /* WINED3DSIH_DEF */ shader_glsl_nop,
11303 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11304 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11305 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11306 /* WINED3DSIH_DIV */ shader_glsl_binop,
11307 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11308 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11309 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11310 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11311 /* WINED3DSIH_DST */ shader_glsl_dst,
11312 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11313 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11314 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11315 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11316 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11317 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11318 /* WINED3DSIH_ELSE */ shader_glsl_else,
11319 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11320 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11321 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11322 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11323 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11324 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11325 /* WINED3DSIH_EQ */ shader_glsl_relop,
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 DWORD 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 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11974 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11975 /* NP2 texture matrix fixups. They are not needed if
11976 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11977 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11978 * matrix. */
11979 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11980 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11981 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11982 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11983 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11984 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11985 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11986 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11987 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11988 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11989 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11990 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11991 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11992 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11993 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11994 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11995 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11996 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11997 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11998 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11999 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12000 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12001 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12002 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12003 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12004 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
12005 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
12006 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
12009 /* TODO:
12010 * - Implement vertex tweening. */
12011 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12013 glsl_vertex_pipe_vp_enable,
12014 glsl_vertex_pipe_vp_get_caps,
12015 glsl_vertex_pipe_vp_get_emul_mask,
12016 glsl_vertex_pipe_vp_alloc,
12017 glsl_vertex_pipe_vp_free,
12018 glsl_vertex_pipe_vp_states,
12021 static void glsl_fragment_pipe_enable(const struct wined3d_context *context, BOOL enable)
12023 /* Nothing to do. */
12026 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
12028 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
12030 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
12031 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
12032 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
12033 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12034 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12035 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12036 | WINED3DTEXOPCAPS_SELECTARG1
12037 | WINED3DTEXOPCAPS_SELECTARG2
12038 | WINED3DTEXOPCAPS_MODULATE4X
12039 | WINED3DTEXOPCAPS_MODULATE2X
12040 | WINED3DTEXOPCAPS_MODULATE
12041 | WINED3DTEXOPCAPS_ADDSIGNED2X
12042 | WINED3DTEXOPCAPS_ADDSIGNED
12043 | WINED3DTEXOPCAPS_ADD
12044 | WINED3DTEXOPCAPS_SUBTRACT
12045 | WINED3DTEXOPCAPS_ADDSMOOTH
12046 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12047 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12048 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12049 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12050 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12051 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12052 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12053 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12054 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12055 | WINED3DTEXOPCAPS_DOTPRODUCT3
12056 | WINED3DTEXOPCAPS_MULTIPLYADD
12057 | WINED3DTEXOPCAPS_LERP
12058 | WINED3DTEXOPCAPS_BUMPENVMAP
12059 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12060 caps->MaxTextureBlendStages = WINED3D_MAX_TEXTURES;
12061 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_TEXTURES);
12064 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12066 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12067 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12068 return 0;
12071 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12073 struct shader_glsl_priv *priv;
12075 if (shader_backend == &glsl_shader_backend)
12077 priv = shader_priv;
12078 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12079 return priv;
12082 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12084 return NULL;
12087 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12089 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12090 struct glsl_ffp_fragment_shader, entry.entry);
12091 struct glsl_shader_prog_link *program, *program2;
12092 struct glsl_ffp_destroy_ctx *ctx = param;
12093 const struct wined3d_gl_info *gl_info;
12095 gl_info = ctx->context_gl->gl_info;
12096 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12097 struct glsl_shader_prog_link, ps.shader_entry)
12099 delete_glsl_program_entry(ctx->priv, gl_info, program);
12101 GL_EXTCALL(glDeleteShader(shader->id));
12102 heap_free(shader);
12105 /* Context activation is done by the caller. */
12106 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12108 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12109 struct shader_glsl_priv *priv = device->fragment_priv;
12110 struct glsl_ffp_destroy_ctx ctx;
12112 ctx.priv = priv;
12113 ctx.context_gl = context_gl;
12114 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12117 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12118 const struct wined3d_state *state, DWORD state_id)
12120 context->last_was_pshader = use_ps(state);
12122 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12125 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12126 const struct wined3d_state *state, DWORD state_id)
12128 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12131 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12132 const struct wined3d_state *state, DWORD state_id)
12134 BOOL use_vshader = use_vs(state);
12135 enum fogsource new_source;
12136 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12137 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12139 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12141 if (!state->render_states[WINED3D_RS_FOGENABLE])
12142 return;
12144 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12146 if (use_vshader)
12147 new_source = FOGSOURCE_VS;
12148 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12149 new_source = FOGSOURCE_COORD;
12150 else
12151 new_source = FOGSOURCE_FFP;
12153 else
12155 new_source = FOGSOURCE_FFP;
12158 if (new_source != context->fog_source || fogstart == fogend)
12160 context->fog_source = new_source;
12161 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12165 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12166 const struct wined3d_state *state, DWORD state_id)
12168 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12170 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12171 if (!shader_glsl_full_ffp_varyings(gl_info))
12172 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12174 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12175 glsl_fragment_pipe_fog(context, state, state_id);
12178 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12179 const struct wined3d_state *state, DWORD state_id)
12181 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12183 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12184 if (!shader_glsl_full_ffp_varyings(gl_info))
12185 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12188 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12189 const struct wined3d_state *state, DWORD state_id)
12191 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12194 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12195 const struct wined3d_state *state, DWORD state_id)
12197 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12200 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12201 const struct wined3d_state *state, DWORD state_id)
12203 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12204 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12205 float ref = wined3d_alpha_ref(state);
12207 if (func)
12209 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12210 checkGLcall("glAlphaFunc");
12214 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12215 const struct wined3d_state *state, DWORD state_id)
12217 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12220 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12221 const struct wined3d_state *state, DWORD state_id)
12223 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12225 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12227 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12228 checkGLcall("glEnable(GL_ALPHA_TEST)");
12230 else
12232 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12233 checkGLcall("glDisable(GL_ALPHA_TEST)");
12237 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12238 const struct wined3d_state *state, DWORD state_id)
12240 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12243 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12244 const struct wined3d_state *state, DWORD state_id)
12246 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12249 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12250 const struct wined3d_state *state, DWORD state_id)
12252 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12255 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12257 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12258 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12259 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12260 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12261 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12262 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12263 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12264 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12265 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12266 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12267 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12268 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12269 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12270 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12271 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12272 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12273 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12274 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12275 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12276 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12277 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12278 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12279 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12280 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12281 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12282 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12283 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12284 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12285 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12286 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12287 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12288 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12289 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12290 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12291 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12292 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12293 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12294 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12295 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12296 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12297 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12298 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12299 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12300 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12301 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12302 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12303 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12304 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12305 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12306 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12307 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12308 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12309 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12310 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12311 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12312 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12313 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12314 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12315 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12316 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12317 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12318 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12319 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12320 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12321 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12322 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12323 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12324 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12325 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12326 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12327 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12328 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12329 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12330 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12331 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12332 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12333 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12334 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12335 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12336 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12337 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12338 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12339 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12340 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12341 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12342 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12343 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12344 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12345 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12346 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12347 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12348 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12349 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12350 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12351 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12352 {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 },
12353 {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 },
12354 {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 },
12355 {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 },
12356 {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 },
12357 {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 },
12358 {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 },
12359 {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 },
12360 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12361 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12362 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12363 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12364 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12365 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12366 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12367 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12368 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12369 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12370 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12371 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12372 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12375 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12377 return TRUE;
12380 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12384 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12386 glsl_fragment_pipe_enable,
12387 glsl_fragment_pipe_get_caps,
12388 glsl_fragment_pipe_get_emul_mask,
12389 glsl_fragment_pipe_alloc,
12390 glsl_fragment_pipe_free,
12391 glsl_fragment_pipe_alloc_context_data,
12392 glsl_fragment_pipe_free_context_data,
12393 shader_glsl_color_fixup_supported,
12394 glsl_fragment_pipe_state_template,
12397 struct glsl_blitter_args
12399 GLenum texture_type;
12400 struct color_fixup_desc fixup;
12401 unsigned short use_colour_key;
12404 struct glsl_blitter_program
12406 struct wine_rb_entry entry;
12407 struct glsl_blitter_args args;
12408 GLuint id;
12411 struct wined3d_glsl_blitter
12413 struct wined3d_blitter blitter;
12414 struct wined3d_string_buffer_list string_buffers;
12415 struct wine_rb_tree programs;
12416 GLuint palette_texture;
12419 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12421 const struct glsl_blitter_args *a = key;
12422 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12424 return memcmp(a, b, sizeof(*a));
12427 /* Context activation is done by the caller. */
12428 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12430 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12431 struct wined3d_context_gl *context_gl = ctx;
12432 const struct wined3d_gl_info *gl_info;
12434 gl_info = context_gl->gl_info;
12435 GL_EXTCALL(glDeleteProgram(program->id));
12436 checkGLcall("glDeleteProgram()");
12437 heap_free(program);
12440 /* Context activation is done by the caller. */
12441 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12443 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12444 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12445 struct wined3d_glsl_blitter *glsl_blitter;
12446 struct wined3d_blitter *next;
12448 if ((next = blitter->next))
12449 next->ops->blitter_destroy(next, context);
12451 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12453 if (glsl_blitter->palette_texture)
12454 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12456 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12457 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12459 heap_free(glsl_blitter);
12462 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12463 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12464 const char *output, const char *tex_type, const char *swizzle)
12466 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12467 shader_addline(buffer, "\nvoid main()\n{\n");
12468 /* The alpha-component contains the palette index. */
12469 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12470 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12471 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12472 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12473 * the middle. */
12474 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12475 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12476 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12477 if (args->use_colour_key)
12478 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12479 shader_addline(buffer, "}\n");
12482 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12483 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12484 const char *tex_type)
12486 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12487 char chroma, luminance;
12488 const char *tex;
12490 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12491 * macropixel, giving effectively 16 bits per pixel. The color consists of
12492 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12493 * luminance values, one for each single pixel it contains, and one U and
12494 * one V value shared between both pixels.
12496 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12497 * component contains the luminance and alpha the chroma. With UYVY it is
12498 * vice versa. Thus take the format into account when generating the read
12499 * swizzles
12501 * Reading the Y value is straightforward - just sample the texture. The
12502 * hardware takes care of filtering in the horizontal and vertical
12503 * direction.
12505 * Reading the U and V values is harder. We have to avoid filtering
12506 * horizontally, because that would mix the U and V values of one pixel or
12507 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12508 * get an unfiltered read, regardless of the filtering setting. Vertical
12509 * filtering works automatically though - the U and V values of two rows
12510 * are mixed nicely.
12512 * Apart of avoiding filtering issues, the code has to know which value it
12513 * just read, and where it can find the other one. To determine this, it
12514 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12515 * accordingly.
12517 * Handling horizontal filtering of U and V values requires reading a 2nd
12518 * pair of pixels, extracting U and V and mixing them. This is not
12519 * implemented yet.
12521 * An alternative implementation idea is to load the texture as A8R8G8B8
12522 * texture, with width / 2. This way one read gives all 3 values, finding
12523 * U and V is easy in an unfiltered situation. Finding the luminance on
12524 * the other hand requires finding out if it is an odd or even pixel. The
12525 * real drawback of this approach is filtering. This would have to be
12526 * emulated completely in the shader, reading up two 2 packed pixels in up
12527 * to 2 rows and interpolating both horizontally and vertically. Beyond
12528 * that it would require adjustments to the texture handling code to deal
12529 * with the width scaling. */
12531 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12533 chroma = 'x';
12534 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12536 else
12538 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12539 luminance = 'x';
12542 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12544 /* First we have to read the chroma values. This means we need at least
12545 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12546 * unmodified chroma, we have to rid ourselves of the filtering when we
12547 * sample the texture. */
12548 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12549 /* We must not allow filtering between pixel x and x+1, this would mix U
12550 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12551 * center is at 0.5, so add 0.5. */
12552 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12553 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12555 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12556 * and 0.75 for the even and odd pixels respectively. */
12557 /* Put the value into either of the chroma values. */
12558 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12559 shader_addline(buffer, " if (even)\n");
12560 shader_addline(buffer, " chroma.y = luminance;\n");
12561 shader_addline(buffer, " else\n");
12562 shader_addline(buffer, " chroma.x = luminance;\n");
12564 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12565 * current one. Otherwise, sample the left pixel. */
12566 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12567 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12569 /* Put the value into the other chroma. */
12570 shader_addline(buffer, " if (even)\n");
12571 shader_addline(buffer, " chroma.x = luminance;\n");
12572 shader_addline(buffer, " else\n");
12573 shader_addline(buffer, " chroma.y = luminance;\n");
12575 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12576 * the current one and lerp the two U and V values. */
12578 /* This gives the correctly filtered luminance value. */
12579 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12582 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12583 const struct wined3d_gl_info *gl_info, const char *tex_type)
12585 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12586 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12588 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12589 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12590 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12591 * have only half the pitch of the luminance plane, the packing into the
12592 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12593 * luminance data it looks approximately like this:
12595 * +----------------------------------+----
12596 * | |
12597 * | |
12598 * | |
12599 * | |
12600 * | | 2
12601 * | LUMINANCE | -
12602 * | | 3
12603 * | |
12604 * | |
12605 * | |
12606 * | |
12607 * +----------------+-----------------+----
12608 * | | |
12609 * | V even rows | V odd rows |
12610 * | | | 1
12611 * +----------------+------------------ -
12612 * | | | 3
12613 * | U even rows | U odd rows |
12614 * | | |
12615 * +----------------+-----------------+----
12616 * | | |
12617 * | 0.5 | 0.5 |
12619 * So it appears as if there are 4 chroma images, but in fact the odd rows
12620 * in the chroma images are in the same row as the even ones. So it is
12621 * kinda tricky to read. */
12623 /* First sample the chroma values. */
12624 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12625 /* The chroma planes have only half the width. */
12626 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12628 /* The first value is between 2/3 and 5/6 of the texture's height, so
12629 * scale+bias the coordinate. Also read the right side of the image when
12630 * reading odd lines.
12632 * Don't forget to clamp the y values in into the range, otherwise we'll
12633 * get filtering bleeding. */
12635 /* Read odd lines from the right side (add 0.5 to the x coordinate). Keep
12636 * in mind that each line of the chroma plane corresponds to 2 lines of the
12637 * resulting image. */
12638 shader_addline(buffer, " if (fract(texcoord.y * size.y * 0.25) >= 0.5)\n");
12639 shader_addline(buffer, " texcoord.x += 0.5;\n");
12641 /* Clamp, keep the half pixel origin in mind. */
12642 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12643 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12645 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12647 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12648 * 6/6th No need to clamp because we're just reusing the already clamped
12649 * value from above. */
12650 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12651 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12653 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12654 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12655 * values from bleeding into the sampled luminance values due to
12656 * filtering. */
12657 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12658 /* Multiply the y coordinate by 2/3 and clamp it. */
12659 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12660 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12663 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12664 const struct wined3d_gl_info *gl_info, const char *tex_type)
12666 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12667 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12669 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12670 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12671 * effective bitdepth is 12 bits per pixel. If the whole texture is
12672 * interpreted as luminance data it looks approximately like this:
12674 * +----------------------------------+----
12675 * | |
12676 * | |
12677 * | |
12678 * | |
12679 * | | 2
12680 * | LUMINANCE | -
12681 * | | 3
12682 * | |
12683 * | |
12684 * | |
12685 * | |
12686 * +----------------------------------+----
12687 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12688 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12689 * | | 1
12690 * | | -
12691 * | | 3
12692 * | |
12693 * | |
12694 * +----------------------------------+---- */
12696 /* First sample the chroma values. */
12697 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12698 /* We only have half the number of chroma pixels. */
12699 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12700 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12702 /* We must not allow filtering horizontally, this would mix U and V.
12703 * Vertical filtering is ok. However, bear in mind that the pixel center
12704 * is at 0.5, so add 0.5. */
12706 /* Convert to non-normalised coordinates so we can find the individual
12707 * pixel. */
12708 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12709 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12710 * 0.5 to hit the center of the pixel. Then convert back to normalised
12711 * coordinates. */
12712 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12713 /* Clamp, keep the half pixel origin in mind. */
12714 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12716 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12717 /* Add 1.0 / size.x to sample the adjacent texel. */
12718 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12719 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12721 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12722 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12723 * values from bleeding into the sampled luminance values due to
12724 * filtering. */
12725 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12726 /* Multiply the y coordinate by 2/3 and clamp it. */
12727 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12728 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12731 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12732 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12733 const char *output, const char *tex_type, const char *swizzle)
12735 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12737 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12738 shader_addline(buffer, "float luminance;\n");
12739 shader_addline(buffer, "vec2 texcoord;\n");
12740 shader_addline(buffer, "vec2 chroma;\n");
12741 shader_addline(buffer, "uniform vec2 size;\n");
12743 shader_addline(buffer, "\nvoid main()\n{\n");
12745 switch (complex_fixup)
12747 case COMPLEX_FIXUP_UYVY:
12748 case COMPLEX_FIXUP_YUY2:
12749 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12750 break;
12752 case COMPLEX_FIXUP_YV12:
12753 gen_yv12_read(buffer, gl_info, tex_type);
12754 break;
12756 case COMPLEX_FIXUP_NV12:
12757 gen_nv12_read(buffer, gl_info, tex_type);
12758 break;
12760 case COMPLEX_FIXUP_YUV:
12761 /* With APPLE_rgb_422, things are much simpler. The only thing we
12762 * have to do here is Y'CbCr to RGB conversion. */
12763 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12764 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12765 shader_addline(buffer, " luminance = yuv.y;\n");
12766 shader_addline(buffer, " chroma = yuv.xz;\n");
12767 break;
12769 default:
12770 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12771 string_buffer_free(buffer);
12772 return;
12775 /* Calculate the final result. Formula is taken from
12776 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12777 * ranges from -0.5 to 0.5. */
12778 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12780 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12781 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12782 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12783 if (args->use_colour_key)
12784 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12786 shader_addline(buffer, "}\n");
12789 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12790 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12791 const char *output, const char *tex_type, const char *swizzle)
12793 shader_addline(buffer, "\nvoid main()\n{\n");
12794 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12795 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12796 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12797 if (args->use_colour_key)
12798 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12799 shader_addline(buffer, "}\n");
12802 /* Context activation is done by the caller. */
12803 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12804 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12806 static const struct
12808 GLenum texture_target;
12809 const char texture_type[7];
12810 const char texcoord_swizzle[4];
12812 texture_data[] =
12814 {GL_TEXTURE_2D, "2D", "xy"},
12815 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12816 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12818 static const char vshader_main[] =
12819 "\n"
12820 "void main()\n"
12821 "{\n"
12822 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12823 " out_texcoord = texcoord;\n"
12824 "}\n";
12825 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12826 struct wined3d_string_buffer *buffer, *output;
12827 GLuint program, vshader_id, fshader_id;
12828 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12829 unsigned int i;
12830 GLint loc;
12832 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12834 if (args->texture_type == texture_data[i].texture_target)
12836 tex_type = texture_data[i].texture_type;
12837 swizzle = texture_data[i].texcoord_swizzle;
12838 break;
12841 if (i == ARRAY_SIZE(texture_data))
12843 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12844 return 0;
12847 program = GL_EXTCALL(glCreateProgram());
12849 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12851 buffer = string_buffer_get(&blitter->string_buffers);
12852 shader_glsl_add_version_declaration(buffer, gl_info);
12853 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12854 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12855 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12856 shader_addline(buffer, vshader_main);
12858 ptr = buffer->buffer;
12859 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12860 GL_EXTCALL(glAttachShader(program, vshader_id));
12861 GL_EXTCALL(glDeleteShader(vshader_id));
12863 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12865 string_buffer_clear(buffer);
12866 shader_glsl_add_version_declaration(buffer, gl_info);
12867 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12868 if (args->use_colour_key)
12870 shader_addline(buffer, "uniform struct\n{\n");
12871 shader_addline(buffer, " vec4 low, high;\n");
12872 shader_addline(buffer, "} colour_key;\n");
12874 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12875 /* TODO: Declare the out variable with the correct type (and put it in the
12876 * blitter args). */
12877 if (!use_legacy_fragment_output(gl_info))
12878 shader_addline(buffer, "out vec4 ps_out[1];\n");
12880 output = string_buffer_get(&blitter->string_buffers);
12881 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12883 switch (complex_fixup)
12885 case COMPLEX_FIXUP_P8:
12886 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12887 break;
12888 case COMPLEX_FIXUP_YUY2:
12889 case COMPLEX_FIXUP_UYVY:
12890 case COMPLEX_FIXUP_YV12:
12891 case COMPLEX_FIXUP_NV12:
12892 case COMPLEX_FIXUP_YUV:
12893 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12894 break;
12895 case COMPLEX_FIXUP_NONE:
12896 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12899 string_buffer_release(&blitter->string_buffers, output);
12901 ptr = buffer->buffer;
12902 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12903 string_buffer_release(&blitter->string_buffers, buffer);
12904 GL_EXTCALL(glAttachShader(program, fshader_id));
12905 GL_EXTCALL(glDeleteShader(fshader_id));
12907 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12908 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12910 if (!use_legacy_fragment_output(gl_info))
12911 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12913 GL_EXTCALL(glCompileShader(vshader_id));
12914 print_glsl_info_log(gl_info, vshader_id, FALSE);
12915 GL_EXTCALL(glCompileShader(fshader_id));
12916 print_glsl_info_log(gl_info, fshader_id, FALSE);
12917 GL_EXTCALL(glLinkProgram(program));
12918 shader_glsl_validate_link(gl_info, program);
12920 GL_EXTCALL(glUseProgram(program));
12921 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12922 GL_EXTCALL(glUniform1i(loc, 0));
12923 if (complex_fixup == COMPLEX_FIXUP_P8)
12925 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12926 GL_EXTCALL(glUniform1i(loc, 1));
12929 return program;
12932 /* Context activation is done by the caller. */
12933 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12934 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12936 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12937 const struct wined3d_palette *palette;
12939 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12941 if (!blitter->palette_texture)
12942 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12944 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
12945 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12946 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12947 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12948 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12950 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
12951 checkGLcall("glBindBuffer");
12953 if (palette)
12955 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12956 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12958 else
12960 static const DWORD black;
12962 FIXME("P8 texture loaded without a palette.\n");
12963 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12964 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12967 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
12970 /* Context activation is done by the caller. */
12971 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12972 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
12974 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12975 struct glsl_blitter_program *program;
12976 struct glsl_blitter_args args;
12977 struct wine_rb_entry *entry;
12979 memset(&args, 0, sizeof(args));
12980 args.texture_type = texture_gl->target;
12981 args.fixup = texture_gl->t.resource.format->color_fixup;
12982 args.use_colour_key = use_colour_key;
12984 if ((entry = wine_rb_get(&blitter->programs, &args)))
12985 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12987 if (!(program = heap_alloc(sizeof(*program))))
12989 ERR("Failed to allocate blitter program memory.\n");
12990 return NULL;
12993 program->args = args;
12994 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
12996 WARN("Failed to generate blitter program.\n");
12997 heap_free(program);
12998 return NULL;
13001 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
13003 ERR("Failed to store blitter program.\n");
13004 GL_EXTCALL(glDeleteProgram(program->id));
13005 heap_free(program);
13006 return NULL;
13009 return program;
13012 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
13013 const struct wined3d_texture_gl *src_texture, DWORD src_location,
13014 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
13016 const struct wined3d_resource *src_resource = &src_texture->t.resource;
13017 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
13018 const struct wined3d_format *src_format = src_resource->format;
13019 const struct wined3d_format *dst_format = dst_resource->format;
13020 bool src_ds, dst_ds;
13021 BOOL decompress;
13023 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13025 src_ds = src_format->depth_size || src_format->stencil_size;
13026 dst_ds = dst_format->depth_size || dst_format->stencil_size;
13027 /* We could in principle support raw depth/stencil <-> colour blits as
13028 * well in some cases, but note that typeless formats like e.g.
13029 * R16_TYPELESS may use a normalised GL format for depth/stencil
13030 * resources, and a floating-point format for colour resources, */
13031 if (src_ds && dst_ds)
13032 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13033 else if (!src_ds && !dst_ds)
13034 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13037 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
13038 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13040 TRACE("Unsupported blit_op %#x.\n", blit_op);
13041 return FALSE;
13044 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13045 return FALSE;
13047 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13048 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13050 TRACE("Multi-sample source textures not supported.\n");
13051 return FALSE;
13054 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13056 TRACE("Destination resource does not have GPU access.\n");
13057 return FALSE;
13060 /* We don't necessarily want to blit from resources without
13061 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13062 * compressed textures. */
13063 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
13064 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
13065 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13067 TRACE("Source resource does not have GPU access.\n");
13068 return FALSE;
13071 if (!is_identity_fixup(dst_format->color_fixup)
13072 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13074 TRACE("Destination fixups are not supported.\n");
13075 return FALSE;
13078 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
13079 && !((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
13080 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13082 TRACE("Destination texture is not FBO attachable.\n");
13083 return FALSE;
13086 TRACE("Returning supported.\n");
13087 return TRUE;
13090 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13091 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13092 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13093 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13094 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
13095 const struct wined3d_format *resolve_format)
13097 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13098 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13099 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13100 struct wined3d_device *device = dst_texture->resource.device;
13101 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13102 struct wined3d_texture *staging_texture = NULL;
13103 struct wined3d_glsl_blitter *glsl_blitter;
13104 struct wined3d_color_key alpha_test_key;
13105 struct glsl_blitter_program *program;
13106 struct wined3d_blitter *next;
13107 unsigned int src_level;
13108 GLint location;
13109 RECT s, d;
13111 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
13112 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
13113 "colour_key %p, filter %s, resolve format %p.\n",
13114 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13115 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13116 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
13118 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13120 if (!(next = blitter->next))
13122 ERR("No blitter to handle blit op %#x.\n", op);
13123 return dst_location;
13126 TRACE("Forwarding to blitter %p.\n", next);
13127 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13128 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
13129 resolve_format);
13132 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13134 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13136 struct wined3d_resource_desc desc;
13137 struct wined3d_box upload_box;
13138 HRESULT hr;
13140 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13142 src_level = src_sub_resource_idx % src_texture->level_count;
13143 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13144 desc.format = src_texture->resource.format->id;
13145 desc.multisample_type = src_texture->resource.multisample_type;
13146 desc.multisample_quality = src_texture->resource.multisample_quality;
13147 desc.usage = WINED3DUSAGE_PRIVATE;
13148 desc.bind_flags = 0;
13149 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13150 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13151 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13152 desc.depth = 1;
13153 desc.size = 0;
13155 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13156 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13158 ERR("Failed to create staging texture, hr %#x.\n", hr);
13159 return dst_location;
13162 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13163 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13164 src_texture, src_sub_resource_idx, &upload_box);
13166 src_texture = staging_texture;
13167 src_texture_gl = wined3d_texture_gl(src_texture);
13168 src_sub_resource_idx = 0;
13170 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13171 && (src_texture->sub_resources[src_sub_resource_idx].locations
13172 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13173 && !wined3d_resource_is_offscreen(&src_texture->resource))
13176 /* Without FBO blits transferring from the drawable to the texture is
13177 * expensive, because we have to flip the data in sysmem. Since we can
13178 * flip in the blitter, we don't actually need that flip anyway. So we
13179 * use the surface's texture as scratch texture, and flip the source
13180 * rectangle instead. */
13181 texture2d_load_fb_texture(src_texture_gl, src_sub_resource_idx, FALSE, context);
13183 s = *src_rect;
13184 src_level = src_sub_resource_idx % src_texture->level_count;
13185 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13186 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13187 src_rect = &s;
13189 else
13191 wined3d_texture_load(src_texture, context, FALSE);
13194 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13195 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13196 else
13197 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13199 wined3d_context_gl_apply_blit_state(context_gl, device);
13201 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13203 d = *dst_rect;
13204 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13205 dst_rect = &d;
13208 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
13210 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13212 const struct wined3d_format *f = src_texture->resource.format;
13214 alpha_test_key.color_space_low_value = 0;
13215 alpha_test_key.color_space_high_value = ~(wined3d_mask_from_size(f->alpha_size) << f->alpha_offset);
13216 colour_key = &alpha_test_key;
13218 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13220 colour_key = NULL;
13223 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13225 ERR("Failed to get blitter program.\n");
13226 return dst_location;
13228 GL_EXTCALL(glUseProgram(program->id));
13229 switch (get_complex_fixup(program->args.fixup))
13231 case COMPLEX_FIXUP_P8:
13232 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13233 break;
13235 case COMPLEX_FIXUP_YUY2:
13236 case COMPLEX_FIXUP_UYVY:
13237 case COMPLEX_FIXUP_YV12:
13238 case COMPLEX_FIXUP_NV12:
13239 case COMPLEX_FIXUP_YUV:
13240 src_level = src_sub_resource_idx % src_texture->level_count;
13241 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13242 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13243 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13244 break;
13246 default:
13247 break;
13249 if (colour_key)
13251 struct wined3d_color float_key[2];
13253 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13254 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13255 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13256 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13257 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13259 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13260 GL_EXTCALL(glUseProgram(0));
13262 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13263 gl_info->gl_ops.gl.p_glFlush();
13265 if (staging_texture)
13266 wined3d_texture_decref(staging_texture);
13268 return dst_location;
13271 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13272 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13273 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
13275 struct wined3d_blitter *next;
13277 if ((next = blitter->next))
13278 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13279 clear_rects, draw_rect, flags, color, depth, stencil);
13282 static const struct wined3d_blitter_ops glsl_blitter_ops =
13284 glsl_blitter_destroy,
13285 glsl_blitter_clear,
13286 glsl_blitter_blit,
13289 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13290 const struct wined3d_device *device)
13292 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13293 struct wined3d_glsl_blitter *blitter;
13295 if (device->shader_backend != &glsl_shader_backend)
13296 return NULL;
13298 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13299 return NULL;
13301 if (!(blitter = heap_alloc(sizeof(*blitter))))
13303 ERR("Failed to allocate blitter.\n");
13304 return NULL;
13307 TRACE("Created blitter %p.\n", blitter);
13309 blitter->blitter.ops = &glsl_blitter_ops;
13310 blitter->blitter.next = *next;
13311 string_buffer_list_init(&blitter->string_buffers);
13312 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13313 blitter->palette_texture = 0;
13314 *next = &blitter->blitter;
13316 return *next;