oleaut32: Remove unnecessary consts.
[wine.git] / dlls / wined3d / glsl_shader.c
blob0c73aba81118213d8f114c56784a04175ba40f21
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 "config.h"
33 #include "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
37 #ifdef HAVE_FLOAT_H
38 # include <float.h>
39 #endif
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
48 #define WINED3D_GLSL_SAMPLE_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
53 static const struct
55 unsigned int coord_size;
56 unsigned int resinfo_size;
57 const char *type_part;
59 resource_type_info[] =
61 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
62 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
63 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
64 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
66 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
67 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
68 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
69 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
70 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
71 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
74 static const struct
76 enum wined3d_data_type data_type;
77 const char *glsl_scalar_type;
78 const char *glsl_vector_type;
80 component_type_info[] =
82 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_UNKNOWN */
83 {WINED3D_DATA_UINT, "uint", "uvec"}, /* WINED3D_TYPE_UINT */
84 {WINED3D_DATA_INT, "int", "ivec"}, /* WINED3D_TYPE_INT */
85 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_FLOAT */
88 struct glsl_dst_param
90 char reg_name[150];
91 char mask_str[6];
94 struct glsl_src_param
96 char reg_name[150];
97 char param_str[200];
100 struct glsl_sample_function
102 struct wined3d_string_buffer *name;
103 unsigned int coord_mask;
104 unsigned int deriv_mask;
105 enum wined3d_data_type data_type;
106 BOOL output_single_component;
107 unsigned int offset_size;
110 enum heap_node_op
112 HEAP_NODE_TRAVERSE_LEFT,
113 HEAP_NODE_TRAVERSE_RIGHT,
114 HEAP_NODE_POP,
117 struct constant_entry
119 unsigned int idx;
120 unsigned int version;
123 struct constant_heap
125 struct constant_entry *entries;
126 BOOL *contained;
127 unsigned int *positions;
128 unsigned int size;
131 /* GLSL shader private data */
132 struct shader_glsl_priv
134 struct wined3d_string_buffer shader_buffer;
135 struct wined3d_string_buffer_list string_buffers;
136 struct wine_rb_tree program_lookup;
137 struct constant_heap vconst_heap;
138 struct constant_heap pconst_heap;
139 unsigned char *stack;
140 UINT next_constant_version;
142 const struct wined3d_vertex_pipe_ops *vertex_pipe;
143 const struct fragment_pipeline *fragment_pipe;
144 struct wine_rb_tree ffp_vertex_shaders;
145 struct wine_rb_tree ffp_fragment_shaders;
146 BOOL ffp_proj_control;
147 BOOL legacy_lighting;
150 struct glsl_vs_program
152 struct list shader_entry;
153 GLuint id;
154 GLenum vertex_color_clamp;
155 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
156 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
157 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
158 GLint pos_fixup_location;
160 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
161 GLint projection_matrix_location;
162 GLint normal_matrix_location;
163 GLint texture_matrix_location[MAX_TEXTURES];
164 GLint material_ambient_location;
165 GLint material_diffuse_location;
166 GLint material_specular_location;
167 GLint material_emissive_location;
168 GLint material_shininess_location;
169 GLint light_ambient_location;
170 struct
172 GLint diffuse;
173 GLint specular;
174 GLint ambient;
175 GLint position;
176 GLint direction;
177 GLint range;
178 GLint falloff;
179 GLint c_att;
180 GLint l_att;
181 GLint q_att;
182 GLint cos_htheta;
183 GLint cos_hphi;
184 } light_location[MAX_ACTIVE_LIGHTS];
185 GLint pointsize_location;
186 GLint pointsize_min_location;
187 GLint pointsize_max_location;
188 GLint pointsize_c_att_location;
189 GLint pointsize_l_att_location;
190 GLint pointsize_q_att_location;
191 GLint clip_planes_location;
194 struct glsl_hs_program
196 struct list shader_entry;
197 GLuint id;
200 struct glsl_ds_program
202 struct list shader_entry;
203 GLuint id;
205 GLint pos_fixup_location;
208 struct glsl_gs_program
210 struct list shader_entry;
211 GLuint id;
213 GLint pos_fixup_location;
216 struct glsl_ps_program
218 struct list shader_entry;
219 GLuint id;
220 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
221 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
222 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
223 GLint bumpenv_mat_location[MAX_TEXTURES];
224 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
225 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
226 GLint tss_constant_location[MAX_TEXTURES];
227 GLint tex_factor_location;
228 GLint specular_enable_location;
229 GLint fog_color_location;
230 GLint fog_density_location;
231 GLint fog_end_location;
232 GLint fog_scale_location;
233 GLint alpha_test_ref_location;
234 GLint ycorrection_location;
235 GLint np2_fixup_location;
236 GLint color_key_location;
237 const struct ps_np2fixup_info *np2_fixup_info;
240 struct glsl_cs_program
242 struct list shader_entry;
243 GLuint id;
246 /* Struct to maintain data about a linked GLSL program */
247 struct glsl_shader_prog_link
249 struct wine_rb_entry program_lookup_entry;
250 struct glsl_vs_program vs;
251 struct glsl_hs_program hs;
252 struct glsl_ds_program ds;
253 struct glsl_gs_program gs;
254 struct glsl_ps_program ps;
255 struct glsl_cs_program cs;
256 GLuint id;
257 DWORD constant_update_mask;
258 unsigned int constant_version;
259 DWORD shader_controlled_clip_distances : 1;
260 DWORD clip_distance_mask : 8; /* MAX_CLIP_DISTANCES, 8 */
261 DWORD padding : 23;
264 struct glsl_program_key
266 GLuint vs_id;
267 GLuint hs_id;
268 GLuint ds_id;
269 GLuint gs_id;
270 GLuint ps_id;
271 GLuint cs_id;
274 struct shader_glsl_ctx_priv {
275 const struct vs_compile_args *cur_vs_args;
276 const struct ds_compile_args *cur_ds_args;
277 const struct ps_compile_args *cur_ps_args;
278 struct ps_np2fixup_info *cur_np2fixup_info;
279 struct wined3d_string_buffer_list *string_buffers;
282 struct glsl_context_data
284 struct glsl_shader_prog_link *glsl_program;
285 GLenum vertex_color_clamp;
286 BOOL rasterization_disabled;
289 struct glsl_ps_compiled_shader
291 struct ps_compile_args args;
292 struct ps_np2fixup_info np2fixup;
293 GLuint id;
296 struct glsl_vs_compiled_shader
298 struct vs_compile_args args;
299 GLuint id;
302 struct glsl_hs_compiled_shader
304 GLuint id;
307 struct glsl_ds_compiled_shader
309 struct ds_compile_args args;
310 GLuint id;
313 struct glsl_gs_compiled_shader
315 struct gs_compile_args args;
316 GLuint id;
319 struct glsl_cs_compiled_shader
321 GLuint id;
324 struct glsl_shader_private
326 union
328 struct glsl_vs_compiled_shader *vs;
329 struct glsl_hs_compiled_shader *hs;
330 struct glsl_ds_compiled_shader *ds;
331 struct glsl_gs_compiled_shader *gs;
332 struct glsl_ps_compiled_shader *ps;
333 struct glsl_cs_compiled_shader *cs;
334 } gl_shaders;
335 unsigned int num_gl_shaders, shader_array_size;
338 struct glsl_ffp_vertex_shader
340 struct wined3d_ffp_vs_desc desc;
341 GLuint id;
342 struct list linked_programs;
345 struct glsl_ffp_fragment_shader
347 struct ffp_frag_desc entry;
348 GLuint id;
349 struct list linked_programs;
352 struct glsl_ffp_destroy_ctx
354 struct shader_glsl_priv *priv;
355 const struct wined3d_gl_info *gl_info;
358 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
360 static const char *debug_gl_shader_type(GLenum type)
362 switch (type)
364 #define WINED3D_TO_STR(u) case u: return #u
365 WINED3D_TO_STR(GL_VERTEX_SHADER);
366 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
367 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
368 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
369 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
370 WINED3D_TO_STR(GL_COMPUTE_SHADER);
371 #undef WINED3D_TO_STR
372 default:
373 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
377 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
379 switch (type)
381 case WINED3D_SHADER_TYPE_VERTEX:
382 return "vs";
384 case WINED3D_SHADER_TYPE_HULL:
385 return "hs";
387 case WINED3D_SHADER_TYPE_DOMAIN:
388 return "ds";
390 case WINED3D_SHADER_TYPE_GEOMETRY:
391 return "gs";
393 case WINED3D_SHADER_TYPE_PIXEL:
394 return "ps";
396 case WINED3D_SHADER_TYPE_COMPUTE:
397 return "cs";
399 default:
400 FIXME("Unhandled shader type %#x.\n", type);
401 return "unknown";
405 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
407 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
408 return 440;
409 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
410 return 150;
411 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
412 return 130;
413 else
414 return 120;
417 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
418 const struct wined3d_gl_info *gl_info)
420 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
423 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
425 char str[4][17];
427 wined3d_ftoa(values[0], str[0]);
428 wined3d_ftoa(values[1], str[1]);
429 wined3d_ftoa(values[2], str[2]);
430 wined3d_ftoa(values[3], str[3]);
431 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
434 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
435 const int *values, unsigned int size)
437 int i;
439 if (!size || size > 4)
441 ERR("Invalid vector size %u.\n", size);
442 return;
445 if (size > 1)
446 shader_addline(buffer, "ivec%u(", size);
448 for (i = 0; i < size; ++i)
449 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
451 if (size > 1)
452 shader_addline(buffer, ")");
455 static const char *get_info_log_line(const char **ptr)
457 const char *p, *q;
459 p = *ptr;
460 if (!(q = strstr(p, "\n")))
462 if (!*p) return NULL;
463 *ptr += strlen(p);
464 return p;
466 *ptr = q + 1;
468 return p;
471 /* Context activation is done by the caller. */
472 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
474 int length = 0;
475 char *log;
477 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
478 return;
480 if (program)
481 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
482 else
483 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
485 /* A size of 1 is just a null-terminated string, so the log should be bigger than
486 * that if there are errors. */
487 if (length > 1)
489 const char *ptr, *line;
491 log = heap_alloc(length);
492 /* The info log is supposed to be zero-terminated, but at least some
493 * versions of fglrx don't terminate the string properly. The reported
494 * length does include the terminator, so explicitly set it to zero
495 * here. */
496 log[length - 1] = 0;
497 if (program)
498 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
499 else
500 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
502 ptr = log;
503 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
505 WARN("Info log received from GLSL shader #%u:\n", id);
506 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
508 else
510 FIXME("Info log received from GLSL shader #%u:\n", id);
511 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
513 heap_free(log);
517 /* Context activation is done by the caller. */
518 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
520 const char *ptr, *line;
522 TRACE("Compiling shader object %u.\n", shader);
524 if (TRACE_ON(d3d_shader))
526 ptr = src;
527 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
530 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
531 checkGLcall("glShaderSource");
532 GL_EXTCALL(glCompileShader(shader));
533 checkGLcall("glCompileShader");
534 print_glsl_info_log(gl_info, shader, FALSE);
537 /* Context activation is done by the caller. */
538 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
540 GLint i, shader_count, source_size = -1;
541 GLuint *shaders;
542 char *source = NULL;
544 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
545 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
547 ERR("Failed to allocate shader array memory.\n");
548 return;
551 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
552 for (i = 0; i < shader_count; ++i)
554 const char *ptr, *line;
555 GLint tmp;
557 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
559 if (source_size < tmp)
561 heap_free(source);
563 if (!(source = heap_alloc_zero(tmp)))
565 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
566 heap_free(shaders);
567 return;
569 source_size = tmp;
572 FIXME("Shader %u:\n", shaders[i]);
573 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
574 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
575 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
576 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
577 FIXME("\n");
579 ptr = source;
580 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
581 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
582 FIXME("\n");
585 heap_free(source);
586 heap_free(shaders);
589 /* Context activation is done by the caller. */
590 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
592 GLint tmp;
594 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
595 return;
597 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
598 if (!tmp)
600 FIXME("Program %u link status invalid.\n", program);
601 shader_glsl_dump_program_source(gl_info, program);
604 print_glsl_info_log(gl_info, program, TRUE);
607 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
609 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
610 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
611 * versions. */
612 return shader_glsl_get_version(gl_info) >= 140;
615 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
617 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
620 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
621 struct shader_glsl_priv *priv, GLuint program_id,
622 const struct wined3d_shader_reg_maps *reg_maps)
624 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
625 struct wined3d_string_buffer *name;
626 unsigned int i, base, count;
627 GLuint block_idx;
629 if (shader_glsl_use_layout_binding_qualifier(gl_info))
630 return;
632 name = string_buffer_get(&priv->string_buffers);
633 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
634 for (i = 0; i < count; ++i)
636 if (!reg_maps->cb_sizes[i])
637 continue;
639 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
640 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
641 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
643 checkGLcall("glUniformBlockBinding");
644 string_buffer_release(&priv->string_buffers, name);
647 /* Context activation is done by the caller. */
648 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
649 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
650 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
652 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
653 unsigned int i, mapped_unit;
654 GLint name_loc;
656 for (i = 0; i < count; ++i)
658 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
659 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
660 if (name_loc == -1)
661 continue;
663 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
664 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
666 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
667 continue;
670 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
671 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
673 checkGLcall("Load sampler bindings");
674 string_buffer_release(&priv->string_buffers, sampler_name);
677 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
678 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
680 const DWORD *tex_unit_map;
681 unsigned int base, count;
683 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
684 if (sampler_idx >= count)
685 return WINED3D_UNMAPPED_STAGE;
686 if (!tex_unit_map)
687 return base + sampler_idx;
688 return tex_unit_map[base + sampler_idx];
691 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
692 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
693 unsigned int sampler_idx)
695 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
696 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
697 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
698 else
699 ERR("Unmapped sampler %u.\n", sampler_idx);
702 /* Context activation is done by the caller. */
703 static void shader_glsl_load_samplers(const struct wined3d_context *context,
704 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
706 const struct wined3d_gl_info *gl_info = context->gl_info;
707 const struct wined3d_shader_version *shader_version;
708 const DWORD *tex_unit_map;
709 unsigned int base, count;
710 const char *prefix;
712 if (shader_glsl_use_layout_binding_qualifier(gl_info))
713 return;
715 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
716 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
717 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
718 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
721 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
722 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
724 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
726 if (icb)
728 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
729 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
730 GLint icb_location;
732 string_buffer_sprintf(icb_name, "%s_icb", prefix);
733 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
734 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
735 checkGLcall("Load immediate constant buffer");
737 string_buffer_release(&priv->string_buffers, icb_name);
741 /* Context activation is done by the caller. */
742 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
743 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
745 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
746 struct wined3d_string_buffer *name;
747 GLint location;
748 unsigned int i;
750 if (shader_glsl_use_layout_binding_qualifier(gl_info))
751 return;
753 name = string_buffer_get(&priv->string_buffers);
754 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
756 if (!reg_maps->uav_resource_info[i].type)
757 continue;
759 string_buffer_sprintf(name, "%s_image%u", prefix, i);
760 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
761 if (location == -1)
762 continue;
764 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
765 GL_EXTCALL(glUniform1i(location, i));
767 checkGLcall("Load image bindings");
768 string_buffer_release(&priv->string_buffers, name);
771 /* Context activation is done by the caller. */
772 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
773 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
775 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
777 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
778 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
779 /* Texture unit mapping is set up to be the same each time the shader
780 * program is used so we can hardcode the sampler uniform values. */
781 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
784 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
785 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
787 if (varyings && *strings)
789 char *ptr = *strings;
791 varyings[*varying_count] = ptr;
793 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
794 ptr += buffer->content_size + 1;
796 *strings = ptr;
799 *strings_length += buffer->content_size + 1;
800 ++(*varying_count);
803 static void append_transform_feedback_skip_components(const char **varyings,
804 unsigned int *varying_count, char **strings, unsigned int *strings_length,
805 struct wined3d_string_buffer *buffer, unsigned int component_count)
807 unsigned int j;
809 for (j = 0; j < component_count / 4; ++j)
811 string_buffer_sprintf(buffer, "gl_SkipComponents4");
812 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
814 if (component_count % 4)
816 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
817 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
821 static BOOL shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
822 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
823 char *strings, unsigned int *strings_length, GLenum buffer_mode)
825 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
826 BOOL have_varyings_to_record = FALSE;
828 count = length = 0;
829 highest_output_slot = 0;
830 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
832 stride = 0;
834 for (i = 0; i < so_desc->element_count; ++i)
836 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
838 highest_output_slot = max(highest_output_slot, e->output_slot);
839 if (e->output_slot != buffer_idx)
840 continue;
842 if (e->stream_idx)
844 FIXME("Unhandled stream %u.\n", e->stream_idx);
845 continue;
848 stride += e->component_count;
850 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
852 append_transform_feedback_skip_components(varyings, &count,
853 &strings, &length, buffer, e->component_count);
854 continue;
857 if (e->component_idx || e->component_count != 4)
859 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
861 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
862 append_transform_feedback_skip_components(varyings, &count,
863 &strings, &length, buffer, e->component_count);
864 continue;
867 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
868 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
869 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
871 else
873 string_buffer_sprintf(buffer, "shader_in_out.reg%u", e->register_idx);
874 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
877 have_varyings_to_record = TRUE;
880 if (buffer_idx < so_desc->buffer_stride_count
881 && stride < so_desc->buffer_strides[buffer_idx] / 4)
883 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
884 append_transform_feedback_skip_components(varyings, &count,
885 &strings, &length, buffer, component_count);
888 if (highest_output_slot <= buffer_idx)
889 break;
891 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
893 string_buffer_sprintf(buffer, "gl_NextBuffer");
894 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
898 if (varying_count)
899 *varying_count = count;
900 if (strings_length)
901 *strings_length = length;
903 return have_varyings_to_record;
906 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
907 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
909 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
910 const struct wined3d_gl_info *gl_info = context->gl_info;
911 struct wined3d_string_buffer *buffer;
912 unsigned int i, count, length;
913 const char **varyings;
914 char *strings;
915 GLenum mode;
917 if (!so_desc->element_count)
918 return;
920 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
922 mode = GL_INTERLEAVED_ATTRIBS;
924 else
926 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
928 for (i = 0; i < so_desc->element_count; ++i)
930 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
932 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
933 return;
935 ++element_count[so_desc->elements[i].output_slot];
938 if (element_count[0] == so_desc->element_count)
940 mode = GL_INTERLEAVED_ATTRIBS;
942 else
944 mode = GL_SEPARATE_ATTRIBS;
945 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
947 if (element_count[i] != 1)
948 break;
950 for (; i < ARRAY_SIZE(element_count); ++i)
952 if (element_count[i])
954 FIXME("Only single element per buffer is allowed in separate mode.\n");
955 return;
961 buffer = string_buffer_get(&priv->string_buffers);
963 if (!shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode))
965 FIXME("No varyings to record, disabling transform feedback.\n");
966 shader->u.gs.so_desc.element_count = 0;
967 string_buffer_release(&priv->string_buffers, buffer);
968 return;
971 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
973 ERR("Out of memory.\n");
974 string_buffer_release(&priv->string_buffers, buffer);
975 return;
977 if (!(strings = heap_calloc(length, sizeof(*strings))))
979 ERR("Out of memory.\n");
980 heap_free(varyings);
981 string_buffer_release(&priv->string_buffers, buffer);
982 return;
985 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
986 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
987 checkGLcall("glTransformFeedbackVaryings");
989 heap_free(varyings);
990 heap_free(strings);
991 string_buffer_release(&priv->string_buffers, buffer);
994 /* Context activation is done by the caller. */
995 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
996 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
998 unsigned int start = ~0U, end = 0;
999 int stack_idx = 0;
1000 unsigned int heap_idx = 1;
1001 unsigned int idx;
1003 if (heap->entries[heap_idx].version <= version) return;
1005 idx = heap->entries[heap_idx].idx;
1006 if (constant_locations[idx] != -1)
1007 start = end = idx;
1008 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1010 while (stack_idx >= 0)
1012 /* Note that we fall through to the next case statement. */
1013 switch(stack[stack_idx])
1015 case HEAP_NODE_TRAVERSE_LEFT:
1017 unsigned int left_idx = heap_idx << 1;
1018 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1020 heap_idx = left_idx;
1021 idx = heap->entries[heap_idx].idx;
1022 if (constant_locations[idx] != -1)
1024 if (start > idx)
1025 start = idx;
1026 if (end < idx)
1027 end = idx;
1030 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1031 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1032 break;
1036 case HEAP_NODE_TRAVERSE_RIGHT:
1038 unsigned int right_idx = (heap_idx << 1) + 1;
1039 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1041 heap_idx = right_idx;
1042 idx = heap->entries[heap_idx].idx;
1043 if (constant_locations[idx] != -1)
1045 if (start > idx)
1046 start = idx;
1047 if (end < idx)
1048 end = idx;
1051 stack[stack_idx++] = HEAP_NODE_POP;
1052 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1053 break;
1057 case HEAP_NODE_POP:
1058 heap_idx >>= 1;
1059 --stack_idx;
1060 break;
1063 if (start <= end)
1064 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1065 checkGLcall("walk_constant_heap()");
1068 /* Context activation is done by the caller. */
1069 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1070 GLint location, const struct wined3d_vec4 *data)
1072 GLfloat clamped_constant[4];
1074 if (location == -1) return;
1076 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1077 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1078 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1079 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1081 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1084 /* Context activation is done by the caller. */
1085 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1086 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1087 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1089 int stack_idx = 0;
1090 unsigned int heap_idx = 1;
1091 unsigned int idx;
1093 if (heap->entries[heap_idx].version <= version) return;
1095 idx = heap->entries[heap_idx].idx;
1096 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1097 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1099 while (stack_idx >= 0)
1101 /* Note that we fall through to the next case statement. */
1102 switch(stack[stack_idx])
1104 case HEAP_NODE_TRAVERSE_LEFT:
1106 unsigned int left_idx = heap_idx << 1;
1107 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1109 heap_idx = left_idx;
1110 idx = heap->entries[heap_idx].idx;
1111 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1113 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1114 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1115 break;
1119 case HEAP_NODE_TRAVERSE_RIGHT:
1121 unsigned int right_idx = (heap_idx << 1) + 1;
1122 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1124 heap_idx = right_idx;
1125 idx = heap->entries[heap_idx].idx;
1126 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1128 stack[stack_idx++] = HEAP_NODE_POP;
1129 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1130 break;
1134 case HEAP_NODE_POP:
1135 heap_idx >>= 1;
1136 --stack_idx;
1137 break;
1140 checkGLcall("walk_constant_heap_clamped()");
1143 /* Context activation is done by the caller. */
1144 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1145 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1146 unsigned char *stack, unsigned int version)
1148 const struct wined3d_shader_lconst *lconst;
1150 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1151 if (shader->reg_maps.shader_version.major == 1
1152 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1153 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1154 else
1155 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1157 if (!shader->load_local_constsF)
1159 TRACE("No need to load local float constants for this shader.\n");
1160 return;
1163 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1164 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1166 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1168 checkGLcall("glUniform4fv()");
1171 /* Context activation is done by the caller. */
1172 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1173 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1175 unsigned int i;
1176 struct list* ptr;
1178 for (i = 0; constants_set; constants_set >>= 1, ++i)
1180 if (!(constants_set & 1)) continue;
1182 /* We found this uniform name in the program - go ahead and send the data */
1183 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1186 /* Load immediate constants */
1187 ptr = list_head(&shader->constantsI);
1188 while (ptr)
1190 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1191 unsigned int idx = lconst->idx;
1192 const GLint *values = (const GLint *)lconst->value;
1194 /* We found this uniform name in the program - go ahead and send the data */
1195 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1196 ptr = list_next(&shader->constantsI, ptr);
1198 checkGLcall("glUniform4iv()");
1201 /* Context activation is done by the caller. */
1202 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1203 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1205 unsigned int i;
1206 struct list* ptr;
1208 for (i = 0; constants_set; constants_set >>= 1, ++i)
1210 if (!(constants_set & 1)) continue;
1212 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1215 /* Load immediate constants */
1216 ptr = list_head(&shader->constantsB);
1217 while (ptr)
1219 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1220 unsigned int idx = lconst->idx;
1221 const GLint *values = (const GLint *)lconst->value;
1223 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1224 ptr = list_next(&shader->constantsB, ptr);
1226 checkGLcall("glUniform1iv()");
1229 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1231 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1234 /* Context activation is done by the caller (state handler). */
1235 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1236 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1238 struct
1240 float sx, sy;
1242 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1243 UINT fixup = ps->np2_fixup_info->active;
1244 UINT i;
1246 for (i = 0; fixup; fixup >>= 1, ++i)
1248 const struct wined3d_texture *tex = state->textures[i];
1249 unsigned char idx = ps->np2_fixup_info->idx[i];
1251 if (!tex)
1253 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1254 continue;
1257 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1258 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1261 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1264 /* Taken and adapted from Mesa. */
1265 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1267 float pos, neg, t, det;
1268 struct wined3d_matrix temp;
1270 /* Calculate the determinant of upper left 3x3 submatrix and
1271 * determine if the matrix is singular. */
1272 pos = neg = 0.0f;
1273 t = in->_11 * in->_22 * in->_33;
1274 if (t >= 0.0f)
1275 pos += t;
1276 else
1277 neg += t;
1279 t = in->_21 * in->_32 * in->_13;
1280 if (t >= 0.0f)
1281 pos += t;
1282 else
1283 neg += t;
1284 t = in->_31 * in->_12 * in->_23;
1285 if (t >= 0.0f)
1286 pos += t;
1287 else
1288 neg += t;
1290 t = -in->_31 * in->_22 * in->_13;
1291 if (t >= 0.0f)
1292 pos += t;
1293 else
1294 neg += t;
1295 t = -in->_21 * in->_12 * in->_33;
1296 if (t >= 0.0f)
1297 pos += t;
1298 else
1299 neg += t;
1301 t = -in->_11 * in->_32 * in->_23;
1302 if (t >= 0.0f)
1303 pos += t;
1304 else
1305 neg += t;
1307 det = pos + neg;
1309 if (fabsf(det) < 1e-25f)
1310 return FALSE;
1312 det = 1.0f / det;
1313 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1314 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1315 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1316 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1317 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1318 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1319 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1320 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1321 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1323 *out = temp;
1324 return TRUE;
1327 static void swap_rows(float **a, float **b)
1329 float *tmp = *a;
1331 *a = *b;
1332 *b = tmp;
1335 static BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1337 float wtmp[4][8];
1338 float m0, m1, m2, m3, s;
1339 float *r0, *r1, *r2, *r3;
1341 r0 = wtmp[0];
1342 r1 = wtmp[1];
1343 r2 = wtmp[2];
1344 r3 = wtmp[3];
1346 r0[0] = m->_11;
1347 r0[1] = m->_12;
1348 r0[2] = m->_13;
1349 r0[3] = m->_14;
1350 r0[4] = 1.0f;
1351 r0[5] = r0[6] = r0[7] = 0.0f;
1353 r1[0] = m->_21;
1354 r1[1] = m->_22;
1355 r1[2] = m->_23;
1356 r1[3] = m->_24;
1357 r1[5] = 1.0f;
1358 r1[4] = r1[6] = r1[7] = 0.0f;
1360 r2[0] = m->_31;
1361 r2[1] = m->_32;
1362 r2[2] = m->_33;
1363 r2[3] = m->_34;
1364 r2[6] = 1.0f;
1365 r2[4] = r2[5] = r2[7] = 0.0f;
1367 r3[0] = m->_41;
1368 r3[1] = m->_42;
1369 r3[2] = m->_43;
1370 r3[3] = m->_44;
1371 r3[7] = 1.0f;
1372 r3[4] = r3[5] = r3[6] = 0.0f;
1374 /* Choose pivot - or die. */
1375 if (fabsf(r3[0]) > fabsf(r2[0]))
1376 swap_rows(&r3, &r2);
1377 if (fabsf(r2[0]) > fabsf(r1[0]))
1378 swap_rows(&r2, &r1);
1379 if (fabsf(r1[0]) > fabsf(r0[0]))
1380 swap_rows(&r1, &r0);
1381 if (r0[0] == 0.0f)
1382 return FALSE;
1384 /* Eliminate first variable. */
1385 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1386 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1387 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1388 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1389 s = r0[4];
1390 if (s != 0.0f)
1392 r1[4] -= m1 * s;
1393 r2[4] -= m2 * s;
1394 r3[4] -= m3 * s;
1396 s = r0[5];
1397 if (s != 0.0f)
1399 r1[5] -= m1 * s;
1400 r2[5] -= m2 * s;
1401 r3[5] -= m3 * s;
1403 s = r0[6];
1404 if (s != 0.0f)
1406 r1[6] -= m1 * s;
1407 r2[6] -= m2 * s;
1408 r3[6] -= m3 * s;
1410 s = r0[7];
1411 if (s != 0.0f)
1413 r1[7] -= m1 * s;
1414 r2[7] -= m2 * s;
1415 r3[7] -= m3 * s;
1418 /* Choose pivot - or die. */
1419 if (fabsf(r3[1]) > fabsf(r2[1]))
1420 swap_rows(&r3, &r2);
1421 if (fabsf(r2[1]) > fabsf(r1[1]))
1422 swap_rows(&r2, &r1);
1423 if (r1[1] == 0.0f)
1424 return FALSE;
1426 /* Eliminate second variable. */
1427 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1428 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1429 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1430 s = r1[4];
1431 if (s != 0.0f)
1433 r2[4] -= m2 * s;
1434 r3[4] -= m3 * s;
1436 s = r1[5];
1437 if (s != 0.0f)
1439 r2[5] -= m2 * s;
1440 r3[5] -= m3 * s;
1442 s = r1[6];
1443 if (s != 0.0f)
1445 r2[6] -= m2 * s;
1446 r3[6] -= m3 * s;
1448 s = r1[7];
1449 if (s != 0.0f)
1451 r2[7] -= m2 * s;
1452 r3[7] -= m3 * s;
1455 /* Choose pivot - or die. */
1456 if (fabsf(r3[2]) > fabsf(r2[2]))
1457 swap_rows(&r3, &r2);
1458 if (r2[2] == 0.0f)
1459 return FALSE;
1461 /* Eliminate third variable. */
1462 m3 = r3[2] / r2[2];
1463 r3[3] -= m3 * r2[3];
1464 r3[4] -= m3 * r2[4];
1465 r3[5] -= m3 * r2[5];
1466 r3[6] -= m3 * r2[6];
1467 r3[7] -= m3 * r2[7];
1469 /* Last check. */
1470 if (r3[3] == 0.0f)
1471 return FALSE;
1473 /* Back substitute row 3. */
1474 s = 1.0f / r3[3];
1475 r3[4] *= s;
1476 r3[5] *= s;
1477 r3[6] *= s;
1478 r3[7] *= s;
1480 /* Back substitute row 2. */
1481 m2 = r2[3];
1482 s = 1.0f / r2[2];
1483 r2[4] = s * (r2[4] - r3[4] * m2);
1484 r2[5] = s * (r2[5] - r3[5] * m2);
1485 r2[6] = s * (r2[6] - r3[6] * m2);
1486 r2[7] = s * (r2[7] - r3[7] * m2);
1487 m1 = r1[3];
1488 r1[4] -= r3[4] * m1;
1489 r1[5] -= r3[5] * m1;
1490 r1[6] -= r3[6] * m1;
1491 r1[7] -= r3[7] * m1;
1492 m0 = r0[3];
1493 r0[4] -= r3[4] * m0;
1494 r0[5] -= r3[5] * m0;
1495 r0[6] -= r3[6] * m0;
1496 r0[7] -= r3[7] * m0;
1498 /* Back substitute row 1. */
1499 m1 = r1[2];
1500 s = 1.0f / r1[1];
1501 r1[4] = s * (r1[4] - r2[4] * m1);
1502 r1[5] = s * (r1[5] - r2[5] * m1);
1503 r1[6] = s * (r1[6] - r2[6] * m1);
1504 r1[7] = s * (r1[7] - r2[7] * m1);
1505 m0 = r0[2];
1506 r0[4] -= r2[4] * m0;
1507 r0[5] -= r2[5] * m0;
1508 r0[6] -= r2[6] * m0;
1509 r0[7] -= r2[7] * m0;
1511 /* Back substitute row 0. */
1512 m0 = r0[1];
1513 s = 1.0f / r0[0];
1514 r0[4] = s * (r0[4] - r1[4] * m0);
1515 r0[5] = s * (r0[5] - r1[5] * m0);
1516 r0[6] = s * (r0[6] - r1[6] * m0);
1517 r0[7] = s * (r0[7] - r1[7] * m0);
1519 out->_11 = r0[4];
1520 out->_12 = r0[5];
1521 out->_13 = r0[6];
1522 out->_14 = r0[7];
1523 out->_21 = r1[4];
1524 out->_22 = r1[5];
1525 out->_23 = r1[6];
1526 out->_24 = r1[7];
1527 out->_31 = r2[4];
1528 out->_32 = r2[5];
1529 out->_33 = r2[6];
1530 out->_34 = r2[7];
1531 out->_41 = r3[4];
1532 out->_42 = r3[5];
1533 out->_43 = r3[6];
1534 out->_44 = r3[7];
1536 return TRUE;
1539 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1541 struct wined3d_matrix temp;
1542 unsigned int i, j;
1544 for (i = 0; i < 4; ++i)
1545 for (j = 0; j < 4; ++j)
1546 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1548 *out = temp;
1551 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1552 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1554 const struct wined3d_gl_info *gl_info = context->gl_info;
1555 float mat[3 * 3];
1556 struct wined3d_matrix mv;
1557 unsigned int i, j;
1559 if (prog->vs.normal_matrix_location == -1)
1560 return;
1562 get_modelview_matrix(context, state, 0, &mv);
1563 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1564 invert_matrix_3d(&mv, &mv);
1565 else
1566 invert_matrix(&mv, &mv);
1567 /* Tests show that singular modelview matrices are used unchanged as normal
1568 * matrices on D3D3 and older. There seems to be no clearly consistent
1569 * behavior on newer D3D versions so always follow older ddraw behavior. */
1570 for (i = 0; i < 3; ++i)
1571 for (j = 0; j < 3; ++j)
1572 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1574 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1575 checkGLcall("glUniformMatrix3fv");
1578 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1579 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1581 const struct wined3d_gl_info *gl_info = context->gl_info;
1582 struct wined3d_matrix mat;
1584 if (tex >= MAX_TEXTURES)
1585 return;
1586 if (prog->vs.texture_matrix_location[tex] == -1)
1587 return;
1589 get_texture_matrix(context, state, tex, &mat);
1590 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1591 checkGLcall("glUniformMatrix4fv");
1594 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1595 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1597 const struct wined3d_gl_info *gl_info = context->gl_info;
1599 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1601 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1602 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1604 else
1606 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1608 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1610 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1611 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1612 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1613 checkGLcall("setting FFP material uniforms");
1616 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1617 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1619 const struct wined3d_gl_info *gl_info = context->gl_info;
1620 struct wined3d_color color;
1622 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1623 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1624 checkGLcall("glUniform3fv");
1627 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1628 const struct wined3d_matrix *src2)
1630 struct wined3d_vec4 temp;
1632 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1633 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1634 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1635 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1637 *dest = temp;
1640 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1641 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1642 struct glsl_shader_prog_link *prog)
1644 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1645 const struct wined3d_gl_info *gl_info = context->gl_info;
1646 struct wined3d_vec4 vec4;
1648 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1649 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1650 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1652 switch (light_info->OriginalParms.type)
1654 case WINED3D_LIGHT_POINT:
1655 multiply_vector_matrix(&vec4, &light_info->position, view);
1656 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1657 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1658 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1659 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1660 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1661 break;
1663 case WINED3D_LIGHT_SPOT:
1664 multiply_vector_matrix(&vec4, &light_info->position, view);
1665 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1667 multiply_vector_matrix(&vec4, &light_info->direction, view);
1668 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1670 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1671 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1672 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1673 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1674 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1675 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1676 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1677 break;
1679 case WINED3D_LIGHT_DIRECTIONAL:
1680 multiply_vector_matrix(&vec4, &light_info->direction, view);
1681 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1682 break;
1684 case WINED3D_LIGHT_PARALLELPOINT:
1685 multiply_vector_matrix(&vec4, &light_info->position, view);
1686 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1687 break;
1689 default:
1690 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1692 checkGLcall("setting FFP lights uniforms");
1695 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1696 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1698 const struct wined3d_gl_info *gl_info = context->gl_info;
1699 float min, max;
1700 float size, att[3];
1702 get_pointsize_minmax(context, state, &min, &max);
1704 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1705 checkGLcall("glUniform1f");
1706 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1707 checkGLcall("glUniform1f");
1709 get_pointsize(context, state, &size, att);
1711 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1712 checkGLcall("glUniform1f");
1713 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1714 checkGLcall("glUniform1f");
1715 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1716 checkGLcall("glUniform1f");
1717 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1718 checkGLcall("glUniform1f");
1721 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1722 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1724 const struct wined3d_gl_info *gl_info = context->gl_info;
1725 struct wined3d_color color;
1726 float start, end, scale;
1727 union
1729 DWORD d;
1730 float f;
1731 } tmpvalue;
1733 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1734 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1735 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1736 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1737 get_fog_start_end(context, state, &start, &end);
1738 scale = 1.0f / (end - start);
1739 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1740 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1741 checkGLcall("fog emulation uniforms");
1744 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1745 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1747 const struct wined3d_gl_info *gl_info = context->gl_info;
1748 struct wined3d_matrix matrix;
1749 struct wined3d_vec4 plane;
1751 plane = state->clip_planes[index];
1753 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1754 if (!use_vs(state))
1756 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1757 transpose_matrix(&matrix, &matrix);
1758 multiply_vector_matrix(&plane, &plane, &matrix);
1761 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1764 /* Context activation is done by the caller (state handler). */
1765 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1766 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1768 struct wined3d_color float_key[2];
1769 const struct wined3d_texture *texture = state->textures[0];
1771 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1772 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1775 /* Context activation is done by the caller (state handler). */
1776 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1777 const struct wined3d_state *state)
1779 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1780 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1781 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1782 const struct wined3d_gl_info *gl_info = context->gl_info;
1783 struct shader_glsl_priv *priv = shader_priv;
1784 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1785 DWORD update_mask;
1787 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1788 UINT constant_version;
1789 int i;
1791 if (!prog) {
1792 /* No GLSL program set - nothing to do. */
1793 return;
1795 constant_version = prog->constant_version;
1796 update_mask = context->constant_update_mask & prog->constant_update_mask;
1798 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1799 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1800 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1802 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1803 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1804 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1806 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1807 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1808 vshader->reg_maps.boolean_constants);
1810 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1812 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1813 shader_glsl_clip_plane_uniform(context, state, i, prog);
1816 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1817 shader_glsl_pointsize_uniform(context, state, prog);
1819 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1821 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1822 max(state->viewport_count, 1) : 1;
1823 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1824 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1825 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1826 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1827 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1828 else
1829 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1830 checkGLcall("glUniform4fv");
1833 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1835 struct wined3d_matrix mat;
1837 get_modelview_matrix(context, state, 0, &mat);
1838 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1839 checkGLcall("glUniformMatrix4fv");
1841 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1844 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1846 struct wined3d_matrix mat;
1848 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1850 if (prog->vs.modelview_matrix_location[i] == -1)
1851 break;
1853 get_modelview_matrix(context, state, i, &mat);
1854 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1855 checkGLcall("glUniformMatrix4fv");
1859 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1861 struct wined3d_matrix projection;
1863 get_projection_matrix(context, state, &projection);
1864 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1865 checkGLcall("glUniformMatrix4fv");
1868 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1870 for (i = 0; i < MAX_TEXTURES; ++i)
1871 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1874 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1875 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1877 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1879 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1880 DWORD point_count = 0;
1881 DWORD spot_count = 0;
1882 DWORD directional_count = 0;
1883 DWORD parallel_point_count = 0;
1885 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1887 if (!state->lights[i])
1888 continue;
1890 switch (state->lights[i]->OriginalParms.type)
1892 case WINED3D_LIGHT_POINT:
1893 ++point_count;
1894 break;
1895 case WINED3D_LIGHT_SPOT:
1896 ++spot_count;
1897 break;
1898 case WINED3D_LIGHT_DIRECTIONAL:
1899 ++directional_count;
1900 break;
1901 case WINED3D_LIGHT_PARALLELPOINT:
1902 ++parallel_point_count;
1903 break;
1904 default:
1905 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1906 break;
1909 point_idx = 0;
1910 spot_idx = point_idx + point_count;
1911 directional_idx = spot_idx + spot_count;
1912 parallel_point_idx = directional_idx + directional_count;
1914 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1915 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1917 const struct wined3d_light_info *light_info = state->lights[i];
1918 unsigned int idx;
1920 if (!light_info)
1921 continue;
1923 switch (light_info->OriginalParms.type)
1925 case WINED3D_LIGHT_POINT:
1926 idx = point_idx++;
1927 break;
1928 case WINED3D_LIGHT_SPOT:
1929 idx = spot_idx++;
1930 break;
1931 case WINED3D_LIGHT_DIRECTIONAL:
1932 idx = directional_idx++;
1933 break;
1934 case WINED3D_LIGHT_PARALLELPOINT:
1935 idx = parallel_point_idx++;
1936 break;
1937 default:
1938 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1939 continue;
1941 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1945 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1946 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1947 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1949 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1950 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1951 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1953 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1954 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1955 pshader->reg_maps.boolean_constants);
1957 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1959 for (i = 0; i < MAX_TEXTURES; ++i)
1961 if (prog->ps.bumpenv_mat_location[i] == -1)
1962 continue;
1964 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1965 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1967 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1969 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1970 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1971 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1972 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1976 checkGLcall("bump env uniforms");
1979 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1981 const struct wined3d_vec4 correction_params =
1983 /* Position is relative to the framebuffer, not the viewport. */
1984 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1985 context->render_offscreen ? 1.0f : -1.0f,
1986 0.0f,
1987 0.0f,
1990 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1993 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1994 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1995 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1996 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1998 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
2000 struct wined3d_color color;
2002 if (prog->ps.tex_factor_location != -1)
2004 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
2005 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
2008 if (state->render_states[WINED3D_RS_SPECULARENABLE])
2009 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
2010 else
2011 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
2013 for (i = 0; i < MAX_TEXTURES; ++i)
2015 if (prog->ps.tss_constant_location[i] == -1)
2016 continue;
2018 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
2019 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
2022 checkGLcall("fixed function uniforms");
2025 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
2026 shader_glsl_load_fog_uniform(context, state, prog);
2028 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
2030 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
2032 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
2033 checkGLcall("alpha test emulation uniform");
2036 if (priv->next_constant_version == UINT_MAX)
2038 TRACE("Max constant version reached, resetting to 0.\n");
2039 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
2040 priv->next_constant_version = 1;
2042 else
2044 prog->constant_version = priv->next_constant_version++;
2048 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
2050 struct constant_entry *entries = heap->entries;
2051 unsigned int *positions = heap->positions;
2052 unsigned int heap_idx, parent_idx;
2054 if (!heap->contained[idx])
2056 heap_idx = heap->size++;
2057 heap->contained[idx] = TRUE;
2059 else
2061 heap_idx = positions[idx];
2064 while (heap_idx > 1)
2066 parent_idx = heap_idx >> 1;
2068 if (new_version <= entries[parent_idx].version) break;
2070 entries[heap_idx] = entries[parent_idx];
2071 positions[entries[parent_idx].idx] = heap_idx;
2072 heap_idx = parent_idx;
2075 entries[heap_idx].version = new_version;
2076 entries[heap_idx].idx = idx;
2077 positions[idx] = heap_idx;
2080 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2082 struct shader_glsl_priv *priv = device->shader_priv;
2083 struct constant_heap *heap = &priv->vconst_heap;
2084 UINT i;
2086 for (i = start; i < count + start; ++i)
2088 update_heap_entry(heap, i, priv->next_constant_version);
2092 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2094 struct shader_glsl_priv *priv = device->shader_priv;
2095 struct constant_heap *heap = &priv->pconst_heap;
2096 UINT i;
2098 for (i = start; i < count + start; ++i)
2100 update_heap_entry(heap, i, priv->next_constant_version);
2104 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2106 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2107 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2108 if(shader_major > 3) return ret;
2110 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2111 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2112 return ret;
2115 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2117 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2120 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2122 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2123 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2126 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2128 return shader_glsl_get_version(gl_info) >= 150;
2131 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2133 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2136 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2137 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2139 va_list args;
2140 int ret;
2142 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2143 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2144 for (;;)
2146 va_start(args, format);
2147 ret = shader_vaddline(buffer, format, args);
2148 va_end(args);
2149 if (!ret)
2150 return;
2151 if (!string_buffer_resize(buffer, ret))
2152 return;
2156 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2157 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2159 va_list args;
2160 int ret;
2162 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2163 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2164 for (;;)
2166 va_start(args, format);
2167 ret = shader_vaddline(buffer, format, args);
2168 va_end(args);
2169 if (!ret)
2170 return;
2171 if (!string_buffer_resize(buffer, ret))
2172 return;
2176 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2178 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2181 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2183 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2186 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2188 switch (mode)
2190 case WINED3DSIM_CONSTANT:
2191 return "flat ";
2192 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2193 return "noperspective ";
2194 default:
2195 FIXME("Unhandled interpolation mode %#x.\n", mode);
2196 case WINED3DSIM_NONE:
2197 case WINED3DSIM_LINEAR:
2198 return "";
2202 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2203 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2205 return wined3d_extract_bits(packed_interpolation_mode,
2206 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2209 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2210 struct wined3d_string_buffer *buffer, unsigned int element_count,
2211 const DWORD *interpolation_mode, BOOL unroll)
2213 enum wined3d_shader_interpolation_mode mode;
2214 unsigned int i;
2216 if (shader_glsl_use_interface_blocks(gl_info))
2218 if (unroll)
2220 shader_addline(buffer, "in shader_in_out {\n");
2221 for (i = 0; i < element_count; ++i)
2223 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2224 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2226 shader_addline(buffer, "} shader_in;\n");
2228 else
2230 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2233 else
2235 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2239 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2240 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2241 const DWORD *interpolation_mode)
2243 enum wined3d_shader_interpolation_mode mode;
2244 unsigned int i;
2246 if (shader_glsl_use_interface_blocks(gl_info))
2248 if (rasterizer_setup)
2250 shader_addline(buffer, "out shader_in_out {\n");
2251 for (i = 0; i < element_count; ++i)
2253 const char *interpolation_qualifiers = "";
2254 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2256 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2257 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2259 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2261 shader_addline(buffer, "} shader_out;\n");
2263 else
2265 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2268 else
2270 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2274 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2276 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2279 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2281 switch (primitive_type)
2283 case WINED3D_PT_POINTLIST:
2284 return "points";
2286 case WINED3D_PT_LINELIST:
2287 return "lines";
2289 case WINED3D_PT_LINESTRIP:
2290 return "line_strip";
2292 case WINED3D_PT_TRIANGLELIST:
2293 return "triangles";
2295 case WINED3D_PT_TRIANGLESTRIP:
2296 return "triangle_strip";
2298 case WINED3D_PT_LINELIST_ADJ:
2299 return "lines_adjacency";
2301 case WINED3D_PT_TRIANGLELIST_ADJ:
2302 return "triangles_adjacency";
2304 default:
2305 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2306 return "";
2310 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2312 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2313 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2314 DWORD input_reg_used = shader->u.ps.input_reg_used;
2315 unsigned int i;
2317 if (reg_maps->shader_version.major < 3)
2318 return input_reg_used & (1u << idx);
2320 for (i = 0; i < input_signature->element_count; ++i)
2322 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2324 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2325 continue;
2327 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2328 && input->semantic_idx == idx)
2329 return input_reg_used & (1u << input->register_idx);
2331 return FALSE;
2334 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2335 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2337 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2339 if (version->major >= 4)
2340 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2341 else
2342 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2345 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2346 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2347 unsigned int index)
2349 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2350 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2351 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2352 index, scalar_type, scalar_type, index);
2355 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2356 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2358 unsigned int index = e->register_idx;
2359 enum wined3d_component_type type;
2361 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2363 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2364 index);
2365 return;
2367 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2369 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2370 index);
2371 return;
2373 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2374 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2376 if (shader_glsl_use_explicit_attrib_location(gl_info))
2377 shader_addline(buffer, "layout(location = %u) ", index);
2379 type = e->component_type;
2380 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2382 FIXME("Unhandled type %#x.\n", type);
2383 type = WINED3D_TYPE_FLOAT;
2385 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2386 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2387 else
2388 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2389 component_type_info[type].glsl_vector_type,
2390 component_type_info[type].glsl_scalar_type, index);
2393 /** Generate the variable & register declarations for the GLSL output target */
2394 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2395 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2396 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2398 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2399 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2400 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2401 const struct wined3d_gl_info *gl_info = context->gl_info;
2402 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2403 unsigned int uniform_block_base, uniform_block_count;
2404 const struct wined3d_shader_lconst *lconst;
2405 const char *prefix;
2406 unsigned int i;
2407 DWORD map;
2409 prefix = shader_glsl_get_prefix(version->type);
2411 /* Prototype the subroutines */
2412 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2414 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2417 /* Declare the constants (aka uniforms) */
2418 if (shader->limits->constant_float > 0)
2420 unsigned max_constantsF;
2422 /* Unless the shader uses indirect addressing, always declare the
2423 * maximum array size and ignore that we need some uniforms privately.
2424 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2425 * and immediate values, still declare VC[256]. If the shader needs
2426 * more uniforms than we have it won't work in any case. If it uses
2427 * less, the compiler will figure out which uniforms are really used
2428 * and strip them out. This allows a shader to use c255 on a dx9 card,
2429 * as long as it doesn't also use all the other constants.
2431 * If the shader uses indirect addressing the compiler must assume
2432 * that all declared uniforms are used. In this case, declare only the
2433 * amount that we're assured to have.
2435 * Thus we run into problems in these two cases:
2436 * 1) The shader really uses more uniforms than supported.
2437 * 2) The shader uses indirect addressing, less constants than
2438 * supported, but uses a constant index > #supported consts. */
2439 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2441 /* No indirect addressing here. */
2442 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2444 else
2446 if (reg_maps->usesrelconstF)
2448 /* Subtract the other potential uniforms from the max
2449 * available (bools, ints, and 1 row of projection matrix).
2450 * Subtract another uniform for immediate values, which have
2451 * to be loaded via uniform by the driver as well. The shader
2452 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2453 * shader code, so one vec4 should be enough. (Unfortunately
2454 * the Nvidia driver doesn't store 128 and -128 in one float).
2456 * Writing gl_ClipVertex requires one uniform for each
2457 * clipplane as well. */
2458 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2459 if (vs_args->clip_enabled)
2460 max_constantsF -= gl_info->limits.user_clip_distances;
2461 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2462 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2463 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2464 * for now take this into account when calculating the number of available constants
2466 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2467 /* Set by driver quirks in directx.c */
2468 max_constantsF -= gl_info->reserved_glsl_constants;
2470 if (max_constantsF < shader->limits->constant_float)
2472 static unsigned int once;
2474 if (!once++)
2475 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2476 " it may not render correctly.\n");
2477 else
2478 WARN("The hardware does not support enough uniform components to run this shader.\n");
2481 else
2483 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2486 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2487 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2490 /* Always declare the full set of constants, the compiler can remove the
2491 * unused ones because d3d doesn't (yet) support indirect int and bool
2492 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2493 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2494 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2496 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2497 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2499 /* Declare immediate constant buffer */
2500 if (reg_maps->icb)
2501 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2503 /* Declare constant buffers */
2504 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2505 &uniform_block_base, &uniform_block_count);
2506 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2508 if (reg_maps->cb_sizes[i])
2510 shader_addline(buffer, "layout(std140");
2511 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2512 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2513 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2514 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2518 /* Declare texture samplers */
2519 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2521 struct wined3d_shader_sampler_map_entry *entry;
2522 const char *sampler_type_prefix, *sampler_type;
2523 BOOL shadow_sampler, tex_rect;
2525 entry = &reg_maps->sampler_map.entries[i];
2527 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2529 ERR("Invalid resource index %u.\n", entry->resource_idx);
2530 continue;
2533 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2535 case WINED3D_DATA_FLOAT:
2536 case WINED3D_DATA_UNORM:
2537 case WINED3D_DATA_SNORM:
2538 sampler_type_prefix = "";
2539 break;
2541 case WINED3D_DATA_INT:
2542 sampler_type_prefix = "i";
2543 break;
2545 case WINED3D_DATA_UINT:
2546 sampler_type_prefix = "u";
2547 break;
2549 default:
2550 sampler_type_prefix = "";
2551 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2552 break;
2555 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2556 switch (reg_maps->resource_info[entry->resource_idx].type)
2558 case WINED3D_SHADER_RESOURCE_BUFFER:
2559 sampler_type = "samplerBuffer";
2560 break;
2562 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2563 if (shadow_sampler)
2564 sampler_type = "sampler1DShadow";
2565 else
2566 sampler_type = "sampler1D";
2567 break;
2569 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2570 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2571 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2572 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2573 if (shadow_sampler)
2575 if (tex_rect)
2576 sampler_type = "sampler2DRectShadow";
2577 else
2578 sampler_type = "sampler2DShadow";
2580 else
2582 if (tex_rect)
2583 sampler_type = "sampler2DRect";
2584 else
2585 sampler_type = "sampler2D";
2587 break;
2589 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2590 if (shadow_sampler)
2591 FIXME("Unsupported 3D shadow sampler.\n");
2592 sampler_type = "sampler3D";
2593 break;
2595 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2596 if (shadow_sampler)
2597 sampler_type = "samplerCubeShadow";
2598 else
2599 sampler_type = "samplerCube";
2600 break;
2602 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2603 if (shadow_sampler)
2604 sampler_type = "sampler1DArrayShadow";
2605 else
2606 sampler_type = "sampler1DArray";
2607 break;
2609 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2610 if (shadow_sampler)
2611 sampler_type = "sampler2DArrayShadow";
2612 else
2613 sampler_type = "sampler2DArray";
2614 break;
2616 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2617 if (shadow_sampler)
2618 sampler_type = "samplerCubeArrayShadow";
2619 else
2620 sampler_type = "samplerCubeArray";
2621 break;
2623 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2624 sampler_type = "sampler2DMS";
2625 break;
2627 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2628 sampler_type = "sampler2DMSArray";
2629 break;
2631 default:
2632 sampler_type = "unsupported_sampler";
2633 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2634 break;
2637 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2638 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2639 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2640 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2643 /* Declare images */
2644 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2646 const char *image_type_prefix, *image_type, *read_format;
2648 if (!reg_maps->uav_resource_info[i].type)
2649 continue;
2651 switch (reg_maps->uav_resource_info[i].data_type)
2653 case WINED3D_DATA_FLOAT:
2654 case WINED3D_DATA_UNORM:
2655 case WINED3D_DATA_SNORM:
2656 image_type_prefix = "";
2657 read_format = "r32f";
2658 break;
2660 case WINED3D_DATA_INT:
2661 image_type_prefix = "i";
2662 read_format = "r32i";
2663 break;
2665 case WINED3D_DATA_UINT:
2666 image_type_prefix = "u";
2667 read_format = "r32ui";
2668 break;
2670 default:
2671 image_type_prefix = "";
2672 read_format = "";
2673 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2674 break;
2677 switch (reg_maps->uav_resource_info[i].type)
2679 case WINED3D_SHADER_RESOURCE_BUFFER:
2680 image_type = "imageBuffer";
2681 break;
2683 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2684 image_type = "image2D";
2685 break;
2687 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2688 image_type = "image3D";
2689 break;
2691 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2692 image_type = "image2DArray";
2693 break;
2695 default:
2696 image_type = "unsupported_image";
2697 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2698 break;
2701 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2702 shader_addline(buffer, "layout(binding = %u)\n", i);
2703 if (reg_maps->uav_read_mask & (1u << i))
2704 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2705 read_format, image_type_prefix, image_type, prefix, i);
2706 else
2707 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2708 image_type_prefix, image_type, prefix, i);
2710 if (reg_maps->uav_counter_mask & (1u << i))
2711 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2712 i, prefix, i);
2715 /* Declare address variables */
2716 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2718 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2721 /* Declare output register temporaries */
2722 if (shader->limits->packed_output)
2723 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2725 /* Declare temporary variables */
2726 if (reg_maps->temporary_count)
2728 for (i = 0; i < reg_maps->temporary_count; ++i)
2729 shader_addline(buffer, "vec4 R%u;\n", i);
2731 else if (version->major < 4)
2733 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2735 if (map & 1)
2736 shader_addline(buffer, "vec4 R%u;\n", i);
2740 /* Declare indexable temporary variables */
2741 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2743 if (idx_temp_reg->component_count != 4)
2744 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2745 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2748 /* Declare loop registers aLx */
2749 if (version->major < 4)
2751 for (i = 0; i < reg_maps->loop_depth; ++i)
2753 shader_addline(buffer, "int aL%u;\n", i);
2754 shader_addline(buffer, "int tmpInt%u;\n", i);
2758 /* Temporary variables for matrix operations */
2759 shader_addline(buffer, "vec4 tmp0;\n");
2760 shader_addline(buffer, "vec4 tmp1;\n");
2762 if (!shader->load_local_constsF)
2764 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2766 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2767 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2768 shader_addline(buffer, ";\n");
2773 /* Prototypes */
2774 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2775 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2777 /** Used for opcode modifiers - They multiply the result by the specified amount */
2778 static const char * const shift_glsl_tab[] = {
2779 "", /* 0 (none) */
2780 "2.0 * ", /* 1 (x2) */
2781 "4.0 * ", /* 2 (x4) */
2782 "8.0 * ", /* 3 (x8) */
2783 "16.0 * ", /* 4 (x16) */
2784 "32.0 * ", /* 5 (x32) */
2785 "", /* 6 (x64) */
2786 "", /* 7 (x128) */
2787 "", /* 8 (d256) */
2788 "", /* 9 (d128) */
2789 "", /* 10 (d64) */
2790 "", /* 11 (d32) */
2791 "0.0625 * ", /* 12 (d16) */
2792 "0.125 * ", /* 13 (d8) */
2793 "0.25 * ", /* 14 (d4) */
2794 "0.5 * " /* 15 (d2) */
2797 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2798 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2799 const char *in_reg, const char *in_regswizzle, char *out_str)
2801 switch (src_modifier)
2803 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2804 case WINED3DSPSM_DW:
2805 case WINED3DSPSM_NONE:
2806 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2807 break;
2808 case WINED3DSPSM_NEG:
2809 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2810 break;
2811 case WINED3DSPSM_NOT:
2812 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2813 break;
2814 case WINED3DSPSM_BIAS:
2815 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2816 break;
2817 case WINED3DSPSM_BIASNEG:
2818 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2819 break;
2820 case WINED3DSPSM_SIGN:
2821 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2822 break;
2823 case WINED3DSPSM_SIGNNEG:
2824 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2825 break;
2826 case WINED3DSPSM_COMP:
2827 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2828 break;
2829 case WINED3DSPSM_X2:
2830 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2831 break;
2832 case WINED3DSPSM_X2NEG:
2833 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2834 break;
2835 case WINED3DSPSM_ABS:
2836 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2837 break;
2838 case WINED3DSPSM_ABSNEG:
2839 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2840 break;
2841 default:
2842 FIXME("Unhandled modifier %u\n", src_modifier);
2843 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2847 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2848 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2850 /* The ARB_shading_language_420pack extension allows swizzle operations on
2851 * scalars. */
2852 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2853 sprintf(register_name, "%s", glsl_variable);
2854 else
2855 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2858 /** Writes the GLSL variable name that corresponds to the register that the
2859 * DX opcode parameter is trying to access */
2860 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2861 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2862 const struct wined3d_shader_instruction *ins)
2864 /* oPos, oFog and oPts in D3D */
2865 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2867 const struct wined3d_shader *shader = ins->ctx->shader;
2868 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2869 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2870 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2871 const char *prefix = shader_glsl_get_prefix(version->type);
2872 struct glsl_src_param rel_param0, rel_param1;
2873 char imm_str[4][17];
2875 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2876 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2877 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2878 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2879 *is_color = FALSE;
2881 switch (reg->type)
2883 case WINED3DSPR_TEMP:
2884 sprintf(register_name, "R%u", reg->idx[0].offset);
2885 break;
2887 case WINED3DSPR_INPUT:
2888 case WINED3DSPR_INCONTROLPOINT:
2889 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2891 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2893 if (reg->idx[0].rel_addr)
2894 FIXME("VS3 input registers relative addressing.\n");
2895 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2896 *is_color = TRUE;
2897 if (reg->idx[0].rel_addr)
2899 sprintf(register_name, "%s_in[%s + %u]",
2900 prefix, rel_param0.param_str, reg->idx[0].offset);
2902 else
2904 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2906 break;
2909 if (version->type == WINED3D_SHADER_TYPE_HULL
2910 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2911 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2913 if (reg->idx[0].rel_addr)
2915 if (reg->idx[1].rel_addr)
2916 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2917 rel_param0.param_str, reg->idx[0].offset,
2918 rel_param1.param_str, reg->idx[1].offset);
2919 else
2920 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2921 rel_param0.param_str, reg->idx[0].offset,
2922 reg->idx[1].offset);
2924 else if (reg->idx[1].rel_addr)
2925 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2926 rel_param1.param_str, reg->idx[1].offset);
2927 else
2928 sprintf(register_name, "shader_in[%u].reg[%u]",
2929 reg->idx[0].offset, reg->idx[1].offset);
2930 break;
2933 /* pixel shaders >= 3.0 */
2934 if (version->major >= 3)
2936 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2937 unsigned int in_count = vec4_varyings(version->major, gl_info);
2939 if (reg->idx[0].rel_addr)
2941 /* Removing a + 0 would be an obvious optimization, but
2942 * OS X doesn't see the NOP operation there. */
2943 if (idx)
2945 if (needs_legacy_glsl_syntax(gl_info)
2946 && shader->u.ps.declared_in_count > in_count)
2948 sprintf(register_name,
2949 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2950 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2951 prefix, rel_param0.param_str, idx);
2953 else
2955 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2958 else
2960 if (needs_legacy_glsl_syntax(gl_info)
2961 && shader->u.ps.declared_in_count > in_count)
2963 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2964 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2965 prefix, rel_param0.param_str);
2967 else
2969 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2973 else
2975 if (idx == in_count) sprintf(register_name, "gl_Color");
2976 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2977 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2980 else
2982 if (!reg->idx[0].offset)
2983 strcpy(register_name, "ffp_varying_diffuse");
2984 else
2985 strcpy(register_name, "ffp_varying_specular");
2986 break;
2988 break;
2990 case WINED3DSPR_CONST:
2992 /* Relative addressing */
2993 if (reg->idx[0].rel_addr)
2995 if (wined3d_settings.check_float_constants)
2996 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2997 rel_param0.param_str, reg->idx[0].offset,
2998 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2999 prefix, rel_param0.param_str, reg->idx[0].offset);
3000 else if (reg->idx[0].offset)
3001 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3002 else
3003 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
3005 else
3007 if (shader_constant_is_local(shader, reg->idx[0].offset))
3008 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
3009 else
3010 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
3013 break;
3015 case WINED3DSPR_CONSTINT:
3016 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
3017 break;
3019 case WINED3DSPR_CONSTBOOL:
3020 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
3021 break;
3023 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
3024 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
3025 sprintf(register_name, "T%u", reg->idx[0].offset);
3026 else
3027 sprintf(register_name, "A%u", reg->idx[0].offset);
3028 break;
3030 case WINED3DSPR_LOOP:
3031 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
3032 break;
3034 case WINED3DSPR_SAMPLER:
3035 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
3036 break;
3038 case WINED3DSPR_COLOROUT:
3039 if (reg->idx[0].offset >= gl_info->limits.buffers)
3040 WARN("Write to render target %u, only %d supported.\n",
3041 reg->idx[0].offset, gl_info->limits.buffers);
3043 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
3044 break;
3046 case WINED3DSPR_RASTOUT:
3047 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
3048 break;
3050 case WINED3DSPR_DEPTHOUT:
3051 case WINED3DSPR_DEPTHOUTGE:
3052 case WINED3DSPR_DEPTHOUTLE:
3053 sprintf(register_name, "gl_FragDepth");
3054 break;
3056 case WINED3DSPR_ATTROUT:
3057 if (!reg->idx[0].offset)
3058 sprintf(register_name, "%s_out[8]", prefix);
3059 else
3060 sprintf(register_name, "%s_out[9]", prefix);
3061 break;
3063 case WINED3DSPR_TEXCRDOUT:
3064 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
3065 if (reg->idx[0].rel_addr)
3066 sprintf(register_name, "%s_out[%s + %u]",
3067 prefix, rel_param0.param_str, reg->idx[0].offset);
3068 else
3069 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3070 break;
3072 case WINED3DSPR_MISCTYPE:
3073 if (!reg->idx[0].offset)
3075 /* vPos */
3076 sprintf(register_name, "vpos");
3078 else if (reg->idx[0].offset == 1)
3080 /* Note that gl_FrontFacing is a bool, while vFace is
3081 * a float for which the sign determines front/back */
3082 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3084 else
3086 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3087 sprintf(register_name, "unrecognized_register");
3089 break;
3091 case WINED3DSPR_IMMCONST:
3092 switch (reg->immconst_type)
3094 case WINED3D_IMMCONST_SCALAR:
3095 switch (data_type)
3097 case WINED3D_DATA_UNORM:
3098 case WINED3D_DATA_SNORM:
3099 case WINED3D_DATA_FLOAT:
3100 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3101 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3102 else
3103 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3104 break;
3105 case WINED3D_DATA_INT:
3106 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3107 break;
3108 case WINED3D_DATA_RESOURCE:
3109 case WINED3D_DATA_SAMPLER:
3110 case WINED3D_DATA_UINT:
3111 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3112 break;
3113 default:
3114 sprintf(register_name, "<unhandled data type %#x>", data_type);
3115 break;
3117 break;
3119 case WINED3D_IMMCONST_VEC4:
3120 switch (data_type)
3122 case WINED3D_DATA_UNORM:
3123 case WINED3D_DATA_SNORM:
3124 case WINED3D_DATA_FLOAT:
3125 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3127 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3128 reg->u.immconst_data[0], reg->u.immconst_data[1],
3129 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3131 else
3133 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3134 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3135 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3136 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3137 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3138 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3140 break;
3141 case WINED3D_DATA_INT:
3142 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3143 reg->u.immconst_data[0], reg->u.immconst_data[1],
3144 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3145 break;
3146 case WINED3D_DATA_RESOURCE:
3147 case WINED3D_DATA_SAMPLER:
3148 case WINED3D_DATA_UINT:
3149 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3150 reg->u.immconst_data[0], reg->u.immconst_data[1],
3151 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3152 break;
3153 default:
3154 sprintf(register_name, "<unhandled data type %#x>", data_type);
3155 break;
3157 break;
3159 default:
3160 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3161 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3163 break;
3165 case WINED3DSPR_CONSTBUFFER:
3166 if (reg->idx[1].rel_addr)
3167 sprintf(register_name, "%s_cb%u[%s + %u]",
3168 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3169 else
3170 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3171 break;
3173 case WINED3DSPR_IMMCONSTBUFFER:
3174 if (reg->idx[0].rel_addr)
3175 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3176 else
3177 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3178 break;
3180 case WINED3DSPR_PRIMID:
3181 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3182 sprintf(register_name, "gl_PrimitiveIDIn");
3183 else
3184 sprintf(register_name, "gl_PrimitiveID");
3185 break;
3187 case WINED3DSPR_IDXTEMP:
3188 if (reg->idx[1].rel_addr)
3189 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3190 else
3191 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3192 break;
3194 case WINED3DSPR_LOCALTHREADINDEX:
3195 shader_glsl_fixup_scalar_register_variable(register_name,
3196 "int(gl_LocalInvocationIndex)", gl_info);
3197 break;
3199 case WINED3DSPR_GSINSTID:
3200 case WINED3DSPR_OUTPOINTID:
3201 shader_glsl_fixup_scalar_register_variable(register_name,
3202 "gl_InvocationID", gl_info);
3203 break;
3205 case WINED3DSPR_THREADID:
3206 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3207 break;
3209 case WINED3DSPR_THREADGROUPID:
3210 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3211 break;
3213 case WINED3DSPR_LOCALTHREADID:
3214 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3215 break;
3217 case WINED3DSPR_FORKINSTID:
3218 case WINED3DSPR_JOININSTID:
3219 shader_glsl_fixup_scalar_register_variable(register_name,
3220 "phase_instance_id", gl_info);
3221 break;
3223 case WINED3DSPR_TESSCOORD:
3224 sprintf(register_name, "gl_TessCoord");
3225 break;
3227 case WINED3DSPR_OUTCONTROLPOINT:
3228 if (reg->idx[0].rel_addr)
3230 if (reg->idx[1].rel_addr)
3231 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3232 rel_param0.param_str, reg->idx[0].offset,
3233 rel_param1.param_str, reg->idx[1].offset);
3234 else
3235 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3236 rel_param0.param_str, reg->idx[0].offset,
3237 reg->idx[1].offset);
3239 else if (reg->idx[1].rel_addr)
3241 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3242 reg->idx[0].offset, rel_param1.param_str,
3243 reg->idx[1].offset);
3245 else
3247 sprintf(register_name, "shader_out[%u].reg[%u]",
3248 reg->idx[0].offset, reg->idx[1].offset);
3250 break;
3252 case WINED3DSPR_PATCHCONST:
3253 if (version->type == WINED3D_SHADER_TYPE_HULL)
3254 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3255 else
3256 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3257 break;
3259 case WINED3DSPR_SAMPLEMASK:
3260 sprintf(register_name, "sample_mask");
3261 break;
3263 default:
3264 FIXME("Unhandled register type %#x.\n", reg->type);
3265 sprintf(register_name, "unrecognized_register");
3266 break;
3270 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3272 *str++ = '.';
3273 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3274 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3275 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3276 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3277 *str = '\0';
3280 /* Get the GLSL write mask for the destination register */
3281 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3283 DWORD mask = param->write_mask;
3285 if (shader_is_scalar(&param->reg))
3287 mask = WINED3DSP_WRITEMASK_0;
3288 *write_mask = '\0';
3290 else
3292 shader_glsl_write_mask_to_str(mask, write_mask);
3295 return mask;
3298 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3300 unsigned int size = 0;
3302 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3303 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3304 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3305 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3307 return size;
3310 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3311 unsigned int component_idx)
3313 /* swizzle bits fields: wwzzyyxx */
3314 return (swizzle >> (2 * component_idx)) & 0x3;
3317 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3319 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3320 * but addressed as "rgba". To fix this we need to swap the register's x
3321 * and z components. */
3322 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3323 unsigned int i;
3325 *str++ = '.';
3326 for (i = 0; i < 4; ++i)
3328 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3329 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3331 *str = '\0';
3334 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3335 BOOL fixup, DWORD mask, char *swizzle_str)
3337 if (shader_is_scalar(&param->reg))
3338 *swizzle_str = '\0';
3339 else
3340 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3343 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3344 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3346 if (dst_data_type == src_data_type)
3348 string_buffer_sprintf(dst_param, "%s", src_param);
3349 return;
3352 if (src_data_type == WINED3D_DATA_FLOAT)
3354 switch (dst_data_type)
3356 case WINED3D_DATA_INT:
3357 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3358 return;
3359 case WINED3D_DATA_RESOURCE:
3360 case WINED3D_DATA_SAMPLER:
3361 case WINED3D_DATA_UINT:
3362 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3363 return;
3364 default:
3365 break;
3369 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3371 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3372 return;
3375 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3377 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3378 return;
3381 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3382 string_buffer_sprintf(dst_param, "%s", src_param);
3385 /* From a given parameter token, generate the corresponding GLSL string.
3386 * Also, return the actual register name and swizzle in case the
3387 * caller needs this information as well. */
3388 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3389 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3390 enum wined3d_data_type data_type)
3392 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3393 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3394 enum wined3d_data_type param_data_type;
3395 BOOL is_color = FALSE;
3396 char swizzle_str[6];
3398 glsl_src->reg_name[0] = '\0';
3399 glsl_src->param_str[0] = '\0';
3400 swizzle_str[0] = '\0';
3402 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ins);
3403 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3405 switch (wined3d_src->reg.type)
3407 case WINED3DSPR_IMMCONST:
3408 param_data_type = data_type;
3409 break;
3410 case WINED3DSPR_FORKINSTID:
3411 case WINED3DSPR_GSINSTID:
3412 case WINED3DSPR_JOININSTID:
3413 case WINED3DSPR_LOCALTHREADID:
3414 case WINED3DSPR_LOCALTHREADINDEX:
3415 case WINED3DSPR_OUTPOINTID:
3416 case WINED3DSPR_PRIMID:
3417 case WINED3DSPR_THREADGROUPID:
3418 case WINED3DSPR_THREADID:
3419 param_data_type = WINED3D_DATA_INT;
3420 break;
3421 default:
3422 param_data_type = WINED3D_DATA_FLOAT;
3423 break;
3426 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3427 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3429 string_buffer_release(priv->string_buffers, reg_name);
3432 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3433 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3435 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3438 /* From a given parameter token, generate the corresponding GLSL string.
3439 * Also, return the actual register name and swizzle in case the
3440 * caller needs this information as well. */
3441 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3442 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3444 BOOL is_color = FALSE;
3446 glsl_dst->mask_str[0] = '\0';
3447 glsl_dst->reg_name[0] = '\0';
3449 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3450 glsl_dst->reg_name, &is_color, ins);
3451 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3454 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3455 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3456 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3457 enum wined3d_data_type data_type)
3459 struct glsl_dst_param glsl_dst;
3460 DWORD mask;
3462 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3464 switch (data_type)
3466 case WINED3D_DATA_FLOAT:
3467 shader_addline(buffer, "%s%s = %s(",
3468 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3469 break;
3470 case WINED3D_DATA_INT:
3471 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3472 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3473 break;
3474 case WINED3D_DATA_RESOURCE:
3475 case WINED3D_DATA_SAMPLER:
3476 case WINED3D_DATA_UINT:
3477 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3478 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3479 break;
3480 default:
3481 FIXME("Unhandled data type %#x.\n", data_type);
3482 shader_addline(buffer, "%s%s = %s(",
3483 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3484 break;
3488 return mask;
3491 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3492 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3494 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3497 /** Process GLSL instruction modifiers */
3498 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3500 struct glsl_dst_param dst_param;
3501 DWORD modifiers;
3503 if (!ins->dst_count) return;
3505 modifiers = ins->dst[0].modifiers;
3506 if (!modifiers) return;
3508 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3510 if (modifiers & WINED3DSPDM_SATURATE)
3512 /* _SAT means to clamp the value of the register to between 0 and 1 */
3513 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3514 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3517 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3519 FIXME("_centroid modifier not handled\n");
3522 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3524 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3528 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3530 switch (op)
3532 case WINED3D_SHADER_REL_OP_GT: return ">";
3533 case WINED3D_SHADER_REL_OP_EQ: return "==";
3534 case WINED3D_SHADER_REL_OP_GE: return ">=";
3535 case WINED3D_SHADER_REL_OP_LT: return "<";
3536 case WINED3D_SHADER_REL_OP_NE: return "!=";
3537 case WINED3D_SHADER_REL_OP_LE: return "<=";
3538 default:
3539 FIXME("Unrecognized operator %#x.\n", op);
3540 return "(\?\?)";
3544 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3546 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3549 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3550 unsigned int *coord_size, unsigned int *deriv_size)
3552 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3553 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3555 *coord_size = resource_type_info[resource_type].coord_size;
3556 *deriv_size = *coord_size;
3557 if (is_array)
3558 --(*deriv_size);
3561 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3562 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3564 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3565 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3566 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3567 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3568 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3569 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3570 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3571 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3572 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3573 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3574 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3575 const char *base = "texture", *type_part = "", *suffix = "";
3576 unsigned int coord_size, deriv_size;
3578 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3580 if (resource_type >= ARRAY_SIZE(resource_type_info))
3582 ERR("Unexpected resource type %#x.\n", resource_type);
3583 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3586 /* Note that there's no such thing as a projected cube texture. */
3587 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3588 projected = FALSE;
3590 if (needs_legacy_glsl_syntax(gl_info))
3592 if (shadow)
3593 base = "shadow";
3595 type_part = resource_type_info[resource_type].type_part;
3596 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3597 type_part = "2DRect";
3598 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3599 FIXME("Unhandled resource type %#x.\n", resource_type);
3601 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3603 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3604 suffix = "ARB";
3605 else
3606 FIXME("Unsupported grad function.\n");
3610 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3612 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3613 if (flags & ~texel_fetch_flags)
3614 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3616 base = "texelFetch";
3617 type_part = "";
3620 sample_function->name = string_buffer_get(priv->string_buffers);
3621 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3622 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3624 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3625 if (shadow)
3626 ++coord_size;
3627 sample_function->offset_size = offset ? deriv_size : 0;
3628 sample_function->coord_mask = (1u << coord_size) - 1;
3629 sample_function->deriv_mask = (1u << deriv_size) - 1;
3630 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3633 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3634 struct glsl_sample_function *sample_function)
3636 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3638 string_buffer_release(priv->string_buffers, sample_function->name);
3641 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3642 BOOL sign_fixup, enum fixup_channel_source channel_source)
3644 switch(channel_source)
3646 case CHANNEL_SOURCE_ZERO:
3647 strcat(arguments, "0.0");
3648 break;
3650 case CHANNEL_SOURCE_ONE:
3651 strcat(arguments, "1.0");
3652 break;
3654 case CHANNEL_SOURCE_X:
3655 strcat(arguments, reg_name);
3656 strcat(arguments, ".x");
3657 break;
3659 case CHANNEL_SOURCE_Y:
3660 strcat(arguments, reg_name);
3661 strcat(arguments, ".y");
3662 break;
3664 case CHANNEL_SOURCE_Z:
3665 strcat(arguments, reg_name);
3666 strcat(arguments, ".z");
3667 break;
3669 case CHANNEL_SOURCE_W:
3670 strcat(arguments, reg_name);
3671 strcat(arguments, ".w");
3672 break;
3674 default:
3675 FIXME("Unhandled channel source %#x\n", channel_source);
3676 strcat(arguments, "undefined");
3677 break;
3680 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3683 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3684 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3686 unsigned int mask_size, remaining;
3687 DWORD fixup_mask = 0;
3688 char arguments[256];
3689 char mask_str[6];
3691 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3692 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3693 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3694 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3695 if (!(mask &= fixup_mask))
3696 return;
3698 if (is_complex_fixup(fixup))
3700 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3701 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3702 return;
3705 shader_glsl_write_mask_to_str(mask, mask_str);
3706 mask_size = shader_glsl_get_write_mask_size(mask);
3708 arguments[0] = '\0';
3709 remaining = mask_size;
3710 if (mask & WINED3DSP_WRITEMASK_0)
3712 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3713 if (--remaining) strcat(arguments, ", ");
3715 if (mask & WINED3DSP_WRITEMASK_1)
3717 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3718 if (--remaining) strcat(arguments, ", ");
3720 if (mask & WINED3DSP_WRITEMASK_2)
3722 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3723 if (--remaining) strcat(arguments, ", ");
3725 if (mask & WINED3DSP_WRITEMASK_3)
3727 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3728 if (--remaining) strcat(arguments, ", ");
3731 if (mask_size > 1)
3732 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3733 else
3734 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3737 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3739 char reg_name[256];
3740 BOOL is_color;
3742 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins);
3743 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3746 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3747 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3748 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3749 const char *coord_reg_fmt, ...)
3751 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3752 char dst_swizzle[6];
3753 struct color_fixup_desc fixup;
3754 BOOL np2_fixup = FALSE;
3755 va_list args;
3756 int ret;
3758 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3760 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3761 * We actually rely on it for vertex shaders and SM4+. */
3762 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3764 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3765 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3767 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3768 np2_fixup = TRUE;
3770 else
3772 fixup = COLOR_FIXUP_IDENTITY;
3775 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3777 if (sample_function->output_single_component)
3778 shader_addline(ins->ctx->buffer, "vec4(");
3780 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3781 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3783 for (;;)
3785 va_start(args, coord_reg_fmt);
3786 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3787 va_end(args);
3788 if (!ret)
3789 break;
3790 if (!string_buffer_resize(ins->ctx->buffer, ret))
3791 break;
3794 if (np2_fixup)
3796 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3797 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3799 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3801 case 1:
3802 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3803 idx >> 1, (idx % 2) ? "z" : "x");
3804 break;
3805 case 2:
3806 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3807 idx >> 1, (idx % 2) ? "zw" : "xy");
3808 break;
3809 case 3:
3810 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3811 idx >> 1, (idx % 2) ? "zw" : "xy");
3812 break;
3813 case 4:
3814 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3815 idx >> 1, (idx % 2) ? "zw" : "xy");
3816 break;
3819 if (dx && dy)
3820 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3821 else if (bias)
3822 shader_addline(ins->ctx->buffer, ", %s", bias);
3823 if (sample_function->offset_size)
3825 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3826 shader_addline(ins->ctx->buffer, ", ");
3827 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3829 shader_addline(ins->ctx->buffer, ")");
3831 if (sample_function->output_single_component)
3832 shader_addline(ins->ctx->buffer, ")");
3834 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3836 if (!is_identity_fixup(fixup))
3837 shader_glsl_color_correction(ins, fixup);
3840 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3842 /* Write the final position.
3844 * OpenGL coordinates specify the center of the pixel while D3D coords
3845 * specify the corner. The offsets are stored in z and w in
3846 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3847 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3848 * a MAD. */
3849 if (use_viewport_index)
3851 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3852 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3854 else
3856 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3857 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3860 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3861 * in utils.c
3863 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3864 * shaders are run before the homogeneous divide, so we have to take the w
3865 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3866 * z = z * 2 - w. */
3867 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3870 /*****************************************************************************
3871 * Begin processing individual instruction opcodes
3872 ****************************************************************************/
3874 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3876 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3877 struct glsl_src_param src0_param;
3878 struct glsl_src_param src1_param;
3879 DWORD write_mask;
3880 const char *op;
3882 /* Determine the GLSL operator to use based on the opcode */
3883 switch (ins->handler_idx)
3885 case WINED3DSIH_ADD: op = "+"; break;
3886 case WINED3DSIH_AND: op = "&"; break;
3887 case WINED3DSIH_DIV: op = "/"; break;
3888 case WINED3DSIH_IADD: op = "+"; break;
3889 case WINED3DSIH_ISHL: op = "<<"; break;
3890 case WINED3DSIH_ISHR: op = ">>"; break;
3891 case WINED3DSIH_MUL: op = "*"; break;
3892 case WINED3DSIH_OR: op = "|"; break;
3893 case WINED3DSIH_SUB: op = "-"; break;
3894 case WINED3DSIH_USHR: op = ">>"; break;
3895 case WINED3DSIH_XOR: op = "^"; break;
3896 default:
3897 op = "<unhandled operator>";
3898 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3899 break;
3902 write_mask = shader_glsl_append_dst(buffer, ins);
3903 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3904 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3905 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3908 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3910 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3911 struct glsl_src_param src0_param;
3912 struct glsl_src_param src1_param;
3913 unsigned int mask_size;
3914 DWORD write_mask;
3915 const char *op;
3917 write_mask = shader_glsl_append_dst(buffer, ins);
3918 mask_size = shader_glsl_get_write_mask_size(write_mask);
3919 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3920 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3922 if (mask_size > 1)
3924 switch (ins->handler_idx)
3926 case WINED3DSIH_EQ: op = "equal"; break;
3927 case WINED3DSIH_IEQ: op = "equal"; break;
3928 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3929 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3930 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3931 case WINED3DSIH_LT: op = "lessThan"; break;
3932 case WINED3DSIH_ILT: op = "lessThan"; break;
3933 case WINED3DSIH_ULT: op = "lessThan"; break;
3934 case WINED3DSIH_NE: op = "notEqual"; break;
3935 case WINED3DSIH_INE: op = "notEqual"; break;
3936 default:
3937 op = "<unhandled operator>";
3938 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3939 break;
3942 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3943 mask_size, op, src0_param.param_str, src1_param.param_str);
3945 else
3947 switch (ins->handler_idx)
3949 case WINED3DSIH_EQ: op = "=="; break;
3950 case WINED3DSIH_IEQ: op = "=="; break;
3951 case WINED3DSIH_GE: op = ">="; break;
3952 case WINED3DSIH_IGE: op = ">="; break;
3953 case WINED3DSIH_UGE: op = ">="; break;
3954 case WINED3DSIH_LT: op = "<"; break;
3955 case WINED3DSIH_ILT: op = "<"; break;
3956 case WINED3DSIH_ULT: op = "<"; break;
3957 case WINED3DSIH_NE: op = "!="; break;
3958 case WINED3DSIH_INE: op = "!="; break;
3959 default:
3960 op = "<unhandled operator>";
3961 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3962 break;
3965 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3966 src0_param.param_str, op, src1_param.param_str);
3970 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3972 struct glsl_src_param src_param;
3973 DWORD write_mask;
3974 const char *op;
3976 switch (ins->handler_idx)
3978 case WINED3DSIH_INEG: op = "-"; break;
3979 case WINED3DSIH_NOT: op = "~"; break;
3980 default:
3981 op = "<unhandled operator>";
3982 ERR("Unhandled opcode %s.\n",
3983 debug_d3dshaderinstructionhandler(ins->handler_idx));
3984 break;
3987 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3988 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3989 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3992 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3994 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3995 struct glsl_src_param src0_param;
3996 struct glsl_src_param src1_param;
3997 DWORD write_mask;
3999 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
4000 * If not, we can emulate it. */
4001 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4002 FIXME("64-bit integer multiplies not implemented.\n");
4004 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4006 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4007 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4008 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4010 shader_addline(ins->ctx->buffer, "%s * %s);\n",
4011 src0_param.param_str, src1_param.param_str);
4015 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
4017 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4018 struct glsl_src_param src0_param, src1_param;
4019 DWORD write_mask;
4021 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4023 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4025 char dst_mask[6];
4027 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4028 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4029 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4030 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
4031 dst_mask, src0_param.param_str, src1_param.param_str);
4033 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4034 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4035 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4036 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4038 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4039 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4041 else
4043 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4044 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4045 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4046 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
4049 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4051 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4052 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4053 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4054 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4058 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
4059 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
4061 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4062 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4063 struct glsl_src_param src0_param;
4064 DWORD write_mask;
4066 write_mask = shader_glsl_append_dst(buffer, ins);
4067 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4069 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
4070 * shader versions WINED3DSIO_MOVA is used for this. */
4071 if (ins->ctx->reg_maps->shader_version.major == 1
4072 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
4073 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
4075 /* This is a simple floor() */
4076 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4077 if (mask_size > 1) {
4078 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4079 } else {
4080 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4083 else if (ins->handler_idx == WINED3DSIH_MOVA)
4085 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4087 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4089 if (mask_size > 1)
4090 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4091 else
4092 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4094 else
4096 if (mask_size > 1)
4097 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4098 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4099 else
4100 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4101 src0_param.param_str, src0_param.param_str);
4104 else
4106 shader_addline(buffer, "%s);\n", src0_param.param_str);
4110 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4111 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4113 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4114 struct glsl_src_param src0_param;
4115 struct glsl_src_param src1_param;
4116 DWORD dst_write_mask, src_write_mask;
4117 unsigned int dst_size;
4119 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4120 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4122 /* dp4 works on vec4, dp3 on vec3, etc. */
4123 if (ins->handler_idx == WINED3DSIH_DP4)
4124 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4125 else if (ins->handler_idx == WINED3DSIH_DP3)
4126 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4127 else
4128 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4130 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4131 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4133 if (dst_size > 1) {
4134 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4135 } else {
4136 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4140 /* Note that this instruction has some restrictions. The destination write mask
4141 * can't contain the w component, and the source swizzles have to be .xyzw */
4142 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4144 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4145 struct glsl_src_param src0_param;
4146 struct glsl_src_param src1_param;
4147 char dst_mask[6];
4149 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4150 shader_glsl_append_dst(ins->ctx->buffer, ins);
4151 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4152 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4153 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4156 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4158 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4160 if (!stream)
4161 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4162 else
4163 FIXME("Unhandled primitive stream %u.\n", stream);
4166 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4167 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4168 * GLSL uses the value as-is. */
4169 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4171 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4172 struct glsl_src_param src0_param;
4173 struct glsl_src_param src1_param;
4174 DWORD dst_write_mask;
4175 unsigned int dst_size;
4177 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4178 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4180 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4181 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4183 if (dst_size > 1)
4185 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4186 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4188 else
4190 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4191 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4195 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4196 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4198 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4199 struct glsl_src_param src_param;
4200 const char *instruction;
4201 DWORD write_mask;
4202 unsigned i;
4204 /* Determine the GLSL function to use based on the opcode */
4205 /* TODO: Possibly make this a table for faster lookups */
4206 switch (ins->handler_idx)
4208 case WINED3DSIH_ABS: instruction = "abs"; break;
4209 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4210 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4211 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4212 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4213 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4214 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4215 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4216 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4217 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4218 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4219 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4220 case WINED3DSIH_FRC: instruction = "fract"; break;
4221 case WINED3DSIH_IMAX: instruction = "max"; break;
4222 case WINED3DSIH_IMIN: instruction = "min"; break;
4223 case WINED3DSIH_MAX: instruction = "max"; break;
4224 case WINED3DSIH_MIN: instruction = "min"; break;
4225 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4226 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4227 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4228 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4229 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4230 case WINED3DSIH_UMAX: instruction = "max"; break;
4231 case WINED3DSIH_UMIN: instruction = "min"; break;
4232 default: instruction = "";
4233 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4234 break;
4237 write_mask = shader_glsl_append_dst(buffer, ins);
4239 /* In D3D bits are numbered from the most significant bit. */
4240 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4241 shader_addline(buffer, "31 - ");
4242 shader_addline(buffer, "%s(", instruction);
4244 if (ins->src_count)
4246 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4247 shader_addline(buffer, "%s", src_param.param_str);
4248 for (i = 1; i < ins->src_count; ++i)
4250 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4251 shader_addline(buffer, ", %s", src_param.param_str);
4255 shader_addline(buffer, "));\n");
4258 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4260 struct wined3d_shader_dst_param dst;
4261 struct glsl_src_param src;
4262 DWORD write_mask;
4263 const char *fmt;
4264 unsigned int i;
4266 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4267 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4269 dst = ins->dst[0];
4270 for (i = 0; i < 4; ++i)
4272 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4273 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4274 &dst, dst.reg.data_type)))
4275 continue;
4277 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4278 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4282 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4284 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4285 struct wined3d_shader_dst_param dst;
4286 struct glsl_src_param src[4];
4287 const char *instruction;
4288 BOOL tmp_dst = FALSE;
4289 char mask_char[6];
4290 unsigned int i, j;
4291 DWORD write_mask;
4293 switch (ins->handler_idx)
4295 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4296 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4297 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4298 default:
4299 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4300 return;
4303 for (i = 0; i < ins->src_count; ++i)
4305 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4306 && ins->dst[0].reg.type == ins->src[i].reg.type)
4307 tmp_dst = TRUE;
4310 dst = ins->dst[0];
4311 for (i = 0; i < 4; ++i)
4313 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4314 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4315 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4316 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4317 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4318 continue;
4320 for (j = 0; j < ins->src_count; ++j)
4321 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4322 shader_addline(buffer, "%s(", instruction);
4323 for (j = 0; j < ins->src_count - 2; ++j)
4324 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4325 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4328 if (tmp_dst)
4330 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4331 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4332 shader_addline(buffer, "tmp0%s);\n", mask_char);
4336 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4338 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4340 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4341 struct glsl_src_param src_param;
4342 unsigned int mask_size;
4343 DWORD write_mask;
4344 char dst_mask[6];
4346 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4347 mask_size = shader_glsl_get_write_mask_size(write_mask);
4348 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4350 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4351 src_param.param_str, src_param.param_str);
4352 shader_glsl_append_dst(buffer, ins);
4354 if (mask_size > 1)
4356 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4357 mask_size, src_param.param_str);
4359 else
4361 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4362 src_param.param_str);
4366 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4368 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4369 ins->ctx->reg_maps->shader_version.minor);
4370 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4371 struct glsl_src_param src0_param;
4372 const char *prefix, *suffix;
4373 unsigned int dst_size;
4374 DWORD dst_write_mask;
4376 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4377 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4379 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4380 dst_write_mask = WINED3DSP_WRITEMASK_3;
4382 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4384 switch (ins->handler_idx)
4386 case WINED3DSIH_EXP:
4387 case WINED3DSIH_EXPP:
4388 prefix = "exp2(";
4389 suffix = ")";
4390 break;
4392 case WINED3DSIH_LOG:
4393 case WINED3DSIH_LOGP:
4394 prefix = "log2(abs(";
4395 suffix = "))";
4396 break;
4398 case WINED3DSIH_RCP:
4399 prefix = "1.0 / ";
4400 suffix = "";
4401 break;
4403 case WINED3DSIH_RSQ:
4404 prefix = "inversesqrt(abs(";
4405 suffix = "))";
4406 break;
4408 default:
4409 prefix = "";
4410 suffix = "";
4411 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4412 break;
4415 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4416 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4417 else
4418 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4421 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4422 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4423 * dst.x = 2^(floor(src))
4424 * dst.y = src - floor(src)
4425 * dst.z = 2^src (partial precision is allowed, but optional)
4426 * dst.w = 1.0;
4427 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4428 * dst = 2^src; (partial precision is allowed, but optional)
4430 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4432 if (ins->ctx->reg_maps->shader_version.major < 2)
4434 struct glsl_src_param src_param;
4435 char dst_mask[6];
4437 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4439 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4440 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4441 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4442 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4444 shader_glsl_append_dst(ins->ctx->buffer, ins);
4445 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4446 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4447 return;
4450 shader_glsl_scalar_op(ins);
4453 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4454 const char *vector_constructor, const char *scalar_constructor)
4456 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4457 struct glsl_src_param src_param;
4458 unsigned int mask_size;
4459 DWORD write_mask;
4461 write_mask = shader_glsl_append_dst(buffer, ins);
4462 mask_size = shader_glsl_get_write_mask_size(write_mask);
4463 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4465 if (mask_size > 1)
4466 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4467 else
4468 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4471 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4473 shader_glsl_cast(ins, "ivec", "int");
4476 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4478 shader_glsl_cast(ins, "uvec", "uint");
4481 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4483 shader_glsl_cast(ins, "vec", "float");
4486 /** Process signed comparison opcodes in GLSL. */
4487 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4489 struct glsl_src_param src0_param;
4490 struct glsl_src_param src1_param;
4491 DWORD write_mask;
4492 unsigned int mask_size;
4494 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4495 mask_size = shader_glsl_get_write_mask_size(write_mask);
4496 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4497 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4499 if (mask_size > 1) {
4500 const char *compare;
4502 switch(ins->handler_idx)
4504 case WINED3DSIH_SLT: compare = "lessThan"; break;
4505 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4506 default: compare = "";
4507 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4510 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4511 src0_param.param_str, src1_param.param_str);
4512 } else {
4513 switch(ins->handler_idx)
4515 case WINED3DSIH_SLT:
4516 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4517 * to return 0.0 but step returns 1.0 because step is not < x
4518 * An alternative is a bvec compare padded with an unused second component.
4519 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4520 * issue. Playing with not() is not possible either because not() does not accept
4521 * a scalar.
4523 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4524 src0_param.param_str, src1_param.param_str);
4525 break;
4526 case WINED3DSIH_SGE:
4527 /* Here we can use the step() function and safe a conditional */
4528 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4529 break;
4530 default:
4531 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4537 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4539 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4540 struct wined3d_shader_dst_param dst[2];
4541 struct glsl_src_param src[3];
4542 unsigned int i, j, k;
4543 char mask_char[6];
4544 DWORD write_mask;
4545 BOOL tmp_dst[2];
4547 for (i = 0; i < ins->dst_count; ++i)
4549 tmp_dst[i] = FALSE;
4550 for (j = 0; j < ins->src_count; ++j)
4552 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4553 && ins->dst[i].reg.type == ins->src[j].reg.type)
4554 tmp_dst[i] = TRUE;
4558 dst[0] = ins->dst[0];
4559 dst[1] = ins->dst[1];
4560 for (i = 0; i < 4; ++i)
4562 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4564 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4565 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4566 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4567 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4568 continue;
4570 for (k = 0; k < ARRAY_SIZE(src); ++k)
4571 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4573 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4574 src[0].param_str, src[1].param_str, src[2].param_str);
4578 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4580 if (tmp_dst[i])
4582 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4583 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4584 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4589 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4591 const char *condition_prefix, *condition_suffix;
4592 struct wined3d_shader_dst_param dst;
4593 struct glsl_src_param src0_param;
4594 struct glsl_src_param src1_param;
4595 struct glsl_src_param src2_param;
4596 BOOL temp_destination = FALSE;
4597 DWORD cmp_channel = 0;
4598 unsigned int i, j;
4599 char mask_char[6];
4600 DWORD write_mask;
4602 switch (ins->handler_idx)
4604 case WINED3DSIH_CMP:
4605 condition_prefix = "";
4606 condition_suffix = " >= 0.0";
4607 break;
4609 case WINED3DSIH_CND:
4610 condition_prefix = "";
4611 condition_suffix = " > 0.5";
4612 break;
4614 case WINED3DSIH_MOVC:
4615 condition_prefix = "bool(";
4616 condition_suffix = ")";
4617 break;
4619 default:
4620 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4621 condition_prefix = "<unhandled prefix>";
4622 condition_suffix = "<unhandled suffix>";
4623 break;
4626 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4628 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4629 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4630 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4631 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4633 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4634 condition_prefix, src0_param.param_str, condition_suffix,
4635 src1_param.param_str, src2_param.param_str);
4636 return;
4639 dst = ins->dst[0];
4641 /* Splitting the instruction up in multiple lines imposes a problem:
4642 * The first lines may overwrite source parameters of the following lines.
4643 * Deal with that by using a temporary destination register if needed. */
4644 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4645 && ins->src[0].reg.type == dst.reg.type)
4646 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4647 && ins->src[1].reg.type == dst.reg.type)
4648 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4649 && ins->src[2].reg.type == dst.reg.type))
4650 temp_destination = TRUE;
4652 /* Cycle through all source0 channels. */
4653 for (i = 0; i < 4; ++i)
4655 write_mask = 0;
4656 /* Find the destination channels which use the current source0 channel. */
4657 for (j = 0; j < 4; ++j)
4659 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4661 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4662 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4665 dst.write_mask = ins->dst[0].write_mask & write_mask;
4667 if (temp_destination)
4669 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4670 continue;
4671 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4673 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4674 continue;
4676 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4677 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4678 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4680 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4681 condition_prefix, src0_param.param_str, condition_suffix,
4682 src1_param.param_str, src2_param.param_str);
4685 if (temp_destination)
4687 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4688 shader_glsl_append_dst(ins->ctx->buffer, ins);
4689 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4693 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4694 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4695 * the compare is done per component of src0. */
4696 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4698 struct glsl_src_param src0_param;
4699 struct glsl_src_param src1_param;
4700 struct glsl_src_param src2_param;
4701 DWORD write_mask;
4702 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4703 ins->ctx->reg_maps->shader_version.minor);
4705 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4707 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4708 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4709 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4710 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4712 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4713 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4714 else
4715 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4716 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4717 return;
4720 shader_glsl_conditional_move(ins);
4723 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4724 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4726 struct glsl_src_param src0_param;
4727 struct glsl_src_param src1_param;
4728 struct glsl_src_param src2_param;
4729 DWORD write_mask;
4731 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4732 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4733 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4734 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4735 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4736 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4739 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4740 Vertex shaders to GLSL codes */
4741 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4743 int i;
4744 int nComponents = 0;
4745 struct wined3d_shader_dst_param tmp_dst = {{0}};
4746 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4747 struct wined3d_shader_instruction tmp_ins;
4749 memset(&tmp_ins, 0, sizeof(tmp_ins));
4751 /* Set constants for the temporary argument */
4752 tmp_ins.ctx = ins->ctx;
4753 tmp_ins.dst_count = 1;
4754 tmp_ins.dst = &tmp_dst;
4755 tmp_ins.src_count = 2;
4756 tmp_ins.src = tmp_src;
4758 switch(ins->handler_idx)
4760 case WINED3DSIH_M4x4:
4761 nComponents = 4;
4762 tmp_ins.handler_idx = WINED3DSIH_DP4;
4763 break;
4764 case WINED3DSIH_M4x3:
4765 nComponents = 3;
4766 tmp_ins.handler_idx = WINED3DSIH_DP4;
4767 break;
4768 case WINED3DSIH_M3x4:
4769 nComponents = 4;
4770 tmp_ins.handler_idx = WINED3DSIH_DP3;
4771 break;
4772 case WINED3DSIH_M3x3:
4773 nComponents = 3;
4774 tmp_ins.handler_idx = WINED3DSIH_DP3;
4775 break;
4776 case WINED3DSIH_M3x2:
4777 nComponents = 2;
4778 tmp_ins.handler_idx = WINED3DSIH_DP3;
4779 break;
4780 default:
4781 break;
4784 tmp_dst = ins->dst[0];
4785 tmp_src[0] = ins->src[0];
4786 tmp_src[1] = ins->src[1];
4787 for (i = 0; i < nComponents; ++i)
4789 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4790 shader_glsl_dot(&tmp_ins);
4791 ++tmp_src[1].reg.idx[0].offset;
4796 The LRP instruction performs a component-wise linear interpolation
4797 between the second and third operands using the first operand as the
4798 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4799 This is equivalent to mix(src2, src1, src0);
4801 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4803 struct glsl_src_param src0_param;
4804 struct glsl_src_param src1_param;
4805 struct glsl_src_param src2_param;
4806 DWORD write_mask;
4808 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4810 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4811 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4812 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4814 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4815 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4818 /** Process the WINED3DSIO_LIT instruction in GLSL:
4819 * dst.x = dst.w = 1.0
4820 * dst.y = (src0.x > 0) ? src0.x
4821 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4822 * where src.w is clamped at +- 128
4824 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4826 struct glsl_src_param src0_param;
4827 struct glsl_src_param src1_param;
4828 struct glsl_src_param src3_param;
4829 char dst_mask[6];
4831 shader_glsl_append_dst(ins->ctx->buffer, ins);
4832 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4834 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4835 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4836 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4838 /* The sdk specifies the instruction like this
4839 * dst.x = 1.0;
4840 * if(src.x > 0.0) dst.y = src.x
4841 * else dst.y = 0.0.
4842 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4843 * else dst.z = 0.0;
4844 * dst.w = 1.0;
4845 * (where power = src.w clamped between -128 and 128)
4847 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4848 * dst.x = 1.0 ... No further explanation needed
4849 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4850 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4851 * dst.w = 1.0. ... Nothing fancy.
4853 * So we still have one conditional in there. So do this:
4854 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4856 * 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),
4857 * 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.
4858 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4860 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4861 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4862 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4864 shader_addline(ins->ctx->buffer,
4865 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4866 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4867 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4868 src0_param.param_str, src3_param.param_str, dst_mask);
4871 /** Process the WINED3DSIO_DST instruction in GLSL:
4872 * dst.x = 1.0
4873 * dst.y = src0.x * src0.y
4874 * dst.z = src0.z
4875 * dst.w = src1.w
4877 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4879 struct glsl_src_param src0y_param;
4880 struct glsl_src_param src0z_param;
4881 struct glsl_src_param src1y_param;
4882 struct glsl_src_param src1w_param;
4883 char dst_mask[6];
4885 shader_glsl_append_dst(ins->ctx->buffer, ins);
4886 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4888 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4889 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4890 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4891 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4893 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4894 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4897 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4898 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4899 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4901 * dst.x = cos(src0.?)
4902 * dst.y = sin(src0.?)
4903 * dst.z = dst.z
4904 * dst.w = dst.w
4906 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4908 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4909 struct glsl_src_param src0_param;
4910 DWORD write_mask;
4912 if (ins->ctx->reg_maps->shader_version.major < 4)
4914 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4916 write_mask = shader_glsl_append_dst(buffer, ins);
4917 switch (write_mask)
4919 case WINED3DSP_WRITEMASK_0:
4920 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4921 break;
4923 case WINED3DSP_WRITEMASK_1:
4924 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4925 break;
4927 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4928 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4929 src0_param.param_str, src0_param.param_str);
4930 break;
4932 default:
4933 ERR("Write mask should be .x, .y or .xy\n");
4934 break;
4937 return;
4940 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4943 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4945 char dst_mask[6];
4947 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4948 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4949 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4951 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4952 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4953 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4955 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4956 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4958 else
4960 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4961 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4962 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4965 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4967 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4968 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4969 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4973 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4974 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4975 * generate invalid code
4977 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4979 struct glsl_src_param src0_param;
4980 DWORD write_mask;
4982 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4983 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4985 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4988 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4989 * Start a for() loop where src1.y is the initial value of aL,
4990 * increment aL by src1.z for a total of src1.x iterations.
4991 * Need to use a temporary variable for this operation.
4993 /* FIXME: I don't think nested loops will work correctly this way. */
4994 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4996 struct wined3d_shader_parser_state *state = ins->ctx->state;
4997 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4998 const struct wined3d_shader *shader = ins->ctx->shader;
4999 const struct wined3d_shader_lconst *constant;
5000 struct glsl_src_param src1_param;
5001 const DWORD *control_values = NULL;
5003 if (ins->ctx->reg_maps->shader_version.major < 4)
5005 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
5007 /* Try to hardcode the loop control parameters if possible. Direct3D 9
5008 * class hardware doesn't support real varying indexing, but Microsoft
5009 * designed this feature for Shader model 2.x+. If the loop control is
5010 * known at compile time, the GLSL compiler can unroll the loop, and
5011 * replace indirect addressing with direct addressing. */
5012 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
5014 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5016 if (constant->idx == ins->src[1].reg.idx[0].offset)
5018 control_values = constant->value;
5019 break;
5024 if (control_values)
5026 struct wined3d_shader_loop_control loop_control;
5027 loop_control.count = control_values[0];
5028 loop_control.start = control_values[1];
5029 loop_control.step = (int)control_values[2];
5031 if (loop_control.step > 0)
5033 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
5034 state->current_loop_depth, loop_control.start,
5035 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
5036 state->current_loop_depth, loop_control.step);
5038 else if (loop_control.step < 0)
5040 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
5041 state->current_loop_depth, loop_control.start,
5042 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
5043 state->current_loop_depth, loop_control.step);
5045 else
5047 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
5048 state->current_loop_depth, loop_control.start, state->current_loop_depth,
5049 state->current_loop_depth, loop_control.count,
5050 state->current_loop_depth);
5053 else
5055 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
5056 state->current_loop_depth, state->current_loop_reg,
5057 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
5058 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
5061 ++state->current_loop_reg;
5063 else
5065 shader_addline(buffer, "for (;;)\n{\n");
5068 ++state->current_loop_depth;
5071 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
5073 struct wined3d_shader_parser_state *state = ins->ctx->state;
5075 shader_addline(ins->ctx->buffer, "}\n");
5077 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5079 --state->current_loop_depth;
5080 --state->current_loop_reg;
5083 if (ins->handler_idx == WINED3DSIH_ENDREP)
5085 --state->current_loop_depth;
5089 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5091 struct wined3d_shader_parser_state *state = ins->ctx->state;
5092 const struct wined3d_shader *shader = ins->ctx->shader;
5093 const struct wined3d_shader_lconst *constant;
5094 struct glsl_src_param src0_param;
5095 const DWORD *control_values = NULL;
5097 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5098 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5100 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5102 if (constant->idx == ins->src[0].reg.idx[0].offset)
5104 control_values = constant->value;
5105 break;
5110 if (control_values)
5112 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5113 state->current_loop_depth, state->current_loop_depth,
5114 control_values[0], state->current_loop_depth);
5116 else
5118 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5119 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5120 state->current_loop_depth, state->current_loop_depth,
5121 src0_param.param_str, state->current_loop_depth);
5124 ++state->current_loop_depth;
5127 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5129 struct glsl_src_param src0_param;
5131 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5132 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5135 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5137 struct glsl_src_param src0_param;
5139 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5140 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5143 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5145 shader_addline(ins->ctx->buffer, "default:\n");
5148 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
5150 struct glsl_src_param src_param;
5151 const char *condition;
5153 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5154 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5155 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5158 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5160 shader_glsl_generate_condition(ins);
5161 shader_addline(ins->ctx->buffer, "{\n");
5164 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5166 struct glsl_src_param src0_param;
5167 struct glsl_src_param src1_param;
5169 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5170 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5172 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5173 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5176 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5178 shader_addline(ins->ctx->buffer, "} else {\n");
5181 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5183 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5184 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5186 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5187 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5188 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5190 if (!stream)
5191 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5192 else
5193 FIXME("Unhandled primitive stream %u.\n", stream);
5196 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5198 shader_addline(ins->ctx->buffer, "break;\n");
5201 /* FIXME: According to MSDN the compare is done per component. */
5202 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5204 struct glsl_src_param src0_param;
5205 struct glsl_src_param src1_param;
5207 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5208 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5210 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5211 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5214 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5216 const char *op;
5218 switch (ins->handler_idx)
5220 case WINED3DSIH_BREAKP:
5221 op = "break;";
5222 break;
5223 case WINED3DSIH_CONTINUEP:
5224 op = "continue;";
5225 break;
5226 case WINED3DSIH_RETP:
5227 op = "return;";
5228 break;
5229 default:
5230 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5231 return;
5234 shader_glsl_generate_condition(ins);
5235 if (ins->handler_idx == WINED3DSIH_RETP)
5237 shader_addline(ins->ctx->buffer, "{\n");
5238 shader_glsl_generate_shader_epilogue(ins->ctx);
5240 shader_addline(ins->ctx->buffer, " %s\n", op);
5241 if (ins->handler_idx == WINED3DSIH_RETP)
5242 shader_addline(ins->ctx->buffer, "}\n");
5245 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5247 shader_addline(ins->ctx->buffer, "continue;\n");
5250 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5252 shader_addline(ins->ctx->buffer, "}\n");
5253 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5255 /* Subroutines appear at the end of the shader. */
5256 ins->ctx->state->in_subroutine = TRUE;
5259 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5261 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5264 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5266 struct glsl_src_param src1_param;
5268 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5269 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5270 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5273 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5275 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5277 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5279 shader_glsl_generate_shader_epilogue(ins->ctx);
5280 shader_addline(ins->ctx->buffer, "return;\n");
5284 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5286 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5287 ins->ctx->reg_maps->shader_version.minor);
5288 struct glsl_sample_function sample_function;
5289 DWORD sample_flags = 0;
5290 DWORD resource_idx;
5291 DWORD mask = 0, swizzle;
5292 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5294 /* 1.0-1.4: Use destination register as sampler source.
5295 * 2.0+: Use provided sampler source. */
5296 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5297 resource_idx = ins->dst[0].reg.idx[0].offset;
5298 else
5299 resource_idx = ins->src[1].reg.idx[0].offset;
5301 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5303 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5304 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5305 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5307 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5308 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5310 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5311 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5313 case WINED3D_TTFF_COUNT1:
5314 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5315 break;
5316 case WINED3D_TTFF_COUNT2:
5317 mask = WINED3DSP_WRITEMASK_1;
5318 break;
5319 case WINED3D_TTFF_COUNT3:
5320 mask = WINED3DSP_WRITEMASK_2;
5321 break;
5322 case WINED3D_TTFF_COUNT4:
5323 case WINED3D_TTFF_DISABLE:
5324 mask = WINED3DSP_WRITEMASK_3;
5325 break;
5329 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5331 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5333 if (src_mod == WINED3DSPSM_DZ) {
5334 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5335 mask = WINED3DSP_WRITEMASK_2;
5336 } else if (src_mod == WINED3DSPSM_DW) {
5337 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5338 mask = WINED3DSP_WRITEMASK_3;
5341 else
5343 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5344 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5346 /* ps 2.0 texldp instruction always divides by the fourth component. */
5347 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5348 mask = WINED3DSP_WRITEMASK_3;
5352 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5353 mask |= sample_function.coord_mask;
5354 sample_function.coord_mask = mask;
5356 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5357 else swizzle = ins->src[1].swizzle;
5359 /* 1.0-1.3: Use destination register as coordinate source.
5360 1.4+: Use provided coordinate source register. */
5361 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5363 char coord_mask[6];
5364 shader_glsl_write_mask_to_str(mask, coord_mask);
5365 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5366 "T%u%s", resource_idx, coord_mask);
5368 else
5370 struct glsl_src_param coord_param;
5371 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5372 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5374 struct glsl_src_param bias;
5375 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5376 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5377 NULL, "%s", coord_param.param_str);
5378 } else {
5379 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5380 "%s", coord_param.param_str);
5383 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5386 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5388 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5389 struct glsl_src_param coord_param, dx_param, dy_param;
5390 struct glsl_sample_function sample_function;
5391 DWORD sampler_idx;
5392 DWORD swizzle = ins->src[1].swizzle;
5394 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5396 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5397 shader_glsl_tex(ins);
5398 return;
5401 sampler_idx = ins->src[1].reg.idx[0].offset;
5403 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5404 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5405 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5406 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5408 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5409 NULL, NULL, "%s", coord_param.param_str);
5410 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5413 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5415 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5416 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5417 struct glsl_src_param coord_param, lod_param;
5418 struct glsl_sample_function sample_function;
5419 DWORD swizzle = ins->src[1].swizzle;
5420 DWORD sampler_idx;
5422 sampler_idx = ins->src[1].reg.idx[0].offset;
5424 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5425 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5427 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5429 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5430 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5432 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5433 * However, the NVIDIA drivers allow them in fragment shaders as well,
5434 * even without the appropriate extension. */
5435 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5437 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5438 "%s", coord_param.param_str);
5439 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5442 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5443 unsigned int resource_idx, unsigned int sampler_idx)
5445 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5446 unsigned int i;
5448 for (i = 0; i < sampler_map->count; ++i)
5450 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5451 return entries[i].bind_idx;
5454 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5456 return ~0u;
5459 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5461 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5462 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5463 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5464 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5465 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5466 struct glsl_src_param structure_idx, offset, data, data2;
5467 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5468 enum wined3d_shader_resource_type resource_type;
5469 struct wined3d_string_buffer *address;
5470 enum wined3d_data_type data_type;
5471 unsigned int resource_idx, stride;
5472 const char *op, *resource;
5473 DWORD coord_mask;
5474 BOOL is_tgsm;
5476 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5477 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5478 if (is_tgsm)
5480 if (resource_idx >= reg_maps->tgsm_count)
5482 ERR("Invalid TGSM index %u.\n", resource_idx);
5483 return;
5485 resource = "g";
5486 data_type = WINED3D_DATA_UINT;
5487 coord_mask = 1;
5488 stride = reg_maps->tgsm[resource_idx].stride;
5490 else
5492 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5494 ERR("Invalid UAV index %u.\n", resource_idx);
5495 return;
5497 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5498 if (resource_type >= ARRAY_SIZE(resource_type_info))
5500 ERR("Unexpected resource type %#x.\n", resource_type);
5501 return;
5503 resource = "image";
5504 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5505 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5506 stride = reg_maps->uav_resource_info[resource_idx].stride;
5509 switch (ins->handler_idx)
5511 case WINED3DSIH_ATOMIC_AND:
5512 case WINED3DSIH_IMM_ATOMIC_AND:
5513 if (is_tgsm)
5514 op = "atomicAnd";
5515 else
5516 op = "imageAtomicAnd";
5517 break;
5518 case WINED3DSIH_ATOMIC_CMP_STORE:
5519 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5520 if (is_tgsm)
5521 op = "atomicCompSwap";
5522 else
5523 op = "imageAtomicCompSwap";
5524 break;
5525 case WINED3DSIH_ATOMIC_IADD:
5526 case WINED3DSIH_IMM_ATOMIC_IADD:
5527 if (is_tgsm)
5528 op = "atomicAdd";
5529 else
5530 op = "imageAtomicAdd";
5531 break;
5532 case WINED3DSIH_ATOMIC_IMAX:
5533 case WINED3DSIH_IMM_ATOMIC_IMAX:
5534 if (is_tgsm)
5535 op = "atomicMax";
5536 else
5537 op = "imageAtomicMax";
5538 if (data_type != WINED3D_DATA_INT)
5540 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5541 return;
5543 break;
5544 case WINED3DSIH_ATOMIC_IMIN:
5545 case WINED3DSIH_IMM_ATOMIC_IMIN:
5546 if (is_tgsm)
5547 op = "atomicMin";
5548 else
5549 op = "imageAtomicMin";
5550 if (data_type != WINED3D_DATA_INT)
5552 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5553 return;
5555 break;
5556 case WINED3DSIH_ATOMIC_OR:
5557 case WINED3DSIH_IMM_ATOMIC_OR:
5558 if (is_tgsm)
5559 op = "atomicOr";
5560 else
5561 op = "imageAtomicOr";
5562 break;
5563 case WINED3DSIH_ATOMIC_UMAX:
5564 case WINED3DSIH_IMM_ATOMIC_UMAX:
5565 if (is_tgsm)
5566 op = "atomicMax";
5567 else
5568 op = "imageAtomicMax";
5569 if (data_type != WINED3D_DATA_UINT)
5571 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5572 return;
5574 break;
5575 case WINED3DSIH_ATOMIC_UMIN:
5576 case WINED3DSIH_IMM_ATOMIC_UMIN:
5577 if (is_tgsm)
5578 op = "atomicMin";
5579 else
5580 op = "imageAtomicMin";
5581 if (data_type != WINED3D_DATA_UINT)
5583 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5584 return;
5586 break;
5587 case WINED3DSIH_ATOMIC_XOR:
5588 case WINED3DSIH_IMM_ATOMIC_XOR:
5589 if (is_tgsm)
5590 op = "atomicXor";
5591 else
5592 op = "imageAtomicXor";
5593 break;
5594 case WINED3DSIH_IMM_ATOMIC_EXCH:
5595 if (is_tgsm)
5596 op = "atomicExchange";
5597 else
5598 op = "imageAtomicExchange";
5599 break;
5600 default:
5601 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5602 return;
5605 address = string_buffer_get(priv->string_buffers);
5606 if (stride)
5608 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5609 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5610 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5612 else
5614 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5615 string_buffer_sprintf(address, "%s", offset.param_str);
5616 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5617 shader_addline(address, "/ 4");
5620 if (is_imm_instruction)
5621 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5623 if (is_tgsm)
5624 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5625 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5626 else
5627 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5628 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5630 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5631 shader_addline(buffer, "%s", data.param_str);
5632 if (ins->src_count >= 3)
5634 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5635 shader_addline(buffer, ", %s", data2.param_str);
5638 if (is_imm_instruction)
5639 shader_addline(buffer, ")");
5640 shader_addline(buffer, ");\n");
5642 string_buffer_release(priv->string_buffers, address);
5645 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5647 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5648 const char *op;
5650 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5651 op = "atomicCounterIncrement";
5652 else
5653 op = "atomicCounterDecrement";
5655 shader_glsl_append_dst(ins->ctx->buffer, ins);
5656 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5659 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5661 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5662 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5663 enum wined3d_shader_resource_type resource_type;
5664 struct glsl_src_param image_coord_param;
5665 enum wined3d_data_type data_type;
5666 DWORD coord_mask, write_mask;
5667 unsigned int uav_idx;
5668 char dst_swizzle[6];
5670 uav_idx = ins->src[1].reg.idx[0].offset;
5671 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5673 ERR("Invalid UAV index %u.\n", uav_idx);
5674 return;
5676 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5677 if (resource_type >= ARRAY_SIZE(resource_type_info))
5679 ERR("Unexpected resource type %#x.\n", resource_type);
5680 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5682 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5683 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5685 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5686 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5688 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5689 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5690 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5693 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5695 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5696 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5697 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5698 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5699 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5700 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5701 struct glsl_src_param structure_idx, offset;
5702 struct wined3d_string_buffer *address;
5703 struct wined3d_shader_dst_param dst;
5704 const char *function, *resource;
5706 resource_idx = src->reg.idx[0].offset;
5707 if (src->reg.type == WINED3DSPR_RESOURCE)
5709 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5711 ERR("Invalid resource index %u.\n", resource_idx);
5712 return;
5714 stride = reg_maps->resource_info[resource_idx].stride;
5715 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5716 function = "texelFetch";
5717 resource = "sampler";
5719 else if (src->reg.type == WINED3DSPR_UAV)
5721 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5723 ERR("Invalid UAV index %u.\n", resource_idx);
5724 return;
5726 stride = reg_maps->uav_resource_info[resource_idx].stride;
5727 bind_idx = resource_idx;
5728 function = "imageLoad";
5729 resource = "image";
5731 else
5733 if (resource_idx >= reg_maps->tgsm_count)
5735 ERR("Invalid TGSM index %u.\n", resource_idx);
5736 return;
5738 stride = reg_maps->tgsm[resource_idx].stride;
5739 bind_idx = resource_idx;
5740 function = NULL;
5741 resource = "g";
5744 address = string_buffer_get(priv->string_buffers);
5745 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5747 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5748 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5750 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5751 shader_addline(address, "%s / 4", offset.param_str);
5753 dst = ins->dst[0];
5754 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5756 /* The instruction is split into multiple lines. The first lines may
5757 * overwrite source parameters of the following lines. */
5758 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5759 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5762 for (i = 0; i < 4; ++i)
5764 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5765 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5766 continue;
5768 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5769 if (function)
5770 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5771 function, prefix, resource, bind_idx, address->buffer, swizzle);
5772 else
5773 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5774 prefix, resource, bind_idx, address->buffer, swizzle);
5777 string_buffer_release(priv->string_buffers, address);
5780 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5782 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5783 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5784 struct glsl_src_param image_coord_param, image_data_param;
5785 enum wined3d_shader_resource_type resource_type;
5786 enum wined3d_data_type data_type;
5787 unsigned int uav_idx;
5788 DWORD coord_mask;
5790 uav_idx = ins->dst[0].reg.idx[0].offset;
5791 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5793 ERR("Invalid UAV index %u.\n", uav_idx);
5794 return;
5796 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5797 if (resource_type >= ARRAY_SIZE(resource_type_info))
5799 ERR("Unexpected resource type %#x.\n", resource_type);
5800 return;
5802 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5803 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5805 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5806 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5807 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5808 shader_glsl_get_prefix(version->type), uav_idx,
5809 image_coord_param.param_str, image_data_param.param_str);
5812 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5814 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5815 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5816 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5817 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5818 struct glsl_src_param structure_idx, offset, data;
5819 unsigned int i, resource_idx, stride, src_idx = 0;
5820 struct wined3d_string_buffer *address;
5821 DWORD write_mask;
5822 BOOL is_tgsm;
5824 resource_idx = ins->dst[0].reg.idx[0].offset;
5825 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5826 if (is_tgsm)
5828 if (resource_idx >= reg_maps->tgsm_count)
5830 ERR("Invalid TGSM index %u.\n", resource_idx);
5831 return;
5833 stride = reg_maps->tgsm[resource_idx].stride;
5835 else
5837 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5839 ERR("Invalid UAV index %u.\n", resource_idx);
5840 return;
5842 stride = reg_maps->uav_resource_info[resource_idx].stride;
5845 address = string_buffer_get(priv->string_buffers);
5846 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5848 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5849 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5851 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5852 shader_addline(address, "%s / 4", offset.param_str);
5854 for (i = 0; i < 4; ++i)
5856 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5857 continue;
5859 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5861 if (is_tgsm)
5862 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5863 prefix, resource_idx, address->buffer, i, data.param_str);
5864 else
5865 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5866 prefix, resource_idx, address->buffer, i, data.param_str);
5869 string_buffer_release(priv->string_buffers, address);
5872 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5874 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5875 unsigned int sync_flags = ins->flags;
5877 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5879 shader_addline(buffer, "barrier();\n");
5880 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5883 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5885 shader_addline(buffer, "memoryBarrierShared();\n");
5886 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5889 if (sync_flags)
5890 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5893 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5894 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5896 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5897 unsigned int idx = reg->idx[0].offset;
5899 if (reg->type == WINED3DSPR_RESOURCE)
5901 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5903 ERR("Invalid resource index %u.\n", idx);
5904 return NULL;
5906 return &reg_maps->resource_info[idx];
5909 if (reg->type == WINED3DSPR_UAV)
5911 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5913 ERR("Invalid UAV index %u.\n", idx);
5914 return NULL;
5916 return &reg_maps->uav_resource_info[idx];
5919 FIXME("Unhandled register type %#x.\n", reg->type);
5920 return NULL;
5923 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5925 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5926 const struct wined3d_shader_resource_info *resource_info;
5927 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5928 unsigned int resource_idx;
5929 char dst_swizzle[6];
5930 DWORD write_mask;
5932 write_mask = shader_glsl_append_dst(buffer, ins);
5933 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5935 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5936 return;
5937 resource_idx = ins->src[0].reg.idx[0].offset;
5939 shader_addline(buffer, "ivec2(");
5940 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5942 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5943 resource_idx, WINED3D_SAMPLER_DEFAULT);
5944 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5946 else
5948 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5950 if (resource_info->stride)
5951 shader_addline(buffer, " / %u", resource_info->stride);
5952 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5953 shader_addline(buffer, " * 4");
5954 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5957 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5959 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5960 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5963 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5965 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5968 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5970 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5971 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5972 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5973 enum wined3d_shader_resource_type resource_type;
5974 enum wined3d_shader_register_type reg_type;
5975 unsigned int resource_idx, bind_idx, i;
5976 enum wined3d_data_type dst_data_type;
5977 struct glsl_src_param lod_param;
5978 BOOL supports_mipmaps;
5979 char dst_swizzle[6];
5980 DWORD write_mask;
5982 dst_data_type = ins->dst[0].reg.data_type;
5983 if (ins->flags == WINED3DSI_RESINFO_UINT)
5984 dst_data_type = WINED3D_DATA_UINT;
5985 else if (ins->flags)
5986 FIXME("Unhandled flags %#x.\n", ins->flags);
5988 reg_type = ins->src[1].reg.type;
5989 resource_idx = ins->src[1].reg.idx[0].offset;
5990 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5991 if (reg_type == WINED3DSPR_RESOURCE)
5993 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5994 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5995 resource_idx, WINED3D_SAMPLER_DEFAULT);
5997 else
5999 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
6000 bind_idx = resource_idx;
6003 if (resource_type >= ARRAY_SIZE(resource_type_info))
6005 ERR("Unexpected resource type %#x.\n", resource_type);
6006 return;
6009 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], dst_data_type);
6010 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
6012 if (dst_data_type == WINED3D_DATA_UINT)
6013 shader_addline(buffer, "uvec4(");
6014 else
6015 shader_addline(buffer, "vec4(");
6017 if (reg_type == WINED3DSPR_RESOURCE)
6019 shader_addline(buffer, "textureSize(%s_sampler%u",
6020 shader_glsl_get_prefix(version->type), bind_idx);
6022 else
6024 shader_addline(buffer, "imageSize(%s_image%u",
6025 shader_glsl_get_prefix(version->type), bind_idx);
6028 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
6029 if (supports_mipmaps)
6030 shader_addline(buffer, ", %s", lod_param.param_str);
6031 shader_addline(buffer, "), ");
6033 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
6034 shader_addline(buffer, "0, ");
6036 if (supports_mipmaps)
6038 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6040 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
6041 shader_glsl_get_prefix(version->type), bind_idx);
6043 else
6045 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
6046 shader_addline(buffer, "1");
6049 else
6051 shader_addline(buffer, "1");
6054 shader_addline(buffer, ")%s);\n", dst_swizzle);
6057 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
6059 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6060 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6061 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6062 const struct wined3d_shader_dst_param *dst = ins->dst;
6063 const struct wined3d_shader_src_param *src = ins->src;
6064 enum wined3d_shader_resource_type resource_type;
6065 enum wined3d_data_type dst_data_type;
6066 unsigned int resource_idx, bind_idx;
6067 char dst_swizzle[6];
6068 DWORD write_mask;
6070 dst_data_type = dst->reg.data_type;
6071 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
6072 dst_data_type = WINED3D_DATA_UINT;
6073 else if (ins->flags)
6074 FIXME("Unhandled flags %#x.\n", ins->flags);
6076 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, dst_data_type);
6077 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
6079 if (dst_data_type == WINED3D_DATA_UINT)
6080 shader_addline(buffer, "uvec4(");
6081 else
6082 shader_addline(buffer, "vec4(");
6084 if (src->reg.type == WINED3DSPR_RASTERIZER)
6086 if (gl_info->supported[ARB_SAMPLE_SHADING])
6088 shader_addline(buffer, "gl_NumSamples");
6090 else
6092 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
6093 shader_addline(buffer, "1");
6096 else
6098 resource_idx = src->reg.idx[0].offset;
6099 resource_type = reg_maps->resource_info[resource_idx].type;
6100 if (resource_type >= ARRAY_SIZE(resource_type_info))
6102 ERR("Unexpected resource type %#x.\n", resource_type);
6103 return;
6105 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
6107 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
6109 shader_addline(buffer, "textureSamples(%s_sampler%u)",
6110 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
6112 else
6114 FIXME("textureSamples() is not supported.\n");
6115 shader_addline(buffer, "1");
6119 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
6122 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6124 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6125 struct glsl_src_param coord_param, lod_param, sample_param;
6126 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6127 struct glsl_sample_function sample_function;
6128 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6129 BOOL has_lod_param;
6131 if (wined3d_shader_instruction_has_texel_offset(ins))
6132 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6134 resource_idx = ins->src[1].reg.idx[0].offset;
6135 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6137 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6139 ERR("Invalid resource index %u.\n", resource_idx);
6140 return;
6142 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6144 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6145 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6146 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6147 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6148 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6150 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6151 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6152 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6154 else
6156 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6157 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6158 "%s", coord_param.param_str);
6160 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6163 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6165 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6166 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6167 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6168 struct glsl_sample_function sample_function;
6169 DWORD flags = 0;
6171 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6172 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6173 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6174 flags |= WINED3D_GLSL_SAMPLE_LOD;
6175 if (wined3d_shader_instruction_has_texel_offset(ins))
6176 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6178 resource_idx = ins->src[1].reg.idx[0].offset;
6179 sampler_idx = ins->src[2].reg.idx[0].offset;
6181 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6182 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6184 switch (ins->handler_idx)
6186 case WINED3DSIH_SAMPLE:
6187 break;
6188 case WINED3DSIH_SAMPLE_B:
6189 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6190 lod_param_str = lod_param.param_str;
6191 break;
6192 case WINED3DSIH_SAMPLE_GRAD:
6193 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6194 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6195 dx_param_str = dx_param.param_str;
6196 dy_param_str = dy_param.param_str;
6197 break;
6198 case WINED3DSIH_SAMPLE_LOD:
6199 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6200 lod_param_str = lod_param.param_str;
6201 break;
6202 default:
6203 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6204 break;
6207 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6208 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6209 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6210 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6213 /* GLSL doesn't provide a function to sample from level zero with depth
6214 * comparison for array textures and cube textures. We use textureGrad*()
6215 * to implement sample_c_lz.
6217 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6218 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6219 unsigned int coord_size, const char *coord_param, const char *ref_param)
6221 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6222 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6223 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6224 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6225 char dst_swizzle[6];
6227 WARN("Emitting textureGrad() for sample_c_lz.\n");
6229 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6230 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6231 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6232 sample_function->offset_size ? "Offset" : "",
6233 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6234 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6235 if (sample_function->offset_size)
6237 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6238 shader_addline(buffer, ", ");
6239 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6241 shader_addline(buffer, "))%s);\n", dst_swizzle);
6244 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6246 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6247 const struct wined3d_shader_resource_info *resource_info;
6248 struct glsl_src_param coord_param, compare_param;
6249 struct glsl_sample_function sample_function;
6250 const char *lod_param = NULL;
6251 unsigned int coord_size;
6252 DWORD flags = 0;
6254 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6256 lod_param = "0";
6257 flags |= WINED3D_GLSL_SAMPLE_LOD;
6260 if (wined3d_shader_instruction_has_texel_offset(ins))
6261 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6263 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6264 return;
6265 resource_idx = ins->src[1].reg.idx[0].offset;
6266 sampler_idx = ins->src[2].reg.idx[0].offset;
6268 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6269 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6270 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6271 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6272 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6273 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6274 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6275 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6277 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6278 coord_size, coord_param.param_str, compare_param.param_str);
6280 else
6282 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6283 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6284 coord_size, coord_param.param_str, compare_param.param_str);
6286 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6289 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6291 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6292 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6293 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6294 struct glsl_src_param coord_param, compare_param, offset_param;
6295 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6296 const struct wined3d_shader_resource_info *resource_info;
6297 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6298 unsigned int coord_size, offset_size;
6299 char dst_swizzle[6];
6300 BOOL has_offset;
6302 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6304 FIXME("OpenGL implementation does not support textureGather.\n");
6305 return;
6308 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6309 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6310 || wined3d_shader_instruction_has_texel_offset(ins);
6312 resource_param_idx =
6313 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6314 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6315 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6316 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6317 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6319 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6320 return;
6322 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6324 ERR("Unexpected resource type %#x.\n", resource_info->type);
6325 return;
6327 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6329 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6330 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6332 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6334 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6335 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6336 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6338 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6339 shader_addline(buffer, ", %s", compare_param.param_str);
6341 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6343 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6344 shader_addline(buffer, ", %s", offset_param.param_str);
6346 else if (has_offset)
6348 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6349 shader_addline(buffer, ", ");
6350 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6352 if (component_idx)
6353 shader_addline(buffer, ", %u", component_idx);
6355 shader_addline(buffer, ")%s);\n", dst_swizzle);
6358 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6360 /* FIXME: Make this work for more than just 2D textures */
6361 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6362 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6364 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6366 char dst_mask[6];
6368 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6369 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6370 ins->dst[0].reg.idx[0].offset, dst_mask);
6372 else
6374 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6375 DWORD reg = ins->src[0].reg.idx[0].offset;
6376 char dst_swizzle[6];
6378 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6380 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6382 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6383 struct glsl_src_param div_param;
6384 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6386 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6388 if (mask_size > 1)
6389 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6390 else
6391 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6393 else
6395 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6400 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6401 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6402 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6403 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6405 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6406 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6407 struct glsl_sample_function sample_function;
6408 struct glsl_src_param src0_param;
6409 UINT mask_size;
6411 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6413 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6414 * scalar, and projected sampling would require 4.
6416 * It is a dependent read - not valid with conditional NP2 textures
6418 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6419 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6421 switch(mask_size)
6423 case 1:
6424 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6425 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6426 break;
6428 case 2:
6429 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6430 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6431 break;
6433 case 3:
6434 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6435 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6436 break;
6438 default:
6439 FIXME("Unexpected mask size %u\n", mask_size);
6440 break;
6442 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6445 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6446 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6447 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6449 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6450 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6451 struct glsl_src_param src0_param;
6452 DWORD dst_mask;
6453 unsigned int mask_size;
6455 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6456 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6457 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6459 if (mask_size > 1) {
6460 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6461 } else {
6462 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6466 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6467 * Calculate the depth as dst.x / dst.y */
6468 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6470 struct glsl_dst_param dst_param;
6472 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6474 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6475 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6476 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6477 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6478 * >= 1.0 or < 0.0
6480 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6481 dst_param.reg_name, dst_param.reg_name);
6484 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6485 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6486 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6487 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6489 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6491 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6492 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6493 struct glsl_src_param src0_param;
6495 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6497 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6498 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6501 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6502 * Calculate the 1st of a 2-row matrix multiplication. */
6503 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6505 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6506 DWORD reg = ins->dst[0].reg.idx[0].offset;
6507 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6508 struct glsl_src_param src0_param;
6510 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6511 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6514 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6515 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6516 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6518 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6519 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6520 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6521 DWORD reg = ins->dst[0].reg.idx[0].offset;
6522 struct glsl_src_param src0_param;
6524 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6525 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6526 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6529 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6531 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6532 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6533 struct glsl_sample_function sample_function;
6534 DWORD reg = ins->dst[0].reg.idx[0].offset;
6535 struct glsl_src_param src0_param;
6537 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6538 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6540 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6542 /* Sample the texture using the calculated coordinates */
6543 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6544 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6547 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6548 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6549 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6551 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6552 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6553 struct glsl_sample_function sample_function;
6554 DWORD reg = ins->dst[0].reg.idx[0].offset;
6555 struct glsl_src_param src0_param;
6557 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6558 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6560 /* Dependent read, not valid with conditional NP2 */
6561 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6563 /* Sample the texture using the calculated coordinates */
6564 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6565 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6567 tex_mx->current_row = 0;
6570 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6571 * Perform the 3rd row of a 3x3 matrix multiply */
6572 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6574 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6575 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6576 DWORD reg = ins->dst[0].reg.idx[0].offset;
6577 struct glsl_src_param src0_param;
6578 char dst_mask[6];
6580 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6582 shader_glsl_append_dst(ins->ctx->buffer, ins);
6583 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6584 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6586 tex_mx->current_row = 0;
6589 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6590 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6591 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6593 struct glsl_src_param src0_param;
6594 struct glsl_src_param src1_param;
6595 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6596 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6597 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6598 struct glsl_sample_function sample_function;
6599 DWORD reg = ins->dst[0].reg.idx[0].offset;
6600 char coord_mask[6];
6602 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6603 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6605 /* Perform the last matrix multiply operation */
6606 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6607 /* Reflection calculation */
6608 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6610 /* Dependent read, not valid with conditional NP2 */
6611 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6612 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6614 /* Sample the texture */
6615 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6616 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6617 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6619 tex_mx->current_row = 0;
6622 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6623 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6624 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6626 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6627 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6628 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6629 struct glsl_sample_function sample_function;
6630 DWORD reg = ins->dst[0].reg.idx[0].offset;
6631 struct glsl_src_param src0_param;
6632 char coord_mask[6];
6634 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6636 /* Perform the last matrix multiply operation */
6637 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6639 /* Construct the eye-ray vector from w coordinates */
6640 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6641 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6642 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6644 /* Dependent read, not valid with conditional NP2 */
6645 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6646 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6648 /* Sample the texture using the calculated coordinates */
6649 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6650 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6651 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6653 tex_mx->current_row = 0;
6656 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6657 * Apply a fake bump map transform.
6658 * texbem is pshader <= 1.3 only, this saves a few version checks
6660 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6662 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6663 struct glsl_sample_function sample_function;
6664 struct glsl_src_param coord_param;
6665 DWORD sampler_idx;
6666 DWORD mask;
6667 DWORD flags;
6668 char coord_mask[6];
6670 sampler_idx = ins->dst[0].reg.idx[0].offset;
6671 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6672 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6674 /* Dependent read, not valid with conditional NP2 */
6675 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6676 mask = sample_function.coord_mask;
6678 shader_glsl_write_mask_to_str(mask, coord_mask);
6680 /* With projected textures, texbem only divides the static texture coord,
6681 * not the displacement, so we can't let GL handle this. */
6682 if (flags & WINED3D_PSARGS_PROJECTED)
6684 DWORD div_mask=0;
6685 char coord_div_mask[3];
6686 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6688 case WINED3D_TTFF_COUNT1:
6689 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6690 break;
6691 case WINED3D_TTFF_COUNT2:
6692 div_mask = WINED3DSP_WRITEMASK_1;
6693 break;
6694 case WINED3D_TTFF_COUNT3:
6695 div_mask = WINED3DSP_WRITEMASK_2;
6696 break;
6697 case WINED3D_TTFF_COUNT4:
6698 case WINED3D_TTFF_DISABLE:
6699 div_mask = WINED3DSP_WRITEMASK_3;
6700 break;
6702 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6703 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6706 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6708 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6709 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6710 coord_param.param_str, coord_mask);
6712 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6714 struct glsl_src_param luminance_param;
6715 struct glsl_dst_param dst_param;
6717 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6718 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6720 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6721 dst_param.reg_name, dst_param.mask_str,
6722 luminance_param.param_str, sampler_idx, sampler_idx);
6724 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6727 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6729 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6730 struct glsl_src_param src0_param, src1_param;
6732 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6733 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6735 shader_glsl_append_dst(ins->ctx->buffer, ins);
6736 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6737 src0_param.param_str, sampler_idx, src1_param.param_str);
6740 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6741 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6742 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6744 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6745 struct glsl_sample_function sample_function;
6746 struct glsl_src_param src0_param;
6748 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6750 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6751 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6752 "%s.wx", src0_param.reg_name);
6753 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6756 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6757 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6758 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6760 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6761 struct glsl_sample_function sample_function;
6762 struct glsl_src_param src0_param;
6764 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6766 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6767 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6768 "%s.yz", src0_param.reg_name);
6769 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6772 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6773 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6774 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6776 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6777 struct glsl_sample_function sample_function;
6778 struct glsl_src_param src0_param;
6780 /* Dependent read, not valid with conditional NP2 */
6781 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6782 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6784 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6785 "%s", src0_param.param_str);
6786 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6789 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6790 * If any of the first 3 components are < 0, discard this pixel */
6791 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6793 if (ins->ctx->reg_maps->shader_version.major >= 4)
6795 shader_glsl_generate_condition(ins);
6796 shader_addline(ins->ctx->buffer, " discard;\n");
6798 else
6800 struct glsl_dst_param dst_param;
6802 /* The argument is a destination parameter, and no writemasks are allowed */
6803 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6805 /* 2.0 shaders compare all 4 components in texkill. */
6806 if (ins->ctx->reg_maps->shader_version.major >= 2)
6807 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6808 /* 1.x shaders only compare the first 3 components, probably due to
6809 * the nature of the texkill instruction as a tex* instruction, and
6810 * phase, which kills all .w components. Even if all 4 components are
6811 * defined, only the first 3 are used. */
6812 else
6813 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6817 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6818 * dst = dot2(src0, src1) + src2 */
6819 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6821 struct glsl_src_param src0_param;
6822 struct glsl_src_param src1_param;
6823 struct glsl_src_param src2_param;
6824 DWORD write_mask;
6825 unsigned int mask_size;
6827 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6828 mask_size = shader_glsl_get_write_mask_size(write_mask);
6830 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6831 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6832 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6834 if (mask_size > 1) {
6835 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6836 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6837 } else {
6838 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6839 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6843 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6844 const struct wined3d_shader_signature *input_signature,
6845 const struct wined3d_shader_reg_maps *reg_maps,
6846 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6848 unsigned int i;
6850 for (i = 0; i < input_signature->element_count; ++i)
6852 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6853 const char *semantic_name;
6854 UINT semantic_idx;
6855 char reg_mask[6];
6857 /* Unused */
6858 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6859 continue;
6861 semantic_name = input->semantic_name;
6862 semantic_idx = input->semantic_idx;
6863 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6865 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6867 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6869 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6870 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6872 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6874 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6876 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6878 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6879 input->register_idx, reg_mask);
6881 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6883 if (gl_info->supported[ARB_SAMPLE_SHADING])
6884 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6885 input->register_idx, reg_mask);
6886 else
6887 FIXME("ARB_sample_shading is not supported.\n");
6889 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6891 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6892 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6893 input->register_idx, reg_mask);
6894 else
6895 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6897 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6899 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6900 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6901 input->register_idx, reg_mask);
6902 else
6903 FIXME("ARB_viewport_array is not supported.\n");
6905 else
6907 if (input->sysval_semantic)
6908 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6909 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6910 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6911 shader_glsl_shader_input_name(gl_info),
6912 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6915 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6917 if (args->pointsprite)
6918 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6919 shader->u.ps.input_reg_map[input->register_idx]);
6920 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6921 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6922 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6923 needs_legacy_glsl_syntax(gl_info)
6924 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6925 else
6926 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6927 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6929 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6931 if (!semantic_idx)
6932 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6933 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6934 else if (semantic_idx == 1)
6935 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6936 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6937 else
6938 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6939 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6941 else
6943 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6944 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6949 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6951 struct glsl_program_key key;
6953 key.vs_id = entry->vs.id;
6954 key.hs_id = entry->hs.id;
6955 key.ds_id = entry->ds.id;
6956 key.gs_id = entry->gs.id;
6957 key.ps_id = entry->ps.id;
6958 key.cs_id = entry->cs.id;
6960 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6962 ERR("Failed to insert program entry.\n");
6966 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6967 const struct glsl_program_key *key)
6969 struct wine_rb_entry *entry;
6971 entry = wine_rb_get(&priv->program_lookup, key);
6972 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6975 /* Context activation is done by the caller. */
6976 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6977 struct glsl_shader_prog_link *entry)
6979 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6981 GL_EXTCALL(glDeleteProgram(entry->id));
6982 if (entry->vs.id)
6983 list_remove(&entry->vs.shader_entry);
6984 if (entry->hs.id)
6985 list_remove(&entry->hs.shader_entry);
6986 if (entry->ds.id)
6987 list_remove(&entry->ds.shader_entry);
6988 if (entry->gs.id)
6989 list_remove(&entry->gs.shader_entry);
6990 if (entry->ps.id)
6991 list_remove(&entry->ps.shader_entry);
6992 if (entry->cs.id)
6993 list_remove(&entry->cs.shader_entry);
6994 heap_free(entry);
6997 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6998 const struct wined3d_gl_info *gl_info, const DWORD *map,
6999 const struct wined3d_shader_signature *input_signature,
7000 const struct wined3d_shader_reg_maps *reg_maps_in,
7001 const struct wined3d_shader_signature *output_signature,
7002 const struct wined3d_shader_reg_maps *reg_maps_out)
7004 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
7005 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
7006 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7007 unsigned int in_count = vec4_varyings(3, gl_info);
7008 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
7009 DWORD in_idx, *set = NULL;
7010 unsigned int i, j;
7011 char reg_mask[6];
7013 set = heap_calloc(max_varyings, sizeof(*set));
7015 for (i = 0; i < input_signature->element_count; ++i)
7017 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
7019 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
7020 continue;
7022 in_idx = map[input->register_idx];
7023 /* Declared, but not read register */
7024 if (in_idx == ~0u)
7025 continue;
7026 if (in_idx >= max_varyings)
7028 FIXME("More input varyings declared than supported, expect issues.\n");
7029 continue;
7032 if (in_idx == in_count)
7033 string_buffer_sprintf(destination, "gl_FrontColor");
7034 else if (in_idx == in_count + 1)
7035 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7036 else
7037 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
7039 if (!set[in_idx])
7040 set[in_idx] = ~0u;
7042 for (j = 0; j < output_signature->element_count; ++j)
7044 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
7045 DWORD mask;
7047 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
7048 || input->semantic_idx != output->semantic_idx
7049 || strcmp(input->semantic_name, output->semantic_name)
7050 || !(mask = input->mask & output->mask))
7051 continue;
7053 if (set[in_idx] == ~0u)
7054 set[in_idx] = 0;
7055 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
7056 shader_glsl_write_mask_to_str(mask, reg_mask);
7058 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
7059 destination->buffer, reg_mask, output->register_idx, reg_mask);
7063 for (i = 0; i < max_varyings; ++i)
7065 unsigned int size;
7067 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
7068 continue;
7070 if (set[i] == ~0u)
7071 set[i] = 0;
7073 size = 0;
7074 if (!(set[i] & WINED3DSP_WRITEMASK_0))
7075 reg_mask[size++] = 'x';
7076 if (!(set[i] & WINED3DSP_WRITEMASK_1))
7077 reg_mask[size++] = 'y';
7078 if (!(set[i] & WINED3DSP_WRITEMASK_2))
7079 reg_mask[size++] = 'z';
7080 if (!(set[i] & WINED3DSP_WRITEMASK_3))
7081 reg_mask[size++] = 'w';
7082 reg_mask[size] = '\0';
7084 if (i == in_count)
7085 string_buffer_sprintf(destination, "gl_FrontColor");
7086 else if (i == in_count + 1)
7087 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7088 else
7089 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
7091 if (size == 1)
7092 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
7093 else
7094 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
7097 heap_free(set);
7098 string_buffer_release(&priv->string_buffers, destination);
7101 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
7102 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
7103 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
7104 BOOL rasterizer_setup)
7106 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7107 char reg_mask[6];
7108 unsigned int i;
7110 for (i = 0; i < output_signature->element_count; ++i)
7112 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7114 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7115 continue;
7117 if (output->stream_idx)
7118 continue;
7120 if (output->register_idx >= input_count)
7121 continue;
7123 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7125 shader_addline(buffer,
7126 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7127 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7131 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7132 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7134 unsigned int i, clip_or_cull_index;
7135 const char *name;
7136 char reg_mask[6];
7138 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7139 /* Assign consecutive indices starting from 0. */
7140 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7141 for (i = 0; i < 4; ++i)
7143 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7144 continue;
7146 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7147 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7148 name, clip_or_cull_index, element->register_idx, reg_mask);
7149 ++clip_or_cull_index;
7153 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7154 const struct wined3d_gl_info *gl_info, const DWORD *map,
7155 const struct wined3d_shader_signature *input_signature,
7156 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7157 const struct wined3d_shader_signature *output_signature,
7158 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7160 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7161 const char *semantic_name;
7162 unsigned int semantic_idx;
7163 char reg_mask[6];
7164 unsigned int i;
7166 /* First, sort out position and point size system values. */
7167 for (i = 0; i < output_signature->element_count; ++i)
7169 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7171 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7172 continue;
7174 if (output->stream_idx)
7175 continue;
7177 semantic_name = output->semantic_name;
7178 semantic_idx = output->semantic_idx;
7179 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7181 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7183 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7184 reg_mask, output->register_idx, reg_mask);
7186 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7188 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7189 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7191 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7193 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7194 output->register_idx, reg_mask);
7196 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7198 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7199 output->register_idx, reg_mask);
7201 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7203 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7205 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7207 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7209 else if (output->sysval_semantic)
7211 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7215 /* Then, setup the pixel shader input. */
7216 if (reg_maps_out->shader_version.major < 4)
7217 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7218 output_signature, reg_maps_out);
7219 else
7220 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7223 /* Context activation is done by the caller. */
7224 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7225 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7226 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7228 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7229 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7230 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7231 const char *semantic_name;
7232 UINT semantic_idx;
7233 char reg_mask[6];
7234 unsigned int i;
7235 GLuint ret;
7237 string_buffer_clear(buffer);
7239 shader_glsl_add_version_declaration(buffer, gl_info);
7241 if (per_vertex_point_size)
7243 shader_addline(buffer, "uniform struct\n{\n");
7244 shader_addline(buffer, " float size_min;\n");
7245 shader_addline(buffer, " float size_max;\n");
7246 shader_addline(buffer, "} ffp_point;\n");
7249 if (ps_major < 3)
7251 DWORD colors_written_mask[2] = {0};
7252 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7254 if (!legacy_syntax)
7256 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7257 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7258 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7259 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7262 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7264 for (i = 0; i < vs->output_signature.element_count; ++i)
7266 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7267 DWORD write_mask;
7269 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7270 continue;
7272 semantic_name = output->semantic_name;
7273 semantic_idx = output->semantic_idx;
7274 write_mask = output->mask;
7275 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7277 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7279 if (legacy_syntax)
7280 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7281 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7282 else
7283 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7284 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7286 colors_written_mask[semantic_idx] = write_mask;
7288 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7290 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7291 reg_mask, output->register_idx, reg_mask);
7293 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7295 if (semantic_idx < MAX_TEXTURES)
7297 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7298 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7299 semantic_idx, reg_mask, output->register_idx, reg_mask);
7300 texcoords_written_mask[semantic_idx] = write_mask;
7303 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7305 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7306 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7308 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7310 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7311 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7312 output->register_idx, reg_mask[1]);
7316 for (i = 0; i < 2; ++i)
7318 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7320 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7321 if (!i)
7322 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7323 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7324 reg_mask, reg_mask);
7325 else
7326 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7327 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7328 reg_mask, reg_mask);
7331 for (i = 0; i < MAX_TEXTURES; ++i)
7333 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7334 continue;
7336 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7338 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7339 && !texcoords_written_mask[i])
7340 continue;
7342 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7343 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7344 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7348 else
7350 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7352 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7353 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7354 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7355 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7358 shader_addline(buffer, "}\n");
7360 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7361 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7362 shader_glsl_compile(gl_info, ret, buffer->buffer);
7364 return ret;
7367 static void shader_glsl_generate_stream_output_setup(struct shader_glsl_priv *priv,
7368 const struct wined3d_shader *shader, const struct wined3d_stream_output_desc *so_desc)
7370 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7371 unsigned int i;
7373 shader_addline(buffer, "out shader_in_out\n{\n");
7374 for (i = 0; i < so_desc->element_count; ++i)
7376 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7378 if (e->stream_idx)
7380 FIXME("Unhandled stream %u.\n", e->stream_idx);
7381 continue;
7383 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7384 continue;
7386 if (e->component_idx || e->component_count != 4)
7388 if (e->component_count == 1)
7389 shader_addline(buffer, "float");
7390 else
7391 shader_addline(buffer, "vec%u", e->component_count);
7392 shader_addline(buffer, " reg%u_%u_%u;\n",
7393 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
7395 else
7397 shader_addline(buffer, "vec4 reg%u;\n", e->register_idx);
7400 shader_addline(buffer, "} shader_out;\n");
7402 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7403 shader->limits->packed_output);
7404 for (i = 0; i < so_desc->element_count; ++i)
7406 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7408 if (e->stream_idx)
7410 FIXME("Unhandled stream %u.\n", e->stream_idx);
7411 continue;
7413 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7414 continue;
7416 if (e->component_idx || e->component_count != 4)
7418 DWORD write_mask;
7419 char str_mask[6];
7421 write_mask = ((1u << e->component_count) - 1) << e->component_idx;
7422 shader_glsl_write_mask_to_str(write_mask, str_mask);
7423 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7424 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1,
7425 e->register_idx, str_mask);
7427 else
7429 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n",
7430 e->register_idx, e->register_idx);
7433 shader_addline(buffer, "}\n");
7436 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7437 const struct wined3d_shader *shader, unsigned int input_count,
7438 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7440 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7441 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7443 if (rasterizer_setup)
7444 input_count = min(vec4_varyings(4, gl_info), input_count);
7446 if (input_count)
7447 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7449 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7450 prefix, shader->limits->packed_output);
7452 if (rasterizer_setup)
7453 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7454 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7455 else
7456 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7457 &shader->reg_maps, "shader_out", rasterizer_setup);
7459 shader_addline(buffer, "}\n");
7462 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7463 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7464 const char *reg_mask)
7466 if (!constant->sysval_semantic)
7468 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7469 return;
7472 switch (constant->sysval_semantic)
7474 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7475 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7476 case WINED3D_SV_TESS_FACTOR_LINEDET:
7477 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7478 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7479 break;
7480 case WINED3D_SV_TESS_FACTOR_QUADINT:
7481 case WINED3D_SV_TESS_FACTOR_TRIINT:
7482 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7483 break;
7484 default:
7485 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7486 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7490 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7491 const struct wined3d_shader_signature *signature, BOOL input_setup)
7493 unsigned int i, register_count, user_constant_index, user_constant_count;
7495 register_count = user_constant_count = 0;
7496 for (i = 0; i < signature->element_count; ++i)
7498 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7499 register_count = max(constant->register_idx + 1, register_count);
7500 if (!constant->sysval_semantic)
7501 ++user_constant_count;
7504 if (user_constant_count)
7505 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7506 input_setup ? "in" : "out", user_constant_count);
7507 if (input_setup)
7508 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7510 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7511 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7513 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7514 char reg_mask[6];
7516 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7518 if (input_setup)
7519 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7520 else
7521 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7523 shader_addline(buffer, " = ");
7525 if (input_setup)
7526 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7527 else
7528 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7530 shader_addline(buffer, ";\n");
7532 shader_addline(buffer, "}\n");
7535 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7536 const struct wined3d_gl_info *gl_info)
7538 const char *output = get_fragment_output(gl_info);
7540 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7541 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7542 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7543 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7544 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7545 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7548 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7549 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7551 const char *output = get_fragment_output(gl_info);
7553 switch (mode)
7555 case WINED3D_FFP_PS_FOG_OFF:
7556 return;
7558 case WINED3D_FFP_PS_FOG_LINEAR:
7559 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7560 break;
7562 case WINED3D_FFP_PS_FOG_EXP:
7563 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7564 break;
7566 case WINED3D_FFP_PS_FOG_EXP2:
7567 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7568 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7569 break;
7571 default:
7572 ERR("Invalid fog mode %#x.\n", mode);
7573 return;
7576 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7577 output, output);
7580 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7581 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7583 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7584 * flipping all the operators here, just negate the comparison below. */
7585 static const char * const comparison_operator[] =
7587 "", /* WINED3D_CMP_NEVER */
7588 "<", /* WINED3D_CMP_LESS */
7589 "==", /* WINED3D_CMP_EQUAL */
7590 "<=", /* WINED3D_CMP_LESSEQUAL */
7591 ">", /* WINED3D_CMP_GREATER */
7592 "!=", /* WINED3D_CMP_NOTEQUAL */
7593 ">=", /* WINED3D_CMP_GREATEREQUAL */
7594 "" /* WINED3D_CMP_ALWAYS */
7597 if (alpha_func == WINED3D_CMP_ALWAYS)
7598 return;
7600 if (alpha_func != WINED3D_CMP_NEVER)
7601 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7602 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7603 shader_addline(buffer, " discard;\n");
7606 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7607 const struct wined3d_gl_info *gl_info)
7609 if (gl_info->supported[ARB_CULL_DISTANCE])
7610 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7611 if (gl_info->supported[ARB_GPU_SHADER5])
7612 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7613 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7614 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7615 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7616 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7617 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7618 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7619 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7620 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7621 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7622 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7623 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7624 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7625 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7626 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7627 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7628 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7629 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7630 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7631 if (gl_info->supported[ARB_TEXTURE_GATHER])
7632 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7633 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7634 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7635 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7636 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7637 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7638 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7639 if (gl_info->supported[EXT_GPU_SHADER4])
7640 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7641 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7642 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7645 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7646 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7647 struct wined3d_string_buffer_list *string_buffers)
7649 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7650 struct wined3d_string_buffer *src, *assignment;
7651 enum wined3d_data_type dst_data_type;
7652 unsigned int i;
7654 if (output_signature->element_count)
7656 src = string_buffer_get(string_buffers);
7657 assignment = string_buffer_get(string_buffers);
7658 for (i = 0; i < output_signature->element_count; ++i)
7660 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7662 /* register_idx is set to ~0u for non-color outputs. */
7663 if (output->register_idx == ~0u)
7664 continue;
7665 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7667 FIXME("Unhandled component type %#x.\n", output->component_type);
7668 continue;
7670 dst_data_type = component_type_info[output->component_type].data_type;
7671 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7672 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7673 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT);
7674 shader_addline(buffer, "%s;\n", assignment->buffer);
7676 string_buffer_release(string_buffers, src);
7677 string_buffer_release(string_buffers, assignment);
7679 else
7681 DWORD mask = shader->reg_maps.rt_mask;
7683 while (mask)
7685 i = wined3d_bit_scan(&mask);
7686 shader_addline(buffer, "color_out%u = ps_out[%u];\n", i, i);
7691 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7692 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7693 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7695 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7697 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7698 if (reg_maps->shader_version.major < 2)
7699 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7701 if (args->srgb_correction)
7702 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7704 /* SM < 3 does not replace the fog stage. */
7705 if (reg_maps->shader_version.major < 3)
7706 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7708 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7710 if (reg_maps->sample_mask)
7711 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7713 if (!needs_legacy_glsl_syntax(gl_info))
7714 shader_glsl_generate_color_output(buffer, gl_info, shader, string_buffers);
7717 /* Context activation is done by the caller. */
7718 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7719 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7720 const struct wined3d_shader *shader,
7721 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7723 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7724 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7725 const char *prefix = shader_glsl_get_prefix(version->type);
7726 const struct wined3d_gl_info *gl_info = context->gl_info;
7727 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7728 unsigned int i, extra_constants_needed = 0;
7729 struct shader_glsl_ctx_priv priv_ctx;
7730 GLuint shader_id;
7731 DWORD map;
7733 memset(&priv_ctx, 0, sizeof(priv_ctx));
7734 priv_ctx.cur_ps_args = args;
7735 priv_ctx.cur_np2fixup_info = np2fixup_info;
7736 priv_ctx.string_buffers = string_buffers;
7738 shader_glsl_add_version_declaration(buffer, gl_info);
7740 shader_glsl_enable_extensions(buffer, gl_info);
7741 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7742 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7743 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7744 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7745 if (shader_glsl_use_explicit_attrib_location(gl_info))
7746 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7747 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7748 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7749 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7750 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7751 if (gl_info->supported[ARB_SAMPLE_SHADING])
7752 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7753 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7754 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7755 /* The spec says that it doesn't have to be explicitly enabled, but the
7756 * nvidia drivers write a warning if we don't do so. */
7757 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7758 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7760 /* Base Declarations */
7761 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7763 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7765 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7766 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7767 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7768 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7771 /* Declare uniforms for NP2 texcoord fixup:
7772 * This is NOT done inside the loop that declares the texture samplers
7773 * since the NP2 fixup code is currently only used for the GeforceFX
7774 * series and when forcing the ARB_npot extension off. Modern cards just
7775 * skip the code anyway, so put it inside a separate loop. */
7776 if (args->np2_fixup)
7778 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7779 unsigned int cur = 0;
7781 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7782 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7783 * samplerNP2Fixup stores texture dimensions and is updated through
7784 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7786 for (i = 0; i < shader->limits->sampler; ++i)
7788 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7789 continue;
7791 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7793 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7794 continue;
7797 fixup->idx[i] = cur++;
7800 fixup->num_consts = (cur + 1) >> 1;
7801 fixup->active = args->np2_fixup;
7802 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7805 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7807 shader_addline(buffer, "uniform struct\n{\n");
7808 shader_addline(buffer, " vec4 color;\n");
7809 shader_addline(buffer, " float density;\n");
7810 shader_addline(buffer, " float end;\n");
7811 shader_addline(buffer, " float scale;\n");
7812 shader_addline(buffer, "} ffp_fog;\n");
7814 if (needs_legacy_glsl_syntax(gl_info))
7816 if (glsl_is_color_reg_read(shader, 0))
7817 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7818 if (glsl_is_color_reg_read(shader, 1))
7819 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7820 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7821 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7823 else
7825 if (glsl_is_color_reg_read(shader, 0))
7826 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7827 if (glsl_is_color_reg_read(shader, 1))
7828 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7829 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7830 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7831 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7835 if (version->major >= 3)
7837 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7839 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7840 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7841 shader->u.ps.interpolation_mode, version->major >= 4);
7842 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7845 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7847 if (!(map & 1))
7848 continue;
7850 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7852 if (reg_maps->luminanceparams & (1u << i))
7854 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7855 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7856 extra_constants_needed++;
7859 extra_constants_needed++;
7862 if (args->srgb_correction)
7864 shader_addline(buffer, "const vec4 srgb_const0 = ");
7865 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7866 shader_addline(buffer, ";\n");
7867 shader_addline(buffer, "const vec4 srgb_const1 = ");
7868 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7869 shader_addline(buffer, ";\n");
7871 if (reg_maps->vpos || reg_maps->usesdsy)
7873 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7875 ++extra_constants_needed;
7876 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7878 if (reg_maps->vpos)
7880 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7882 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7883 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7884 args->render_offscreen ? "" : "origin_upper_left, ");
7885 else if (!args->render_offscreen)
7886 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7888 shader_addline(buffer, "vec4 vpos;\n");
7892 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7893 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7895 if (!needs_legacy_glsl_syntax(gl_info))
7897 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7899 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7900 if (output_signature->element_count)
7902 for (i = 0; i < output_signature->element_count; ++i)
7904 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7906 if (output->register_idx == ~0u)
7907 continue;
7908 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7910 FIXME("Unhandled component type %#x.\n", output->component_type);
7911 continue;
7913 if (shader_glsl_use_explicit_attrib_location(gl_info))
7914 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7915 shader_addline(buffer, "out %s4 color_out%u;\n",
7916 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7919 else
7921 DWORD mask = reg_maps->rt_mask;
7923 while (mask)
7925 i = wined3d_bit_scan(&mask);
7926 if (shader_glsl_use_explicit_attrib_location(gl_info))
7927 shader_addline(buffer, "layout(location = %u) ", i);
7928 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7933 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7934 FIXME("Insufficient uniforms to run this shader.\n");
7936 if (shader->u.ps.force_early_depth_stencil)
7937 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7939 shader_addline(buffer, "void main()\n{\n");
7941 if (reg_maps->sample_mask)
7942 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7944 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7945 * add approximately 0.5. This causes off-by-one problems as spotted by
7946 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7947 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7948 * causes precision troubles when we just subtract 0.5.
7950 * To deal with that, just floor() the position. This will eliminate the
7951 * fraction on all cards.
7953 * TODO: Test how this behaves with multisampling.
7955 * An advantage of floor is that it works even if the driver doesn't add
7956 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7957 * to return in gl_FragCoord, even though coordinates specify the pixel
7958 * centers instead of the pixel corners. This code will behave correctly
7959 * on drivers that returns integer values. */
7960 if (reg_maps->vpos)
7962 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7963 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7964 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7965 shader_addline(buffer,
7966 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7967 else
7968 shader_addline(buffer,
7969 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7972 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7974 unsigned int i;
7975 WORD map = reg_maps->texcoord;
7977 if (legacy_syntax)
7979 if (glsl_is_color_reg_read(shader, 0))
7980 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7981 if (glsl_is_color_reg_read(shader, 1))
7982 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7985 for (i = 0; map; map >>= 1, ++i)
7987 if (map & 1)
7989 if (args->pointsprite)
7990 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7991 else if (args->texcoords_initialized & (1u << i))
7992 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7993 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7994 else
7995 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7996 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
8000 if (legacy_syntax)
8001 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
8004 /* Pack 3.0 inputs */
8005 if (reg_maps->shader_version.major >= 3)
8006 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
8007 reg_maps->shader_version.major >= 4);
8009 /* Base Shader Body */
8010 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8011 return 0;
8013 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8014 if (reg_maps->shader_version.major < 4)
8015 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
8017 shader_addline(buffer, "}\n");
8019 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8020 TRACE("Compiling shader object %u.\n", shader_id);
8021 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8023 return shader_id;
8026 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
8027 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8028 const struct vs_compile_args *args)
8030 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8031 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8032 unsigned int i;
8034 /* Unpack outputs. */
8035 shader_addline(buffer, "setup_vs_output(vs_out);\n");
8037 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
8038 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
8039 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
8040 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
8042 if (reg_maps->shader_version.major < 3)
8044 if (args->fog_src == VS_FOG_Z)
8045 shader_addline(buffer, "%s = gl_Position.z;\n",
8046 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
8047 else if (!reg_maps->fog)
8048 shader_addline(buffer, "%s = 0.0;\n",
8049 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
8052 /* We always store the clipplanes without y inversion. */
8053 if (args->clip_enabled)
8055 if (legacy_syntax)
8056 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
8057 else
8058 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8059 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
8062 if (args->point_size && !args->per_vertex_point_size)
8063 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
8065 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8066 shader_glsl_fixup_position(buffer, FALSE);
8069 /* Context activation is done by the caller. */
8070 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
8071 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
8073 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8074 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8075 const struct wined3d_shader_version *version = &reg_maps->shader_version;
8076 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8077 const struct wined3d_gl_info *gl_info = context->gl_info;
8078 struct shader_glsl_ctx_priv priv_ctx;
8079 GLuint shader_id;
8080 unsigned int i;
8082 memset(&priv_ctx, 0, sizeof(priv_ctx));
8083 priv_ctx.cur_vs_args = args;
8084 priv_ctx.string_buffers = string_buffers;
8086 shader_glsl_add_version_declaration(buffer, gl_info);
8088 shader_glsl_enable_extensions(buffer, gl_info);
8089 if (gl_info->supported[ARB_DRAW_INSTANCED])
8090 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8091 if (shader_glsl_use_explicit_attrib_location(gl_info))
8092 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8094 /* Base Declarations */
8095 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8097 for (i = 0; i < shader->input_signature.element_count; ++i)
8098 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8100 if (args->point_size && !args->per_vertex_point_size)
8102 shader_addline(buffer, "uniform struct\n{\n");
8103 shader_addline(buffer, " float size;\n");
8104 shader_addline(buffer, " float size_min;\n");
8105 shader_addline(buffer, " float size_max;\n");
8106 shader_addline(buffer, "} ffp_point;\n");
8109 if (!needs_legacy_glsl_syntax(gl_info))
8111 if (args->clip_enabled)
8112 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8114 if (version->major < 3)
8116 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8117 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8118 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8119 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8123 if (version->major < 4)
8124 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8126 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8127 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8129 if (reg_maps->shader_version.major >= 4)
8130 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8131 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8133 shader_addline(buffer, "void main()\n{\n");
8135 if (reg_maps->input_rel_addressing)
8137 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8138 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8139 for (i = 0; i < shader->input_signature.element_count; ++i)
8141 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8142 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8146 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8147 return 0;
8149 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8150 if (reg_maps->shader_version.major < 4)
8151 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8153 shader_addline(buffer, "}\n");
8155 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8156 TRACE("Compiling shader object %u.\n", shader_id);
8157 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8159 return shader_id;
8162 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8163 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8165 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8166 char reg_mask[6];
8167 unsigned int i;
8169 for (i = 0; i < output_signature->element_count; ++i)
8171 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8173 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8174 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8175 output->register_idx, reg_mask, output->register_idx, reg_mask);
8179 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8180 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8181 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8182 const char *phase_name, unsigned phase_idx)
8184 unsigned int i;
8185 HRESULT hr;
8187 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8188 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8189 for (i = 0; i < phase->temporary_count; ++i)
8190 shader_addline(buffer, "vec4 R%u;\n", i);
8191 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8192 shader_addline(buffer, "}\n");
8193 return hr;
8196 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8197 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8199 if (phase->instance_count)
8201 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8202 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8203 shader_addline(buffer, "}\n");
8205 else
8207 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8211 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
8212 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8214 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8215 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8216 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8217 const struct wined3d_gl_info *gl_info = context->gl_info;
8218 const struct wined3d_hull_shader *hs = &shader->u.hs;
8219 const struct wined3d_shader_phase *phase;
8220 struct shader_glsl_ctx_priv priv_ctx;
8221 GLuint shader_id;
8222 unsigned int i;
8224 memset(&priv_ctx, 0, sizeof(priv_ctx));
8225 priv_ctx.string_buffers = string_buffers;
8227 shader_glsl_add_version_declaration(buffer, gl_info);
8229 shader_glsl_enable_extensions(buffer, gl_info);
8230 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8232 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8234 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8236 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8237 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8239 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8241 if (hs->phases.control_point)
8243 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8244 shader->limits->packed_output);
8245 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8246 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8247 shader_addline(buffer, "}\n");
8250 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8251 if ((phase = hs->phases.control_point))
8253 for (i = 0; i < phase->temporary_count; ++i)
8254 shader_addline(buffer, "vec4 R%u;\n", i);
8255 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8256 return 0;
8257 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8259 else
8261 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8263 shader_addline(buffer, "}\n");
8265 for (i = 0; i < hs->phases.fork_count; ++i)
8267 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8268 &hs->phases.fork[i], "fork", i)))
8269 return 0;
8272 for (i = 0; i < hs->phases.join_count; ++i)
8274 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8275 &hs->phases.join[i], "join", i)))
8276 return 0;
8279 shader_addline(buffer, "void main()\n{\n");
8280 shader_addline(buffer, "hs_control_point_phase();\n");
8281 if (reg_maps->vocp)
8282 shader_addline(buffer, "barrier();\n");
8283 for (i = 0; i < hs->phases.fork_count; ++i)
8284 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8285 for (i = 0; i < hs->phases.join_count; ++i)
8286 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8287 shader_addline(buffer, "setup_patch_constant_output();\n");
8288 shader_addline(buffer, "}\n");
8290 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8291 TRACE("Compiling shader object %u.\n", shader_id);
8292 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8294 return shader_id;
8297 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8298 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8299 const struct ds_compile_args *args)
8301 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8303 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8304 shader_glsl_fixup_position(buffer, FALSE);
8307 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
8308 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8310 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8311 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8312 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8313 const struct wined3d_gl_info *gl_info = context->gl_info;
8314 struct shader_glsl_ctx_priv priv_ctx;
8315 GLuint shader_id;
8317 memset(&priv_ctx, 0, sizeof(priv_ctx));
8318 priv_ctx.cur_ds_args = args;
8319 priv_ctx.string_buffers = string_buffers;
8321 shader_glsl_add_version_declaration(buffer, gl_info);
8323 shader_glsl_enable_extensions(buffer, gl_info);
8324 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8326 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8328 shader_addline(buffer, "layout(");
8329 switch (shader->u.ds.tessellator_domain)
8331 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8332 shader_addline(buffer, "isolines");
8333 break;
8334 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8335 shader_addline(buffer, "quads");
8336 break;
8337 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8338 shader_addline(buffer, "triangles");
8339 break;
8341 switch (args->tessellator_output_primitive)
8343 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8344 if (args->render_offscreen)
8345 shader_addline(buffer, ", ccw");
8346 else
8347 shader_addline(buffer, ", cw");
8348 break;
8349 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8350 if (args->render_offscreen)
8351 shader_addline(buffer, ", cw");
8352 else
8353 shader_addline(buffer, ", ccw");
8354 break;
8355 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8356 shader_addline(buffer, ", point_mode");
8357 break;
8358 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8359 break;
8361 switch (args->tessellator_partitioning)
8363 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8364 shader_addline(buffer, ", fractional_odd_spacing");
8365 break;
8366 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8367 shader_addline(buffer, ", fractional_even_spacing");
8368 break;
8369 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8370 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8371 shader_addline(buffer, ", equal_spacing");
8372 break;
8374 shader_addline(buffer, ") in;\n");
8376 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8378 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8379 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8381 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8382 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8383 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8385 shader_addline(buffer, "void main()\n{\n");
8386 shader_addline(buffer, "setup_patch_constant_input();\n");
8388 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8389 return 0;
8391 shader_addline(buffer, "}\n");
8393 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8394 TRACE("Compiling shader object %u.\n", shader_id);
8395 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8397 return shader_id;
8400 /* Context activation is done by the caller. */
8401 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
8402 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8404 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8405 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8406 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8407 const struct wined3d_gl_info *gl_info = context->gl_info;
8408 const struct wined3d_shader_signature_element *output;
8409 enum wined3d_primitive_type primitive_type;
8410 struct shader_glsl_ctx_priv priv_ctx;
8411 unsigned int max_vertices;
8412 unsigned int i, j;
8413 GLuint shader_id;
8415 memset(&priv_ctx, 0, sizeof(priv_ctx));
8416 priv_ctx.string_buffers = string_buffers;
8418 shader_glsl_add_version_declaration(buffer, gl_info);
8420 shader_glsl_enable_extensions(buffer, gl_info);
8422 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8424 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8425 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8426 if (shader->u.gs.instance_count > 1)
8427 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8428 shader_addline(buffer, ") in;\n");
8430 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8431 if (!(max_vertices = shader->u.gs.vertices_out))
8433 switch (args->primitive_type)
8435 case WINED3D_PT_POINTLIST:
8436 max_vertices = 1;
8437 break;
8438 case WINED3D_PT_LINELIST:
8439 max_vertices = 2;
8440 break;
8441 case WINED3D_PT_TRIANGLELIST:
8442 max_vertices = 3;
8443 break;
8444 default:
8445 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8446 break;
8449 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8450 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8451 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8453 if (!gl_info->supported[ARB_CLIP_CONTROL])
8455 shader_addline(buffer, "uniform vec4 pos_fixup");
8456 if (reg_maps->viewport_array)
8457 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8458 shader_addline(buffer, ";\n");
8461 if (is_rasterization_disabled(shader))
8463 shader_glsl_generate_stream_output_setup(priv, shader, &shader->u.gs.so_desc);
8465 else
8467 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8468 gl_info, TRUE, args->interpolation_mode);
8471 shader_addline(buffer, "void main()\n{\n");
8472 if (shader->function)
8474 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8475 return 0;
8477 else
8479 for (i = 0; i < max_vertices; ++i)
8481 for (j = 0; j < shader->output_signature.element_count; ++j)
8483 output = &shader->output_signature.elements[j];
8484 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8485 output->register_idx, i, output->register_idx);
8487 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8488 if (!gl_info->supported[ARB_CLIP_CONTROL])
8489 shader_glsl_fixup_position(buffer, FALSE);
8490 shader_addline(buffer, "EmitVertex();\n");
8493 shader_addline(buffer, "}\n");
8495 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8496 TRACE("Compiling shader object %u.\n", shader_id);
8497 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8499 return shader_id;
8502 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8504 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8505 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8506 struct wined3d_string_buffer *buffer = ctx->buffer;
8507 const struct wined3d_shader *shader = ctx->shader;
8509 switch (shader->reg_maps.shader_version.type)
8511 case WINED3D_SHADER_TYPE_PIXEL:
8512 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8513 break;
8514 case WINED3D_SHADER_TYPE_VERTEX:
8515 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8516 break;
8517 case WINED3D_SHADER_TYPE_DOMAIN:
8518 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8519 break;
8520 case WINED3D_SHADER_TYPE_GEOMETRY:
8521 case WINED3D_SHADER_TYPE_COMPUTE:
8522 break;
8523 default:
8524 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8525 break;
8529 /* Context activation is done by the caller. */
8530 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8531 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8532 const struct wined3d_shader *shader)
8534 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8535 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8536 const struct wined3d_gl_info *gl_info = context->gl_info;
8537 struct shader_glsl_ctx_priv priv_ctx;
8538 GLuint shader_id;
8539 unsigned int i;
8541 memset(&priv_ctx, 0, sizeof(priv_ctx));
8542 priv_ctx.string_buffers = string_buffers;
8544 shader_glsl_add_version_declaration(buffer, gl_info);
8546 shader_glsl_enable_extensions(buffer, gl_info);
8547 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8549 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8551 for (i = 0; i < reg_maps->tgsm_count; ++i)
8553 if (reg_maps->tgsm[i].size)
8554 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8557 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8558 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8560 shader_addline(buffer, "void main()\n{\n");
8561 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8562 shader_addline(buffer, "}\n");
8564 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8565 TRACE("Compiling shader object %u.\n", shader_id);
8566 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8568 return shader_id;
8571 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8572 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8573 struct wined3d_shader *shader,
8574 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8576 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8577 struct glsl_shader_private *shader_data;
8578 struct ps_np2fixup_info *np2fixup;
8579 UINT i;
8580 DWORD new_size;
8581 GLuint ret;
8583 if (!shader->backend_data)
8585 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8587 ERR("Failed to allocate backend data.\n");
8588 return 0;
8591 shader_data = shader->backend_data;
8592 gl_shaders = shader_data->gl_shaders.ps;
8594 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8595 * so a linear search is more performant than a hashmap or a binary search
8596 * (cache coherency etc)
8598 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8600 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8602 if (args->np2_fixup)
8603 *np2fixup_info = &gl_shaders[i].np2fixup;
8604 return gl_shaders[i].id;
8608 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8609 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8611 if (shader_data->num_gl_shaders)
8613 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8614 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8616 else
8618 new_array = heap_alloc(sizeof(*gl_shaders));
8619 new_size = 1;
8622 if(!new_array) {
8623 ERR("Out of memory\n");
8624 return 0;
8626 shader_data->gl_shaders.ps = new_array;
8627 shader_data->shader_array_size = new_size;
8628 gl_shaders = new_array;
8631 gl_shaders[shader_data->num_gl_shaders].args = *args;
8633 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8634 memset(np2fixup, 0, sizeof(*np2fixup));
8635 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8637 pixelshader_update_resource_types(shader, args->tex_types);
8639 string_buffer_clear(buffer);
8640 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8641 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8643 return ret;
8646 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8647 const DWORD use_map)
8649 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8650 return FALSE;
8651 if ((stored->clip_enabled) != new->clip_enabled)
8652 return FALSE;
8653 if (stored->point_size != new->point_size)
8654 return FALSE;
8655 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8656 return FALSE;
8657 if (stored->flatshading != new->flatshading)
8658 return FALSE;
8659 if (stored->next_shader_type != new->next_shader_type)
8660 return FALSE;
8661 if (stored->next_shader_input_count != new->next_shader_input_count)
8662 return FALSE;
8663 if (stored->fog_src != new->fog_src)
8664 return FALSE;
8665 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8668 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8669 struct wined3d_shader *shader, const struct vs_compile_args *args)
8671 UINT i;
8672 DWORD new_size;
8673 DWORD use_map = context->stream_info.use_map;
8674 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8675 struct glsl_shader_private *shader_data;
8676 GLuint ret;
8678 if (!shader->backend_data)
8680 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8682 ERR("Failed to allocate backend data.\n");
8683 return 0;
8686 shader_data = shader->backend_data;
8687 gl_shaders = shader_data->gl_shaders.vs;
8689 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8690 * so a linear search is more performant than a hashmap or a binary search
8691 * (cache coherency etc)
8693 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8695 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8696 return gl_shaders[i].id;
8699 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8701 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8703 if (shader_data->num_gl_shaders)
8705 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8706 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8708 else
8710 new_array = heap_alloc(sizeof(*gl_shaders));
8711 new_size = 1;
8714 if(!new_array) {
8715 ERR("Out of memory\n");
8716 return 0;
8718 shader_data->gl_shaders.vs = new_array;
8719 shader_data->shader_array_size = new_size;
8720 gl_shaders = new_array;
8723 gl_shaders[shader_data->num_gl_shaders].args = *args;
8725 string_buffer_clear(&priv->shader_buffer);
8726 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8727 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8729 return ret;
8732 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8733 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8735 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8736 struct glsl_shader_private *shader_data;
8737 unsigned int new_size;
8738 GLuint ret;
8740 if (!shader->backend_data)
8742 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8744 ERR("Failed to allocate backend data.\n");
8745 return 0;
8748 shader_data = shader->backend_data;
8749 gl_shaders = shader_data->gl_shaders.hs;
8751 if (shader_data->num_gl_shaders > 0)
8753 assert(shader_data->num_gl_shaders == 1);
8754 return gl_shaders[0].id;
8757 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8759 assert(!shader_data->gl_shaders.hs);
8760 new_size = 1;
8761 if (!(new_array = heap_alloc(sizeof(*new_array))))
8763 ERR("Failed to allocate GL shaders array.\n");
8764 return 0;
8766 shader_data->gl_shaders.hs = new_array;
8767 shader_data->shader_array_size = new_size;
8768 gl_shaders = new_array;
8770 string_buffer_clear(&priv->shader_buffer);
8771 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8772 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8774 return ret;
8777 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8778 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8780 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8781 struct glsl_shader_private *shader_data;
8782 unsigned int i, new_size;
8783 GLuint ret;
8785 if (!shader->backend_data)
8787 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8789 ERR("Failed to allocate backend data.\n");
8790 return 0;
8793 shader_data = shader->backend_data;
8794 gl_shaders = shader_data->gl_shaders.ds;
8796 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8798 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8799 return gl_shaders[i].id;
8802 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8804 if (shader_data->num_gl_shaders)
8806 new_size = shader_data->shader_array_size + 1;
8807 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8809 else
8811 new_array = heap_alloc(sizeof(*new_array));
8812 new_size = 1;
8815 if (!new_array)
8817 ERR("Failed to allocate GL shaders array.\n");
8818 return 0;
8820 shader_data->gl_shaders.ds = new_array;
8821 shader_data->shader_array_size = new_size;
8822 gl_shaders = new_array;
8824 string_buffer_clear(&priv->shader_buffer);
8825 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8826 gl_shaders[shader_data->num_gl_shaders].args = *args;
8827 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8829 return ret;
8832 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8833 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8835 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8836 struct glsl_shader_private *shader_data;
8837 unsigned int i, new_size;
8838 GLuint ret;
8840 if (!shader->backend_data)
8842 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8844 ERR("Failed to allocate backend data.\n");
8845 return 0;
8848 shader_data = shader->backend_data;
8849 gl_shaders = shader_data->gl_shaders.gs;
8851 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8853 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8854 return gl_shaders[i].id;
8857 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8859 if (shader_data->num_gl_shaders)
8861 new_size = shader_data->shader_array_size + 1;
8862 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8864 else
8866 new_array = heap_alloc(sizeof(*new_array));
8867 new_size = 1;
8870 if (!new_array)
8872 ERR("Failed to allocate GL shaders array.\n");
8873 return 0;
8875 shader_data->gl_shaders.gs = new_array;
8876 shader_data->shader_array_size = new_size;
8877 gl_shaders = new_array;
8879 string_buffer_clear(&priv->shader_buffer);
8880 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8881 gl_shaders[shader_data->num_gl_shaders].args = *args;
8882 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8884 return ret;
8887 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8889 switch (mcs)
8891 case WINED3D_MCS_MATERIAL:
8892 return material;
8893 case WINED3D_MCS_COLOR1:
8894 return "ffp_attrib_diffuse";
8895 case WINED3D_MCS_COLOR2:
8896 return "ffp_attrib_specular";
8897 default:
8898 ERR("Invalid material color source %#x.\n", mcs);
8899 return "<invalid>";
8903 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8904 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8906 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8907 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8908 if (settings->localviewer)
8909 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8910 else
8911 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8912 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8913 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8916 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8917 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8919 const char *diffuse, *specular, *emissive, *ambient;
8920 unsigned int i, idx;
8922 if (!settings->lighting)
8924 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8925 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8926 return;
8929 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8930 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8931 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8932 shader_addline(buffer, "vec3 dir, dst;\n");
8933 shader_addline(buffer, "float att, t;\n");
8935 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8936 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8937 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8938 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8940 idx = 0;
8941 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8943 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8944 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8945 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8946 shader_addline(buffer, "dst.x = 1.0;\n");
8947 if (legacy_lighting)
8949 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8950 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8951 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8953 else
8955 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8957 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8958 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8959 if (!legacy_lighting)
8960 shader_addline(buffer, "att = 1.0 / att;\n");
8961 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8962 if (!settings->normal)
8964 shader_addline(buffer, "}\n");
8965 continue;
8967 shader_addline(buffer, "dir = normalize(dir);\n");
8968 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8969 shader_addline(buffer, "}\n");
8972 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8974 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8975 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8976 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8977 shader_addline(buffer, "dst.x = 1.0;\n");
8978 if (legacy_lighting)
8980 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8981 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8982 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8984 else
8986 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8988 shader_addline(buffer, "dir = normalize(dir);\n");
8989 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8990 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8991 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8992 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8993 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8994 idx, idx, idx, idx);
8995 if (legacy_lighting)
8996 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8997 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8998 idx, idx, idx);
8999 else
9000 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
9001 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
9002 idx, idx, idx);
9003 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
9004 if (!settings->normal)
9006 shader_addline(buffer, "}\n");
9007 continue;
9009 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
9010 shader_addline(buffer, "}\n");
9013 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
9015 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
9016 if (!settings->normal)
9017 continue;
9018 shader_addline(buffer, "att = 1.0;\n");
9019 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
9020 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
9023 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
9025 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
9026 if (!settings->normal)
9027 continue;
9028 shader_addline(buffer, "att = 1.0;\n");
9029 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
9030 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
9033 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
9034 ambient, diffuse, emissive);
9035 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
9036 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
9039 /* Context activation is done by the caller. */
9040 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
9041 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
9043 static const struct attrib_info
9045 const char type[6];
9046 const char name[24];
9048 attrib_info[] =
9050 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
9051 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
9052 /* TODO: Indexed vertex blending */
9053 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
9054 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
9055 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
9056 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
9057 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
9059 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9060 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9061 BOOL output_legacy_fogcoord = legacy_syntax;
9062 BOOL legacy_lighting = priv->legacy_lighting;
9063 GLuint shader_obj;
9064 unsigned int i;
9066 string_buffer_clear(buffer);
9068 shader_glsl_add_version_declaration(buffer, gl_info);
9070 if (shader_glsl_use_explicit_attrib_location(gl_info))
9071 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9073 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
9075 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9077 if (shader_glsl_use_explicit_attrib_location(gl_info))
9078 shader_addline(buffer, "layout(location = %u) ", i);
9079 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9081 shader_addline(buffer, "\n");
9083 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9084 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9085 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9086 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
9088 shader_addline(buffer, "uniform struct\n{\n");
9089 shader_addline(buffer, " vec4 emissive;\n");
9090 shader_addline(buffer, " vec4 ambient;\n");
9091 shader_addline(buffer, " vec4 diffuse;\n");
9092 shader_addline(buffer, " vec4 specular;\n");
9093 shader_addline(buffer, " float shininess;\n");
9094 shader_addline(buffer, "} ffp_material;\n");
9096 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9097 shader_addline(buffer, "uniform struct\n{\n");
9098 shader_addline(buffer, " vec4 diffuse;\n");
9099 shader_addline(buffer, " vec4 specular;\n");
9100 shader_addline(buffer, " vec4 ambient;\n");
9101 shader_addline(buffer, " vec4 position;\n");
9102 shader_addline(buffer, " vec3 direction;\n");
9103 shader_addline(buffer, " float range;\n");
9104 shader_addline(buffer, " float falloff;\n");
9105 shader_addline(buffer, " float c_att;\n");
9106 shader_addline(buffer, " float l_att;\n");
9107 shader_addline(buffer, " float q_att;\n");
9108 shader_addline(buffer, " float cos_htheta;\n");
9109 shader_addline(buffer, " float cos_hphi;\n");
9110 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
9112 if (settings->point_size)
9114 shader_addline(buffer, "uniform struct\n{\n");
9115 shader_addline(buffer, " float size;\n");
9116 shader_addline(buffer, " float size_min;\n");
9117 shader_addline(buffer, " float size_max;\n");
9118 shader_addline(buffer, " float c_att;\n");
9119 shader_addline(buffer, " float l_att;\n");
9120 shader_addline(buffer, " float q_att;\n");
9121 shader_addline(buffer, "} ffp_point;\n");
9124 if (legacy_syntax)
9126 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9127 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9128 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9129 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9131 else
9133 if (settings->clipping)
9134 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9136 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9137 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9138 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9139 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9142 shader_addline(buffer, "\nvoid main()\n{\n");
9143 shader_addline(buffer, "float m;\n");
9144 shader_addline(buffer, "vec3 r;\n");
9146 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9148 if (attrib_info[i].name[0])
9149 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9150 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9152 for (i = 0; i < MAX_TEXTURES; ++i)
9154 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9155 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9156 && settings->texcoords & (1u << i))
9157 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9160 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9162 if (settings->transformed)
9164 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9165 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9166 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9168 else
9170 for (i = 0; i < settings->vertexblends; ++i)
9171 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9173 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9174 for (i = 0; i < settings->vertexblends + 1; ++i)
9175 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9177 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9178 if (settings->clipping)
9180 if (legacy_syntax)
9181 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9182 else
9183 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9184 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9186 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9189 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9190 if (settings->normal)
9192 if (!settings->vertexblends)
9194 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9196 else
9198 for (i = 0; i < settings->vertexblends + 1; ++i)
9199 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9202 if (settings->normalize)
9203 shader_addline(buffer, "normal = normalize(normal);\n");
9206 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9207 if (legacy_syntax)
9209 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9210 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9212 else
9214 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9215 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9218 for (i = 0; i < MAX_TEXTURES; ++i)
9220 BOOL output_legacy_texcoord = legacy_syntax;
9222 switch (settings->texgen[i] & 0xffff0000)
9224 case WINED3DTSS_TCI_PASSTHRU:
9225 if (settings->texcoords & (1u << i))
9226 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9227 i, i, i);
9228 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
9229 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9230 else
9231 output_legacy_texcoord = FALSE;
9232 break;
9234 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9235 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9236 break;
9238 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9239 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9240 break;
9242 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9243 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9244 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9245 break;
9247 case WINED3DTSS_TCI_SPHEREMAP:
9248 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9249 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9250 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9251 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9252 break;
9254 default:
9255 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9256 break;
9258 if (output_legacy_texcoord)
9259 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9262 switch (settings->fog_mode)
9264 case WINED3D_FFP_VS_FOG_OFF:
9265 output_legacy_fogcoord = FALSE;
9266 break;
9268 case WINED3D_FFP_VS_FOG_FOGCOORD:
9269 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9270 break;
9272 case WINED3D_FFP_VS_FOG_RANGE:
9273 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9274 break;
9276 case WINED3D_FFP_VS_FOG_DEPTH:
9277 if (settings->ortho_fog)
9279 if (gl_info->supported[ARB_CLIP_CONTROL])
9280 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9281 else
9282 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9283 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9285 else if (settings->transformed)
9287 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9289 else
9291 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9293 break;
9295 default:
9296 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9297 break;
9299 if (output_legacy_fogcoord)
9300 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9302 if (settings->point_size)
9304 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9305 " + ffp_point.l_att * length(ec_pos.xyz)"
9306 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9307 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9308 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9311 shader_addline(buffer, "}\n");
9313 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9314 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9316 return shader_obj;
9319 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9320 DWORD argnum, unsigned int stage, DWORD arg)
9322 const char *ret;
9324 if (arg == ARG_UNUSED)
9325 return "<unused arg>";
9327 switch (arg & WINED3DTA_SELECTMASK)
9329 case WINED3DTA_DIFFUSE:
9330 ret = "ffp_varying_diffuse";
9331 break;
9333 case WINED3DTA_CURRENT:
9334 ret = "ret";
9335 break;
9337 case WINED3DTA_TEXTURE:
9338 switch (stage)
9340 case 0: ret = "tex0"; break;
9341 case 1: ret = "tex1"; break;
9342 case 2: ret = "tex2"; break;
9343 case 3: ret = "tex3"; break;
9344 case 4: ret = "tex4"; break;
9345 case 5: ret = "tex5"; break;
9346 case 6: ret = "tex6"; break;
9347 case 7: ret = "tex7"; break;
9348 default:
9349 ret = "<invalid texture>";
9350 break;
9352 break;
9354 case WINED3DTA_TFACTOR:
9355 ret = "tex_factor";
9356 break;
9358 case WINED3DTA_SPECULAR:
9359 ret = "ffp_varying_specular";
9360 break;
9362 case WINED3DTA_TEMP:
9363 ret = "temp_reg";
9364 break;
9366 case WINED3DTA_CONSTANT:
9367 switch (stage)
9369 case 0: ret = "tss_const0"; break;
9370 case 1: ret = "tss_const1"; break;
9371 case 2: ret = "tss_const2"; break;
9372 case 3: ret = "tss_const3"; break;
9373 case 4: ret = "tss_const4"; break;
9374 case 5: ret = "tss_const5"; break;
9375 case 6: ret = "tss_const6"; break;
9376 case 7: ret = "tss_const7"; break;
9377 default:
9378 ret = "<invalid constant>";
9379 break;
9381 break;
9383 default:
9384 return "<unhandled arg>";
9387 if (arg & WINED3DTA_COMPLEMENT)
9389 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9390 if (argnum == 0)
9391 ret = "arg0";
9392 else if (argnum == 1)
9393 ret = "arg1";
9394 else if (argnum == 2)
9395 ret = "arg2";
9398 if (arg & WINED3DTA_ALPHAREPLICATE)
9400 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9401 if (argnum == 0)
9402 ret = "arg0";
9403 else if (argnum == 1)
9404 ret = "arg1";
9405 else if (argnum == 2)
9406 ret = "arg2";
9409 return ret;
9412 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9413 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9415 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9417 if (color && alpha)
9418 dstmask = "";
9419 else if (color)
9420 dstmask = ".xyz";
9421 else
9422 dstmask = ".w";
9424 dstreg = tmp_dst ? "temp_reg" : "ret";
9426 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9427 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9428 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9430 switch (op)
9432 case WINED3D_TOP_DISABLE:
9433 break;
9435 case WINED3D_TOP_SELECT_ARG1:
9436 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9437 break;
9439 case WINED3D_TOP_SELECT_ARG2:
9440 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9441 break;
9443 case WINED3D_TOP_MODULATE:
9444 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9445 break;
9447 case WINED3D_TOP_MODULATE_4X:
9448 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9449 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9450 break;
9452 case WINED3D_TOP_MODULATE_2X:
9453 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9454 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9455 break;
9457 case WINED3D_TOP_ADD:
9458 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9459 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9460 break;
9462 case WINED3D_TOP_ADD_SIGNED:
9463 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9464 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9465 break;
9467 case WINED3D_TOP_ADD_SIGNED_2X:
9468 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9469 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9470 break;
9472 case WINED3D_TOP_SUBTRACT:
9473 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9474 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9475 break;
9477 case WINED3D_TOP_ADD_SMOOTH:
9478 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9479 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9480 break;
9482 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9483 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9484 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9485 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9486 break;
9488 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9489 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9490 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9491 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9492 break;
9494 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9495 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9496 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9497 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9498 break;
9500 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9501 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9502 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9503 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9504 break;
9506 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9507 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9508 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9509 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9510 break;
9512 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9513 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9514 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9515 break;
9517 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9518 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9519 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9520 break;
9522 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9523 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9524 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9525 break;
9526 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9527 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9528 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9529 break;
9531 case WINED3D_TOP_BUMPENVMAP:
9532 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9533 /* These are handled in the first pass, nothing to do. */
9534 break;
9536 case WINED3D_TOP_DOTPRODUCT3:
9537 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9538 dstreg, dstmask, arg1, arg2, dstmask);
9539 break;
9541 case WINED3D_TOP_MULTIPLY_ADD:
9542 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9543 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9544 break;
9546 case WINED3D_TOP_LERP:
9547 /* MSDN isn't quite right here. */
9548 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9549 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9550 break;
9552 default:
9553 FIXME("Unhandled operation %#x.\n", op);
9554 break;
9558 /* Context activation is done by the caller. */
9559 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9560 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9562 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9563 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9564 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9565 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9566 const struct wined3d_gl_info *gl_info = context->gl_info;
9567 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9568 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9569 UINT lowest_disabled_stage;
9570 GLuint shader_id;
9571 DWORD arg0, arg1, arg2;
9572 unsigned int stage;
9574 string_buffer_clear(buffer);
9576 /* Find out which textures are read */
9577 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9579 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9580 break;
9582 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9583 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9584 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9586 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9587 || (stage == 0 && settings->color_key_enabled))
9588 tex_map |= 1u << stage;
9589 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9590 tfactor_used = TRUE;
9591 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9592 tempreg_used = TRUE;
9593 if (settings->op[stage].tmp_dst)
9594 tempreg_used = TRUE;
9595 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9596 tss_const_map |= 1u << stage;
9598 switch (settings->op[stage].cop)
9600 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9601 lum_map |= 1u << stage;
9602 /* fall through */
9603 case WINED3D_TOP_BUMPENVMAP:
9604 bump_map |= 1u << stage;
9605 /* fall through */
9606 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9607 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9608 tex_map |= 1u << stage;
9609 break;
9611 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9612 tfactor_used = TRUE;
9613 break;
9615 default:
9616 break;
9619 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9620 continue;
9622 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9623 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9624 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9626 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9627 tex_map |= 1u << stage;
9628 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9629 tfactor_used = TRUE;
9630 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9631 tempreg_used = TRUE;
9632 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9633 tss_const_map |= 1u << stage;
9635 lowest_disabled_stage = stage;
9637 shader_glsl_add_version_declaration(buffer, gl_info);
9639 if (shader_glsl_use_explicit_attrib_location(gl_info))
9640 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9641 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9642 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9643 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9644 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9646 if (!needs_legacy_glsl_syntax(gl_info))
9648 shader_addline(buffer, "vec4 ps_out[1];\n");
9649 if (shader_glsl_use_explicit_attrib_location(gl_info))
9650 shader_addline(buffer, "layout(location = 0) ");
9651 shader_addline(buffer, "out vec4 color_out0;\n");
9654 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9655 shader_addline(buffer, "vec4 ret;\n");
9656 if (tempreg_used || settings->sRGB_write)
9657 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9658 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9660 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9662 const char *sampler_type;
9664 if (tss_const_map & (1u << stage))
9665 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9667 if (!(tex_map & (1u << stage)))
9668 continue;
9670 switch (settings->op[stage].tex_type)
9672 case WINED3D_GL_RES_TYPE_TEX_1D:
9673 sampler_type = "1D";
9674 break;
9675 case WINED3D_GL_RES_TYPE_TEX_2D:
9676 sampler_type = "2D";
9677 break;
9678 case WINED3D_GL_RES_TYPE_TEX_3D:
9679 sampler_type = "3D";
9680 break;
9681 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9682 sampler_type = "Cube";
9683 break;
9684 case WINED3D_GL_RES_TYPE_TEX_RECT:
9685 sampler_type = "2DRect";
9686 break;
9687 default:
9688 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9689 sampler_type = NULL;
9690 break;
9692 if (sampler_type)
9694 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9695 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9696 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9699 shader_addline(buffer, "vec4 tex%u;\n", stage);
9701 if (!(bump_map & (1u << stage)))
9702 continue;
9703 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9705 if (!(lum_map & (1u << stage)))
9706 continue;
9707 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9708 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9710 if (tfactor_used)
9711 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9712 if (settings->color_key_enabled)
9713 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9714 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9716 if (settings->sRGB_write)
9718 shader_addline(buffer, "const vec4 srgb_const0 = ");
9719 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9720 shader_addline(buffer, ";\n");
9721 shader_addline(buffer, "const vec4 srgb_const1 = ");
9722 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9723 shader_addline(buffer, ";\n");
9726 shader_addline(buffer, "uniform struct\n{\n");
9727 shader_addline(buffer, " vec4 color;\n");
9728 shader_addline(buffer, " float density;\n");
9729 shader_addline(buffer, " float end;\n");
9730 shader_addline(buffer, " float scale;\n");
9731 shader_addline(buffer, "} ffp_fog;\n");
9733 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9734 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9736 if (legacy_syntax)
9738 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9739 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9740 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9741 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9742 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9744 else
9746 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9747 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9748 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9749 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9750 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9753 shader_addline(buffer, "void main()\n{\n");
9755 if (legacy_syntax)
9757 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9758 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9761 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9763 if (tex_map & (1u << stage))
9765 if (settings->pointsprite)
9766 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9767 else if (settings->texcoords_initialized & (1u << stage))
9768 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9769 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9770 else
9771 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9775 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9776 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9778 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9779 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9781 /* Generate texture sampling instructions */
9782 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9784 const char *texture_function, *coord_mask;
9785 BOOL proj;
9787 if (!(tex_map & (1u << stage)))
9788 continue;
9790 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9792 proj = FALSE;
9794 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9795 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9797 proj = TRUE;
9799 else
9801 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9802 proj = TRUE;
9805 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9806 proj = FALSE;
9808 switch (settings->op[stage].tex_type)
9810 case WINED3D_GL_RES_TYPE_TEX_1D:
9811 if (proj)
9813 texture_function = "texture1DProj";
9814 coord_mask = "xw";
9816 else
9818 texture_function = "texture1D";
9819 coord_mask = "x";
9821 break;
9822 case WINED3D_GL_RES_TYPE_TEX_2D:
9823 if (proj)
9825 texture_function = "texture2DProj";
9826 coord_mask = "xyw";
9828 else
9830 texture_function = "texture2D";
9831 coord_mask = "xy";
9833 break;
9834 case WINED3D_GL_RES_TYPE_TEX_3D:
9835 if (proj)
9837 texture_function = "texture3DProj";
9838 coord_mask = "xyzw";
9840 else
9842 texture_function = "texture3D";
9843 coord_mask = "xyz";
9845 break;
9846 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9847 texture_function = "textureCube";
9848 coord_mask = "xyz";
9849 break;
9850 case WINED3D_GL_RES_TYPE_TEX_RECT:
9851 if (proj)
9853 texture_function = "texture2DRectProj";
9854 coord_mask = "xyw";
9856 else
9858 texture_function = "texture2DRect";
9859 coord_mask = "xy";
9861 break;
9862 default:
9863 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9864 texture_function = "";
9865 coord_mask = "xyzw";
9866 break;
9868 if (!legacy_syntax)
9869 texture_function = proj ? "textureProj" : "texture";
9871 if (stage > 0
9872 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9873 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9875 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9877 /* With projective textures, texbem only divides the static
9878 * texture coordinate, not the displacement, so multiply the
9879 * displacement with the dividing parameter before passing it to
9880 * TXP. */
9881 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9883 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9885 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9886 stage, stage);
9887 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9889 else
9891 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9892 stage, stage);
9893 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9896 else
9898 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9901 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9902 stage, texture_function, stage, coord_mask);
9904 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9905 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9906 stage, stage - 1, stage - 1, stage - 1);
9908 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9910 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9911 stage, texture_function, stage, stage);
9913 else
9915 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9916 stage, texture_function, stage, stage, coord_mask);
9919 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9920 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9921 settings->op[stage].color_fixup);
9924 if (settings->color_key_enabled)
9926 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9927 shader_addline(buffer, " discard;\n");
9930 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9932 /* Generate the main shader */
9933 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9935 BOOL op_equal;
9937 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9938 break;
9940 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9941 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9942 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9943 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9944 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9945 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9946 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9947 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9948 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9949 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9950 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9951 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9952 else
9953 op_equal = settings->op[stage].aop == settings->op[stage].cop
9954 && settings->op[stage].carg0 == settings->op[stage].aarg0
9955 && settings->op[stage].carg1 == settings->op[stage].aarg1
9956 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9958 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9960 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9961 settings->op[stage].cop, settings->op[stage].carg0,
9962 settings->op[stage].carg1, settings->op[stage].carg2);
9964 else if (op_equal)
9966 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9967 settings->op[stage].cop, settings->op[stage].carg0,
9968 settings->op[stage].carg1, settings->op[stage].carg2);
9970 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9971 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9973 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9974 settings->op[stage].cop, settings->op[stage].carg0,
9975 settings->op[stage].carg1, settings->op[stage].carg2);
9976 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9977 settings->op[stage].aop, settings->op[stage].aarg0,
9978 settings->op[stage].aarg1, settings->op[stage].aarg2);
9982 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9983 get_fragment_output(gl_info));
9985 if (settings->sRGB_write)
9986 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9988 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9990 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9991 if (!needs_legacy_glsl_syntax(gl_info))
9992 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9994 shader_addline(buffer, "}\n");
9996 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9997 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9999 string_buffer_release(&priv->string_buffers, tex_reg_name);
10000 return shader_id;
10003 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
10004 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
10006 struct glsl_ffp_vertex_shader *shader;
10007 const struct wine_rb_entry *entry;
10009 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
10010 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
10012 if (!(shader = heap_alloc(sizeof(*shader))))
10013 return NULL;
10015 shader->desc.settings = *settings;
10016 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
10017 list_init(&shader->linked_programs);
10018 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
10019 ERR("Failed to insert ffp vertex shader.\n");
10021 return shader;
10024 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
10025 const struct ffp_frag_settings *args, const struct wined3d_context *context)
10027 struct glsl_ffp_fragment_shader *glsl_desc;
10028 const struct ffp_frag_desc *desc;
10030 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
10031 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
10033 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
10034 return NULL;
10036 glsl_desc->entry.settings = *args;
10037 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
10038 list_init(&glsl_desc->linked_programs);
10039 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
10041 return glsl_desc;
10045 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
10046 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
10048 unsigned int i;
10049 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10051 for (i = 0; i < vs_c_count; ++i)
10053 string_buffer_sprintf(name, "vs_c[%u]", i);
10054 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10056 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
10058 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10060 string_buffer_sprintf(name, "vs_i[%u]", i);
10061 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10064 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10066 string_buffer_sprintf(name, "vs_b[%u]", i);
10067 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10070 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10072 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
10074 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
10075 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10077 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
10078 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
10079 for (i = 0; i < MAX_TEXTURES; ++i)
10081 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
10082 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10084 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
10085 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
10086 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
10087 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
10088 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
10089 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
10090 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
10092 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
10093 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10094 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
10095 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10096 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
10097 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10098 string_buffer_sprintf(name, "ffp_light[%u].position", i);
10099 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10100 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
10101 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10102 string_buffer_sprintf(name, "ffp_light[%u].range", i);
10103 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10104 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
10105 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10106 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
10107 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10108 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10109 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10110 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10111 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10112 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10113 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10114 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10115 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10117 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10118 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10119 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10120 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10121 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10122 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10123 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10125 string_buffer_release(&priv->string_buffers, name);
10128 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10129 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10131 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10134 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10135 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10137 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10140 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10141 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10143 unsigned int i;
10144 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10146 for (i = 0; i < ps_c_count; ++i)
10148 string_buffer_sprintf(name, "ps_c[%u]", i);
10149 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10151 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10153 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10155 string_buffer_sprintf(name, "ps_i[%u]", i);
10156 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10159 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10161 string_buffer_sprintf(name, "ps_b[%u]", i);
10162 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10165 for (i = 0; i < MAX_TEXTURES; ++i)
10167 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10168 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10169 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10170 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10171 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10172 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10173 string_buffer_sprintf(name, "tss_const%u", i);
10174 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10177 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10178 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10180 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10181 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10182 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10183 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10185 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10187 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10188 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10189 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10191 string_buffer_release(&priv->string_buffers, name);
10194 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10195 const struct wined3d_context *context, struct wined3d_shader *shader)
10197 struct glsl_context_data *ctx_data = context->shader_backend_data;
10198 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10199 const struct wined3d_gl_info *gl_info = context->gl_info;
10200 struct glsl_cs_compiled_shader *gl_shaders;
10201 struct glsl_shader_private *shader_data;
10202 struct glsl_shader_prog_link *entry;
10203 GLuint shader_id, program_id;
10205 if (!(entry = heap_alloc(sizeof(*entry))))
10207 ERR("Out of memory.\n");
10208 return E_OUTOFMEMORY;
10211 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10213 ERR("Failed to allocate backend data.\n");
10214 heap_free(entry);
10215 return E_OUTOFMEMORY;
10217 shader_data = shader->backend_data;
10218 gl_shaders = shader_data->gl_shaders.cs;
10220 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10222 ERR("Failed to allocate GL shader array.\n");
10223 heap_free(entry);
10224 heap_free(shader->backend_data);
10225 shader->backend_data = NULL;
10226 return E_OUTOFMEMORY;
10228 shader_data->shader_array_size = 1;
10229 gl_shaders = shader_data->gl_shaders.cs;
10231 TRACE("Compiling compute shader %p.\n", shader);
10233 string_buffer_clear(buffer);
10234 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
10235 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10237 program_id = GL_EXTCALL(glCreateProgram());
10238 TRACE("Created new GLSL shader program %u.\n", program_id);
10240 entry->id = program_id;
10241 entry->vs.id = 0;
10242 entry->hs.id = 0;
10243 entry->ds.id = 0;
10244 entry->gs.id = 0;
10245 entry->ps.id = 0;
10246 entry->cs.id = shader_id;
10247 entry->constant_version = 0;
10248 entry->shader_controlled_clip_distances = 0;
10249 entry->ps.np2_fixup_info = NULL;
10250 add_glsl_program_entry(priv, entry);
10252 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10253 GL_EXTCALL(glAttachShader(program_id, shader_id));
10254 checkGLcall("glAttachShader");
10256 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10258 TRACE("Linking GLSL shader program %u.\n", program_id);
10259 GL_EXTCALL(glLinkProgram(program_id));
10260 shader_glsl_validate_link(gl_info, program_id);
10262 GL_EXTCALL(glUseProgram(program_id));
10263 checkGLcall("glUseProgram");
10264 shader_glsl_load_program_resources(context, priv, program_id, shader);
10265 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10267 entry->constant_update_mask = 0;
10269 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10270 checkGLcall("glUseProgram");
10271 return WINED3D_OK;
10274 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
10275 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10277 struct glsl_shader_private *shader_data;
10279 if (!shader->backend_data)
10281 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10282 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
10284 ERR("Failed to compile compute shader %p.\n", shader);
10285 return 0;
10288 shader_data = shader->backend_data;
10289 return shader_data->gl_shaders.cs[0].id;
10292 /* Context activation is done by the caller. */
10293 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
10294 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10296 struct glsl_shader_prog_link *entry;
10297 struct wined3d_shader *shader;
10298 struct glsl_program_key key;
10299 GLuint cs_id;
10301 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10302 return;
10304 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10306 WARN("Compute shader is NULL.\n");
10307 ctx_data->glsl_program = NULL;
10308 return;
10311 cs_id = find_glsl_compute_shader(context, priv, shader);
10312 memset(&key, 0, sizeof(key));
10313 key.cs_id = cs_id;
10314 if (!(entry = get_glsl_program_entry(priv, &key)))
10315 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10316 ctx_data->glsl_program = entry;
10319 /* Context activation is done by the caller. */
10320 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
10321 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10323 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
10324 const struct wined3d_gl_info *gl_info = context->gl_info;
10325 const struct wined3d_shader *pre_rasterization_shader;
10326 const struct ps_np2fixup_info *np2fixup_info = NULL;
10327 struct wined3d_shader *hshader, *dshader, *gshader;
10328 struct glsl_shader_prog_link *entry = NULL;
10329 struct wined3d_shader *vshader = NULL;
10330 struct wined3d_shader *pshader = NULL;
10331 GLuint reorder_shader_id = 0;
10332 struct glsl_program_key key;
10333 GLuint program_id;
10334 unsigned int i;
10335 GLuint vs_id = 0;
10336 GLuint hs_id = 0;
10337 GLuint ds_id = 0;
10338 GLuint gs_id = 0;
10339 GLuint ps_id = 0;
10340 struct list *ps_list, *vs_list;
10341 WORD attribs_map;
10342 struct wined3d_string_buffer *tmp_name;
10344 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10346 vs_id = ctx_data->glsl_program->vs.id;
10347 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10349 if (use_vs(state))
10350 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10352 else if (use_vs(state))
10354 struct vs_compile_args vs_compile_args;
10356 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10358 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
10359 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
10360 vs_list = &vshader->linked_programs;
10362 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10364 struct glsl_ffp_vertex_shader *ffp_shader;
10365 struct wined3d_ffp_vs_settings settings;
10367 wined3d_ffp_get_vs_settings(context, state, &settings);
10368 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10369 vs_id = ffp_shader->id;
10370 vs_list = &ffp_shader->linked_programs;
10373 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10374 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10375 hs_id = ctx_data->glsl_program->hs.id;
10376 else if (hshader)
10377 hs_id = find_glsl_hull_shader(context, priv, hshader);
10379 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10380 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10382 ds_id = ctx_data->glsl_program->ds.id;
10384 else if (dshader)
10386 struct ds_compile_args args;
10388 find_ds_compile_args(state, dshader, &args, context);
10389 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
10392 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10393 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10395 gs_id = ctx_data->glsl_program->gs.id;
10397 else if (gshader)
10399 struct gs_compile_args args;
10401 find_gs_compile_args(state, gshader, &args, context);
10402 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
10405 /* A pixel shader is not used when rasterization is disabled. */
10406 if (is_rasterization_disabled(gshader))
10408 ps_id = 0;
10409 ps_list = NULL;
10411 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10413 ps_id = ctx_data->glsl_program->ps.id;
10414 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10416 if (use_ps(state))
10417 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10419 else if (use_ps(state))
10421 struct ps_compile_args ps_compile_args;
10422 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10423 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
10424 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
10425 pshader, &ps_compile_args, &np2fixup_info);
10426 ps_list = &pshader->linked_programs;
10428 else if (priv->fragment_pipe == &glsl_fragment_pipe
10429 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10431 struct glsl_ffp_fragment_shader *ffp_shader;
10432 struct ffp_frag_settings settings;
10434 gen_ffp_frag_op(context, state, &settings, FALSE);
10435 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
10436 ps_id = ffp_shader->id;
10437 ps_list = &ffp_shader->linked_programs;
10440 key.vs_id = vs_id;
10441 key.hs_id = hs_id;
10442 key.ds_id = ds_id;
10443 key.gs_id = gs_id;
10444 key.ps_id = ps_id;
10445 key.cs_id = 0;
10446 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10448 ctx_data->glsl_program = entry;
10449 return;
10452 /* If we get to this point, then no matching program exists, so we create one */
10453 program_id = GL_EXTCALL(glCreateProgram());
10454 TRACE("Created new GLSL shader program %u.\n", program_id);
10456 /* Create the entry */
10457 entry = heap_alloc(sizeof(*entry));
10458 entry->id = program_id;
10459 entry->vs.id = vs_id;
10460 entry->hs.id = hs_id;
10461 entry->ds.id = ds_id;
10462 entry->gs.id = gs_id;
10463 entry->ps.id = ps_id;
10464 entry->cs.id = 0;
10465 entry->constant_version = 0;
10466 entry->shader_controlled_clip_distances = 0;
10467 entry->ps.np2_fixup_info = np2fixup_info;
10468 /* Add the hash table entry */
10469 add_glsl_program_entry(priv, entry);
10471 /* Set the current program */
10472 ctx_data->glsl_program = entry;
10474 /* Attach GLSL vshader */
10475 if (vs_id)
10477 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10478 GL_EXTCALL(glAttachShader(program_id, vs_id));
10479 checkGLcall("glAttachShader");
10481 list_add_head(vs_list, &entry->vs.shader_entry);
10484 if (vshader)
10486 attribs_map = vshader->reg_maps.input_registers;
10487 if (vshader->reg_maps.shader_version.major < 4)
10489 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10490 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10491 d3d_info->emulated_flatshading
10492 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10493 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10494 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10495 checkGLcall("glAttachShader");
10496 /* Flag the reorder function for deletion, it will be freed
10497 * automatically when the program is destroyed. */
10498 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10501 else
10503 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10506 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10508 /* Bind vertex attributes to a corresponding index number to match
10509 * the same index numbers as ARB_vertex_programs (makes loading
10510 * vertex attributes simpler). With this method, we can use the
10511 * exact same code to load the attributes later for both ARB and
10512 * GLSL shaders.
10514 * We have to do this here because we need to know the Program ID
10515 * in order to make the bindings work, and it has to be done prior
10516 * to linking the GLSL program. */
10517 tmp_name = string_buffer_get(&priv->string_buffers);
10518 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10520 if (!(attribs_map & 1))
10521 continue;
10523 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10524 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10525 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10527 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10528 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10529 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10530 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10533 checkGLcall("glBindAttribLocation");
10535 if (!needs_legacy_glsl_syntax(gl_info))
10537 for (i = 0; i < MAX_RENDER_TARGET_VIEWS; ++i)
10539 string_buffer_sprintf(tmp_name, "color_out%u", i);
10540 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10541 checkGLcall("glBindFragDataLocation");
10544 string_buffer_release(&priv->string_buffers, tmp_name);
10547 if (hshader)
10549 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10550 GL_EXTCALL(glAttachShader(program_id, hs_id));
10551 checkGLcall("glAttachShader");
10553 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10556 if (dshader)
10558 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10559 GL_EXTCALL(glAttachShader(program_id, ds_id));
10560 checkGLcall("glAttachShader");
10562 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10565 if (gshader)
10567 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10568 GL_EXTCALL(glAttachShader(program_id, gs_id));
10569 checkGLcall("glAttachShader");
10571 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10573 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10576 /* Attach GLSL pshader */
10577 if (ps_id)
10579 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10580 GL_EXTCALL(glAttachShader(program_id, ps_id));
10581 checkGLcall("glAttachShader");
10583 list_add_head(ps_list, &entry->ps.shader_entry);
10586 /* Link the program */
10587 TRACE("Linking GLSL shader program %u.\n", program_id);
10588 GL_EXTCALL(glLinkProgram(program_id));
10589 shader_glsl_validate_link(gl_info, program_id);
10591 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10592 vshader ? vshader->limits->constant_float : 0);
10593 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10594 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10595 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10596 pshader ? pshader->limits->constant_float : 0);
10597 checkGLcall("find glsl program uniform locations");
10599 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10600 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10602 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10603 entry->shader_controlled_clip_distances = 1;
10604 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10607 if (needs_legacy_glsl_syntax(gl_info))
10609 if (pshader && pshader->reg_maps.shader_version.major >= 3
10610 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10612 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10613 entry->vs.vertex_color_clamp = GL_FALSE;
10615 else
10617 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10620 else
10622 /* With core profile we never change vertex_color_clamp from
10623 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10624 * glClampColorARB(). */
10625 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10628 /* Set the shader to allow uniform loading on it */
10629 GL_EXTCALL(glUseProgram(program_id));
10630 checkGLcall("glUseProgram");
10632 entry->constant_update_mask = 0;
10633 if (vshader)
10635 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10636 if (vshader->reg_maps.integer_constants)
10637 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10638 if (vshader->reg_maps.boolean_constants)
10639 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10640 if (entry->vs.pos_fixup_location != -1)
10641 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10643 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10645 else
10647 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10648 | WINED3D_SHADER_CONST_FFP_PROJ;
10650 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10652 if (entry->vs.modelview_matrix_location[i] != -1)
10654 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10655 break;
10659 for (i = 0; i < MAX_TEXTURES; ++i)
10661 if (entry->vs.texture_matrix_location[i] != -1)
10663 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10664 break;
10667 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10668 || entry->vs.material_specular_location != -1
10669 || entry->vs.material_emissive_location != -1
10670 || entry->vs.material_shininess_location != -1)
10671 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10672 if (entry->vs.light_ambient_location != -1)
10673 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10675 if (entry->vs.clip_planes_location != -1)
10676 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10677 if (entry->vs.pointsize_min_location != -1)
10678 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10680 if (hshader)
10681 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10683 if (dshader)
10685 if (entry->ds.pos_fixup_location != -1)
10686 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10688 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10691 if (gshader)
10693 if (entry->gs.pos_fixup_location != -1)
10694 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10696 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10699 if (ps_id)
10701 if (pshader)
10703 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10704 if (pshader->reg_maps.integer_constants)
10705 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10706 if (pshader->reg_maps.boolean_constants)
10707 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10708 if (entry->ps.ycorrection_location != -1)
10709 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10711 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10712 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10714 else
10716 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10718 shader_glsl_load_samplers(context, priv, program_id, NULL);
10721 for (i = 0; i < MAX_TEXTURES; ++i)
10723 if (entry->ps.bumpenv_mat_location[i] != -1)
10725 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10726 break;
10730 if (entry->ps.fog_color_location != -1)
10731 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10732 if (entry->ps.alpha_test_ref_location != -1)
10733 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10734 if (entry->ps.np2_fixup_location != -1)
10735 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10736 if (entry->ps.color_key_location != -1)
10737 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10741 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10743 struct wined3d_device *device = shader->device;
10744 struct wined3d_context *context;
10746 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10748 context = context_acquire(device, NULL, 0);
10749 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10750 context_release(context);
10754 /* Context activation is done by the caller. */
10755 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10756 const struct wined3d_state *state)
10758 struct glsl_context_data *ctx_data = context->shader_backend_data;
10759 const struct wined3d_gl_info *gl_info = context->gl_info;
10760 struct shader_glsl_priv *priv = shader_priv;
10761 struct glsl_shader_prog_link *glsl_program;
10762 GLenum current_vertex_color_clamp;
10763 GLuint program_id, prev_id;
10765 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10766 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10768 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10769 set_glsl_shader_program(context, state, priv, ctx_data);
10770 glsl_program = ctx_data->glsl_program;
10772 if (glsl_program)
10774 program_id = glsl_program->id;
10775 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10776 if (glsl_program->shader_controlled_clip_distances)
10777 context_enable_clip_distances(context, glsl_program->clip_distance_mask);
10779 else
10781 program_id = 0;
10782 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10785 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10787 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10788 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10790 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10791 checkGLcall("glClampColorARB");
10793 else
10795 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10799 TRACE("Using GLSL program %u.\n", program_id);
10801 if (prev_id != program_id)
10803 GL_EXTCALL(glUseProgram(program_id));
10804 checkGLcall("glUseProgram");
10806 if (glsl_program)
10807 context->constant_update_mask |= glsl_program->constant_update_mask;
10810 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10813 /* Context activation is done by the caller. */
10814 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10815 const struct wined3d_state *state)
10817 struct glsl_context_data *ctx_data = context->shader_backend_data;
10818 const struct wined3d_gl_info *gl_info = context->gl_info;
10819 struct shader_glsl_priv *priv = shader_priv;
10820 GLuint program_id, prev_id;
10822 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10823 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10824 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10826 TRACE("Using GLSL program %u.\n", program_id);
10828 if (prev_id != program_id)
10830 GL_EXTCALL(glUseProgram(program_id));
10831 checkGLcall("glUseProgram");
10834 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10835 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10836 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10837 | (1u << WINED3D_SHADER_TYPE_HULL)
10838 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10841 /* "context" is not necessarily the currently active context. */
10842 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10844 struct glsl_context_data *ctx_data = context->shader_backend_data;
10846 ctx_data->glsl_program = NULL;
10847 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10848 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10849 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10850 | (1u << WINED3D_SHADER_TYPE_HULL)
10851 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10852 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10855 /* Context activation is done by the caller. */
10856 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10858 struct glsl_context_data *ctx_data = context->shader_backend_data;
10859 const struct wined3d_gl_info *gl_info = context->gl_info;
10860 struct shader_glsl_priv *priv = shader_priv;
10862 shader_glsl_invalidate_current_program(context);
10863 GL_EXTCALL(glUseProgram(0));
10864 checkGLcall("glUseProgram");
10866 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10867 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10869 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10871 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10872 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10873 checkGLcall("glClampColorARB");
10877 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10878 const struct glsl_shader_prog_link *program)
10880 const struct glsl_context_data *ctx_data;
10881 struct wined3d_context *context;
10882 unsigned int i;
10884 for (i = 0; i < device->context_count; ++i)
10886 context = device->contexts[i];
10887 ctx_data = context->shader_backend_data;
10889 if (ctx_data->glsl_program == program)
10890 shader_glsl_invalidate_current_program(context);
10894 static void shader_glsl_destroy(struct wined3d_shader *shader)
10896 struct glsl_shader_private *shader_data = shader->backend_data;
10897 struct wined3d_device *device = shader->device;
10898 struct shader_glsl_priv *priv = device->shader_priv;
10899 const struct wined3d_gl_info *gl_info;
10900 const struct list *linked_programs;
10901 struct wined3d_context *context;
10903 if (!shader_data || !shader_data->num_gl_shaders)
10905 heap_free(shader_data);
10906 shader->backend_data = NULL;
10907 return;
10910 context = context_acquire(device, NULL, 0);
10911 gl_info = context->gl_info;
10913 TRACE("Deleting linked programs.\n");
10914 linked_programs = &shader->linked_programs;
10915 if (linked_programs->next)
10917 struct glsl_shader_prog_link *entry, *entry2;
10918 UINT i;
10920 switch (shader->reg_maps.shader_version.type)
10922 case WINED3D_SHADER_TYPE_PIXEL:
10924 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10926 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10928 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10929 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10930 checkGLcall("glDeleteShader");
10932 heap_free(shader_data->gl_shaders.ps);
10934 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10935 struct glsl_shader_prog_link, ps.shader_entry)
10937 shader_glsl_invalidate_contexts_program(device, entry);
10938 delete_glsl_program_entry(priv, gl_info, entry);
10941 break;
10944 case WINED3D_SHADER_TYPE_VERTEX:
10946 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10948 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10950 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10951 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10952 checkGLcall("glDeleteShader");
10954 heap_free(shader_data->gl_shaders.vs);
10956 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10957 struct glsl_shader_prog_link, vs.shader_entry)
10959 shader_glsl_invalidate_contexts_program(device, entry);
10960 delete_glsl_program_entry(priv, gl_info, entry);
10963 break;
10966 case WINED3D_SHADER_TYPE_HULL:
10968 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10970 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10972 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10973 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10974 checkGLcall("glDeleteShader");
10976 heap_free(shader_data->gl_shaders.hs);
10978 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10979 struct glsl_shader_prog_link, hs.shader_entry)
10981 shader_glsl_invalidate_contexts_program(device, entry);
10982 delete_glsl_program_entry(priv, gl_info, entry);
10985 break;
10988 case WINED3D_SHADER_TYPE_DOMAIN:
10990 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10992 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10994 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10995 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10996 checkGLcall("glDeleteShader");
10998 heap_free(shader_data->gl_shaders.ds);
11000 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11001 struct glsl_shader_prog_link, ds.shader_entry)
11003 shader_glsl_invalidate_contexts_program(device, entry);
11004 delete_glsl_program_entry(priv, gl_info, entry);
11007 break;
11010 case WINED3D_SHADER_TYPE_GEOMETRY:
11012 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
11014 for (i = 0; i < shader_data->num_gl_shaders; ++i)
11016 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
11017 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
11018 checkGLcall("glDeleteShader");
11020 heap_free(shader_data->gl_shaders.gs);
11022 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11023 struct glsl_shader_prog_link, gs.shader_entry)
11025 shader_glsl_invalidate_contexts_program(device, entry);
11026 delete_glsl_program_entry(priv, gl_info, entry);
11029 break;
11032 case WINED3D_SHADER_TYPE_COMPUTE:
11034 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
11036 for (i = 0; i < shader_data->num_gl_shaders; ++i)
11038 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
11039 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
11040 checkGLcall("glDeleteShader");
11042 heap_free(shader_data->gl_shaders.cs);
11044 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11045 struct glsl_shader_prog_link, cs.shader_entry)
11047 shader_glsl_invalidate_contexts_program(device, entry);
11048 delete_glsl_program_entry(priv, gl_info, entry);
11051 break;
11054 default:
11055 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
11056 break;
11060 heap_free(shader->backend_data);
11061 shader->backend_data = NULL;
11063 context_release(context);
11066 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
11068 const struct glsl_program_key *k = key;
11069 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
11070 const struct glsl_shader_prog_link, program_lookup_entry);
11072 if (k->vs_id > prog->vs.id) return 1;
11073 else if (k->vs_id < prog->vs.id) return -1;
11075 if (k->gs_id > prog->gs.id) return 1;
11076 else if (k->gs_id < prog->gs.id) return -1;
11078 if (k->ps_id > prog->ps.id) return 1;
11079 else if (k->ps_id < prog->ps.id) return -1;
11081 if (k->hs_id > prog->hs.id) return 1;
11082 else if (k->hs_id < prog->hs.id) return -1;
11084 if (k->ds_id > prog->ds.id) return 1;
11085 else if (k->ds_id < prog->ds.id) return -1;
11087 if (k->cs_id > prog->cs.id) return 1;
11088 else if (k->cs_id < prog->cs.id) return -1;
11090 return 0;
11093 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
11095 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
11096 + constant_count * sizeof(*heap->contained)
11097 + constant_count * sizeof(*heap->positions);
11098 void *mem;
11100 if (!(mem = heap_alloc(size)))
11102 ERR("Failed to allocate memory\n");
11103 return FALSE;
11106 heap->entries = mem;
11107 heap->entries[1].version = 0;
11108 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11109 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11110 heap->positions = (unsigned int *)(heap->contained + constant_count);
11111 heap->size = 1;
11113 return TRUE;
11116 static void constant_heap_free(struct constant_heap *heap)
11118 heap_free(heap->entries);
11121 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11122 const struct fragment_pipeline *fragment_pipe)
11124 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11125 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
11126 struct fragment_caps fragment_caps;
11127 void *vertex_priv, *fragment_priv;
11128 struct shader_glsl_priv *priv;
11130 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11131 return E_OUTOFMEMORY;
11133 string_buffer_list_init(&priv->string_buffers);
11135 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11137 ERR("Failed to initialize vertex pipe.\n");
11138 heap_free(priv);
11139 return E_FAIL;
11142 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11144 ERR("Failed to initialize fragment pipe.\n");
11145 vertex_pipe->vp_free(device);
11146 heap_free(priv);
11147 return E_FAIL;
11150 if (!string_buffer_init(&priv->shader_buffer))
11152 ERR("Failed to initialize shader buffer.\n");
11153 goto fail;
11156 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11158 ERR("Failed to allocate memory.\n");
11159 goto fail;
11162 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11164 ERR("Failed to initialize vertex shader constant heap\n");
11165 goto fail;
11168 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11170 ERR("Failed to initialize pixel shader constant heap\n");
11171 goto fail;
11174 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11176 priv->next_constant_version = 1;
11177 priv->vertex_pipe = vertex_pipe;
11178 priv->fragment_pipe = fragment_pipe;
11179 fragment_pipe->get_caps(gl_info, &fragment_caps);
11180 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11181 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11183 device->vertex_priv = vertex_priv;
11184 device->fragment_priv = fragment_priv;
11185 device->shader_priv = priv;
11187 return WINED3D_OK;
11189 fail:
11190 constant_heap_free(&priv->pconst_heap);
11191 constant_heap_free(&priv->vconst_heap);
11192 heap_free(priv->stack);
11193 string_buffer_free(&priv->shader_buffer);
11194 fragment_pipe->free_private(device);
11195 vertex_pipe->vp_free(device);
11196 heap_free(priv);
11197 return E_OUTOFMEMORY;
11200 /* Context activation is done by the caller. */
11201 static void shader_glsl_free(struct wined3d_device *device)
11203 struct shader_glsl_priv *priv = device->shader_priv;
11205 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11206 constant_heap_free(&priv->pconst_heap);
11207 constant_heap_free(&priv->vconst_heap);
11208 heap_free(priv->stack);
11209 string_buffer_list_cleanup(&priv->string_buffers);
11210 string_buffer_free(&priv->shader_buffer);
11211 priv->fragment_pipe->free_private(device);
11212 priv->vertex_pipe->vp_free(device);
11214 heap_free(device->shader_priv);
11215 device->shader_priv = NULL;
11218 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11220 struct glsl_context_data *ctx_data;
11222 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11223 return FALSE;
11224 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11225 context->shader_backend_data = ctx_data;
11226 return TRUE;
11229 static void shader_glsl_free_context_data(struct wined3d_context *context)
11231 heap_free(context->shader_backend_data);
11234 static void shader_glsl_init_context_state(struct wined3d_context *context)
11236 const struct wined3d_gl_info *gl_info = context->gl_info;
11238 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11239 checkGLcall("GL_PROGRAM_POINT_SIZE");
11242 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11244 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11245 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11246 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11248 if (shader_model_4
11249 && gl_info->supported[ARB_COMPUTE_SHADER]
11250 && gl_info->supported[ARB_CULL_DISTANCE]
11251 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11252 && gl_info->supported[ARB_GPU_SHADER5]
11253 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11254 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11255 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11256 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11257 && gl_info->supported[ARB_TESSELLATION_SHADER]
11258 && gl_info->supported[ARB_TEXTURE_GATHER]
11259 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11260 return 5;
11262 if (shader_model_4)
11263 return 4;
11265 /* Support for texldd and texldl instructions in pixel shaders is required
11266 * for SM3. */
11267 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11268 return 3;
11270 return 2;
11273 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
11275 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11277 TRACE("Shader model %u.\n", shader_model);
11279 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11280 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11281 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11282 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11283 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11284 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11286 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11287 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11289 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11290 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11291 caps->varying_count = gl_info->limits.glsl_varyings;
11293 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11294 * Direct3D minimum requirement.
11296 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11297 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11299 * The problem is that the refrast clamps temporary results in the shader to
11300 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11301 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11302 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11303 * offer a way to query this.
11305 if (shader_model >= 4)
11306 caps->ps_1x_max_value = FLT_MAX;
11307 else
11308 caps->ps_1x_max_value = 1024.0f;
11310 /* Ideally we'd only set caps like sRGB writes here if supported by both
11311 * the shader backend and the fragment pipe, but we can get called before
11312 * shader_glsl_alloc(). */
11313 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11314 | WINED3D_SHADER_CAP_SRGB_WRITE;
11317 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11319 /* We support everything except YUV conversions. */
11320 return !is_complex_fixup(fixup);
11323 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11325 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11326 /* WINED3DSIH_ADD */ shader_glsl_binop,
11327 /* WINED3DSIH_AND */ shader_glsl_binop,
11328 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11329 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11330 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11331 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11332 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11333 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11334 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11335 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11336 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11337 /* WINED3DSIH_BEM */ shader_glsl_bem,
11338 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11339 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11340 /* WINED3DSIH_BREAK */ shader_glsl_break,
11341 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11342 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11343 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11344 /* WINED3DSIH_CALL */ shader_glsl_call,
11345 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11346 /* WINED3DSIH_CASE */ shader_glsl_case,
11347 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11348 /* WINED3DSIH_CND */ shader_glsl_cnd,
11349 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11350 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11351 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11352 /* WINED3DSIH_CRS */ shader_glsl_cross,
11353 /* WINED3DSIH_CUT */ shader_glsl_cut,
11354 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11355 /* WINED3DSIH_DCL */ shader_glsl_nop,
11356 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11357 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11358 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11359 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11360 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11361 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11362 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11363 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11364 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11365 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11366 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11367 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11368 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11369 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11370 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11371 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
11372 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
11373 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11374 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11375 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11376 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11377 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11378 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11379 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11380 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11381 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11382 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11383 /* WINED3DSIH_DCL_STREAM */ NULL,
11384 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11385 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11386 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11387 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11388 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11389 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11390 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11391 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11392 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11393 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11394 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11395 /* WINED3DSIH_DEF */ shader_glsl_nop,
11396 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11397 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11398 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11399 /* WINED3DSIH_DIV */ shader_glsl_binop,
11400 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11401 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11402 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11403 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11404 /* WINED3DSIH_DST */ shader_glsl_dst,
11405 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11406 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11407 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11408 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11409 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11410 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11411 /* WINED3DSIH_ELSE */ shader_glsl_else,
11412 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11413 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11414 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11415 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11416 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11417 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11418 /* WINED3DSIH_EQ */ shader_glsl_relop,
11419 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ NULL,
11420 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11421 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11422 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11423 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11424 /* WINED3DSIH_FCALL */ NULL,
11425 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11426 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11427 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11428 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11429 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11430 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11431 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11432 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11433 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11434 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11435 /* WINED3DSIH_GE */ shader_glsl_relop,
11436 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11437 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11438 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11439 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11440 /* WINED3DSIH_IADD */ shader_glsl_binop,
11441 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11442 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11443 /* WINED3DSIH_IF */ shader_glsl_if,
11444 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11445 /* WINED3DSIH_IGE */ shader_glsl_relop,
11446 /* WINED3DSIH_ILT */ shader_glsl_relop,
11447 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11448 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11449 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11450 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11451 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11452 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11453 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11454 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11455 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11456 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11457 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11458 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11459 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11460 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11461 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11462 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11463 /* WINED3DSIH_INE */ shader_glsl_relop,
11464 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11465 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11466 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11467 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11468 /* WINED3DSIH_LABEL */ shader_glsl_label,
11469 /* WINED3DSIH_LD */ shader_glsl_ld,
11470 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11471 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11472 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11473 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11474 /* WINED3DSIH_LIT */ shader_glsl_lit,
11475 /* WINED3DSIH_LOD */ NULL,
11476 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11477 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11478 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11479 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11480 /* WINED3DSIH_LT */ shader_glsl_relop,
11481 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11482 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11483 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11484 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11485 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11486 /* WINED3DSIH_MAD */ shader_glsl_mad,
11487 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11488 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11489 /* WINED3DSIH_MOV */ shader_glsl_mov,
11490 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11491 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11492 /* WINED3DSIH_MUL */ shader_glsl_binop,
11493 /* WINED3DSIH_NE */ shader_glsl_relop,
11494 /* WINED3DSIH_NOP */ shader_glsl_nop,
11495 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11496 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11497 /* WINED3DSIH_OR */ shader_glsl_binop,
11498 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11499 /* WINED3DSIH_POW */ shader_glsl_pow,
11500 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11501 /* WINED3DSIH_REP */ shader_glsl_rep,
11502 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11503 /* WINED3DSIH_RET */ shader_glsl_ret,
11504 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11505 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11506 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11507 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11508 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11509 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11510 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11511 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11512 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11513 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11514 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11515 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11516 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11517 /* WINED3DSIH_SAMPLE_POS */ NULL,
11518 /* WINED3DSIH_SETP */ NULL,
11519 /* WINED3DSIH_SGE */ shader_glsl_compare,
11520 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11521 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11522 /* WINED3DSIH_SLT */ shader_glsl_compare,
11523 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11524 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11525 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11526 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11527 /* WINED3DSIH_SUB */ shader_glsl_binop,
11528 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11529 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11530 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11531 /* WINED3DSIH_TEX */ shader_glsl_tex,
11532 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11533 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11534 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11535 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11536 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11537 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11538 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11539 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11540 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11541 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11542 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11543 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11544 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11545 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11546 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11547 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11548 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11549 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11550 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11551 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11552 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11553 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11554 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11555 /* WINED3DSIH_UGE */ shader_glsl_relop,
11556 /* WINED3DSIH_ULT */ shader_glsl_relop,
11557 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11558 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11559 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11560 /* WINED3DSIH_USHR */ shader_glsl_binop,
11561 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11562 /* WINED3DSIH_XOR */ shader_glsl_binop,
11565 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11566 SHADER_HANDLER hw_fct;
11568 /* Select handler */
11569 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11571 /* Unhandled opcode */
11572 if (!hw_fct)
11574 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11575 return;
11577 hw_fct(ins);
11579 shader_glsl_add_instruction_modifiers(ins);
11582 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11584 struct shader_glsl_priv *priv = shader_priv;
11586 return priv->ffp_proj_control;
11589 const struct wined3d_shader_backend_ops glsl_shader_backend =
11591 shader_glsl_handle_instruction,
11592 shader_glsl_precompile,
11593 shader_glsl_select,
11594 shader_glsl_select_compute,
11595 shader_glsl_disable,
11596 shader_glsl_update_float_vertex_constants,
11597 shader_glsl_update_float_pixel_constants,
11598 shader_glsl_load_constants,
11599 shader_glsl_destroy,
11600 shader_glsl_alloc,
11601 shader_glsl_free,
11602 shader_glsl_allocate_context_data,
11603 shader_glsl_free_context_data,
11604 shader_glsl_init_context_state,
11605 shader_glsl_get_caps,
11606 shader_glsl_color_fixup_supported,
11607 shader_glsl_has_ffp_proj_control,
11610 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11612 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11614 caps->xyzrhw = TRUE;
11615 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11616 caps->ffp_generic_attributes = TRUE;
11617 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11618 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11619 caps->max_vertex_blend_matrix_index = 0;
11620 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11621 | WINED3DVTXPCAPS_MATERIALSOURCE7
11622 | WINED3DVTXPCAPS_VERTEXFOG
11623 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11624 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11625 | WINED3DVTXPCAPS_LOCALVIEWER
11626 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11627 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11628 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11629 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11632 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11634 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11635 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11636 return 0;
11639 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11641 struct shader_glsl_priv *priv;
11643 if (shader_backend == &glsl_shader_backend)
11645 priv = shader_priv;
11646 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11647 return priv;
11650 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11652 return NULL;
11655 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11657 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11658 struct glsl_ffp_vertex_shader, desc.entry);
11659 struct glsl_shader_prog_link *program, *program2;
11660 struct glsl_ffp_destroy_ctx *ctx = context;
11662 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11663 struct glsl_shader_prog_link, vs.shader_entry)
11665 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11667 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11668 heap_free(shader);
11671 /* Context activation is done by the caller. */
11672 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11674 struct shader_glsl_priv *priv = device->vertex_priv;
11675 struct glsl_ffp_destroy_ctx ctx;
11677 ctx.priv = priv;
11678 ctx.gl_info = &device->adapter->gl_info;
11679 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11682 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11683 const struct wined3d_state *state, DWORD state_id) {}
11685 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11686 const struct wined3d_state *state, DWORD state_id)
11688 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11691 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11692 const struct wined3d_state *state, DWORD state_id)
11694 const struct wined3d_gl_info *gl_info = context->gl_info;
11695 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11696 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11697 BOOL transformed = context->stream_info.position_transformed;
11698 BOOL wasrhw = context->last_was_rhw;
11699 unsigned int i;
11701 context->last_was_rhw = transformed;
11703 /* If the vertex declaration contains a transformed position attribute,
11704 * the draw uses the fixed function vertex pipeline regardless of any
11705 * vertex shader set by the application. */
11706 if (transformed != wasrhw
11707 || context->stream_info.swizzle_map != context->last_swizzle_map)
11708 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11710 context->last_swizzle_map = context->stream_info.swizzle_map;
11712 if (!use_vs(state))
11714 if (context->last_was_vshader)
11716 if (legacy_clip_planes)
11717 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11718 clipplane(context, state, STATE_CLIPPLANE(i));
11719 else
11720 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11723 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11725 /* Because of settings->texcoords, we have to regenerate the vertex
11726 * shader on a vdecl change if there aren't enough varyings to just
11727 * always output all the texture coordinates. */
11728 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11729 || normal != context->last_was_normal)
11730 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11732 if (use_ps(state)
11733 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11734 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11735 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11737 else
11739 if (!context->last_was_vshader)
11741 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11742 if (legacy_clip_planes)
11743 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11744 clipplane(context, state, STATE_CLIPPLANE(i));
11745 else
11746 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11750 context->last_was_vshader = use_vs(state);
11751 context->last_was_normal = normal;
11754 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11755 const struct wined3d_state *state, DWORD state_id)
11757 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11758 /* Different vertex shaders potentially require a different vertex attributes setup. */
11759 if (!isStateDirty(context, STATE_VDECL))
11760 context_apply_state(context, state, STATE_VDECL);
11763 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11764 const struct wined3d_state *state, DWORD state_id)
11766 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11767 * winding) are defined in Hull Shaders, while in GLSL those are
11768 * specified in Tessellation Evaluation Shaders. */
11769 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11771 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11772 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11775 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11776 const struct wined3d_state *state, DWORD state_id)
11778 struct glsl_context_data *ctx_data = context->shader_backend_data;
11779 BOOL rasterization_disabled;
11781 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11782 if (ctx_data->rasterization_disabled != rasterization_disabled)
11783 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11784 ctx_data->rasterization_disabled = rasterization_disabled;
11786 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11787 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11788 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11789 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11790 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11793 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11794 const struct wined3d_state *state, DWORD state_id)
11796 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11797 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11798 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11799 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11800 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11801 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11802 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11805 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11806 const struct wined3d_state *state, DWORD state_id)
11808 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11811 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11812 const struct wined3d_state *state, DWORD state_id)
11814 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11817 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11819 const struct wined3d_gl_info *gl_info = context->gl_info;
11820 unsigned int k;
11822 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11823 | WINED3D_SHADER_CONST_FFP_LIGHTS
11824 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11826 if (needs_legacy_glsl_syntax(gl_info))
11828 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11830 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11831 clipplane(context, state, STATE_CLIPPLANE(k));
11834 else
11836 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11840 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11841 const struct wined3d_state *state, DWORD state_id)
11843 /* Table fog behavior depends on the projection matrix. */
11844 if (state->render_states[WINED3D_RS_FOGENABLE]
11845 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11846 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11847 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11850 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11851 const struct wined3d_state *state, DWORD state_id)
11853 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11854 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11855 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11856 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11857 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11858 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11861 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11862 const struct wined3d_state *state, DWORD state_id)
11864 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11867 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11868 const struct wined3d_state *state, DWORD state_id)
11870 DWORD sampler = state_id - STATE_SAMPLER(0);
11871 const struct wined3d_texture *texture = state->textures[sampler];
11872 BOOL np2;
11874 if (!texture)
11875 return;
11877 if (sampler >= MAX_TEXTURES)
11878 return;
11880 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11881 || context->lastWasPow2Texture & (1u << sampler))
11883 if (np2)
11884 context->lastWasPow2Texture |= 1u << sampler;
11885 else
11886 context->lastWasPow2Texture &= ~(1u << sampler);
11888 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11892 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11893 const struct wined3d_state *state, DWORD state_id)
11895 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11898 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11899 const struct wined3d_state *state, DWORD state_id)
11901 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11904 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11905 const struct wined3d_state *state, DWORD state_id)
11907 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11910 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11911 const struct wined3d_state *state, DWORD state_id)
11913 if (!use_vs(state))
11914 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11917 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11918 const struct wined3d_state *state, DWORD state_id)
11920 static unsigned int once;
11922 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11923 FIXME("Non-point sprite points not supported in core profile.\n");
11926 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11927 const struct wined3d_state *state, DWORD state_id)
11929 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11932 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11933 const struct wined3d_state *state, DWORD state_id)
11935 const struct wined3d_gl_info *gl_info = context->gl_info;
11936 UINT index = state_id - STATE_CLIPPLANE(0);
11938 if (index >= gl_info->limits.user_clip_distances)
11939 return;
11941 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11944 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11946 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11947 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11948 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11949 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11950 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11951 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11952 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11953 /* Clip planes */
11954 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11955 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11956 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11957 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11958 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11959 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11960 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11961 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11962 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11963 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11964 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11965 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11966 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11967 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11968 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11969 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11970 /* Lights */
11971 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11972 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11973 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11974 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11975 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11976 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11977 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11978 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11979 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11980 /* Viewport */
11981 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11982 /* Transform states */
11983 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11984 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11985 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11986 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11987 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11988 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11989 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11990 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11991 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11992 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11993 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11994 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11995 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11996 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11997 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11998 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11999 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12000 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12001 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12002 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12003 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12004 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12005 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12006 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12007 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12008 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12009 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12010 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12011 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12012 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12013 /* Fog */
12014 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12015 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12016 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12017 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12018 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
12019 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
12020 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12021 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12022 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12023 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12024 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12025 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12026 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12027 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12028 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12029 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12030 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
12031 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
12032 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
12033 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
12034 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
12035 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
12036 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12037 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12038 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12039 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
12040 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12041 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12042 /* NP2 texture matrix fixups. They are not needed if
12043 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
12044 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
12045 * matrix. */
12046 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12047 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12048 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12049 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12050 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12051 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12052 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12053 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12054 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12055 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12056 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12057 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12058 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12059 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12060 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12061 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12062 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12063 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12064 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12065 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12066 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12067 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12068 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12069 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12070 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12071 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
12072 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
12073 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
12076 /* TODO:
12077 * - Implement vertex tweening. */
12078 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12080 glsl_vertex_pipe_vp_enable,
12081 glsl_vertex_pipe_vp_get_caps,
12082 glsl_vertex_pipe_vp_get_emul_mask,
12083 glsl_vertex_pipe_vp_alloc,
12084 glsl_vertex_pipe_vp_free,
12085 glsl_vertex_pipe_vp_states,
12088 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
12090 /* Nothing to do. */
12093 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
12095 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
12096 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
12097 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
12098 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12099 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12100 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12101 | WINED3DTEXOPCAPS_SELECTARG1
12102 | WINED3DTEXOPCAPS_SELECTARG2
12103 | WINED3DTEXOPCAPS_MODULATE4X
12104 | WINED3DTEXOPCAPS_MODULATE2X
12105 | WINED3DTEXOPCAPS_MODULATE
12106 | WINED3DTEXOPCAPS_ADDSIGNED2X
12107 | WINED3DTEXOPCAPS_ADDSIGNED
12108 | WINED3DTEXOPCAPS_ADD
12109 | WINED3DTEXOPCAPS_SUBTRACT
12110 | WINED3DTEXOPCAPS_ADDSMOOTH
12111 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12112 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12113 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12114 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12115 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12116 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12117 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12118 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12119 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12120 | WINED3DTEXOPCAPS_DOTPRODUCT3
12121 | WINED3DTEXOPCAPS_MULTIPLYADD
12122 | WINED3DTEXOPCAPS_LERP
12123 | WINED3DTEXOPCAPS_BUMPENVMAP
12124 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12125 caps->MaxTextureBlendStages = MAX_TEXTURES;
12126 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
12129 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12131 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12132 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12133 return 0;
12136 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12138 struct shader_glsl_priv *priv;
12140 if (shader_backend == &glsl_shader_backend)
12142 priv = shader_priv;
12143 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12144 return priv;
12147 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12149 return NULL;
12152 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
12154 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12155 struct glsl_ffp_fragment_shader, entry.entry);
12156 struct glsl_shader_prog_link *program, *program2;
12157 struct glsl_ffp_destroy_ctx *ctx = context;
12159 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12160 struct glsl_shader_prog_link, ps.shader_entry)
12162 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
12164 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
12165 heap_free(shader);
12168 /* Context activation is done by the caller. */
12169 static void glsl_fragment_pipe_free(struct wined3d_device *device)
12171 struct shader_glsl_priv *priv = device->fragment_priv;
12172 struct glsl_ffp_destroy_ctx ctx;
12174 ctx.priv = priv;
12175 ctx.gl_info = &device->adapter->gl_info;
12176 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12179 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12180 const struct wined3d_state *state, DWORD state_id)
12182 context->last_was_pshader = use_ps(state);
12184 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12187 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12188 const struct wined3d_state *state, DWORD state_id)
12190 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12193 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12194 const struct wined3d_state *state, DWORD state_id)
12196 BOOL use_vshader = use_vs(state);
12197 enum fogsource new_source;
12198 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12199 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12201 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12203 if (!state->render_states[WINED3D_RS_FOGENABLE])
12204 return;
12206 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12208 if (use_vshader)
12209 new_source = FOGSOURCE_VS;
12210 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12211 new_source = FOGSOURCE_COORD;
12212 else
12213 new_source = FOGSOURCE_FFP;
12215 else
12217 new_source = FOGSOURCE_FFP;
12220 if (new_source != context->fog_source || fogstart == fogend)
12222 context->fog_source = new_source;
12223 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12227 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12228 const struct wined3d_state *state, DWORD state_id)
12230 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12231 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
12232 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12234 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12235 glsl_fragment_pipe_fog(context, state, state_id);
12238 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12239 const struct wined3d_state *state, DWORD state_id)
12241 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12242 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
12243 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12246 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12247 const struct wined3d_state *state, DWORD state_id)
12249 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12252 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12253 const struct wined3d_state *state, DWORD state_id)
12255 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12258 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12259 const struct wined3d_state *state, DWORD state_id)
12261 const struct wined3d_gl_info *gl_info = context->gl_info;
12262 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12263 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
12265 if (func)
12267 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12268 checkGLcall("glAlphaFunc");
12272 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12273 const struct wined3d_state *state, DWORD state_id)
12275 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12278 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12279 const struct wined3d_state *state, DWORD state_id)
12281 const struct wined3d_gl_info *gl_info = context->gl_info;
12283 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12285 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12286 checkGLcall("glEnable(GL_ALPHA_TEST)");
12288 else
12290 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12291 checkGLcall("glDisable(GL_ALPHA_TEST)");
12295 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12296 const struct wined3d_state *state, DWORD state_id)
12298 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12301 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12302 const struct wined3d_state *state, DWORD state_id)
12304 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12307 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12308 const struct wined3d_state *state, DWORD state_id)
12310 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12313 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
12315 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12316 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12317 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12318 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12319 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12320 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12321 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12322 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12323 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12324 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12325 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12326 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12327 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12328 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12329 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12330 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12331 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12332 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12333 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12334 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12335 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12336 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12337 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12338 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12339 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12340 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12341 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12342 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12343 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12344 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12345 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12346 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12347 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12348 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12349 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12350 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12351 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12352 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12353 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12354 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12355 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12356 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12357 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12358 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12359 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12360 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12361 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12362 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12363 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12364 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12365 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12366 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12367 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12368 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12369 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12370 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12371 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12372 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12373 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12374 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12375 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12376 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12377 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12378 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12379 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12380 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12381 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12382 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12383 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12384 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12385 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12386 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12387 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12388 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12389 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12390 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12391 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12392 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12393 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12394 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12395 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12396 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12397 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12398 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12399 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12400 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12401 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12402 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12403 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12404 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12405 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12406 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12407 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12408 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12409 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12410 {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 },
12411 {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 },
12412 {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 },
12413 {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 },
12414 {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 },
12415 {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 },
12416 {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 },
12417 {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 },
12418 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12419 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12420 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12421 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12422 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12423 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12424 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12425 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12426 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12427 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12428 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12429 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12430 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12433 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12435 return TRUE;
12438 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12442 const struct fragment_pipeline glsl_fragment_pipe =
12444 glsl_fragment_pipe_enable,
12445 glsl_fragment_pipe_get_caps,
12446 glsl_fragment_pipe_get_emul_mask,
12447 glsl_fragment_pipe_alloc,
12448 glsl_fragment_pipe_free,
12449 glsl_fragment_pipe_alloc_context_data,
12450 glsl_fragment_pipe_free_context_data,
12451 shader_glsl_color_fixup_supported,
12452 glsl_fragment_pipe_state_template,
12455 struct glsl_blitter_args
12457 GLenum texture_type;
12458 struct color_fixup_desc fixup;
12459 unsigned short padding;
12462 struct glsl_blitter_program
12464 struct wine_rb_entry entry;
12465 struct glsl_blitter_args args;
12466 GLuint id;
12469 struct wined3d_glsl_blitter
12471 struct wined3d_blitter blitter;
12472 struct wined3d_string_buffer_list string_buffers;
12473 struct wine_rb_tree programs;
12474 GLuint palette_texture;
12477 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12479 const struct glsl_blitter_args *a = key;
12480 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12482 return memcmp(a, b, sizeof(*a));
12485 /* Context activation is done by the caller. */
12486 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12488 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12489 struct wined3d_context *context = ctx;
12490 const struct wined3d_gl_info *gl_info = context->gl_info;
12492 GL_EXTCALL(glDeleteProgram(program->id));
12493 checkGLcall("glDeleteProgram()");
12494 heap_free(program);
12497 /* Context activation is done by the caller. */
12498 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12500 const struct wined3d_gl_info *gl_info = context->gl_info;
12501 struct wined3d_glsl_blitter *glsl_blitter;
12502 struct wined3d_blitter *next;
12504 if ((next = blitter->next))
12505 next->ops->blitter_destroy(next, context);
12507 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12509 if (glsl_blitter->palette_texture)
12510 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12512 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context);
12513 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12515 heap_free(glsl_blitter);
12518 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12519 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12520 const char *output, const char *tex_type, const char *swizzle)
12522 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12523 shader_addline(buffer, "\nvoid main()\n{\n");
12524 /* The alpha-component contains the palette index. */
12525 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12526 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12527 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12528 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12529 * the middle. */
12530 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12531 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12532 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12533 shader_addline(buffer, "}\n");
12536 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12537 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12538 const char *tex_type)
12540 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12541 char chroma, luminance;
12542 const char *tex;
12544 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12545 * macropixel, giving effectively 16 bits per pixel. The color consists of
12546 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12547 * luminance values, one for each single pixel it contains, and one U and
12548 * one V value shared between both pixels.
12550 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12551 * component contains the luminance and alpha the chroma. With UYVY it is
12552 * vice versa. Thus take the format into account when generating the read
12553 * swizzles
12555 * Reading the Y value is straightforward - just sample the texture. The
12556 * hardware takes care of filtering in the horizontal and vertical
12557 * direction.
12559 * Reading the U and V values is harder. We have to avoid filtering
12560 * horizontally, because that would mix the U and V values of one pixel or
12561 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12562 * get an unfiltered read, regardless of the filtering setting. Vertical
12563 * filtering works automatically though - the U and V values of two rows
12564 * are mixed nicely.
12566 * Apart of avoiding filtering issues, the code has to know which value it
12567 * just read, and where it can find the other one. To determine this, it
12568 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12569 * accordingly.
12571 * Handling horizontal filtering of U and V values requires reading a 2nd
12572 * pair of pixels, extracting U and V and mixing them. This is not
12573 * implemented yet.
12575 * An alternative implementation idea is to load the texture as A8R8G8B8
12576 * texture, with width / 2. This way one read gives all 3 values, finding
12577 * U and V is easy in an unfiltered situation. Finding the luminance on
12578 * the other hand requires finding out if it is an odd or even pixel. The
12579 * real drawback of this approach is filtering. This would have to be
12580 * emulated completely in the shader, reading up two 2 packed pixels in up
12581 * to 2 rows and interpolating both horizontally and vertically. Beyond
12582 * that it would require adjustments to the texture handling code to deal
12583 * with the width scaling. */
12585 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12587 chroma = 'x';
12588 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12590 else
12592 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12593 luminance = 'x';
12596 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12598 /* First we have to read the chroma values. This means we need at least
12599 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12600 * unmodified chroma, we have to rid ourselves of the filtering when we
12601 * sample the texture. */
12602 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12603 /* We must not allow filtering between pixel x and x+1, this would mix U
12604 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12605 * center is at 0.5, so add 0.5. */
12606 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12607 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12609 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12610 * and 0.75 for the even and odd pixels respectively. */
12611 /* Put the value into either of the chroma values. */
12612 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12613 shader_addline(buffer, " if (even)\n");
12614 shader_addline(buffer, " chroma.y = luminance;\n");
12615 shader_addline(buffer, " else\n");
12616 shader_addline(buffer, " chroma.x = luminance;\n");
12618 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12619 * current one. Otherwise, sample the left pixel. */
12620 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12621 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12623 /* Put the value into the other chroma. */
12624 shader_addline(buffer, " if (even)\n");
12625 shader_addline(buffer, " chroma.x = luminance;\n");
12626 shader_addline(buffer, " else\n");
12627 shader_addline(buffer, " chroma.y = luminance;\n");
12629 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12630 * the current one and lerp the two U and V values. */
12632 /* This gives the correctly filtered luminance value. */
12633 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12636 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12637 const struct wined3d_gl_info *gl_info, const char *tex_type)
12639 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12640 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12642 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12643 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12644 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12645 * have only half the pitch of the luminance plane, the packing into the
12646 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12647 * luminance data it looks approximately like this:
12649 * +----------------------------------+----
12650 * | |
12651 * | |
12652 * | |
12653 * | |
12654 * | | 2
12655 * | LUMINANCE | -
12656 * | | 3
12657 * | |
12658 * | |
12659 * | |
12660 * | |
12661 * +----------------+-----------------+----
12662 * | | |
12663 * | V even rows | V odd rows |
12664 * | | | 1
12665 * +----------------+------------------ -
12666 * | | | 3
12667 * | U even rows | U odd rows |
12668 * | | |
12669 * +----------------+-----------------+----
12670 * | | |
12671 * | 0.5 | 0.5 |
12673 * So it appears as if there are 4 chroma images, but in fact the odd rows
12674 * in the chroma images are in the same row as the even ones. So it is
12675 * kinda tricky to read. */
12677 /* First sample the chroma values. */
12678 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12679 /* The chroma planes have only half the width. */
12680 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12682 /* The first value is between 2/3 and 5/6 of the texture's height, so
12683 * scale+bias the coordinate. Also read the right side of the image when
12684 * reading odd lines.
12686 * Don't forget to clamp the y values in into the range, otherwise we'll
12687 * get filtering bleeding. */
12689 /* Read odd lines from the right side (add 0.5 to the x coordinate). */
12690 shader_addline(buffer, " if (fract(floor(texcoord.y * size.y) * 0.5 + 1.0 / 6.0) >= 0.5)\n");
12691 shader_addline(buffer, " texcoord.x += 0.5;\n");
12693 /* Clamp, keep the half pixel origin in mind. */
12694 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12695 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12697 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12699 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12700 * 6/6th No need to clamp because we're just reusing the already clamped
12701 * value from above. */
12702 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12703 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12705 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12706 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12707 * values from bleeding into the sampled luminance values due to
12708 * filtering. */
12709 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12710 /* Multiply the y coordinate by 2/3 and clamp it. */
12711 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12712 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12715 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12716 const struct wined3d_gl_info *gl_info, const char *tex_type)
12718 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12719 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12721 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12722 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12723 * effective bitdepth is 12 bits per pixel. If the whole texture is
12724 * interpreted as luminance data it looks approximately like this:
12726 * +----------------------------------+----
12727 * | |
12728 * | |
12729 * | |
12730 * | |
12731 * | | 2
12732 * | LUMINANCE | -
12733 * | | 3
12734 * | |
12735 * | |
12736 * | |
12737 * | |
12738 * +----------------------------------+----
12739 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12740 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12741 * | | 1
12742 * | | -
12743 * | | 3
12744 * | |
12745 * | |
12746 * +----------------------------------+---- */
12748 /* First sample the chroma values. */
12749 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12750 /* We only have half the number of chroma pixels. */
12751 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12752 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12754 /* We must not allow filtering horizontally, this would mix U and V.
12755 * Vertical filtering is ok. However, bear in mind that the pixel center
12756 * is at 0.5, so add 0.5. */
12758 /* Convert to non-normalised coordinates so we can find the individual
12759 * pixel. */
12760 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12761 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12762 * 0.5 to hit the center of the pixel. Then convert back to normalised
12763 * coordinates. */
12764 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12765 /* Clamp, keep the half pixel origin in mind. */
12766 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12768 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12769 /* Add 1.0 / size.x to sample the adjacent texel. */
12770 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12771 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12773 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12774 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12775 * values from bleeding into the sampled luminance values due to
12776 * filtering. */
12777 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12778 /* Multiply the y coordinate by 2/3 and clamp it. */
12779 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12780 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12783 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12784 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12785 const char *output, const char *tex_type, const char *swizzle)
12787 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12789 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12790 shader_addline(buffer, "float luminance;\n");
12791 shader_addline(buffer, "vec2 texcoord;\n");
12792 shader_addline(buffer, "vec2 chroma;\n");
12793 shader_addline(buffer, "uniform vec2 size;\n");
12795 shader_addline(buffer, "\nvoid main()\n{\n");
12797 switch (complex_fixup)
12799 case COMPLEX_FIXUP_UYVY:
12800 case COMPLEX_FIXUP_YUY2:
12801 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12802 break;
12804 case COMPLEX_FIXUP_YV12:
12805 gen_yv12_read(buffer, gl_info, tex_type);
12806 break;
12808 case COMPLEX_FIXUP_NV12:
12809 gen_nv12_read(buffer, gl_info, tex_type);
12810 break;
12812 default:
12813 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12814 string_buffer_free(buffer);
12815 return;
12818 /* Calculate the final result. Formula is taken from
12819 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12820 * ranges from -0.5 to 0.5. */
12821 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12823 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12824 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12825 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12827 shader_addline(buffer, "}\n");
12830 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12831 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12832 const char *output, const char *tex_type, const char *swizzle)
12834 shader_addline(buffer, "\nvoid main()\n{\n");
12835 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12836 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12837 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12838 shader_addline(buffer, "}\n");
12841 /* Context activation is done by the caller. */
12842 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12843 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12845 static const struct
12847 GLenum texture_target;
12848 const char texture_type[7];
12849 const char texcoord_swizzle[4];
12851 texture_data[] =
12853 {GL_TEXTURE_2D, "2D", "xy"},
12854 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12855 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12857 static const char vshader_main[] =
12858 "\n"
12859 "void main()\n"
12860 "{\n"
12861 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12862 " out_texcoord = texcoord;\n"
12863 "}\n";
12864 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12865 struct wined3d_string_buffer *buffer, *output;
12866 GLuint program, vshader_id, fshader_id;
12867 const char *tex_type, *swizzle, *ptr;
12868 unsigned int i;
12869 GLint loc;
12871 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12873 if (args->texture_type == texture_data[i].texture_target)
12875 tex_type = texture_data[i].texture_type;
12876 swizzle = texture_data[i].texcoord_swizzle;
12877 break;
12880 if (i == ARRAY_SIZE(texture_data))
12882 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12883 return 0;
12886 program = GL_EXTCALL(glCreateProgram());
12888 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12890 buffer = string_buffer_get(&blitter->string_buffers);
12891 shader_glsl_add_version_declaration(buffer, gl_info);
12892 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12893 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12894 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12895 shader_addline(buffer, vshader_main);
12897 ptr = buffer->buffer;
12898 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12899 GL_EXTCALL(glAttachShader(program, vshader_id));
12900 GL_EXTCALL(glDeleteShader(vshader_id));
12902 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12904 string_buffer_clear(buffer);
12905 shader_glsl_add_version_declaration(buffer, gl_info);
12906 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12907 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12908 /* TODO: Declare the out variable with the correct type (and put it in the
12909 * blitter args). */
12910 if (!needs_legacy_glsl_syntax(gl_info))
12911 shader_addline(buffer, "out vec4 ps_out[1];\n");
12913 output = string_buffer_get(&blitter->string_buffers);
12914 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12916 switch (complex_fixup)
12918 case COMPLEX_FIXUP_P8:
12919 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12920 break;
12921 case COMPLEX_FIXUP_YUY2:
12922 case COMPLEX_FIXUP_UYVY:
12923 case COMPLEX_FIXUP_YV12:
12924 case COMPLEX_FIXUP_NV12:
12925 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12926 break;
12927 case COMPLEX_FIXUP_NONE:
12928 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12931 string_buffer_release(&blitter->string_buffers, output);
12933 ptr = buffer->buffer;
12934 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12935 string_buffer_release(&blitter->string_buffers, buffer);
12936 GL_EXTCALL(glAttachShader(program, fshader_id));
12937 GL_EXTCALL(glDeleteShader(fshader_id));
12939 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12940 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12942 if (!needs_legacy_glsl_syntax(gl_info))
12943 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12945 GL_EXTCALL(glCompileShader(vshader_id));
12946 print_glsl_info_log(gl_info, vshader_id, FALSE);
12947 GL_EXTCALL(glCompileShader(fshader_id));
12948 print_glsl_info_log(gl_info, fshader_id, FALSE);
12949 GL_EXTCALL(glLinkProgram(program));
12950 shader_glsl_validate_link(gl_info, program);
12952 GL_EXTCALL(glUseProgram(program));
12953 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12954 GL_EXTCALL(glUniform1i(loc, 0));
12955 if (complex_fixup == COMPLEX_FIXUP_P8)
12957 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12958 GL_EXTCALL(glUniform1i(loc, 1));
12961 return program;
12964 /* Context activation is done by the caller. */
12965 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12966 struct wined3d_context *context, const struct wined3d_texture *texture)
12968 const struct wined3d_gl_info *gl_info = context->gl_info;
12969 const struct wined3d_palette *palette;
12971 palette = texture->swapchain ? texture->swapchain->palette : NULL;
12973 if (!blitter->palette_texture)
12974 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12976 context_active_texture(context, gl_info, 1);
12977 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12978 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12979 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12980 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12982 if (palette)
12984 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12985 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12987 else
12989 static const DWORD black;
12991 FIXME("P8 texture loaded without a palette.\n");
12992 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12993 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12996 context_active_texture(context, gl_info, 0);
12999 /* Context activation is done by the caller. */
13000 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
13001 struct wined3d_context *context, const struct wined3d_texture *texture)
13003 const struct wined3d_gl_info *gl_info = context->gl_info;
13004 struct glsl_blitter_program *program;
13005 struct glsl_blitter_args args;
13006 struct wine_rb_entry *entry;
13008 memset(&args, 0, sizeof(args));
13009 args.texture_type = texture->target;
13010 args.fixup = texture->resource.format->color_fixup;
13012 if ((entry = wine_rb_get(&blitter->programs, &args)))
13013 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
13015 if (!(program = heap_alloc(sizeof(*program))))
13017 ERR("Failed to allocate blitter program memory.\n");
13018 return NULL;
13021 program->args = args;
13022 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
13024 WARN("Failed to generate blitter program.\n");
13025 heap_free(program);
13026 return NULL;
13029 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
13031 ERR("Failed to store blitter program.\n");
13032 GL_EXTCALL(glDeleteProgram(program->id));
13033 heap_free(program);
13034 return NULL;
13037 return program;
13040 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
13041 const struct wined3d_texture *src_texture, DWORD src_location,
13042 const struct wined3d_texture *dst_texture, DWORD dst_location)
13044 const struct wined3d_resource *src_resource = &src_texture->resource;
13045 const struct wined3d_resource *dst_resource = &dst_texture->resource;
13046 const struct wined3d_format *src_format = src_resource->format;
13047 const struct wined3d_format *dst_format = dst_resource->format;
13048 BOOL decompress;
13050 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13052 if (dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
13053 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13054 else
13055 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13058 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT)
13060 TRACE("Unsupported blit_op %#x.\n", blit_op);
13061 return FALSE;
13064 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13065 return FALSE;
13067 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13068 || dst_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13069 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY
13070 || dst_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13072 TRACE("Multi-sample textures not supported.\n");
13073 return FALSE;
13076 /* We don't necessarily want to blit from resources without
13077 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13078 * compressed textures. */
13079 decompress = src_format && (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
13080 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
13081 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13083 TRACE("Source or destination resource does not have GPU access.\n");
13084 return FALSE;
13087 if (!is_identity_fixup(dst_format->color_fixup)
13088 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13090 TRACE("Destination fixups are not supported.\n");
13091 return FALSE;
13094 TRACE("Returning supported.\n");
13095 return TRUE;
13098 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13099 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13100 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13101 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13102 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
13104 struct wined3d_device *device = dst_texture->resource.device;
13105 const struct wined3d_gl_info *gl_info = context->gl_info;
13106 struct wined3d_texture *staging_texture = NULL;
13107 struct wined3d_glsl_blitter *glsl_blitter;
13108 struct glsl_blitter_program *program;
13109 struct wined3d_blitter *next;
13110 unsigned int src_level;
13111 GLint location;
13112 RECT s, d;
13114 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
13115 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
13116 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13117 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13118 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
13120 if (!glsl_blitter_supported(op, context, src_texture, src_location, dst_texture, dst_location))
13122 if (!(next = blitter->next))
13124 ERR("No blitter to handle blit op %#x.\n", op);
13125 return dst_location;
13128 TRACE("Forwarding to blitter %p.\n", next);
13129 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13130 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
13133 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13135 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13137 struct wined3d_resource_desc desc;
13138 struct wined3d_box upload_box;
13139 HRESULT hr;
13141 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13143 src_level = src_sub_resource_idx % src_texture->level_count;
13144 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13145 desc.format = src_texture->resource.format->id;
13146 desc.multisample_type = src_texture->resource.multisample_type;
13147 desc.multisample_quality = src_texture->resource.multisample_quality;
13148 desc.usage = WINED3DUSAGE_PRIVATE;
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_sub_resource_idx = 0;
13169 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13170 && (src_texture->sub_resources[src_sub_resource_idx].locations
13171 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13172 && !wined3d_resource_is_offscreen(&src_texture->resource))
13175 /* Without FBO blits transferring from the drawable to the texture is
13176 * expensive, because we have to flip the data in sysmem. Since we can
13177 * flip in the blitter, we don't actually need that flip anyway. So we
13178 * use the surface's texture as scratch texture, and flip the source
13179 * rectangle instead. */
13180 texture2d_load_fb_texture(src_texture, src_sub_resource_idx, FALSE, context);
13182 s = *src_rect;
13183 src_level = src_sub_resource_idx % src_texture->level_count;
13184 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13185 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13186 src_rect = &s;
13188 else
13190 wined3d_texture_load(src_texture, context, FALSE);
13193 context_apply_blit_state(context, device);
13195 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13197 d = *dst_rect;
13198 wined3d_texture_translate_drawable_coords(dst_texture, context->win_handle, &d);
13199 dst_rect = &d;
13202 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
13204 GLenum buffer;
13206 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13208 TRACE("Destination texture %p is onscreen.\n", dst_texture);
13209 buffer = wined3d_texture_get_gl_buffer(dst_texture);
13211 else
13213 TRACE("Destination texture %p is offscreen.\n", dst_texture);
13214 buffer = GL_COLOR_ATTACHMENT0;
13216 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER,
13217 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
13218 context_set_draw_buffer(context, buffer);
13219 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
13220 context_invalidate_state(context, STATE_FRAMEBUFFER);
13223 if (!(program = glsl_blitter_get_program(glsl_blitter, context, src_texture)))
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, src_texture);
13233 break;
13235 case COMPLEX_FIXUP_YUY2:
13236 case COMPLEX_FIXUP_UYVY:
13237 case COMPLEX_FIXUP_YV12:
13238 case COMPLEX_FIXUP_NV12:
13239 src_level = src_sub_resource_idx % src_texture->level_count;
13240 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13241 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13242 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13243 break;
13245 default:
13246 break;
13248 context_draw_shaded_quad(context, src_texture, src_sub_resource_idx, src_rect, dst_rect, filter);
13249 GL_EXTCALL(glUseProgram(0));
13251 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13252 gl_info->gl_ops.gl.p_glFlush();
13254 if (staging_texture)
13255 wined3d_texture_decref(staging_texture);
13257 return dst_location;
13260 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13261 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13262 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
13264 struct wined3d_blitter *next;
13266 if ((next = blitter->next))
13267 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13268 clear_rects, draw_rect, flags, color, depth, stencil);
13271 static const struct wined3d_blitter_ops glsl_blitter_ops =
13273 glsl_blitter_destroy,
13274 glsl_blitter_clear,
13275 glsl_blitter_blit,
13278 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13279 const struct wined3d_device *device)
13281 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13282 struct wined3d_glsl_blitter *blitter;
13284 if (device->shader_backend != &glsl_shader_backend)
13285 return NULL;
13287 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13288 return NULL;
13290 if (!(blitter = heap_alloc(sizeof(*blitter))))
13292 ERR("Failed to allocate blitter.\n");
13293 return NULL;
13296 TRACE("Created blitter %p.\n", blitter);
13298 blitter->blitter.ops = &glsl_blitter_ops;
13299 blitter->blitter.next = *next;
13300 string_buffer_list_init(&blitter->string_buffers);
13301 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13302 blitter->palette_texture = 0;
13303 *next = &blitter->blitter;
13305 return *next;