wined3d: Introduce helper function to generate conditional instructions.
[wine.git] / dlls / wined3d / glsl_shader.c
blob98cd32177f105aa94a1cebffb3eeb1ea4cb408bb
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 struct glsl_dst_param
76 char reg_name[150];
77 char mask_str[6];
80 struct glsl_src_param
82 char reg_name[150];
83 char param_str[200];
86 struct glsl_sample_function
88 struct wined3d_string_buffer *name;
89 unsigned int coord_mask;
90 unsigned int deriv_mask;
91 enum wined3d_data_type data_type;
92 BOOL output_single_component;
93 unsigned int offset_size;
96 enum heap_node_op
98 HEAP_NODE_TRAVERSE_LEFT,
99 HEAP_NODE_TRAVERSE_RIGHT,
100 HEAP_NODE_POP,
103 struct constant_entry
105 unsigned int idx;
106 unsigned int version;
109 struct constant_heap
111 struct constant_entry *entries;
112 BOOL *contained;
113 unsigned int *positions;
114 unsigned int size;
117 /* GLSL shader private data */
118 struct shader_glsl_priv
120 struct wined3d_string_buffer shader_buffer;
121 struct wined3d_string_buffer_list string_buffers;
122 struct wine_rb_tree program_lookup;
123 struct constant_heap vconst_heap;
124 struct constant_heap pconst_heap;
125 unsigned char *stack;
126 UINT next_constant_version;
128 const struct wined3d_vertex_pipe_ops *vertex_pipe;
129 const struct fragment_pipeline *fragment_pipe;
130 struct wine_rb_tree ffp_vertex_shaders;
131 struct wine_rb_tree ffp_fragment_shaders;
132 BOOL ffp_proj_control;
133 BOOL legacy_lighting;
136 struct glsl_vs_program
138 struct list shader_entry;
139 GLuint id;
140 GLenum vertex_color_clamp;
141 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
142 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
143 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
144 GLint pos_fixup_location;
146 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
147 GLint projection_matrix_location;
148 GLint normal_matrix_location;
149 GLint texture_matrix_location[MAX_TEXTURES];
150 GLint material_ambient_location;
151 GLint material_diffuse_location;
152 GLint material_specular_location;
153 GLint material_emissive_location;
154 GLint material_shininess_location;
155 GLint light_ambient_location;
156 struct
158 GLint diffuse;
159 GLint specular;
160 GLint ambient;
161 GLint position;
162 GLint direction;
163 GLint range;
164 GLint falloff;
165 GLint c_att;
166 GLint l_att;
167 GLint q_att;
168 GLint cos_htheta;
169 GLint cos_hphi;
170 } light_location[MAX_ACTIVE_LIGHTS];
171 GLint pointsize_location;
172 GLint pointsize_min_location;
173 GLint pointsize_max_location;
174 GLint pointsize_c_att_location;
175 GLint pointsize_l_att_location;
176 GLint pointsize_q_att_location;
177 GLint clip_planes_location;
180 struct glsl_hs_program
182 struct list shader_entry;
183 GLuint id;
186 struct glsl_ds_program
188 struct list shader_entry;
189 GLuint id;
191 GLint pos_fixup_location;
194 struct glsl_gs_program
196 struct list shader_entry;
197 GLuint id;
199 GLint pos_fixup_location;
202 struct glsl_ps_program
204 struct list shader_entry;
205 GLuint id;
206 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
207 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
208 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
209 GLint bumpenv_mat_location[MAX_TEXTURES];
210 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
211 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
212 GLint tss_constant_location[MAX_TEXTURES];
213 GLint tex_factor_location;
214 GLint specular_enable_location;
215 GLint fog_color_location;
216 GLint fog_density_location;
217 GLint fog_end_location;
218 GLint fog_scale_location;
219 GLint alpha_test_ref_location;
220 GLint ycorrection_location;
221 GLint np2_fixup_location;
222 GLint color_key_location;
223 const struct ps_np2fixup_info *np2_fixup_info;
226 struct glsl_cs_program
228 struct list shader_entry;
229 GLuint id;
232 /* Struct to maintain data about a linked GLSL program */
233 struct glsl_shader_prog_link
235 struct wine_rb_entry program_lookup_entry;
236 struct glsl_vs_program vs;
237 struct glsl_hs_program hs;
238 struct glsl_ds_program ds;
239 struct glsl_gs_program gs;
240 struct glsl_ps_program ps;
241 struct glsl_cs_program cs;
242 GLuint id;
243 DWORD constant_update_mask;
244 UINT constant_version;
247 struct glsl_program_key
249 GLuint vs_id;
250 GLuint hs_id;
251 GLuint ds_id;
252 GLuint gs_id;
253 GLuint ps_id;
254 GLuint cs_id;
257 struct shader_glsl_ctx_priv {
258 const struct vs_compile_args *cur_vs_args;
259 const struct ds_compile_args *cur_ds_args;
260 const struct ps_compile_args *cur_ps_args;
261 struct ps_np2fixup_info *cur_np2fixup_info;
262 struct wined3d_string_buffer_list *string_buffers;
265 struct glsl_context_data
267 struct glsl_shader_prog_link *glsl_program;
268 GLenum vertex_color_clamp;
269 BOOL rasterization_disabled;
272 struct glsl_ps_compiled_shader
274 struct ps_compile_args args;
275 struct ps_np2fixup_info np2fixup;
276 GLuint id;
279 struct glsl_vs_compiled_shader
281 struct vs_compile_args args;
282 GLuint id;
285 struct glsl_hs_compiled_shader
287 GLuint id;
290 struct glsl_ds_compiled_shader
292 struct ds_compile_args args;
293 GLuint id;
296 struct glsl_gs_compiled_shader
298 struct gs_compile_args args;
299 GLuint id;
302 struct glsl_cs_compiled_shader
304 GLuint id;
307 struct glsl_shader_private
309 union
311 struct glsl_vs_compiled_shader *vs;
312 struct glsl_hs_compiled_shader *hs;
313 struct glsl_ds_compiled_shader *ds;
314 struct glsl_gs_compiled_shader *gs;
315 struct glsl_ps_compiled_shader *ps;
316 struct glsl_cs_compiled_shader *cs;
317 } gl_shaders;
318 unsigned int num_gl_shaders, shader_array_size;
321 struct glsl_ffp_vertex_shader
323 struct wined3d_ffp_vs_desc desc;
324 GLuint id;
325 struct list linked_programs;
328 struct glsl_ffp_fragment_shader
330 struct ffp_frag_desc entry;
331 GLuint id;
332 struct list linked_programs;
335 struct glsl_ffp_destroy_ctx
337 struct shader_glsl_priv *priv;
338 const struct wined3d_gl_info *gl_info;
341 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
343 static const char *debug_gl_shader_type(GLenum type)
345 switch (type)
347 #define WINED3D_TO_STR(u) case u: return #u
348 WINED3D_TO_STR(GL_VERTEX_SHADER);
349 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
350 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
351 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
352 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
353 WINED3D_TO_STR(GL_COMPUTE_SHADER);
354 #undef WINED3D_TO_STR
355 default:
356 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
360 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
362 switch (type)
364 case WINED3D_SHADER_TYPE_VERTEX:
365 return "vs";
367 case WINED3D_SHADER_TYPE_HULL:
368 return "hs";
370 case WINED3D_SHADER_TYPE_DOMAIN:
371 return "ds";
373 case WINED3D_SHADER_TYPE_GEOMETRY:
374 return "gs";
376 case WINED3D_SHADER_TYPE_PIXEL:
377 return "ps";
379 case WINED3D_SHADER_TYPE_COMPUTE:
380 return "cs";
382 default:
383 FIXME("Unhandled shader type %#x.\n", type);
384 return "unknown";
388 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
390 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
391 return 440;
392 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
393 return 150;
394 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
395 return 130;
396 else
397 return 120;
400 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
401 const struct wined3d_gl_info *gl_info)
403 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
406 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
408 char str[4][17];
410 wined3d_ftoa(values[0], str[0]);
411 wined3d_ftoa(values[1], str[1]);
412 wined3d_ftoa(values[2], str[2]);
413 wined3d_ftoa(values[3], str[3]);
414 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
417 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
418 const int *values, unsigned int size)
420 int i;
422 if (!size || size > 4)
424 ERR("Invalid vector size %u.\n", size);
425 return;
428 if (size > 1)
429 shader_addline(buffer, "ivec%u(", size);
431 for (i = 0; i < size; ++i)
432 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
434 if (size > 1)
435 shader_addline(buffer, ")");
438 static const char *get_info_log_line(const char **ptr)
440 const char *p, *q;
442 p = *ptr;
443 if (!(q = strstr(p, "\n")))
445 if (!*p) return NULL;
446 *ptr += strlen(p);
447 return p;
449 *ptr = q + 1;
451 return p;
454 /* Context activation is done by the caller. */
455 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
457 int length = 0;
458 char *log;
460 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
461 return;
463 if (program)
464 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
465 else
466 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
468 /* A size of 1 is just a null-terminated string, so the log should be bigger than
469 * that if there are errors. */
470 if (length > 1)
472 const char *ptr, *line;
474 log = HeapAlloc(GetProcessHeap(), 0, length);
475 /* The info log is supposed to be zero-terminated, but at least some
476 * versions of fglrx don't terminate the string properly. The reported
477 * length does include the terminator, so explicitly set it to zero
478 * here. */
479 log[length - 1] = 0;
480 if (program)
481 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
482 else
483 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
485 ptr = log;
486 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
488 WARN("Info log received from GLSL shader #%u:\n", id);
489 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
491 else
493 FIXME("Info log received from GLSL shader #%u:\n", id);
494 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
496 HeapFree(GetProcessHeap(), 0, log);
500 /* Context activation is done by the caller. */
501 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
503 const char *ptr, *line;
505 TRACE("Compiling shader object %u.\n", shader);
507 if (TRACE_ON(d3d_shader))
509 ptr = src;
510 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
513 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
514 checkGLcall("glShaderSource");
515 GL_EXTCALL(glCompileShader(shader));
516 checkGLcall("glCompileShader");
517 print_glsl_info_log(gl_info, shader, FALSE);
520 /* Context activation is done by the caller. */
521 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
523 GLint i, shader_count, source_size = -1;
524 GLuint *shaders;
525 char *source = NULL;
527 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
528 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
530 ERR("Failed to allocate shader array memory.\n");
531 return;
534 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
535 for (i = 0; i < shader_count; ++i)
537 const char *ptr, *line;
538 GLint tmp;
540 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
542 if (source_size < tmp)
544 HeapFree(GetProcessHeap(), 0, source);
546 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
547 if (!source)
549 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
550 HeapFree(GetProcessHeap(), 0, shaders);
551 return;
553 source_size = tmp;
556 FIXME("Shader %u:\n", shaders[i]);
557 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
558 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
559 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
560 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
561 FIXME("\n");
563 ptr = source;
564 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
565 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
566 FIXME("\n");
569 HeapFree(GetProcessHeap(), 0, source);
570 HeapFree(GetProcessHeap(), 0, shaders);
573 /* Context activation is done by the caller. */
574 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
576 GLint tmp;
578 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
579 return;
581 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
582 if (!tmp)
584 FIXME("Program %u link status invalid.\n", program);
585 shader_glsl_dump_program_source(gl_info, program);
588 print_glsl_info_log(gl_info, program, TRUE);
591 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
593 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
594 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
595 * versions. */
596 return shader_glsl_get_version(gl_info) >= 140;
599 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
601 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
604 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
605 struct shader_glsl_priv *priv, GLuint program_id,
606 const struct wined3d_shader_reg_maps *reg_maps)
608 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
609 struct wined3d_string_buffer *name;
610 unsigned int i, base, count;
611 GLuint block_idx;
613 if (shader_glsl_use_layout_binding_qualifier(gl_info))
614 return;
616 name = string_buffer_get(&priv->string_buffers);
617 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
618 for (i = 0; i < count; ++i)
620 if (!reg_maps->cb_sizes[i])
621 continue;
623 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
624 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
625 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
627 checkGLcall("glUniformBlockBinding");
628 string_buffer_release(&priv->string_buffers, name);
631 /* Context activation is done by the caller. */
632 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
633 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
634 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
636 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
637 unsigned int i, mapped_unit;
638 GLint name_loc;
640 for (i = 0; i < count; ++i)
642 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
643 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
644 if (name_loc == -1)
645 continue;
647 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
648 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
650 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
651 continue;
654 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
655 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
657 checkGLcall("Load sampler bindings");
658 string_buffer_release(&priv->string_buffers, sampler_name);
661 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
662 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
664 const DWORD *tex_unit_map;
665 unsigned int base, count;
667 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
668 if (sampler_idx >= count)
669 return WINED3D_UNMAPPED_STAGE;
670 if (!tex_unit_map)
671 return base + sampler_idx;
672 return tex_unit_map[base + sampler_idx];
675 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
676 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
677 unsigned int sampler_idx)
679 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
680 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
681 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
682 else
683 ERR("Unmapped sampler %u.\n", sampler_idx);
686 /* Context activation is done by the caller. */
687 static void shader_glsl_load_samplers(const struct wined3d_context *context,
688 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
690 const struct wined3d_gl_info *gl_info = context->gl_info;
691 const struct wined3d_shader_version *shader_version;
692 const DWORD *tex_unit_map;
693 unsigned int base, count;
694 const char *prefix;
696 if (shader_glsl_use_layout_binding_qualifier(gl_info))
697 return;
699 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
700 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
701 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
702 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
705 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
706 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
708 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
710 if (icb)
712 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
713 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
714 GLint icb_location;
716 string_buffer_sprintf(icb_name, "%s_icb", prefix);
717 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
718 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
719 checkGLcall("Load immediate constant buffer");
721 string_buffer_release(&priv->string_buffers, icb_name);
725 /* Context activation is done by the caller. */
726 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
727 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
729 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
730 struct wined3d_string_buffer *name;
731 GLint location;
732 unsigned int i;
734 if (shader_glsl_use_layout_binding_qualifier(gl_info))
735 return;
737 name = string_buffer_get(&priv->string_buffers);
738 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
740 if (!reg_maps->uav_resource_info[i].type)
741 continue;
743 string_buffer_sprintf(name, "%s_image%u", prefix, i);
744 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
745 if (location == -1)
746 continue;
748 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
749 GL_EXTCALL(glUniform1i(location, i));
751 checkGLcall("Load image bindings");
752 string_buffer_release(&priv->string_buffers, name);
755 /* Context activation is done by the caller. */
756 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
757 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
759 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
761 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
762 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
763 /* Texture unit mapping is set up to be the same each time the shader
764 * program is used so we can hardcode the sampler uniform values. */
765 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
768 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
769 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
771 if (varyings && *strings)
773 char *ptr = *strings;
775 varyings[*varying_count] = ptr;
777 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
778 ptr += buffer->content_size + 1;
780 *strings = ptr;
783 *strings_length += buffer->content_size + 1;
784 ++(*varying_count);
787 static void append_transform_feedback_skip_components(const char **varyings,
788 unsigned int *varying_count, char **strings, unsigned int *strings_length,
789 struct wined3d_string_buffer *buffer, unsigned int component_count)
791 unsigned int j;
793 for (j = 0; j < component_count / 4; ++j)
795 string_buffer_sprintf(buffer, "gl_SkipComponents4");
796 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
798 if (component_count % 4)
800 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
801 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
805 static void shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
806 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
807 char *strings, unsigned int *strings_length, GLenum buffer_mode)
809 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
811 count = length = 0;
812 highest_output_slot = 0;
813 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
815 stride = 0;
817 for (i = 0; i < so_desc->element_count; ++i)
819 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
821 highest_output_slot = max(highest_output_slot, e->output_slot);
822 if (e->output_slot != buffer_idx)
823 continue;
825 if (e->stream_idx)
827 FIXME("Unhandled stream %u.\n", e->stream_idx);
828 continue;
831 stride += e->component_count;
833 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
835 append_transform_feedback_skip_components(varyings, &count,
836 &strings, &length, buffer, e->component_count);
837 continue;
840 if (e->component_idx || e->component_count != 4)
842 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
844 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
845 append_transform_feedback_skip_components(varyings, &count,
846 &strings, &length, buffer, e->component_count);
847 continue;
850 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
851 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
852 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
854 else
856 string_buffer_sprintf(buffer, "shader_in_out.reg%u", e->register_idx);
857 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
861 if (buffer_idx < so_desc->buffer_stride_count
862 && stride < so_desc->buffer_strides[buffer_idx] / 4)
864 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
865 append_transform_feedback_skip_components(varyings, &count,
866 &strings, &length, buffer, component_count);
869 if (highest_output_slot <= buffer_idx)
870 break;
872 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
874 string_buffer_sprintf(buffer, "gl_NextBuffer");
875 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
879 if (varying_count)
880 *varying_count = count;
881 if (strings_length)
882 *strings_length = length;
885 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
886 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
888 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
889 const struct wined3d_gl_info *gl_info = context->gl_info;
890 struct wined3d_string_buffer *buffer;
891 unsigned int i, count, length;
892 const char **varyings;
893 char *strings;
894 GLenum mode;
896 if (!so_desc->element_count)
897 return;
899 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
901 mode = GL_INTERLEAVED_ATTRIBS;
903 else
905 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
907 for (i = 0; i < so_desc->element_count; ++i)
909 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
911 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
912 return;
914 ++element_count[so_desc->elements[i].output_slot];
917 if (element_count[0] == so_desc->element_count)
919 mode = GL_INTERLEAVED_ATTRIBS;
921 else
923 mode = GL_SEPARATE_ATTRIBS;
924 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
926 if (element_count[i] != 1)
927 break;
929 for (; i < ARRAY_SIZE(element_count); ++i)
931 if (element_count[i])
933 FIXME("Only single element per buffer is allowed in separate mode.\n");
934 return;
940 buffer = string_buffer_get(&priv->string_buffers);
942 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode);
944 if (!(varyings = wined3d_calloc(count, sizeof(*varyings))))
946 ERR("Out of memory.\n");
947 string_buffer_release(&priv->string_buffers, buffer);
948 return;
950 if (!(strings = wined3d_calloc(length, sizeof(*strings))))
952 ERR("Out of memory.\n");
953 HeapFree(GetProcessHeap(), 0, varyings);
954 string_buffer_release(&priv->string_buffers, buffer);
955 return;
958 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
959 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
960 checkGLcall("glTransformFeedbackVaryings");
962 HeapFree(GetProcessHeap(), 0, varyings);
963 HeapFree(GetProcessHeap(), 0, strings);
964 string_buffer_release(&priv->string_buffers, buffer);
967 /* Context activation is done by the caller. */
968 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
969 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
971 unsigned int start = ~0U, end = 0;
972 int stack_idx = 0;
973 unsigned int heap_idx = 1;
974 unsigned int idx;
976 if (heap->entries[heap_idx].version <= version) return;
978 idx = heap->entries[heap_idx].idx;
979 if (constant_locations[idx] != -1)
980 start = end = idx;
981 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
983 while (stack_idx >= 0)
985 /* Note that we fall through to the next case statement. */
986 switch(stack[stack_idx])
988 case HEAP_NODE_TRAVERSE_LEFT:
990 unsigned int left_idx = heap_idx << 1;
991 if (left_idx < heap->size && heap->entries[left_idx].version > version)
993 heap_idx = left_idx;
994 idx = heap->entries[heap_idx].idx;
995 if (constant_locations[idx] != -1)
997 if (start > idx)
998 start = idx;
999 if (end < idx)
1000 end = idx;
1003 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1004 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1005 break;
1009 case HEAP_NODE_TRAVERSE_RIGHT:
1011 unsigned int right_idx = (heap_idx << 1) + 1;
1012 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1014 heap_idx = right_idx;
1015 idx = heap->entries[heap_idx].idx;
1016 if (constant_locations[idx] != -1)
1018 if (start > idx)
1019 start = idx;
1020 if (end < idx)
1021 end = idx;
1024 stack[stack_idx++] = HEAP_NODE_POP;
1025 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1026 break;
1030 case HEAP_NODE_POP:
1031 heap_idx >>= 1;
1032 --stack_idx;
1033 break;
1036 if (start <= end)
1037 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1038 checkGLcall("walk_constant_heap()");
1041 /* Context activation is done by the caller. */
1042 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1043 GLint location, const struct wined3d_vec4 *data)
1045 GLfloat clamped_constant[4];
1047 if (location == -1) return;
1049 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1050 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1051 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1052 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1054 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1057 /* Context activation is done by the caller. */
1058 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1059 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1060 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1062 int stack_idx = 0;
1063 unsigned int heap_idx = 1;
1064 unsigned int idx;
1066 if (heap->entries[heap_idx].version <= version) return;
1068 idx = heap->entries[heap_idx].idx;
1069 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1070 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1072 while (stack_idx >= 0)
1074 /* Note that we fall through to the next case statement. */
1075 switch(stack[stack_idx])
1077 case HEAP_NODE_TRAVERSE_LEFT:
1079 unsigned int left_idx = heap_idx << 1;
1080 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1082 heap_idx = left_idx;
1083 idx = heap->entries[heap_idx].idx;
1084 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1086 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1087 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1088 break;
1092 case HEAP_NODE_TRAVERSE_RIGHT:
1094 unsigned int right_idx = (heap_idx << 1) + 1;
1095 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1097 heap_idx = right_idx;
1098 idx = heap->entries[heap_idx].idx;
1099 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1101 stack[stack_idx++] = HEAP_NODE_POP;
1102 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1103 break;
1107 case HEAP_NODE_POP:
1108 heap_idx >>= 1;
1109 --stack_idx;
1110 break;
1113 checkGLcall("walk_constant_heap_clamped()");
1116 /* Context activation is done by the caller. */
1117 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1118 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1119 unsigned char *stack, unsigned int version)
1121 const struct wined3d_shader_lconst *lconst;
1123 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1124 if (shader->reg_maps.shader_version.major == 1
1125 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1126 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1127 else
1128 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1130 if (!shader->load_local_constsF)
1132 TRACE("No need to load local float constants for this shader.\n");
1133 return;
1136 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1137 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1139 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1141 checkGLcall("glUniform4fv()");
1144 /* Context activation is done by the caller. */
1145 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1146 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1148 unsigned int i;
1149 struct list* ptr;
1151 for (i = 0; constants_set; constants_set >>= 1, ++i)
1153 if (!(constants_set & 1)) continue;
1155 /* We found this uniform name in the program - go ahead and send the data */
1156 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1159 /* Load immediate constants */
1160 ptr = list_head(&shader->constantsI);
1161 while (ptr)
1163 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1164 unsigned int idx = lconst->idx;
1165 const GLint *values = (const GLint *)lconst->value;
1167 /* We found this uniform name in the program - go ahead and send the data */
1168 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1169 ptr = list_next(&shader->constantsI, ptr);
1171 checkGLcall("glUniform4iv()");
1174 /* Context activation is done by the caller. */
1175 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1176 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1178 unsigned int i;
1179 struct list* ptr;
1181 for (i = 0; constants_set; constants_set >>= 1, ++i)
1183 if (!(constants_set & 1)) continue;
1185 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1188 /* Load immediate constants */
1189 ptr = list_head(&shader->constantsB);
1190 while (ptr)
1192 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1193 unsigned int idx = lconst->idx;
1194 const GLint *values = (const GLint *)lconst->value;
1196 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1197 ptr = list_next(&shader->constantsB, ptr);
1199 checkGLcall("glUniform1iv()");
1202 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1204 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1207 /* Context activation is done by the caller (state handler). */
1208 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1209 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1211 struct
1213 float sx, sy;
1215 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1216 UINT fixup = ps->np2_fixup_info->active;
1217 UINT i;
1219 for (i = 0; fixup; fixup >>= 1, ++i)
1221 const struct wined3d_texture *tex = state->textures[i];
1222 unsigned char idx = ps->np2_fixup_info->idx[i];
1224 if (!tex)
1226 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1227 continue;
1230 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1231 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1234 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1237 /* Taken and adapted from Mesa. */
1238 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1240 float pos, neg, t, det;
1241 struct wined3d_matrix temp;
1243 /* Calculate the determinant of upper left 3x3 submatrix and
1244 * determine if the matrix is singular. */
1245 pos = neg = 0.0f;
1246 t = in->_11 * in->_22 * in->_33;
1247 if (t >= 0.0f)
1248 pos += t;
1249 else
1250 neg += t;
1252 t = in->_21 * in->_32 * in->_13;
1253 if (t >= 0.0f)
1254 pos += t;
1255 else
1256 neg += t;
1257 t = in->_31 * in->_12 * in->_23;
1258 if (t >= 0.0f)
1259 pos += t;
1260 else
1261 neg += t;
1263 t = -in->_31 * in->_22 * in->_13;
1264 if (t >= 0.0f)
1265 pos += t;
1266 else
1267 neg += t;
1268 t = -in->_21 * in->_12 * in->_33;
1269 if (t >= 0.0f)
1270 pos += t;
1271 else
1272 neg += t;
1274 t = -in->_11 * in->_32 * in->_23;
1275 if (t >= 0.0f)
1276 pos += t;
1277 else
1278 neg += t;
1280 det = pos + neg;
1282 if (fabsf(det) < 1e-25f)
1283 return FALSE;
1285 det = 1.0f / det;
1286 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1287 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1288 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1289 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1290 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1291 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1292 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1293 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1294 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1296 *out = temp;
1297 return TRUE;
1300 static void swap_rows(float **a, float **b)
1302 float *tmp = *a;
1304 *a = *b;
1305 *b = tmp;
1308 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
1310 float wtmp[4][8];
1311 float m0, m1, m2, m3, s;
1312 float *r0, *r1, *r2, *r3;
1314 r0 = wtmp[0];
1315 r1 = wtmp[1];
1316 r2 = wtmp[2];
1317 r3 = wtmp[3];
1319 r0[0] = m->_11;
1320 r0[1] = m->_12;
1321 r0[2] = m->_13;
1322 r0[3] = m->_14;
1323 r0[4] = 1.0f;
1324 r0[5] = r0[6] = r0[7] = 0.0f;
1326 r1[0] = m->_21;
1327 r1[1] = m->_22;
1328 r1[2] = m->_23;
1329 r1[3] = m->_24;
1330 r1[5] = 1.0f;
1331 r1[4] = r1[6] = r1[7] = 0.0f;
1333 r2[0] = m->_31;
1334 r2[1] = m->_32;
1335 r2[2] = m->_33;
1336 r2[3] = m->_34;
1337 r2[6] = 1.0f;
1338 r2[4] = r2[5] = r2[7] = 0.0f;
1340 r3[0] = m->_41;
1341 r3[1] = m->_42;
1342 r3[2] = m->_43;
1343 r3[3] = m->_44;
1344 r3[7] = 1.0f;
1345 r3[4] = r3[5] = r3[6] = 0.0f;
1347 /* Choose pivot - or die. */
1348 if (fabsf(r3[0]) > fabsf(r2[0]))
1349 swap_rows(&r3, &r2);
1350 if (fabsf(r2[0]) > fabsf(r1[0]))
1351 swap_rows(&r2, &r1);
1352 if (fabsf(r1[0]) > fabsf(r0[0]))
1353 swap_rows(&r1, &r0);
1354 if (r0[0] == 0.0f)
1355 return FALSE;
1357 /* Eliminate first variable. */
1358 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1359 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1360 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1361 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1362 s = r0[4];
1363 if (s != 0.0f)
1365 r1[4] -= m1 * s;
1366 r2[4] -= m2 * s;
1367 r3[4] -= m3 * s;
1369 s = r0[5];
1370 if (s != 0.0f)
1372 r1[5] -= m1 * s;
1373 r2[5] -= m2 * s;
1374 r3[5] -= m3 * s;
1376 s = r0[6];
1377 if (s != 0.0f)
1379 r1[6] -= m1 * s;
1380 r2[6] -= m2 * s;
1381 r3[6] -= m3 * s;
1383 s = r0[7];
1384 if (s != 0.0f)
1386 r1[7] -= m1 * s;
1387 r2[7] -= m2 * s;
1388 r3[7] -= m3 * s;
1391 /* Choose pivot - or die. */
1392 if (fabsf(r3[1]) > fabsf(r2[1]))
1393 swap_rows(&r3, &r2);
1394 if (fabsf(r2[1]) > fabsf(r1[1]))
1395 swap_rows(&r2, &r1);
1396 if (r1[1] == 0.0f)
1397 return FALSE;
1399 /* Eliminate second variable. */
1400 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1401 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1402 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1403 s = r1[4];
1404 if (s != 0.0f)
1406 r2[4] -= m2 * s;
1407 r3[4] -= m3 * s;
1409 s = r1[5];
1410 if (s != 0.0f)
1412 r2[5] -= m2 * s;
1413 r3[5] -= m3 * s;
1415 s = r1[6];
1416 if (s != 0.0f)
1418 r2[6] -= m2 * s;
1419 r3[6] -= m3 * s;
1421 s = r1[7];
1422 if (s != 0.0f)
1424 r2[7] -= m2 * s;
1425 r3[7] -= m3 * s;
1428 /* Choose pivot - or die. */
1429 if (fabsf(r3[2]) > fabsf(r2[2]))
1430 swap_rows(&r3, &r2);
1431 if (r2[2] == 0.0f)
1432 return FALSE;
1434 /* Eliminate third variable. */
1435 m3 = r3[2] / r2[2];
1436 r3[3] -= m3 * r2[3];
1437 r3[4] -= m3 * r2[4];
1438 r3[5] -= m3 * r2[5];
1439 r3[6] -= m3 * r2[6];
1440 r3[7] -= m3 * r2[7];
1442 /* Last check. */
1443 if (r3[3] == 0.0f)
1444 return FALSE;
1446 /* Back substitute row 3. */
1447 s = 1.0f / r3[3];
1448 r3[4] *= s;
1449 r3[5] *= s;
1450 r3[6] *= s;
1451 r3[7] *= s;
1453 /* Back substitute row 2. */
1454 m2 = r2[3];
1455 s = 1.0f / r2[2];
1456 r2[4] = s * (r2[4] - r3[4] * m2);
1457 r2[5] = s * (r2[5] - r3[5] * m2);
1458 r2[6] = s * (r2[6] - r3[6] * m2);
1459 r2[7] = s * (r2[7] - r3[7] * m2);
1460 m1 = r1[3];
1461 r1[4] -= r3[4] * m1;
1462 r1[5] -= r3[5] * m1;
1463 r1[6] -= r3[6] * m1;
1464 r1[7] -= r3[7] * m1;
1465 m0 = r0[3];
1466 r0[4] -= r3[4] * m0;
1467 r0[5] -= r3[5] * m0;
1468 r0[6] -= r3[6] * m0;
1469 r0[7] -= r3[7] * m0;
1471 /* Back substitute row 1. */
1472 m1 = r1[2];
1473 s = 1.0f / r1[1];
1474 r1[4] = s * (r1[4] - r2[4] * m1);
1475 r1[5] = s * (r1[5] - r2[5] * m1);
1476 r1[6] = s * (r1[6] - r2[6] * m1);
1477 r1[7] = s * (r1[7] - r2[7] * m1);
1478 m0 = r0[2];
1479 r0[4] -= r2[4] * m0;
1480 r0[5] -= r2[5] * m0;
1481 r0[6] -= r2[6] * m0;
1482 r0[7] -= r2[7] * m0;
1484 /* Back substitute row 0. */
1485 m0 = r0[1];
1486 s = 1.0f / r0[0];
1487 r0[4] = s * (r0[4] - r1[4] * m0);
1488 r0[5] = s * (r0[5] - r1[5] * m0);
1489 r0[6] = s * (r0[6] - r1[6] * m0);
1490 r0[7] = s * (r0[7] - r1[7] * m0);
1492 out->_11 = r0[4];
1493 out->_12 = r0[5];
1494 out->_13 = r0[6];
1495 out->_14 = r0[7];
1496 out->_21 = r1[4];
1497 out->_22 = r1[5];
1498 out->_23 = r1[6];
1499 out->_24 = r1[7];
1500 out->_31 = r2[4];
1501 out->_32 = r2[5];
1502 out->_33 = r2[6];
1503 out->_34 = r2[7];
1504 out->_41 = r3[4];
1505 out->_42 = r3[5];
1506 out->_43 = r3[6];
1507 out->_44 = r3[7];
1509 return TRUE;
1512 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1513 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1515 const struct wined3d_gl_info *gl_info = context->gl_info;
1516 float mat[3 * 3];
1517 struct wined3d_matrix mv;
1518 unsigned int i, j;
1520 if (prog->vs.normal_matrix_location == -1)
1521 return;
1523 get_modelview_matrix(context, state, 0, &mv);
1524 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1525 invert_matrix_3d(&mv, &mv);
1526 else
1527 invert_matrix(&mv, &mv);
1528 /* Tests show that singular modelview matrices are used unchanged as normal
1529 * matrices on D3D3 and older. There seems to be no clearly consistent
1530 * behavior on newer D3D versions so always follow older ddraw behavior. */
1531 for (i = 0; i < 3; ++i)
1532 for (j = 0; j < 3; ++j)
1533 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1535 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1536 checkGLcall("glUniformMatrix3fv");
1539 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1540 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1542 const struct wined3d_gl_info *gl_info = context->gl_info;
1543 struct wined3d_matrix mat;
1545 if (tex >= MAX_TEXTURES)
1546 return;
1547 if (prog->vs.texture_matrix_location[tex] == -1)
1548 return;
1550 get_texture_matrix(context, state, tex, &mat);
1551 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1552 checkGLcall("glUniformMatrix4fv");
1555 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1556 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1558 const struct wined3d_gl_info *gl_info = context->gl_info;
1560 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1562 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1563 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1565 else
1567 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1569 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1571 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1572 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1573 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1574 checkGLcall("setting FFP material uniforms");
1577 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1578 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1580 const struct wined3d_gl_info *gl_info = context->gl_info;
1581 struct wined3d_color color;
1583 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1584 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1585 checkGLcall("glUniform3fv");
1588 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1589 const struct wined3d_matrix *src2)
1591 struct wined3d_vec4 temp;
1593 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1594 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1595 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1596 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1598 *dest = temp;
1601 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1602 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1603 struct glsl_shader_prog_link *prog)
1605 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1606 const struct wined3d_gl_info *gl_info = context->gl_info;
1607 struct wined3d_vec4 vec4;
1609 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1610 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1611 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1613 switch (light_info->OriginalParms.type)
1615 case WINED3D_LIGHT_POINT:
1616 multiply_vector_matrix(&vec4, &light_info->position, view);
1617 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1618 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1619 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1620 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1621 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1622 break;
1624 case WINED3D_LIGHT_SPOT:
1625 multiply_vector_matrix(&vec4, &light_info->position, view);
1626 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1628 multiply_vector_matrix(&vec4, &light_info->direction, view);
1629 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1631 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1632 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1633 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1634 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1635 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1636 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1637 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1638 break;
1640 case WINED3D_LIGHT_DIRECTIONAL:
1641 multiply_vector_matrix(&vec4, &light_info->direction, view);
1642 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1643 break;
1645 case WINED3D_LIGHT_PARALLELPOINT:
1646 multiply_vector_matrix(&vec4, &light_info->position, view);
1647 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1648 break;
1650 default:
1651 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1653 checkGLcall("setting FFP lights uniforms");
1656 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1657 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1659 const struct wined3d_gl_info *gl_info = context->gl_info;
1660 float min, max;
1661 float size, att[3];
1663 get_pointsize_minmax(context, state, &min, &max);
1665 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1666 checkGLcall("glUniform1f");
1667 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1668 checkGLcall("glUniform1f");
1670 get_pointsize(context, state, &size, att);
1672 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1673 checkGLcall("glUniform1f");
1674 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1675 checkGLcall("glUniform1f");
1676 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1677 checkGLcall("glUniform1f");
1678 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1679 checkGLcall("glUniform1f");
1682 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1683 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1685 const struct wined3d_gl_info *gl_info = context->gl_info;
1686 struct wined3d_color color;
1687 float start, end, scale;
1688 union
1690 DWORD d;
1691 float f;
1692 } tmpvalue;
1694 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1695 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1696 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1697 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1698 get_fog_start_end(context, state, &start, &end);
1699 scale = 1.0f / (end - start);
1700 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1701 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1702 checkGLcall("fog emulation uniforms");
1705 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1706 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1708 const struct wined3d_gl_info *gl_info = context->gl_info;
1709 struct wined3d_vec4 plane;
1711 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1712 if (!use_vs(state))
1713 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1714 else
1715 plane = state->clip_planes[index];
1717 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1720 /* Context activation is done by the caller (state handler). */
1721 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1722 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1724 struct wined3d_color float_key[2];
1725 const struct wined3d_texture *texture = state->textures[0];
1727 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1728 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1731 /* Context activation is done by the caller (state handler). */
1732 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1733 const struct wined3d_state *state)
1735 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1736 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1737 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1738 const struct wined3d_gl_info *gl_info = context->gl_info;
1739 struct shader_glsl_priv *priv = shader_priv;
1740 float position_fixup[4];
1741 DWORD update_mask;
1743 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1744 UINT constant_version;
1745 int i;
1747 if (!prog) {
1748 /* No GLSL program set - nothing to do. */
1749 return;
1751 constant_version = prog->constant_version;
1752 update_mask = context->constant_update_mask & prog->constant_update_mask;
1754 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1755 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1756 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1758 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1759 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1760 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1762 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1763 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1764 vshader->reg_maps.boolean_constants);
1766 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1768 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1769 shader_glsl_clip_plane_uniform(context, state, i, prog);
1772 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1773 shader_glsl_pointsize_uniform(context, state, prog);
1775 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1777 shader_get_position_fixup(context, state, position_fixup);
1778 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1779 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1780 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1781 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1782 else
1783 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1784 checkGLcall("glUniform4fv");
1787 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1789 struct wined3d_matrix mat;
1791 get_modelview_matrix(context, state, 0, &mat);
1792 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1793 checkGLcall("glUniformMatrix4fv");
1795 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1798 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1800 struct wined3d_matrix mat;
1802 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1804 if (prog->vs.modelview_matrix_location[i] == -1)
1805 break;
1807 get_modelview_matrix(context, state, i, &mat);
1808 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1809 checkGLcall("glUniformMatrix4fv");
1813 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1815 struct wined3d_matrix projection;
1817 get_projection_matrix(context, state, &projection);
1818 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1819 checkGLcall("glUniformMatrix4fv");
1822 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1824 for (i = 0; i < MAX_TEXTURES; ++i)
1825 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1828 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1829 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1831 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1833 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1834 DWORD point_count = 0;
1835 DWORD spot_count = 0;
1836 DWORD directional_count = 0;
1837 DWORD parallel_point_count = 0;
1839 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1841 if (!state->lights[i])
1842 continue;
1844 switch (state->lights[i]->OriginalParms.type)
1846 case WINED3D_LIGHT_POINT:
1847 ++point_count;
1848 break;
1849 case WINED3D_LIGHT_SPOT:
1850 ++spot_count;
1851 break;
1852 case WINED3D_LIGHT_DIRECTIONAL:
1853 ++directional_count;
1854 break;
1855 case WINED3D_LIGHT_PARALLELPOINT:
1856 ++parallel_point_count;
1857 break;
1858 default:
1859 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1860 break;
1863 point_idx = 0;
1864 spot_idx = point_idx + point_count;
1865 directional_idx = spot_idx + spot_count;
1866 parallel_point_idx = directional_idx + directional_count;
1868 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1869 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1871 const struct wined3d_light_info *light_info = state->lights[i];
1872 unsigned int idx;
1874 if (!light_info)
1875 continue;
1877 switch (light_info->OriginalParms.type)
1879 case WINED3D_LIGHT_POINT:
1880 idx = point_idx++;
1881 break;
1882 case WINED3D_LIGHT_SPOT:
1883 idx = spot_idx++;
1884 break;
1885 case WINED3D_LIGHT_DIRECTIONAL:
1886 idx = directional_idx++;
1887 break;
1888 case WINED3D_LIGHT_PARALLELPOINT:
1889 idx = parallel_point_idx++;
1890 break;
1891 default:
1892 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1893 continue;
1895 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1899 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1900 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1901 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1903 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1904 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1905 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1907 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1908 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1909 pshader->reg_maps.boolean_constants);
1911 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1913 for (i = 0; i < MAX_TEXTURES; ++i)
1915 if (prog->ps.bumpenv_mat_location[i] == -1)
1916 continue;
1918 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1919 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1921 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1923 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1924 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1925 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1926 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1930 checkGLcall("bump env uniforms");
1933 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1935 const struct wined3d_vec4 correction_params =
1937 /* Position is relative to the framebuffer, not the viewport. */
1938 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1939 context->render_offscreen ? 1.0f : -1.0f,
1940 0.0f,
1941 0.0f,
1944 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1947 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1948 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1949 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1950 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1952 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1954 struct wined3d_color color;
1956 if (prog->ps.tex_factor_location != -1)
1958 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1959 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1962 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1963 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1964 else
1965 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1967 for (i = 0; i < MAX_TEXTURES; ++i)
1969 if (prog->ps.tss_constant_location[i] == -1)
1970 continue;
1972 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1973 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1976 checkGLcall("fixed function uniforms");
1979 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1980 shader_glsl_load_fog_uniform(context, state, prog);
1982 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1984 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1986 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1987 checkGLcall("alpha test emulation uniform");
1990 if (priv->next_constant_version == UINT_MAX)
1992 TRACE("Max constant version reached, resetting to 0.\n");
1993 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1994 priv->next_constant_version = 1;
1996 else
1998 prog->constant_version = priv->next_constant_version++;
2002 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
2004 struct constant_entry *entries = heap->entries;
2005 unsigned int *positions = heap->positions;
2006 unsigned int heap_idx, parent_idx;
2008 if (!heap->contained[idx])
2010 heap_idx = heap->size++;
2011 heap->contained[idx] = TRUE;
2013 else
2015 heap_idx = positions[idx];
2018 while (heap_idx > 1)
2020 parent_idx = heap_idx >> 1;
2022 if (new_version <= entries[parent_idx].version) break;
2024 entries[heap_idx] = entries[parent_idx];
2025 positions[entries[parent_idx].idx] = heap_idx;
2026 heap_idx = parent_idx;
2029 entries[heap_idx].version = new_version;
2030 entries[heap_idx].idx = idx;
2031 positions[idx] = heap_idx;
2034 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2036 struct shader_glsl_priv *priv = device->shader_priv;
2037 struct constant_heap *heap = &priv->vconst_heap;
2038 UINT i;
2040 for (i = start; i < count + start; ++i)
2042 update_heap_entry(heap, i, priv->next_constant_version);
2046 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2048 struct shader_glsl_priv *priv = device->shader_priv;
2049 struct constant_heap *heap = &priv->pconst_heap;
2050 UINT i;
2052 for (i = start; i < count + start; ++i)
2054 update_heap_entry(heap, i, priv->next_constant_version);
2058 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2060 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2061 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2062 if(shader_major > 3) return ret;
2064 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2065 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2066 return ret;
2069 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2071 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2074 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2076 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2077 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2080 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2082 return shader_glsl_get_version(gl_info) >= 150;
2085 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2087 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2090 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2091 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2093 va_list args;
2094 int ret;
2096 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2097 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2098 for (;;)
2100 va_start(args, format);
2101 ret = shader_vaddline(buffer, format, args);
2102 va_end(args);
2103 if (!ret)
2104 return;
2105 if (!string_buffer_resize(buffer, ret))
2106 return;
2110 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2111 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2113 va_list args;
2114 int ret;
2116 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2117 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2118 for (;;)
2120 va_start(args, format);
2121 ret = shader_vaddline(buffer, format, args);
2122 va_end(args);
2123 if (!ret)
2124 return;
2125 if (!string_buffer_resize(buffer, ret))
2126 return;
2130 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2132 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2135 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2137 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2140 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2142 switch (mode)
2144 case WINED3DSIM_CONSTANT:
2145 return "flat";
2146 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2147 return "noperspective";
2148 default:
2149 FIXME("Unhandled interpolation mode %#x.\n", mode);
2150 case WINED3DSIM_NONE:
2151 case WINED3DSIM_LINEAR:
2152 return "";
2156 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2157 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2159 return wined3d_extract_bits(packed_interpolation_mode,
2160 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2163 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2164 struct wined3d_string_buffer *buffer, unsigned int element_count,
2165 const DWORD *interpolation_mode, BOOL unroll)
2167 enum wined3d_shader_interpolation_mode mode;
2168 unsigned int i;
2170 if (shader_glsl_use_interface_blocks(gl_info))
2172 if (unroll)
2174 shader_addline(buffer, "in shader_in_out {\n");
2175 for (i = 0; i < element_count; ++i)
2177 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2178 shader_addline(buffer, "%s vec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2180 shader_addline(buffer, "} shader_in;\n");
2182 else
2184 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2187 else
2189 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2193 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2194 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2195 const DWORD *interpolation_mode)
2197 enum wined3d_shader_interpolation_mode mode;
2198 unsigned int i;
2200 if (shader_glsl_use_interface_blocks(gl_info))
2202 if (rasterizer_setup)
2204 shader_addline(buffer, "out shader_in_out {\n");
2205 for (i = 0; i < element_count; ++i)
2207 const char *interpolation_qualifiers = "";
2208 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2210 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2211 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2213 shader_addline(buffer, "%s vec4 reg%u;\n", interpolation_qualifiers, i);
2215 shader_addline(buffer, "} shader_out;\n");
2217 else
2219 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2222 else
2224 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2228 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2230 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2233 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2235 switch (primitive_type)
2237 case WINED3D_PT_POINTLIST:
2238 return "points";
2240 case WINED3D_PT_LINELIST:
2241 return "lines";
2243 case WINED3D_PT_LINESTRIP:
2244 return "line_strip";
2246 case WINED3D_PT_TRIANGLELIST:
2247 return "triangles";
2249 case WINED3D_PT_TRIANGLESTRIP:
2250 return "triangle_strip";
2252 case WINED3D_PT_LINELIST_ADJ:
2253 return "lines_adjacency";
2255 case WINED3D_PT_TRIANGLELIST_ADJ:
2256 return "triangles_adjacency";
2258 default:
2259 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2260 return "";
2264 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2266 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2267 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2268 DWORD input_reg_used = shader->u.ps.input_reg_used;
2269 unsigned int i;
2271 if (reg_maps->shader_version.major < 3)
2272 return input_reg_used & (1u << idx);
2274 for (i = 0; i < input_signature->element_count; ++i)
2276 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2278 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2279 continue;
2281 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2282 && input->semantic_idx == idx)
2283 return input_reg_used & (1u << input->register_idx);
2285 return FALSE;
2288 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2289 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2291 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2293 if (version->major >= 4)
2294 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2295 else
2296 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2299 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2300 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2301 unsigned int index)
2303 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2304 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2305 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2306 index, scalar_type, scalar_type, index);
2309 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2310 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2312 unsigned int index = e->register_idx;
2314 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2316 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2317 index);
2318 return;
2320 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2322 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2323 index);
2324 return;
2326 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2327 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2329 if (shader_glsl_use_explicit_attrib_location(gl_info))
2330 shader_addline(buffer, "layout(location = %u) ", index);
2332 switch (e->component_type)
2334 case WINED3D_TYPE_UINT:
2335 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
2336 break;
2337 case WINED3D_TYPE_INT:
2338 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
2339 break;
2341 default:
2342 FIXME("Unhandled type %#x.\n", e->component_type);
2343 /* Fall through. */
2344 case WINED3D_TYPE_UNKNOWN:
2345 case WINED3D_TYPE_FLOAT:
2346 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2347 break;
2351 /** Generate the variable & register declarations for the GLSL output target */
2352 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2353 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2354 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2356 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2357 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2358 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2359 const struct wined3d_gl_info *gl_info = context->gl_info;
2360 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2361 unsigned int uniform_block_base, uniform_block_count;
2362 const struct wined3d_shader_lconst *lconst;
2363 const char *prefix;
2364 unsigned int i;
2365 DWORD map;
2367 prefix = shader_glsl_get_prefix(version->type);
2369 /* Prototype the subroutines */
2370 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2372 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2375 /* Declare the constants (aka uniforms) */
2376 if (shader->limits->constant_float > 0)
2378 unsigned max_constantsF;
2380 /* Unless the shader uses indirect addressing, always declare the
2381 * maximum array size and ignore that we need some uniforms privately.
2382 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2383 * and immediate values, still declare VC[256]. If the shader needs
2384 * more uniforms than we have it won't work in any case. If it uses
2385 * less, the compiler will figure out which uniforms are really used
2386 * and strip them out. This allows a shader to use c255 on a dx9 card,
2387 * as long as it doesn't also use all the other constants.
2389 * If the shader uses indirect addressing the compiler must assume
2390 * that all declared uniforms are used. In this case, declare only the
2391 * amount that we're assured to have.
2393 * Thus we run into problems in these two cases:
2394 * 1) The shader really uses more uniforms than supported.
2395 * 2) The shader uses indirect addressing, less constants than
2396 * supported, but uses a constant index > #supported consts. */
2397 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2399 /* No indirect addressing here. */
2400 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2402 else
2404 if (reg_maps->usesrelconstF)
2406 /* Subtract the other potential uniforms from the max
2407 * available (bools, ints, and 1 row of projection matrix).
2408 * Subtract another uniform for immediate values, which have
2409 * to be loaded via uniform by the driver as well. The shader
2410 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2411 * shader code, so one vec4 should be enough. (Unfortunately
2412 * the Nvidia driver doesn't store 128 and -128 in one float).
2414 * Writing gl_ClipVertex requires one uniform for each
2415 * clipplane as well. */
2416 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2417 if (vs_args->clip_enabled)
2418 max_constantsF -= gl_info->limits.user_clip_distances;
2419 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2420 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2421 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2422 * for now take this into account when calculating the number of available constants
2424 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2425 /* Set by driver quirks in directx.c */
2426 max_constantsF -= gl_info->reserved_glsl_constants;
2428 if (max_constantsF < shader->limits->constant_float)
2430 static unsigned int once;
2432 if (!once++)
2433 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2434 " it may not render correctly.\n");
2435 else
2436 WARN("The hardware does not support enough uniform components to run this shader.\n");
2439 else
2441 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2444 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2445 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2448 /* Always declare the full set of constants, the compiler can remove the
2449 * unused ones because d3d doesn't (yet) support indirect int and bool
2450 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2451 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2452 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2454 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2455 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2457 /* Declare immediate constant buffer */
2458 if (reg_maps->icb)
2459 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2461 /* Declare constant buffers */
2462 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2463 &uniform_block_base, &uniform_block_count);
2464 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2466 if (reg_maps->cb_sizes[i])
2468 shader_addline(buffer, "layout(std140");
2469 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2470 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2471 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2472 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2476 /* Declare texture samplers */
2477 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2479 struct wined3d_shader_sampler_map_entry *entry;
2480 const char *sampler_type_prefix, *sampler_type;
2481 BOOL shadow_sampler, tex_rect;
2483 entry = &reg_maps->sampler_map.entries[i];
2485 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2487 ERR("Invalid resource index %u.\n", entry->resource_idx);
2488 continue;
2491 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2493 case WINED3D_DATA_FLOAT:
2494 case WINED3D_DATA_UNORM:
2495 case WINED3D_DATA_SNORM:
2496 sampler_type_prefix = "";
2497 break;
2499 case WINED3D_DATA_INT:
2500 sampler_type_prefix = "i";
2501 break;
2503 case WINED3D_DATA_UINT:
2504 sampler_type_prefix = "u";
2505 break;
2507 default:
2508 sampler_type_prefix = "";
2509 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2510 break;
2513 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2514 switch (reg_maps->resource_info[entry->resource_idx].type)
2516 case WINED3D_SHADER_RESOURCE_BUFFER:
2517 sampler_type = "samplerBuffer";
2518 break;
2520 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2521 if (shadow_sampler)
2522 sampler_type = "sampler1DShadow";
2523 else
2524 sampler_type = "sampler1D";
2525 break;
2527 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2528 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2529 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2530 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2531 if (shadow_sampler)
2533 if (tex_rect)
2534 sampler_type = "sampler2DRectShadow";
2535 else
2536 sampler_type = "sampler2DShadow";
2538 else
2540 if (tex_rect)
2541 sampler_type = "sampler2DRect";
2542 else
2543 sampler_type = "sampler2D";
2545 break;
2547 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2548 if (shadow_sampler)
2549 FIXME("Unsupported 3D shadow sampler.\n");
2550 sampler_type = "sampler3D";
2551 break;
2553 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2554 if (shadow_sampler)
2555 sampler_type = "samplerCubeShadow";
2556 else
2557 sampler_type = "samplerCube";
2558 break;
2560 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2561 if (shadow_sampler)
2562 sampler_type = "sampler2DArrayShadow";
2563 else
2564 sampler_type = "sampler2DArray";
2565 break;
2567 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2568 if (shadow_sampler)
2569 sampler_type = "samplerCubeArrayShadow";
2570 else
2571 sampler_type = "samplerCubeArray";
2572 break;
2574 default:
2575 sampler_type = "unsupported_sampler";
2576 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2577 break;
2580 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2581 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2582 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2583 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2586 /* Declare images */
2587 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2589 const char *image_type_prefix, *image_type, *read_format;
2591 if (!reg_maps->uav_resource_info[i].type)
2592 continue;
2594 switch (reg_maps->uav_resource_info[i].data_type)
2596 case WINED3D_DATA_FLOAT:
2597 case WINED3D_DATA_UNORM:
2598 case WINED3D_DATA_SNORM:
2599 image_type_prefix = "";
2600 read_format = "r32f";
2601 break;
2603 case WINED3D_DATA_INT:
2604 image_type_prefix = "i";
2605 read_format = "r32i";
2606 break;
2608 case WINED3D_DATA_UINT:
2609 image_type_prefix = "u";
2610 read_format = "r32ui";
2611 break;
2613 default:
2614 image_type_prefix = "";
2615 read_format = "";
2616 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2617 break;
2620 switch (reg_maps->uav_resource_info[i].type)
2622 case WINED3D_SHADER_RESOURCE_BUFFER:
2623 image_type = "imageBuffer";
2624 break;
2626 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2627 image_type = "image2D";
2628 break;
2630 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2631 image_type = "image3D";
2632 break;
2634 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2635 image_type = "image2DArray";
2636 break;
2638 default:
2639 image_type = "unsupported_image";
2640 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2641 break;
2644 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2645 shader_addline(buffer, "layout(binding = %u)\n", i);
2646 if (reg_maps->uav_read_mask & (1u << i))
2647 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2648 read_format, image_type_prefix, image_type, prefix, i);
2649 else
2650 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2651 image_type_prefix, image_type, prefix, i);
2653 if (reg_maps->uav_counter_mask & (1u << i))
2654 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2655 i, prefix, i);
2658 /* Declare address variables */
2659 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2661 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2664 /* Declare output register temporaries */
2665 if (shader->limits->packed_output)
2666 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2668 /* Declare temporary variables */
2669 if (reg_maps->temporary_count)
2671 for (i = 0; i < reg_maps->temporary_count; ++i)
2672 shader_addline(buffer, "vec4 R%u;\n", i);
2674 else if (version->major < 4)
2676 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2678 if (map & 1)
2679 shader_addline(buffer, "vec4 R%u;\n", i);
2683 /* Declare indexable temporary variables */
2684 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2686 if (idx_temp_reg->component_count != 4)
2687 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2688 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2691 /* Declare loop registers aLx */
2692 if (version->major < 4)
2694 for (i = 0; i < reg_maps->loop_depth; ++i)
2696 shader_addline(buffer, "int aL%u;\n", i);
2697 shader_addline(buffer, "int tmpInt%u;\n", i);
2701 /* Temporary variables for matrix operations */
2702 shader_addline(buffer, "vec4 tmp0;\n");
2703 shader_addline(buffer, "vec4 tmp1;\n");
2705 if (!shader->load_local_constsF)
2707 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2709 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2710 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2711 shader_addline(buffer, ";\n");
2716 /*****************************************************************************
2717 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2718 ****************************************************************************/
2720 /* Prototypes */
2721 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2722 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2724 /** Used for opcode modifiers - They multiply the result by the specified amount */
2725 static const char * const shift_glsl_tab[] = {
2726 "", /* 0 (none) */
2727 "2.0 * ", /* 1 (x2) */
2728 "4.0 * ", /* 2 (x4) */
2729 "8.0 * ", /* 3 (x8) */
2730 "16.0 * ", /* 4 (x16) */
2731 "32.0 * ", /* 5 (x32) */
2732 "", /* 6 (x64) */
2733 "", /* 7 (x128) */
2734 "", /* 8 (d256) */
2735 "", /* 9 (d128) */
2736 "", /* 10 (d64) */
2737 "", /* 11 (d32) */
2738 "0.0625 * ", /* 12 (d16) */
2739 "0.125 * ", /* 13 (d8) */
2740 "0.25 * ", /* 14 (d4) */
2741 "0.5 * " /* 15 (d2) */
2744 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2745 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2746 const char *in_reg, const char *in_regswizzle, char *out_str)
2748 switch (src_modifier)
2750 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2751 case WINED3DSPSM_DW:
2752 case WINED3DSPSM_NONE:
2753 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2754 break;
2755 case WINED3DSPSM_NEG:
2756 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2757 break;
2758 case WINED3DSPSM_NOT:
2759 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2760 break;
2761 case WINED3DSPSM_BIAS:
2762 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2763 break;
2764 case WINED3DSPSM_BIASNEG:
2765 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2766 break;
2767 case WINED3DSPSM_SIGN:
2768 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2769 break;
2770 case WINED3DSPSM_SIGNNEG:
2771 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2772 break;
2773 case WINED3DSPSM_COMP:
2774 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2775 break;
2776 case WINED3DSPSM_X2:
2777 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2778 break;
2779 case WINED3DSPSM_X2NEG:
2780 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2781 break;
2782 case WINED3DSPSM_ABS:
2783 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2784 break;
2785 case WINED3DSPSM_ABSNEG:
2786 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2787 break;
2788 default:
2789 FIXME("Unhandled modifier %u\n", src_modifier);
2790 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2794 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2795 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2797 /* The ARB_shading_language_420pack extension allows swizzle operations on
2798 * scalars. */
2799 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2800 sprintf(register_name, "%s", glsl_variable);
2801 else
2802 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2805 /** Writes the GLSL variable name that corresponds to the register that the
2806 * DX opcode parameter is trying to access */
2807 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2808 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2809 const struct wined3d_shader_instruction *ins)
2811 /* oPos, oFog and oPts in D3D */
2812 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2814 const struct wined3d_shader *shader = ins->ctx->shader;
2815 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2816 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2817 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2818 const char *prefix = shader_glsl_get_prefix(version->type);
2819 struct glsl_src_param rel_param0, rel_param1;
2820 char imm_str[4][17];
2822 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2823 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2824 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2825 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2826 *is_color = FALSE;
2828 switch (reg->type)
2830 case WINED3DSPR_TEMP:
2831 sprintf(register_name, "R%u", reg->idx[0].offset);
2832 break;
2834 case WINED3DSPR_INPUT:
2835 case WINED3DSPR_INCONTROLPOINT:
2836 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2838 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2840 if (reg->idx[0].rel_addr)
2841 FIXME("VS3+ input registers relative addressing.\n");
2842 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2843 *is_color = TRUE;
2844 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2845 break;
2848 if (version->type == WINED3D_SHADER_TYPE_HULL
2849 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2850 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2852 if (reg->idx[0].rel_addr)
2854 if (reg->idx[1].rel_addr)
2855 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2856 rel_param0.param_str, reg->idx[0].offset,
2857 rel_param1.param_str, reg->idx[1].offset);
2858 else
2859 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2860 rel_param0.param_str, reg->idx[0].offset,
2861 reg->idx[1].offset);
2863 else if (reg->idx[1].rel_addr)
2864 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2865 rel_param1.param_str, reg->idx[1].offset);
2866 else
2867 sprintf(register_name, "shader_in[%u].reg[%u]",
2868 reg->idx[0].offset, reg->idx[1].offset);
2869 break;
2872 /* pixel shaders >= 3.0 */
2873 if (version->major >= 3)
2875 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2876 unsigned int in_count = vec4_varyings(version->major, gl_info);
2878 if (reg->idx[0].rel_addr)
2880 /* Removing a + 0 would be an obvious optimization, but
2881 * OS X doesn't see the NOP operation there. */
2882 if (idx)
2884 if (needs_legacy_glsl_syntax(gl_info)
2885 && shader->u.ps.declared_in_count > in_count)
2887 sprintf(register_name,
2888 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2889 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2890 prefix, rel_param0.param_str, idx);
2892 else
2894 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2897 else
2899 if (needs_legacy_glsl_syntax(gl_info)
2900 && shader->u.ps.declared_in_count > in_count)
2902 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2903 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2904 prefix, rel_param0.param_str);
2906 else
2908 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2912 else
2914 if (idx == in_count) sprintf(register_name, "gl_Color");
2915 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2916 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2919 else
2921 if (!reg->idx[0].offset)
2922 strcpy(register_name, "ffp_varying_diffuse");
2923 else
2924 strcpy(register_name, "ffp_varying_specular");
2925 break;
2927 break;
2929 case WINED3DSPR_CONST:
2931 /* Relative addressing */
2932 if (reg->idx[0].rel_addr)
2934 if (wined3d_settings.check_float_constants)
2935 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2936 rel_param0.param_str, reg->idx[0].offset,
2937 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2938 prefix, rel_param0.param_str, reg->idx[0].offset);
2939 else if (reg->idx[0].offset)
2940 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2941 else
2942 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2944 else
2946 if (shader_constant_is_local(shader, reg->idx[0].offset))
2947 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2948 else
2949 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2952 break;
2954 case WINED3DSPR_CONSTINT:
2955 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2956 break;
2958 case WINED3DSPR_CONSTBOOL:
2959 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2960 break;
2962 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2963 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2964 sprintf(register_name, "T%u", reg->idx[0].offset);
2965 else
2966 sprintf(register_name, "A%u", reg->idx[0].offset);
2967 break;
2969 case WINED3DSPR_LOOP:
2970 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2971 break;
2973 case WINED3DSPR_SAMPLER:
2974 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2975 break;
2977 case WINED3DSPR_COLOROUT:
2978 if (reg->idx[0].offset >= gl_info->limits.buffers)
2979 WARN("Write to render target %u, only %d supported.\n",
2980 reg->idx[0].offset, gl_info->limits.buffers);
2982 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2983 break;
2985 case WINED3DSPR_RASTOUT:
2986 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2987 break;
2989 case WINED3DSPR_DEPTHOUT:
2990 case WINED3DSPR_DEPTHOUTGE:
2991 case WINED3DSPR_DEPTHOUTLE:
2992 sprintf(register_name, "gl_FragDepth");
2993 break;
2995 case WINED3DSPR_ATTROUT:
2996 if (!reg->idx[0].offset)
2997 sprintf(register_name, "%s_out[8]", prefix);
2998 else
2999 sprintf(register_name, "%s_out[9]", prefix);
3000 break;
3002 case WINED3DSPR_TEXCRDOUT:
3003 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
3004 if (reg->idx[0].rel_addr)
3005 sprintf(register_name, "%s_out[%s + %u]",
3006 prefix, rel_param0.param_str, reg->idx[0].offset);
3007 else
3008 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3009 break;
3011 case WINED3DSPR_MISCTYPE:
3012 if (!reg->idx[0].offset)
3014 /* vPos */
3015 sprintf(register_name, "vpos");
3017 else if (reg->idx[0].offset == 1)
3019 /* Note that gl_FrontFacing is a bool, while vFace is
3020 * a float for which the sign determines front/back */
3021 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3023 else
3025 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3026 sprintf(register_name, "unrecognized_register");
3028 break;
3030 case WINED3DSPR_IMMCONST:
3031 switch (reg->immconst_type)
3033 case WINED3D_IMMCONST_SCALAR:
3034 switch (data_type)
3036 case WINED3D_DATA_FLOAT:
3037 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3038 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3039 else
3040 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3041 break;
3042 case WINED3D_DATA_INT:
3043 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3044 break;
3045 case WINED3D_DATA_RESOURCE:
3046 case WINED3D_DATA_SAMPLER:
3047 case WINED3D_DATA_UINT:
3048 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3049 break;
3050 default:
3051 sprintf(register_name, "<unhandled data type %#x>", data_type);
3052 break;
3054 break;
3056 case WINED3D_IMMCONST_VEC4:
3057 switch (data_type)
3059 case WINED3D_DATA_FLOAT:
3060 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3062 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3063 reg->u.immconst_data[0], reg->u.immconst_data[1],
3064 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3066 else
3068 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3069 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3070 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3071 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3072 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3073 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3075 break;
3076 case WINED3D_DATA_INT:
3077 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3078 reg->u.immconst_data[0], reg->u.immconst_data[1],
3079 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3080 break;
3081 case WINED3D_DATA_RESOURCE:
3082 case WINED3D_DATA_SAMPLER:
3083 case WINED3D_DATA_UINT:
3084 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3085 reg->u.immconst_data[0], reg->u.immconst_data[1],
3086 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3087 break;
3088 default:
3089 sprintf(register_name, "<unhandled data type %#x>", data_type);
3090 break;
3092 break;
3094 default:
3095 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3096 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3098 break;
3100 case WINED3DSPR_CONSTBUFFER:
3101 if (reg->idx[1].rel_addr)
3102 sprintf(register_name, "%s_cb%u[%s + %u]",
3103 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3104 else
3105 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3106 break;
3108 case WINED3DSPR_IMMCONSTBUFFER:
3109 if (reg->idx[0].rel_addr)
3110 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3111 else
3112 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3113 break;
3115 case WINED3DSPR_PRIMID:
3116 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3117 sprintf(register_name, "gl_PrimitiveIDIn");
3118 else
3119 sprintf(register_name, "gl_PrimitiveID");
3120 break;
3122 case WINED3DSPR_IDXTEMP:
3123 if (reg->idx[1].rel_addr)
3124 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3125 else
3126 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3127 break;
3129 case WINED3DSPR_LOCALTHREADINDEX:
3130 shader_glsl_fixup_scalar_register_variable(register_name,
3131 "int(gl_LocalInvocationIndex)", gl_info);
3132 break;
3134 case WINED3DSPR_GSINSTID:
3135 case WINED3DSPR_OUTPOINTID:
3136 shader_glsl_fixup_scalar_register_variable(register_name,
3137 "gl_InvocationID", gl_info);
3138 break;
3140 case WINED3DSPR_THREADID:
3141 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3142 break;
3144 case WINED3DSPR_THREADGROUPID:
3145 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3146 break;
3148 case WINED3DSPR_LOCALTHREADID:
3149 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3150 break;
3152 case WINED3DSPR_FORKINSTID:
3153 case WINED3DSPR_JOININSTID:
3154 shader_glsl_fixup_scalar_register_variable(register_name,
3155 "phase_instance_id", gl_info);
3156 break;
3158 case WINED3DSPR_TESSCOORD:
3159 sprintf(register_name, "gl_TessCoord");
3160 break;
3162 case WINED3DSPR_OUTCONTROLPOINT:
3163 if (reg->idx[0].rel_addr)
3165 if (reg->idx[1].rel_addr)
3166 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3167 rel_param0.param_str, reg->idx[0].offset,
3168 rel_param1.param_str, reg->idx[1].offset);
3169 else
3170 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3171 rel_param0.param_str, reg->idx[0].offset,
3172 reg->idx[1].offset);
3174 else if (reg->idx[1].rel_addr)
3176 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3177 reg->idx[0].offset, rel_param1.param_str,
3178 reg->idx[1].offset);
3180 else
3182 sprintf(register_name, "shader_out[%u].reg[%u]",
3183 reg->idx[0].offset, reg->idx[1].offset);
3185 break;
3187 case WINED3DSPR_PATCHCONST:
3188 if (version->type == WINED3D_SHADER_TYPE_HULL)
3189 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3190 else
3191 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3192 break;
3194 default:
3195 FIXME("Unhandled register type %#x.\n", reg->type);
3196 sprintf(register_name, "unrecognized_register");
3197 break;
3201 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3203 *str++ = '.';
3204 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3205 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3206 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3207 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3208 *str = '\0';
3211 /* Get the GLSL write mask for the destination register */
3212 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3214 DWORD mask = param->write_mask;
3216 if (shader_is_scalar(&param->reg))
3218 mask = WINED3DSP_WRITEMASK_0;
3219 *write_mask = '\0';
3221 else
3223 shader_glsl_write_mask_to_str(mask, write_mask);
3226 return mask;
3229 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3231 unsigned int size = 0;
3233 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3234 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3235 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3236 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3238 return size;
3241 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3242 unsigned int component_idx)
3244 /* swizzle bits fields: wwzzyyxx */
3245 return (swizzle >> (2 * component_idx)) & 0x3;
3248 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3250 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3251 * but addressed as "rgba". To fix this we need to swap the register's x
3252 * and z components. */
3253 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3254 unsigned int i;
3256 *str++ = '.';
3257 for (i = 0; i < 4; ++i)
3259 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3260 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3262 *str = '\0';
3265 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3266 BOOL fixup, DWORD mask, char *swizzle_str)
3268 if (shader_is_scalar(&param->reg))
3269 *swizzle_str = '\0';
3270 else
3271 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3274 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3275 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3277 if (dst_data_type == src_data_type)
3279 string_buffer_sprintf(dst_param, "%s", src_param);
3280 return;
3283 if (src_data_type == WINED3D_DATA_FLOAT)
3285 switch (dst_data_type)
3287 case WINED3D_DATA_INT:
3288 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3289 return;
3290 case WINED3D_DATA_RESOURCE:
3291 case WINED3D_DATA_SAMPLER:
3292 case WINED3D_DATA_UINT:
3293 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3294 return;
3295 default:
3296 break;
3300 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3302 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3303 return;
3306 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3308 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3309 return;
3312 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3313 string_buffer_sprintf(dst_param, "%s", src_param);
3316 /* From a given parameter token, generate the corresponding GLSL string.
3317 * Also, return the actual register name and swizzle in case the
3318 * caller needs this information as well. */
3319 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3320 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3321 enum wined3d_data_type data_type)
3323 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3324 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3325 enum wined3d_data_type param_data_type;
3326 BOOL is_color = FALSE;
3327 char swizzle_str[6];
3329 glsl_src->reg_name[0] = '\0';
3330 glsl_src->param_str[0] = '\0';
3331 swizzle_str[0] = '\0';
3333 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ins);
3334 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3336 switch (wined3d_src->reg.type)
3338 case WINED3DSPR_IMMCONST:
3339 param_data_type = data_type;
3340 break;
3341 case WINED3DSPR_FORKINSTID:
3342 case WINED3DSPR_GSINSTID:
3343 case WINED3DSPR_JOININSTID:
3344 case WINED3DSPR_LOCALTHREADID:
3345 case WINED3DSPR_LOCALTHREADINDEX:
3346 case WINED3DSPR_OUTPOINTID:
3347 case WINED3DSPR_PRIMID:
3348 case WINED3DSPR_THREADGROUPID:
3349 case WINED3DSPR_THREADID:
3350 param_data_type = WINED3D_DATA_INT;
3351 break;
3352 default:
3353 param_data_type = WINED3D_DATA_FLOAT;
3354 break;
3357 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3358 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3360 string_buffer_release(priv->string_buffers, reg_name);
3363 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3364 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3366 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3369 /* From a given parameter token, generate the corresponding GLSL string.
3370 * Also, return the actual register name and swizzle in case the
3371 * caller needs this information as well. */
3372 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3373 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3375 BOOL is_color = FALSE;
3377 glsl_dst->mask_str[0] = '\0';
3378 glsl_dst->reg_name[0] = '\0';
3380 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3381 glsl_dst->reg_name, &is_color, ins);
3382 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3385 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3386 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3387 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3388 enum wined3d_data_type data_type)
3390 struct glsl_dst_param glsl_dst;
3391 DWORD mask;
3393 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3395 switch (data_type)
3397 case WINED3D_DATA_FLOAT:
3398 shader_addline(buffer, "%s%s = %s(",
3399 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3400 break;
3401 case WINED3D_DATA_INT:
3402 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3403 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3404 break;
3405 case WINED3D_DATA_RESOURCE:
3406 case WINED3D_DATA_SAMPLER:
3407 case WINED3D_DATA_UINT:
3408 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3409 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3410 break;
3411 default:
3412 FIXME("Unhandled data type %#x.\n", data_type);
3413 shader_addline(buffer, "%s%s = %s(",
3414 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3415 break;
3419 return mask;
3422 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3423 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3425 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3428 /** Process GLSL instruction modifiers */
3429 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3431 struct glsl_dst_param dst_param;
3432 DWORD modifiers;
3434 if (!ins->dst_count) return;
3436 modifiers = ins->dst[0].modifiers;
3437 if (!modifiers) return;
3439 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3441 if (modifiers & WINED3DSPDM_SATURATE)
3443 /* _SAT means to clamp the value of the register to between 0 and 1 */
3444 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3445 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3448 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3450 FIXME("_centroid modifier not handled\n");
3453 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3455 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3459 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3461 switch (op)
3463 case WINED3D_SHADER_REL_OP_GT: return ">";
3464 case WINED3D_SHADER_REL_OP_EQ: return "==";
3465 case WINED3D_SHADER_REL_OP_GE: return ">=";
3466 case WINED3D_SHADER_REL_OP_LT: return "<";
3467 case WINED3D_SHADER_REL_OP_NE: return "!=";
3468 case WINED3D_SHADER_REL_OP_LE: return "<=";
3469 default:
3470 FIXME("Unrecognized operator %#x.\n", op);
3471 return "(\?\?)";
3475 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3477 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3480 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3481 unsigned int *coord_size, unsigned int *deriv_size)
3483 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3484 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3486 *coord_size = resource_type_info[resource_type].coord_size;
3487 *deriv_size = *coord_size;
3488 if (is_array)
3489 --(*deriv_size);
3492 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3493 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3495 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3496 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3497 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3498 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3499 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3500 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3501 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3502 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3503 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3504 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3505 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3506 const char *base = "texture", *type_part = "", *suffix = "";
3507 unsigned int coord_size, deriv_size;
3509 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3511 if (resource_type >= ARRAY_SIZE(resource_type_info))
3513 ERR("Unexpected resource type %#x.\n", resource_type);
3514 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3517 /* Note that there's no such thing as a projected cube texture. */
3518 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3519 projected = FALSE;
3521 if (needs_legacy_glsl_syntax(gl_info))
3523 if (shadow)
3524 base = "shadow";
3526 type_part = resource_type_info[resource_type].type_part;
3527 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3528 type_part = "2DRect";
3529 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3530 FIXME("Unhandled resource type %#x.\n", resource_type);
3532 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3534 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3535 suffix = "ARB";
3536 else
3537 FIXME("Unsupported grad function.\n");
3541 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3543 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3544 if (flags & ~texel_fetch_flags)
3545 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3547 base = "texelFetch";
3548 type_part = "";
3551 sample_function->name = string_buffer_get(priv->string_buffers);
3552 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3553 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3555 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3556 if (shadow)
3557 ++coord_size;
3558 sample_function->offset_size = offset ? deriv_size : 0;
3559 sample_function->coord_mask = (1u << coord_size) - 1;
3560 sample_function->deriv_mask = (1u << deriv_size) - 1;
3561 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3564 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3565 struct glsl_sample_function *sample_function)
3567 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3569 string_buffer_release(priv->string_buffers, sample_function->name);
3572 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3573 BOOL sign_fixup, enum fixup_channel_source channel_source)
3575 switch(channel_source)
3577 case CHANNEL_SOURCE_ZERO:
3578 strcat(arguments, "0.0");
3579 break;
3581 case CHANNEL_SOURCE_ONE:
3582 strcat(arguments, "1.0");
3583 break;
3585 case CHANNEL_SOURCE_X:
3586 strcat(arguments, reg_name);
3587 strcat(arguments, ".x");
3588 break;
3590 case CHANNEL_SOURCE_Y:
3591 strcat(arguments, reg_name);
3592 strcat(arguments, ".y");
3593 break;
3595 case CHANNEL_SOURCE_Z:
3596 strcat(arguments, reg_name);
3597 strcat(arguments, ".z");
3598 break;
3600 case CHANNEL_SOURCE_W:
3601 strcat(arguments, reg_name);
3602 strcat(arguments, ".w");
3603 break;
3605 default:
3606 FIXME("Unhandled channel source %#x\n", channel_source);
3607 strcat(arguments, "undefined");
3608 break;
3611 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3614 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3615 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3617 unsigned int mask_size, remaining;
3618 DWORD fixup_mask = 0;
3619 char arguments[256];
3620 char mask_str[6];
3622 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3623 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3624 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3625 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3626 if (!(mask &= fixup_mask))
3627 return;
3629 if (is_complex_fixup(fixup))
3631 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3632 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3633 return;
3636 shader_glsl_write_mask_to_str(mask, mask_str);
3637 mask_size = shader_glsl_get_write_mask_size(mask);
3639 arguments[0] = '\0';
3640 remaining = mask_size;
3641 if (mask & WINED3DSP_WRITEMASK_0)
3643 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3644 if (--remaining) strcat(arguments, ", ");
3646 if (mask & WINED3DSP_WRITEMASK_1)
3648 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3649 if (--remaining) strcat(arguments, ", ");
3651 if (mask & WINED3DSP_WRITEMASK_2)
3653 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3654 if (--remaining) strcat(arguments, ", ");
3656 if (mask & WINED3DSP_WRITEMASK_3)
3658 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3659 if (--remaining) strcat(arguments, ", ");
3662 if (mask_size > 1)
3663 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3664 else
3665 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3668 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3670 char reg_name[256];
3671 BOOL is_color;
3673 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins);
3674 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3677 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3678 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3679 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3680 const char *coord_reg_fmt, ...)
3682 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3683 char dst_swizzle[6];
3684 struct color_fixup_desc fixup;
3685 BOOL np2_fixup = FALSE;
3686 va_list args;
3687 int ret;
3689 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3691 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3692 * We actually rely on it for vertex shaders and SM4+. */
3693 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3695 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3696 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3698 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3699 np2_fixup = TRUE;
3701 else
3703 fixup = COLOR_FIXUP_IDENTITY;
3706 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3708 if (sample_function->output_single_component)
3709 shader_addline(ins->ctx->buffer, "vec4(");
3711 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3712 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3714 for (;;)
3716 va_start(args, coord_reg_fmt);
3717 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3718 va_end(args);
3719 if (!ret)
3720 break;
3721 if (!string_buffer_resize(ins->ctx->buffer, ret))
3722 break;
3725 if (np2_fixup)
3727 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3728 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3730 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3732 case 1:
3733 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3734 idx >> 1, (idx % 2) ? "z" : "x");
3735 break;
3736 case 2:
3737 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3738 idx >> 1, (idx % 2) ? "zw" : "xy");
3739 break;
3740 case 3:
3741 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3742 idx >> 1, (idx % 2) ? "zw" : "xy");
3743 break;
3744 case 4:
3745 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3746 idx >> 1, (idx % 2) ? "zw" : "xy");
3747 break;
3750 if (dx && dy)
3751 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3752 else if (bias)
3753 shader_addline(ins->ctx->buffer, ", %s", bias);
3754 if (sample_function->offset_size)
3756 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3757 shader_addline(ins->ctx->buffer, ", ");
3758 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3760 shader_addline(ins->ctx->buffer, ")");
3762 if (sample_function->output_single_component)
3763 shader_addline(ins->ctx->buffer, ")");
3765 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3767 if (!is_identity_fixup(fixup))
3768 shader_glsl_color_correction(ins, fixup);
3771 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3773 /* Write the final position.
3775 * OpenGL coordinates specify the center of the pixel while D3D coords
3776 * specify the corner. The offsets are stored in z and w in
3777 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3778 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3779 * a MAD. */
3780 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3781 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3783 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3784 * in utils.c
3786 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3787 * shaders are run before the homogeneous divide, so we have to take the w
3788 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3789 * z = z * 2 - w. */
3790 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3793 /*****************************************************************************
3794 * Begin processing individual instruction opcodes
3795 ****************************************************************************/
3797 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3799 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3800 struct glsl_src_param src0_param;
3801 struct glsl_src_param src1_param;
3802 DWORD write_mask;
3803 const char *op;
3805 /* Determine the GLSL operator to use based on the opcode */
3806 switch (ins->handler_idx)
3808 case WINED3DSIH_ADD: op = "+"; break;
3809 case WINED3DSIH_AND: op = "&"; break;
3810 case WINED3DSIH_DIV: op = "/"; break;
3811 case WINED3DSIH_IADD: op = "+"; break;
3812 case WINED3DSIH_ISHL: op = "<<"; break;
3813 case WINED3DSIH_ISHR: op = ">>"; break;
3814 case WINED3DSIH_MUL: op = "*"; break;
3815 case WINED3DSIH_OR: op = "|"; break;
3816 case WINED3DSIH_SUB: op = "-"; break;
3817 case WINED3DSIH_USHR: op = ">>"; break;
3818 case WINED3DSIH_XOR: op = "^"; break;
3819 default:
3820 op = "<unhandled operator>";
3821 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3822 break;
3825 write_mask = shader_glsl_append_dst(buffer, ins);
3826 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3827 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3828 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3831 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3833 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3834 struct glsl_src_param src0_param;
3835 struct glsl_src_param src1_param;
3836 unsigned int mask_size;
3837 DWORD write_mask;
3838 const char *op;
3840 write_mask = shader_glsl_append_dst(buffer, ins);
3841 mask_size = shader_glsl_get_write_mask_size(write_mask);
3842 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3843 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3845 if (mask_size > 1)
3847 switch (ins->handler_idx)
3849 case WINED3DSIH_EQ: op = "equal"; break;
3850 case WINED3DSIH_IEQ: op = "equal"; break;
3851 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3852 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3853 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3854 case WINED3DSIH_LT: op = "lessThan"; break;
3855 case WINED3DSIH_ILT: op = "lessThan"; break;
3856 case WINED3DSIH_ULT: op = "lessThan"; break;
3857 case WINED3DSIH_NE: op = "notEqual"; break;
3858 case WINED3DSIH_INE: op = "notEqual"; break;
3859 default:
3860 op = "<unhandled operator>";
3861 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3862 break;
3865 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3866 mask_size, op, src0_param.param_str, src1_param.param_str);
3868 else
3870 switch (ins->handler_idx)
3872 case WINED3DSIH_EQ: op = "=="; break;
3873 case WINED3DSIH_IEQ: op = "=="; break;
3874 case WINED3DSIH_GE: op = ">="; break;
3875 case WINED3DSIH_IGE: op = ">="; break;
3876 case WINED3DSIH_UGE: op = ">="; break;
3877 case WINED3DSIH_LT: op = "<"; break;
3878 case WINED3DSIH_ILT: op = "<"; break;
3879 case WINED3DSIH_ULT: op = "<"; break;
3880 case WINED3DSIH_NE: op = "!="; break;
3881 case WINED3DSIH_INE: op = "!="; break;
3882 default:
3883 op = "<unhandled operator>";
3884 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3885 break;
3888 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3889 src0_param.param_str, op, src1_param.param_str);
3893 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3895 struct glsl_src_param src_param;
3896 DWORD write_mask;
3897 const char *op;
3899 switch (ins->handler_idx)
3901 case WINED3DSIH_INEG: op = "-"; break;
3902 case WINED3DSIH_NOT: op = "~"; break;
3903 default:
3904 op = "<unhandled operator>";
3905 ERR("Unhandled opcode %s.\n",
3906 debug_d3dshaderinstructionhandler(ins->handler_idx));
3907 break;
3910 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3911 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3912 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3915 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3917 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3918 struct glsl_src_param src0_param;
3919 struct glsl_src_param src1_param;
3920 DWORD write_mask;
3922 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3923 * If not, we can emulate it. */
3924 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3925 FIXME("64-bit integer multiplies not implemented.\n");
3927 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3929 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3930 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3931 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3933 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3934 src0_param.param_str, src1_param.param_str);
3938 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3940 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3941 struct glsl_src_param src0_param, src1_param;
3942 DWORD write_mask;
3944 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3946 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3948 char dst_mask[6];
3950 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3951 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3952 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3953 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3954 dst_mask, src0_param.param_str, src1_param.param_str);
3956 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3957 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3958 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3959 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3961 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3962 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3964 else
3966 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3967 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3968 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3969 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3972 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3974 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3975 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3976 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3977 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3981 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3982 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3984 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3985 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3986 struct glsl_src_param src0_param;
3987 DWORD write_mask;
3989 write_mask = shader_glsl_append_dst(buffer, ins);
3990 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3992 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3993 * shader versions WINED3DSIO_MOVA is used for this. */
3994 if (ins->ctx->reg_maps->shader_version.major == 1
3995 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3996 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3998 /* This is a simple floor() */
3999 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4000 if (mask_size > 1) {
4001 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4002 } else {
4003 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4006 else if (ins->handler_idx == WINED3DSIH_MOVA)
4008 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4010 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4012 if (mask_size > 1)
4013 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4014 else
4015 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4017 else
4019 if (mask_size > 1)
4020 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4021 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4022 else
4023 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4024 src0_param.param_str, src0_param.param_str);
4027 else
4029 shader_addline(buffer, "%s);\n", src0_param.param_str);
4033 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4034 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4036 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4037 struct glsl_src_param src0_param;
4038 struct glsl_src_param src1_param;
4039 DWORD dst_write_mask, src_write_mask;
4040 unsigned int dst_size;
4042 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4043 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4045 /* dp4 works on vec4, dp3 on vec3, etc. */
4046 if (ins->handler_idx == WINED3DSIH_DP4)
4047 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4048 else if (ins->handler_idx == WINED3DSIH_DP3)
4049 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4050 else
4051 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4053 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4054 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4056 if (dst_size > 1) {
4057 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4058 } else {
4059 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4063 /* Note that this instruction has some restrictions. The destination write mask
4064 * can't contain the w component, and the source swizzles have to be .xyzw */
4065 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4067 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4068 struct glsl_src_param src0_param;
4069 struct glsl_src_param src1_param;
4070 char dst_mask[6];
4072 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4073 shader_glsl_append_dst(ins->ctx->buffer, ins);
4074 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4075 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4076 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4079 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4081 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4083 if (!stream)
4084 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4085 else
4086 FIXME("Unhandled primitive stream %u.\n", stream);
4089 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4090 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4091 * GLSL uses the value as-is. */
4092 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4094 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4095 struct glsl_src_param src0_param;
4096 struct glsl_src_param src1_param;
4097 DWORD dst_write_mask;
4098 unsigned int dst_size;
4100 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4101 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4103 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4104 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4106 if (dst_size > 1)
4108 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4109 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4111 else
4113 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4114 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4118 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4119 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4121 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4122 struct glsl_src_param src_param;
4123 const char *instruction;
4124 DWORD write_mask;
4125 unsigned i;
4127 /* Determine the GLSL function to use based on the opcode */
4128 /* TODO: Possibly make this a table for faster lookups */
4129 switch (ins->handler_idx)
4131 case WINED3DSIH_ABS: instruction = "abs"; break;
4132 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4133 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4134 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4135 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4136 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4137 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4138 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4139 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4140 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4141 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4142 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4143 case WINED3DSIH_FRC: instruction = "fract"; break;
4144 case WINED3DSIH_IMAX: instruction = "max"; break;
4145 case WINED3DSIH_IMIN: instruction = "min"; break;
4146 case WINED3DSIH_MAX: instruction = "max"; break;
4147 case WINED3DSIH_MIN: instruction = "min"; break;
4148 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4149 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4150 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4151 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4152 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4153 case WINED3DSIH_UMAX: instruction = "max"; break;
4154 case WINED3DSIH_UMIN: instruction = "min"; break;
4155 default: instruction = "";
4156 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4157 break;
4160 write_mask = shader_glsl_append_dst(buffer, ins);
4162 /* In D3D bits are numbered from the most significant bit. */
4163 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4164 shader_addline(buffer, "31 - ");
4165 shader_addline(buffer, "%s(", instruction);
4167 if (ins->src_count)
4169 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4170 shader_addline(buffer, "%s", src_param.param_str);
4171 for (i = 1; i < ins->src_count; ++i)
4173 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4174 shader_addline(buffer, ", %s", src_param.param_str);
4178 shader_addline(buffer, "));\n");
4181 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4183 struct wined3d_shader_dst_param dst;
4184 struct glsl_src_param src;
4185 DWORD write_mask;
4186 const char *fmt;
4187 unsigned int i;
4189 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4190 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4192 dst = ins->dst[0];
4193 for (i = 0; i < 4; ++i)
4195 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4196 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4197 &dst, dst.reg.data_type)))
4198 continue;
4200 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4201 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4205 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4207 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4208 struct wined3d_shader_dst_param dst;
4209 struct glsl_src_param src[4];
4210 const char *instruction;
4211 BOOL tmp_dst = FALSE;
4212 char mask_char[6];
4213 unsigned int i, j;
4214 DWORD write_mask;
4216 switch (ins->handler_idx)
4218 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4219 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4220 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4221 default:
4222 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4223 return;
4226 for (i = 0; i < ins->src_count; ++i)
4228 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4229 && ins->dst[0].reg.type == ins->src[i].reg.type)
4230 tmp_dst = TRUE;
4233 dst = ins->dst[0];
4234 for (i = 0; i < 4; ++i)
4236 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4237 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4238 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4239 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4240 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4241 continue;
4243 for (j = 0; j < ins->src_count; ++j)
4244 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4245 shader_addline(buffer, "%s(", instruction);
4246 for (j = 0; j < ins->src_count - 2; ++j)
4247 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4248 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4251 if (tmp_dst)
4253 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4254 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4255 shader_addline(buffer, "tmp0%s);\n", mask_char);
4259 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4261 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4263 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4264 struct glsl_src_param src_param;
4265 unsigned int mask_size;
4266 DWORD write_mask;
4267 char dst_mask[6];
4269 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4270 mask_size = shader_glsl_get_write_mask_size(write_mask);
4271 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4273 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4274 src_param.param_str, src_param.param_str);
4275 shader_glsl_append_dst(buffer, ins);
4277 if (mask_size > 1)
4279 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4280 mask_size, src_param.param_str);
4282 else
4284 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4285 src_param.param_str);
4289 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4291 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4292 ins->ctx->reg_maps->shader_version.minor);
4293 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4294 struct glsl_src_param src0_param;
4295 const char *prefix, *suffix;
4296 unsigned int dst_size;
4297 DWORD dst_write_mask;
4299 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4300 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4302 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4303 dst_write_mask = WINED3DSP_WRITEMASK_3;
4305 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4307 switch (ins->handler_idx)
4309 case WINED3DSIH_EXP:
4310 case WINED3DSIH_EXPP:
4311 prefix = "exp2(";
4312 suffix = ")";
4313 break;
4315 case WINED3DSIH_LOG:
4316 case WINED3DSIH_LOGP:
4317 prefix = "log2(abs(";
4318 suffix = "))";
4319 break;
4321 case WINED3DSIH_RCP:
4322 prefix = "1.0 / ";
4323 suffix = "";
4324 break;
4326 case WINED3DSIH_RSQ:
4327 prefix = "inversesqrt(abs(";
4328 suffix = "))";
4329 break;
4331 default:
4332 prefix = "";
4333 suffix = "";
4334 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4335 break;
4338 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4339 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4340 else
4341 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4344 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4345 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4346 * dst.x = 2^(floor(src))
4347 * dst.y = src - floor(src)
4348 * dst.z = 2^src (partial precision is allowed, but optional)
4349 * dst.w = 1.0;
4350 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4351 * dst = 2^src; (partial precision is allowed, but optional)
4353 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4355 if (ins->ctx->reg_maps->shader_version.major < 2)
4357 struct glsl_src_param src_param;
4358 char dst_mask[6];
4360 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4362 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4363 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4364 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4365 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4367 shader_glsl_append_dst(ins->ctx->buffer, ins);
4368 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4369 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4370 return;
4373 shader_glsl_scalar_op(ins);
4376 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4377 const char *vector_constructor, const char *scalar_constructor)
4379 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4380 struct glsl_src_param src_param;
4381 unsigned int mask_size;
4382 DWORD write_mask;
4384 write_mask = shader_glsl_append_dst(buffer, ins);
4385 mask_size = shader_glsl_get_write_mask_size(write_mask);
4386 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4388 if (mask_size > 1)
4389 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4390 else
4391 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4394 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4396 shader_glsl_cast(ins, "ivec", "int");
4399 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4401 shader_glsl_cast(ins, "uvec", "uint");
4404 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4406 shader_glsl_cast(ins, "vec", "float");
4409 /** Process signed comparison opcodes in GLSL. */
4410 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4412 struct glsl_src_param src0_param;
4413 struct glsl_src_param src1_param;
4414 DWORD write_mask;
4415 unsigned int mask_size;
4417 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4418 mask_size = shader_glsl_get_write_mask_size(write_mask);
4419 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4420 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4422 if (mask_size > 1) {
4423 const char *compare;
4425 switch(ins->handler_idx)
4427 case WINED3DSIH_SLT: compare = "lessThan"; break;
4428 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4429 default: compare = "";
4430 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4433 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4434 src0_param.param_str, src1_param.param_str);
4435 } else {
4436 switch(ins->handler_idx)
4438 case WINED3DSIH_SLT:
4439 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4440 * to return 0.0 but step returns 1.0 because step is not < x
4441 * An alternative is a bvec compare padded with an unused second component.
4442 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4443 * issue. Playing with not() is not possible either because not() does not accept
4444 * a scalar.
4446 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4447 src0_param.param_str, src1_param.param_str);
4448 break;
4449 case WINED3DSIH_SGE:
4450 /* Here we can use the step() function and safe a conditional */
4451 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4452 break;
4453 default:
4454 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4460 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4462 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4463 struct wined3d_shader_dst_param dst[2];
4464 struct glsl_src_param src[3];
4465 unsigned int i, j, k;
4466 char mask_char[6];
4467 DWORD write_mask;
4468 BOOL tmp_dst[2];
4470 for (i = 0; i < ins->dst_count; ++i)
4472 tmp_dst[i] = FALSE;
4473 for (j = 0; j < ins->src_count; ++j)
4475 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4476 && ins->dst[i].reg.type == ins->src[j].reg.type)
4477 tmp_dst[i] = TRUE;
4481 dst[0] = ins->dst[0];
4482 dst[1] = ins->dst[1];
4483 for (i = 0; i < 4; ++i)
4485 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4487 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4488 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4489 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4490 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4491 continue;
4493 for (k = 0; k < ARRAY_SIZE(src); ++k)
4494 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4496 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4497 src[0].param_str, src[1].param_str, src[2].param_str);
4501 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4503 if (tmp_dst[i])
4505 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4506 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4507 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4512 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4514 const char *condition_prefix, *condition_suffix;
4515 struct wined3d_shader_dst_param dst;
4516 struct glsl_src_param src0_param;
4517 struct glsl_src_param src1_param;
4518 struct glsl_src_param src2_param;
4519 BOOL temp_destination = FALSE;
4520 DWORD cmp_channel = 0;
4521 unsigned int i, j;
4522 char mask_char[6];
4523 DWORD write_mask;
4525 switch (ins->handler_idx)
4527 case WINED3DSIH_CMP:
4528 condition_prefix = "";
4529 condition_suffix = " >= 0.0";
4530 break;
4532 case WINED3DSIH_CND:
4533 condition_prefix = "";
4534 condition_suffix = " > 0.5";
4535 break;
4537 case WINED3DSIH_MOVC:
4538 condition_prefix = "bool(";
4539 condition_suffix = ")";
4540 break;
4542 default:
4543 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4544 condition_prefix = "<unhandled prefix>";
4545 condition_suffix = "<unhandled suffix>";
4546 break;
4549 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4551 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4552 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4553 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4554 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4556 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4557 condition_prefix, src0_param.param_str, condition_suffix,
4558 src1_param.param_str, src2_param.param_str);
4559 return;
4562 dst = ins->dst[0];
4564 /* Splitting the instruction up in multiple lines imposes a problem:
4565 * The first lines may overwrite source parameters of the following lines.
4566 * Deal with that by using a temporary destination register if needed. */
4567 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4568 && ins->src[0].reg.type == dst.reg.type)
4569 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4570 && ins->src[1].reg.type == dst.reg.type)
4571 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4572 && ins->src[2].reg.type == dst.reg.type))
4573 temp_destination = TRUE;
4575 /* Cycle through all source0 channels. */
4576 for (i = 0; i < 4; ++i)
4578 write_mask = 0;
4579 /* Find the destination channels which use the current source0 channel. */
4580 for (j = 0; j < 4; ++j)
4582 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4584 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4585 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4588 dst.write_mask = ins->dst[0].write_mask & write_mask;
4590 if (temp_destination)
4592 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4593 continue;
4594 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4596 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4597 continue;
4599 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4600 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4601 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4603 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4604 condition_prefix, src0_param.param_str, condition_suffix,
4605 src1_param.param_str, src2_param.param_str);
4608 if (temp_destination)
4610 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4611 shader_glsl_append_dst(ins->ctx->buffer, ins);
4612 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4616 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4617 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4618 * the compare is done per component of src0. */
4619 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4621 struct glsl_src_param src0_param;
4622 struct glsl_src_param src1_param;
4623 struct glsl_src_param src2_param;
4624 DWORD write_mask;
4625 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4626 ins->ctx->reg_maps->shader_version.minor);
4628 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4630 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4631 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4632 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4633 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4635 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4636 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4637 else
4638 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4639 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4640 return;
4643 shader_glsl_conditional_move(ins);
4646 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4647 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4649 struct glsl_src_param src0_param;
4650 struct glsl_src_param src1_param;
4651 struct glsl_src_param src2_param;
4652 DWORD write_mask;
4654 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4655 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4656 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4657 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4658 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4659 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4662 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4663 Vertex shaders to GLSL codes */
4664 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4666 int i;
4667 int nComponents = 0;
4668 struct wined3d_shader_dst_param tmp_dst = {{0}};
4669 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4670 struct wined3d_shader_instruction tmp_ins;
4672 memset(&tmp_ins, 0, sizeof(tmp_ins));
4674 /* Set constants for the temporary argument */
4675 tmp_ins.ctx = ins->ctx;
4676 tmp_ins.dst_count = 1;
4677 tmp_ins.dst = &tmp_dst;
4678 tmp_ins.src_count = 2;
4679 tmp_ins.src = tmp_src;
4681 switch(ins->handler_idx)
4683 case WINED3DSIH_M4x4:
4684 nComponents = 4;
4685 tmp_ins.handler_idx = WINED3DSIH_DP4;
4686 break;
4687 case WINED3DSIH_M4x3:
4688 nComponents = 3;
4689 tmp_ins.handler_idx = WINED3DSIH_DP4;
4690 break;
4691 case WINED3DSIH_M3x4:
4692 nComponents = 4;
4693 tmp_ins.handler_idx = WINED3DSIH_DP3;
4694 break;
4695 case WINED3DSIH_M3x3:
4696 nComponents = 3;
4697 tmp_ins.handler_idx = WINED3DSIH_DP3;
4698 break;
4699 case WINED3DSIH_M3x2:
4700 nComponents = 2;
4701 tmp_ins.handler_idx = WINED3DSIH_DP3;
4702 break;
4703 default:
4704 break;
4707 tmp_dst = ins->dst[0];
4708 tmp_src[0] = ins->src[0];
4709 tmp_src[1] = ins->src[1];
4710 for (i = 0; i < nComponents; ++i)
4712 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4713 shader_glsl_dot(&tmp_ins);
4714 ++tmp_src[1].reg.idx[0].offset;
4719 The LRP instruction performs a component-wise linear interpolation
4720 between the second and third operands using the first operand as the
4721 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4722 This is equivalent to mix(src2, src1, src0);
4724 static void shader_glsl_lrp(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);
4733 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4734 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4735 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4737 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4738 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4741 /** Process the WINED3DSIO_LIT instruction in GLSL:
4742 * dst.x = dst.w = 1.0
4743 * dst.y = (src0.x > 0) ? src0.x
4744 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4745 * where src.w is clamped at +- 128
4747 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4749 struct glsl_src_param src0_param;
4750 struct glsl_src_param src1_param;
4751 struct glsl_src_param src3_param;
4752 char dst_mask[6];
4754 shader_glsl_append_dst(ins->ctx->buffer, ins);
4755 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4757 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4758 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4759 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4761 /* The sdk specifies the instruction like this
4762 * dst.x = 1.0;
4763 * if(src.x > 0.0) dst.y = src.x
4764 * else dst.y = 0.0.
4765 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4766 * else dst.z = 0.0;
4767 * dst.w = 1.0;
4768 * (where power = src.w clamped between -128 and 128)
4770 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4771 * dst.x = 1.0 ... No further explanation needed
4772 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4773 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4774 * dst.w = 1.0. ... Nothing fancy.
4776 * So we still have one conditional in there. So do this:
4777 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4779 * 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),
4780 * 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.
4781 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4783 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4784 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4785 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4787 shader_addline(ins->ctx->buffer,
4788 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4789 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4790 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4791 src0_param.param_str, src3_param.param_str, dst_mask);
4794 /** Process the WINED3DSIO_DST instruction in GLSL:
4795 * dst.x = 1.0
4796 * dst.y = src0.x * src0.y
4797 * dst.z = src0.z
4798 * dst.w = src1.w
4800 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4802 struct glsl_src_param src0y_param;
4803 struct glsl_src_param src0z_param;
4804 struct glsl_src_param src1y_param;
4805 struct glsl_src_param src1w_param;
4806 char dst_mask[6];
4808 shader_glsl_append_dst(ins->ctx->buffer, ins);
4809 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4811 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4812 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4813 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4814 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4816 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4817 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4820 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4821 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4822 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4824 * dst.x = cos(src0.?)
4825 * dst.y = sin(src0.?)
4826 * dst.z = dst.z
4827 * dst.w = dst.w
4829 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4831 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4832 struct glsl_src_param src0_param;
4833 DWORD write_mask;
4835 if (ins->ctx->reg_maps->shader_version.major < 4)
4837 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4839 write_mask = shader_glsl_append_dst(buffer, ins);
4840 switch (write_mask)
4842 case WINED3DSP_WRITEMASK_0:
4843 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4844 break;
4846 case WINED3DSP_WRITEMASK_1:
4847 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4848 break;
4850 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4851 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4852 src0_param.param_str, src0_param.param_str);
4853 break;
4855 default:
4856 ERR("Write mask should be .x, .y or .xy\n");
4857 break;
4860 return;
4863 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4866 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4868 char dst_mask[6];
4870 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4871 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4872 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4874 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4875 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4876 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4878 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4879 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4881 else
4883 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4884 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4885 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4888 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4890 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4891 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4892 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4896 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4897 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4898 * generate invalid code
4900 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4902 struct glsl_src_param src0_param;
4903 DWORD write_mask;
4905 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4906 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4908 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4911 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4912 * Start a for() loop where src1.y is the initial value of aL,
4913 * increment aL by src1.z for a total of src1.x iterations.
4914 * Need to use a temporary variable for this operation.
4916 /* FIXME: I don't think nested loops will work correctly this way. */
4917 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4919 struct wined3d_shader_parser_state *state = ins->ctx->state;
4920 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4921 const struct wined3d_shader *shader = ins->ctx->shader;
4922 const struct wined3d_shader_lconst *constant;
4923 struct glsl_src_param src1_param;
4924 const DWORD *control_values = NULL;
4926 if (ins->ctx->reg_maps->shader_version.major < 4)
4928 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4930 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4931 * class hardware doesn't support real varying indexing, but Microsoft
4932 * designed this feature for Shader model 2.x+. If the loop control is
4933 * known at compile time, the GLSL compiler can unroll the loop, and
4934 * replace indirect addressing with direct addressing. */
4935 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4937 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4939 if (constant->idx == ins->src[1].reg.idx[0].offset)
4941 control_values = constant->value;
4942 break;
4947 if (control_values)
4949 struct wined3d_shader_loop_control loop_control;
4950 loop_control.count = control_values[0];
4951 loop_control.start = control_values[1];
4952 loop_control.step = (int)control_values[2];
4954 if (loop_control.step > 0)
4956 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4957 state->current_loop_depth, loop_control.start,
4958 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4959 state->current_loop_depth, loop_control.step);
4961 else if (loop_control.step < 0)
4963 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4964 state->current_loop_depth, loop_control.start,
4965 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4966 state->current_loop_depth, loop_control.step);
4968 else
4970 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4971 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4972 state->current_loop_depth, loop_control.count,
4973 state->current_loop_depth);
4976 else
4978 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4979 state->current_loop_depth, state->current_loop_reg,
4980 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4981 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4984 ++state->current_loop_reg;
4986 else
4988 shader_addline(buffer, "for (;;)\n{\n");
4991 ++state->current_loop_depth;
4994 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4996 struct wined3d_shader_parser_state *state = ins->ctx->state;
4998 shader_addline(ins->ctx->buffer, "}\n");
5000 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5002 --state->current_loop_depth;
5003 --state->current_loop_reg;
5006 if (ins->handler_idx == WINED3DSIH_ENDREP)
5008 --state->current_loop_depth;
5012 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5014 struct wined3d_shader_parser_state *state = ins->ctx->state;
5015 const struct wined3d_shader *shader = ins->ctx->shader;
5016 const struct wined3d_shader_lconst *constant;
5017 struct glsl_src_param src0_param;
5018 const DWORD *control_values = NULL;
5020 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5021 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5023 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5025 if (constant->idx == ins->src[0].reg.idx[0].offset)
5027 control_values = constant->value;
5028 break;
5033 if (control_values)
5035 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5036 state->current_loop_depth, state->current_loop_depth,
5037 control_values[0], state->current_loop_depth);
5039 else
5041 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5042 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5043 state->current_loop_depth, state->current_loop_depth,
5044 src0_param.param_str, state->current_loop_depth);
5047 ++state->current_loop_depth;
5050 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5052 struct glsl_src_param src0_param;
5054 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5055 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5058 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5060 struct glsl_src_param src0_param;
5062 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5063 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5066 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5068 shader_addline(ins->ctx->buffer, "default:\n");
5071 static void shader_glsl_generate_conditional_op(const struct wined3d_shader_instruction *ins,
5072 const char *op)
5074 struct glsl_src_param src_param;
5075 const char *condition;
5077 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5078 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5079 shader_addline(ins->ctx->buffer, "if (%s(%s)) %s\n", condition, src_param.param_str, op);
5082 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5084 shader_glsl_generate_conditional_op(ins, "{");
5087 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5089 struct glsl_src_param src0_param;
5090 struct glsl_src_param src1_param;
5092 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5093 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5095 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5096 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5099 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5101 shader_addline(ins->ctx->buffer, "} else {\n");
5104 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5106 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5108 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5109 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5110 shader_glsl_fixup_position(ins->ctx->buffer);
5112 if (!stream)
5113 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5114 else
5115 FIXME("Unhandled primitive stream %u.\n", stream);
5118 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5120 shader_addline(ins->ctx->buffer, "break;\n");
5123 /* FIXME: According to MSDN the compare is done per component. */
5124 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5126 struct glsl_src_param src0_param;
5127 struct glsl_src_param src1_param;
5129 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5130 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5132 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5133 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5136 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5138 const char *op;
5140 switch (ins->handler_idx)
5142 case WINED3DSIH_BREAKP: op = "break;"; break;
5143 case WINED3DSIH_CONTINUEP: op = "continue;"; break;
5144 case WINED3DSIH_RETP: op = "return;"; break;
5145 default:
5146 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5147 return;
5150 shader_glsl_generate_conditional_op(ins, op);
5153 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5155 shader_addline(ins->ctx->buffer, "continue;\n");
5158 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5160 shader_addline(ins->ctx->buffer, "}\n");
5161 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5163 /* Subroutines appear at the end of the shader. */
5164 ins->ctx->state->in_subroutine = TRUE;
5167 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5169 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5172 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5174 struct glsl_src_param src1_param;
5176 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5177 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5178 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5181 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5183 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5185 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5187 shader_glsl_generate_shader_epilogue(ins->ctx);
5188 shader_addline(ins->ctx->buffer, "return;\n");
5192 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5194 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5195 ins->ctx->reg_maps->shader_version.minor);
5196 struct glsl_sample_function sample_function;
5197 DWORD sample_flags = 0;
5198 DWORD resource_idx;
5199 DWORD mask = 0, swizzle;
5200 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5202 /* 1.0-1.4: Use destination register as sampler source.
5203 * 2.0+: Use provided sampler source. */
5204 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5205 resource_idx = ins->dst[0].reg.idx[0].offset;
5206 else
5207 resource_idx = ins->src[1].reg.idx[0].offset;
5209 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5211 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5212 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5213 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5215 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5216 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5218 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5219 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5221 case WINED3D_TTFF_COUNT1:
5222 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5223 break;
5224 case WINED3D_TTFF_COUNT2:
5225 mask = WINED3DSP_WRITEMASK_1;
5226 break;
5227 case WINED3D_TTFF_COUNT3:
5228 mask = WINED3DSP_WRITEMASK_2;
5229 break;
5230 case WINED3D_TTFF_COUNT4:
5231 case WINED3D_TTFF_DISABLE:
5232 mask = WINED3DSP_WRITEMASK_3;
5233 break;
5237 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5239 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5241 if (src_mod == WINED3DSPSM_DZ) {
5242 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5243 mask = WINED3DSP_WRITEMASK_2;
5244 } else if (src_mod == WINED3DSPSM_DW) {
5245 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5246 mask = WINED3DSP_WRITEMASK_3;
5249 else
5251 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5252 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5254 /* ps 2.0 texldp instruction always divides by the fourth component. */
5255 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5256 mask = WINED3DSP_WRITEMASK_3;
5260 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5261 mask |= sample_function.coord_mask;
5262 sample_function.coord_mask = mask;
5264 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5265 else swizzle = ins->src[1].swizzle;
5267 /* 1.0-1.3: Use destination register as coordinate source.
5268 1.4+: Use provided coordinate source register. */
5269 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5271 char coord_mask[6];
5272 shader_glsl_write_mask_to_str(mask, coord_mask);
5273 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5274 "T%u%s", resource_idx, coord_mask);
5276 else
5278 struct glsl_src_param coord_param;
5279 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5280 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5282 struct glsl_src_param bias;
5283 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5284 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5285 NULL, "%s", coord_param.param_str);
5286 } else {
5287 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5288 "%s", coord_param.param_str);
5291 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5294 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5296 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5297 struct glsl_src_param coord_param, dx_param, dy_param;
5298 struct glsl_sample_function sample_function;
5299 DWORD sampler_idx;
5300 DWORD swizzle = ins->src[1].swizzle;
5302 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5304 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5305 shader_glsl_tex(ins);
5306 return;
5309 sampler_idx = ins->src[1].reg.idx[0].offset;
5311 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5312 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5313 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5314 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5316 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5317 NULL, NULL, "%s", coord_param.param_str);
5318 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5321 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5323 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5324 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5325 struct glsl_src_param coord_param, lod_param;
5326 struct glsl_sample_function sample_function;
5327 DWORD swizzle = ins->src[1].swizzle;
5328 DWORD sampler_idx;
5330 sampler_idx = ins->src[1].reg.idx[0].offset;
5332 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5333 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5335 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5337 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5338 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5340 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5341 * However, the NVIDIA drivers allow them in fragment shaders as well,
5342 * even without the appropriate extension. */
5343 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5345 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5346 "%s", coord_param.param_str);
5347 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5350 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5351 unsigned int resource_idx, unsigned int sampler_idx)
5353 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5354 unsigned int i;
5356 for (i = 0; i < sampler_map->count; ++i)
5358 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5359 return entries[i].bind_idx;
5362 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5364 return ~0u;
5367 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5369 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5370 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5371 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5372 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5373 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5374 struct glsl_src_param structure_idx, offset, data, data2;
5375 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5376 enum wined3d_shader_resource_type resource_type;
5377 struct wined3d_string_buffer *address;
5378 enum wined3d_data_type data_type;
5379 unsigned int resource_idx, stride;
5380 const char *op, *resource;
5381 DWORD coord_mask;
5382 BOOL is_tgsm;
5384 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5385 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5386 if (is_tgsm)
5388 if (resource_idx >= reg_maps->tgsm_count)
5390 ERR("Invalid TGSM index %u.\n", resource_idx);
5391 return;
5393 resource = "g";
5394 data_type = WINED3D_DATA_UINT;
5395 coord_mask = 1;
5396 stride = reg_maps->tgsm[resource_idx].stride;
5398 else
5400 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5402 ERR("Invalid UAV index %u.\n", resource_idx);
5403 return;
5405 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5406 if (resource_type >= ARRAY_SIZE(resource_type_info))
5408 ERR("Unexpected resource type %#x.\n", resource_type);
5409 return;
5411 resource = "image";
5412 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5413 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5414 stride = reg_maps->uav_resource_info[resource_idx].stride;
5417 switch (ins->handler_idx)
5419 case WINED3DSIH_ATOMIC_AND:
5420 case WINED3DSIH_IMM_ATOMIC_AND:
5421 if (is_tgsm)
5422 op = "atomicAnd";
5423 else
5424 op = "imageAtomicAnd";
5425 break;
5426 case WINED3DSIH_ATOMIC_CMP_STORE:
5427 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5428 if (is_tgsm)
5429 op = "atomicCompSwap";
5430 else
5431 op = "imageAtomicCompSwap";
5432 break;
5433 case WINED3DSIH_ATOMIC_IADD:
5434 case WINED3DSIH_IMM_ATOMIC_IADD:
5435 if (is_tgsm)
5436 op = "atomicAdd";
5437 else
5438 op = "imageAtomicAdd";
5439 break;
5440 case WINED3DSIH_ATOMIC_IMAX:
5441 case WINED3DSIH_IMM_ATOMIC_IMAX:
5442 if (is_tgsm)
5443 op = "atomicMax";
5444 else
5445 op = "imageAtomicMax";
5446 if (data_type != WINED3D_DATA_INT)
5448 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5449 return;
5451 break;
5452 case WINED3DSIH_ATOMIC_IMIN:
5453 case WINED3DSIH_IMM_ATOMIC_IMIN:
5454 if (is_tgsm)
5455 op = "atomicMin";
5456 else
5457 op = "imageAtomicMin";
5458 if (data_type != WINED3D_DATA_INT)
5460 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5461 return;
5463 break;
5464 case WINED3DSIH_ATOMIC_OR:
5465 case WINED3DSIH_IMM_ATOMIC_OR:
5466 if (is_tgsm)
5467 op = "atomicOr";
5468 else
5469 op = "imageAtomicOr";
5470 break;
5471 case WINED3DSIH_ATOMIC_UMAX:
5472 case WINED3DSIH_IMM_ATOMIC_UMAX:
5473 if (is_tgsm)
5474 op = "atomicMax";
5475 else
5476 op = "imageAtomicMax";
5477 if (data_type != WINED3D_DATA_UINT)
5479 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5480 return;
5482 break;
5483 case WINED3DSIH_ATOMIC_UMIN:
5484 case WINED3DSIH_IMM_ATOMIC_UMIN:
5485 if (is_tgsm)
5486 op = "atomicMin";
5487 else
5488 op = "imageAtomicMin";
5489 if (data_type != WINED3D_DATA_UINT)
5491 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5492 return;
5494 break;
5495 case WINED3DSIH_ATOMIC_XOR:
5496 case WINED3DSIH_IMM_ATOMIC_XOR:
5497 if (is_tgsm)
5498 op = "atomicXor";
5499 else
5500 op = "imageAtomicXor";
5501 break;
5502 case WINED3DSIH_IMM_ATOMIC_EXCH:
5503 if (is_tgsm)
5504 op = "atomicExchange";
5505 else
5506 op = "imageAtomicExchange";
5507 break;
5508 default:
5509 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5510 return;
5513 address = string_buffer_get(priv->string_buffers);
5514 if (stride)
5516 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5517 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5518 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5520 else
5522 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5523 string_buffer_sprintf(address, "%s", offset.param_str);
5524 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5525 shader_addline(address, "/ 4");
5528 if (is_imm_instruction)
5529 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5531 if (is_tgsm)
5532 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5533 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5534 else
5535 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5536 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5538 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5539 shader_addline(buffer, "%s", data.param_str);
5540 if (ins->src_count >= 3)
5542 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5543 shader_addline(buffer, ", %s", data2.param_str);
5546 if (is_imm_instruction)
5547 shader_addline(buffer, ")");
5548 shader_addline(buffer, ");\n");
5550 string_buffer_release(priv->string_buffers, address);
5553 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5555 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5556 const char *op;
5558 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5559 op = "atomicCounterIncrement";
5560 else
5561 op = "atomicCounterDecrement";
5563 shader_glsl_append_dst(ins->ctx->buffer, ins);
5564 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5567 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5569 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5570 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5571 enum wined3d_shader_resource_type resource_type;
5572 struct glsl_src_param image_coord_param;
5573 enum wined3d_data_type data_type;
5574 DWORD coord_mask, write_mask;
5575 unsigned int uav_idx;
5576 char dst_swizzle[6];
5578 uav_idx = ins->src[1].reg.idx[0].offset;
5579 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5581 ERR("Invalid UAV index %u.\n", uav_idx);
5582 return;
5584 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5585 if (resource_type >= ARRAY_SIZE(resource_type_info))
5587 ERR("Unexpected resource type %#x.\n", resource_type);
5588 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5590 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5591 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5593 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5594 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5596 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5597 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5598 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5601 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5603 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5604 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5605 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5606 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5607 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5608 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5609 struct glsl_src_param structure_idx, offset;
5610 struct wined3d_string_buffer *address;
5611 struct wined3d_shader_dst_param dst;
5612 const char *function, *resource;
5614 resource_idx = src->reg.idx[0].offset;
5615 if (src->reg.type == WINED3DSPR_RESOURCE)
5617 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5619 ERR("Invalid resource index %u.\n", resource_idx);
5620 return;
5622 stride = reg_maps->resource_info[resource_idx].stride;
5623 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5624 function = "texelFetch";
5625 resource = "sampler";
5627 else if (src->reg.type == WINED3DSPR_UAV)
5629 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5631 ERR("Invalid UAV index %u.\n", resource_idx);
5632 return;
5634 stride = reg_maps->uav_resource_info[resource_idx].stride;
5635 bind_idx = resource_idx;
5636 function = "imageLoad";
5637 resource = "image";
5639 else
5641 if (resource_idx >= reg_maps->tgsm_count)
5643 ERR("Invalid TGSM index %u.\n", resource_idx);
5644 return;
5646 stride = reg_maps->tgsm[resource_idx].stride;
5647 bind_idx = resource_idx;
5648 function = NULL;
5649 resource = "g";
5652 address = string_buffer_get(priv->string_buffers);
5653 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5655 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5656 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5658 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5659 shader_addline(address, "%s / 4", offset.param_str);
5661 dst = ins->dst[0];
5662 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5664 /* The instruction is split into multiple lines. The first lines may
5665 * overwrite source parameters of the following lines. */
5666 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5667 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5670 for (i = 0; i < 4; ++i)
5672 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5673 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5674 continue;
5676 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5677 if (function)
5678 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5679 function, prefix, resource, bind_idx, address->buffer, swizzle);
5680 else
5681 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5682 prefix, resource, bind_idx, address->buffer, swizzle);
5685 string_buffer_release(priv->string_buffers, address);
5688 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5690 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5691 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5692 struct glsl_src_param image_coord_param, image_data_param;
5693 enum wined3d_shader_resource_type resource_type;
5694 enum wined3d_data_type data_type;
5695 unsigned int uav_idx;
5696 DWORD coord_mask;
5698 uav_idx = ins->dst[0].reg.idx[0].offset;
5699 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5701 ERR("Invalid UAV index %u.\n", uav_idx);
5702 return;
5704 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5705 if (resource_type >= ARRAY_SIZE(resource_type_info))
5707 ERR("Unexpected resource type %#x.\n", resource_type);
5708 return;
5710 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5711 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5713 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5714 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5715 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5716 shader_glsl_get_prefix(version->type), uav_idx,
5717 image_coord_param.param_str, image_data_param.param_str);
5720 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5722 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5723 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5724 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5725 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5726 struct glsl_src_param structure_idx, offset, data;
5727 unsigned int i, resource_idx, stride, src_idx = 0;
5728 struct wined3d_string_buffer *address;
5729 DWORD write_mask;
5730 BOOL is_tgsm;
5732 resource_idx = ins->dst[0].reg.idx[0].offset;
5733 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5734 if (is_tgsm)
5736 if (resource_idx >= reg_maps->tgsm_count)
5738 ERR("Invalid TGSM index %u.\n", resource_idx);
5739 return;
5741 stride = reg_maps->tgsm[resource_idx].stride;
5743 else
5745 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5747 ERR("Invalid UAV index %u.\n", resource_idx);
5748 return;
5750 stride = reg_maps->uav_resource_info[resource_idx].stride;
5753 address = string_buffer_get(priv->string_buffers);
5754 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5756 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5757 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5759 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5760 shader_addline(address, "%s / 4", offset.param_str);
5762 for (i = 0; i < 4; ++i)
5764 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5765 continue;
5767 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5769 if (is_tgsm)
5770 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5771 prefix, resource_idx, address->buffer, i, data.param_str);
5772 else
5773 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5774 prefix, resource_idx, address->buffer, i, data.param_str);
5777 string_buffer_release(priv->string_buffers, address);
5780 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5782 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5783 unsigned int sync_flags = ins->flags;
5785 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5787 shader_addline(buffer, "barrier();\n");
5788 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5791 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5793 shader_addline(buffer, "memoryBarrierShared();\n");
5794 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5797 if (sync_flags)
5798 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5801 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5802 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5804 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5805 unsigned int idx = reg->idx[0].offset;
5807 if (reg->type == WINED3DSPR_RESOURCE)
5809 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5811 ERR("Invalid resource index %u.\n", idx);
5812 return NULL;
5814 return &reg_maps->resource_info[idx];
5817 if (reg->type == WINED3DSPR_UAV)
5819 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5821 ERR("Invalid UAV index %u.\n", idx);
5822 return NULL;
5824 return &reg_maps->uav_resource_info[idx];
5827 FIXME("Unhandled register type %#x.\n", reg->type);
5828 return NULL;
5831 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5833 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5834 const struct wined3d_shader_resource_info *resource_info;
5835 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5836 unsigned int resource_idx;
5837 char dst_swizzle[6];
5838 DWORD write_mask;
5840 write_mask = shader_glsl_append_dst(buffer, ins);
5841 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5843 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5844 return;
5845 resource_idx = ins->src[0].reg.idx[0].offset;
5847 shader_addline(buffer, "ivec2(");
5848 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5850 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5851 resource_idx, WINED3D_SAMPLER_DEFAULT);
5852 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5854 else
5856 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5858 if (resource_info->stride)
5859 shader_addline(buffer, " / %u", resource_info->stride);
5860 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5861 shader_addline(buffer, " * 4");
5862 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5865 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5867 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5868 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5869 enum wined3d_shader_resource_type resource_type;
5870 enum wined3d_shader_register_type reg_type;
5871 unsigned int resource_idx, bind_idx, i;
5872 enum wined3d_data_type dst_data_type;
5873 struct glsl_src_param lod_param;
5874 char dst_swizzle[6];
5875 DWORD write_mask;
5877 dst_data_type = ins->dst[0].reg.data_type;
5878 if (ins->flags == WINED3DSI_RESINFO_UINT)
5879 dst_data_type = WINED3D_DATA_UINT;
5880 else if (ins->flags)
5881 FIXME("Unhandled flags %#x.\n", ins->flags);
5883 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5884 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5886 reg_type = ins->src[1].reg.type;
5887 resource_idx = ins->src[1].reg.idx[0].offset;
5888 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5889 if (reg_type == WINED3DSPR_RESOURCE)
5891 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5892 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5893 resource_idx, WINED3D_SAMPLER_DEFAULT);
5895 else
5897 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5898 bind_idx = resource_idx;
5901 if (resource_type >= ARRAY_SIZE(resource_type_info))
5903 ERR("Unexpected resource type %#x.\n", resource_type);
5904 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5907 if (dst_data_type == WINED3D_DATA_UINT)
5908 shader_addline(ins->ctx->buffer, "uvec4(");
5909 else
5910 shader_addline(ins->ctx->buffer, "vec4(");
5912 if (reg_type == WINED3DSPR_RESOURCE)
5914 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5915 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5917 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5918 shader_addline(ins->ctx->buffer, "0, ");
5920 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5922 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5923 shader_glsl_get_prefix(version->type), bind_idx);
5925 else
5927 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5928 shader_addline(ins->ctx->buffer, "1");
5931 else
5933 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5934 shader_glsl_get_prefix(version->type), bind_idx);
5936 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5937 shader_addline(ins->ctx->buffer, "0, ");
5939 /* For UAVs the returned miplevel count is always 1. */
5940 shader_addline(ins->ctx->buffer, "1");
5943 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5946 /* FIXME: The current implementation does not handle multisample textures correctly. */
5947 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5949 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5950 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5951 struct glsl_src_param coord_param, lod_param;
5952 struct glsl_sample_function sample_function;
5953 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5954 BOOL has_lod_param;
5956 if (wined3d_shader_instruction_has_texel_offset(ins))
5957 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5959 resource_idx = ins->src[1].reg.idx[0].offset;
5960 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5962 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5964 ERR("Invalid resource index %u.\n", resource_idx);
5965 return;
5967 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5969 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5970 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5971 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5972 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5973 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5974 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5975 "%s", coord_param.param_str);
5976 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5979 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5981 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5982 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5983 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5984 struct glsl_sample_function sample_function;
5985 DWORD flags = 0;
5987 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5988 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5989 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5990 flags |= WINED3D_GLSL_SAMPLE_LOD;
5991 if (wined3d_shader_instruction_has_texel_offset(ins))
5992 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5994 resource_idx = ins->src[1].reg.idx[0].offset;
5995 sampler_idx = ins->src[2].reg.idx[0].offset;
5997 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5998 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6000 switch (ins->handler_idx)
6002 case WINED3DSIH_SAMPLE:
6003 break;
6004 case WINED3DSIH_SAMPLE_B:
6005 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6006 lod_param_str = lod_param.param_str;
6007 break;
6008 case WINED3DSIH_SAMPLE_GRAD:
6009 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6010 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6011 dx_param_str = dx_param.param_str;
6012 dy_param_str = dy_param.param_str;
6013 break;
6014 case WINED3DSIH_SAMPLE_LOD:
6015 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6016 lod_param_str = lod_param.param_str;
6017 break;
6018 default:
6019 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6020 break;
6023 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6024 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6025 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6026 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6029 /* GLSL doesn't provide a function to sample from level zero with depth
6030 * comparison for array textures and cube textures. We use textureGrad*()
6031 * to implement sample_c_lz.
6033 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6034 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6035 unsigned int coord_size, const char *coord_param, const char *ref_param)
6037 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6038 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6039 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6040 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6041 char dst_swizzle[6];
6043 WARN("Emitting textureGrad() for sample_c_lz.\n");
6045 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6046 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6047 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6048 sample_function->offset_size ? "Offset" : "",
6049 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6050 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6051 if (sample_function->offset_size)
6053 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6054 shader_addline(buffer, ", ");
6055 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6057 shader_addline(buffer, "))%s);\n", dst_swizzle);
6060 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6062 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6063 const struct wined3d_shader_resource_info *resource_info;
6064 struct glsl_src_param coord_param, compare_param;
6065 struct glsl_sample_function sample_function;
6066 const char *lod_param = NULL;
6067 unsigned int coord_size;
6068 DWORD flags = 0;
6070 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6072 lod_param = "0";
6073 flags |= WINED3D_GLSL_SAMPLE_LOD;
6076 if (wined3d_shader_instruction_has_texel_offset(ins))
6077 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6079 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6080 return;
6081 resource_idx = ins->src[1].reg.idx[0].offset;
6082 sampler_idx = ins->src[2].reg.idx[0].offset;
6084 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6085 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6086 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6087 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6088 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6089 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6090 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6091 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6093 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6094 coord_size, coord_param.param_str, compare_param.param_str);
6096 else
6098 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6099 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6100 coord_size, coord_param.param_str, compare_param.param_str);
6102 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6105 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6107 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6108 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6109 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6110 struct glsl_src_param coord_param, compare_param, offset_param;
6111 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6112 const struct wined3d_shader_resource_info *resource_info;
6113 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6114 unsigned int coord_size, offset_size;
6115 char dst_swizzle[6];
6116 BOOL has_offset;
6118 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6120 FIXME("OpenGL implementation does not support textureGather.\n");
6121 return;
6124 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6125 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6126 || wined3d_shader_instruction_has_texel_offset(ins);
6128 resource_param_idx =
6129 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6130 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6131 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6132 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6133 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6135 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6136 return;
6138 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6140 ERR("Unexpected resource type %#x.\n", resource_info->type);
6141 return;
6143 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6145 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6146 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6148 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6150 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6151 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6152 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6154 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6155 shader_addline(buffer, ", %s", compare_param.param_str);
6157 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6159 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6160 shader_addline(buffer, ", %s", offset_param.param_str);
6162 else if (has_offset)
6164 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6165 shader_addline(buffer, ", ");
6166 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6168 if (component_idx)
6169 shader_addline(buffer, ", %u", component_idx);
6171 shader_addline(buffer, ")%s);\n", dst_swizzle);
6174 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6176 /* FIXME: Make this work for more than just 2D textures */
6177 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6178 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6180 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6182 char dst_mask[6];
6184 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6185 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6186 ins->dst[0].reg.idx[0].offset, dst_mask);
6188 else
6190 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6191 DWORD reg = ins->src[0].reg.idx[0].offset;
6192 char dst_swizzle[6];
6194 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6196 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6198 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6199 struct glsl_src_param div_param;
6200 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6202 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6204 if (mask_size > 1)
6205 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6206 else
6207 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6209 else
6211 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6216 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6217 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6218 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6219 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6221 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6222 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6223 struct glsl_sample_function sample_function;
6224 struct glsl_src_param src0_param;
6225 UINT mask_size;
6227 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6229 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6230 * scalar, and projected sampling would require 4.
6232 * It is a dependent read - not valid with conditional NP2 textures
6234 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6235 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6237 switch(mask_size)
6239 case 1:
6240 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6241 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6242 break;
6244 case 2:
6245 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6246 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6247 break;
6249 case 3:
6250 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6251 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6252 break;
6254 default:
6255 FIXME("Unexpected mask size %u\n", mask_size);
6256 break;
6258 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6261 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6262 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6263 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6265 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6266 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6267 struct glsl_src_param src0_param;
6268 DWORD dst_mask;
6269 unsigned int mask_size;
6271 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6272 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6273 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6275 if (mask_size > 1) {
6276 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6277 } else {
6278 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6282 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6283 * Calculate the depth as dst.x / dst.y */
6284 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6286 struct glsl_dst_param dst_param;
6288 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6290 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6291 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6292 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6293 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6294 * >= 1.0 or < 0.0
6296 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6297 dst_param.reg_name, dst_param.reg_name);
6300 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6301 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6302 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6303 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6305 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6307 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6308 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6309 struct glsl_src_param src0_param;
6311 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6313 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6314 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6317 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6318 * Calculate the 1st of a 2-row matrix multiplication. */
6319 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6321 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6322 DWORD reg = ins->dst[0].reg.idx[0].offset;
6323 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6324 struct glsl_src_param src0_param;
6326 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6327 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6330 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6331 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6332 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6334 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6335 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6336 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6337 DWORD reg = ins->dst[0].reg.idx[0].offset;
6338 struct glsl_src_param src0_param;
6340 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6341 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6342 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6345 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6347 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6348 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6349 struct glsl_sample_function sample_function;
6350 DWORD reg = ins->dst[0].reg.idx[0].offset;
6351 struct glsl_src_param src0_param;
6353 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6354 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6356 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6358 /* Sample the texture using the calculated coordinates */
6359 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6360 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6363 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6364 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6365 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6367 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6368 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6369 struct glsl_sample_function sample_function;
6370 DWORD reg = ins->dst[0].reg.idx[0].offset;
6371 struct glsl_src_param src0_param;
6373 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6374 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6376 /* Dependent read, not valid with conditional NP2 */
6377 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6379 /* Sample the texture using the calculated coordinates */
6380 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6381 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6383 tex_mx->current_row = 0;
6386 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6387 * Perform the 3rd row of a 3x3 matrix multiply */
6388 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6390 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6391 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6392 DWORD reg = ins->dst[0].reg.idx[0].offset;
6393 struct glsl_src_param src0_param;
6394 char dst_mask[6];
6396 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6398 shader_glsl_append_dst(ins->ctx->buffer, ins);
6399 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6400 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6402 tex_mx->current_row = 0;
6405 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6406 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6407 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6409 struct glsl_src_param src0_param;
6410 struct glsl_src_param src1_param;
6411 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6412 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6413 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6414 struct glsl_sample_function sample_function;
6415 DWORD reg = ins->dst[0].reg.idx[0].offset;
6416 char coord_mask[6];
6418 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6419 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6421 /* Perform the last matrix multiply operation */
6422 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6423 /* Reflection calculation */
6424 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6426 /* Dependent read, not valid with conditional NP2 */
6427 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6428 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6430 /* Sample the texture */
6431 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6432 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6433 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6435 tex_mx->current_row = 0;
6438 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6439 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6440 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6442 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6443 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6444 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6445 struct glsl_sample_function sample_function;
6446 DWORD reg = ins->dst[0].reg.idx[0].offset;
6447 struct glsl_src_param src0_param;
6448 char coord_mask[6];
6450 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6452 /* Perform the last matrix multiply operation */
6453 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6455 /* Construct the eye-ray vector from w coordinates */
6456 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6457 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6458 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6460 /* Dependent read, not valid with conditional NP2 */
6461 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6462 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6464 /* Sample the texture using the calculated coordinates */
6465 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6466 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6467 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6469 tex_mx->current_row = 0;
6472 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6473 * Apply a fake bump map transform.
6474 * texbem is pshader <= 1.3 only, this saves a few version checks
6476 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6478 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6479 struct glsl_sample_function sample_function;
6480 struct glsl_src_param coord_param;
6481 DWORD sampler_idx;
6482 DWORD mask;
6483 DWORD flags;
6484 char coord_mask[6];
6486 sampler_idx = ins->dst[0].reg.idx[0].offset;
6487 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6488 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6490 /* Dependent read, not valid with conditional NP2 */
6491 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6492 mask = sample_function.coord_mask;
6494 shader_glsl_write_mask_to_str(mask, coord_mask);
6496 /* With projected textures, texbem only divides the static texture coord,
6497 * not the displacement, so we can't let GL handle this. */
6498 if (flags & WINED3D_PSARGS_PROJECTED)
6500 DWORD div_mask=0;
6501 char coord_div_mask[3];
6502 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6504 case WINED3D_TTFF_COUNT1:
6505 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6506 break;
6507 case WINED3D_TTFF_COUNT2:
6508 div_mask = WINED3DSP_WRITEMASK_1;
6509 break;
6510 case WINED3D_TTFF_COUNT3:
6511 div_mask = WINED3DSP_WRITEMASK_2;
6512 break;
6513 case WINED3D_TTFF_COUNT4:
6514 case WINED3D_TTFF_DISABLE:
6515 div_mask = WINED3DSP_WRITEMASK_3;
6516 break;
6518 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6519 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6522 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6524 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6525 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6526 coord_param.param_str, coord_mask);
6528 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6530 struct glsl_src_param luminance_param;
6531 struct glsl_dst_param dst_param;
6533 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6534 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6536 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6537 dst_param.reg_name, dst_param.mask_str,
6538 luminance_param.param_str, sampler_idx, sampler_idx);
6540 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6543 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6545 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6546 struct glsl_src_param src0_param, src1_param;
6548 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6549 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6551 shader_glsl_append_dst(ins->ctx->buffer, ins);
6552 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6553 src0_param.param_str, sampler_idx, src1_param.param_str);
6556 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6557 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6558 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6560 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6561 struct glsl_sample_function sample_function;
6562 struct glsl_src_param src0_param;
6564 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6566 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6567 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6568 "%s.wx", src0_param.reg_name);
6569 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6572 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6573 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6574 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6576 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6577 struct glsl_sample_function sample_function;
6578 struct glsl_src_param src0_param;
6580 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6582 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6583 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6584 "%s.yz", src0_param.reg_name);
6585 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6588 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6589 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6590 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6592 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6593 struct glsl_sample_function sample_function;
6594 struct glsl_src_param src0_param;
6596 /* Dependent read, not valid with conditional NP2 */
6597 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6598 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6600 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6601 "%s", src0_param.param_str);
6602 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6605 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6606 * If any of the first 3 components are < 0, discard this pixel */
6607 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6609 if (ins->ctx->reg_maps->shader_version.major >= 4)
6611 shader_glsl_generate_conditional_op(ins, "discard;");
6613 else
6615 struct glsl_dst_param dst_param;
6617 /* The argument is a destination parameter, and no writemasks are allowed */
6618 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6620 /* 2.0 shaders compare all 4 components in texkill. */
6621 if (ins->ctx->reg_maps->shader_version.major >= 2)
6622 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6623 /* 1.x shaders only compare the first 3 components, probably due to
6624 * the nature of the texkill instruction as a tex* instruction, and
6625 * phase, which kills all .w components. Even if all 4 components are
6626 * defined, only the first 3 are used. */
6627 else
6628 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6632 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6633 * dst = dot2(src0, src1) + src2 */
6634 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6636 struct glsl_src_param src0_param;
6637 struct glsl_src_param src1_param;
6638 struct glsl_src_param src2_param;
6639 DWORD write_mask;
6640 unsigned int mask_size;
6642 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6643 mask_size = shader_glsl_get_write_mask_size(write_mask);
6645 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6646 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6647 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6649 if (mask_size > 1) {
6650 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6651 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6652 } else {
6653 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6654 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6658 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6659 const struct wined3d_shader_signature *input_signature,
6660 const struct wined3d_shader_reg_maps *reg_maps,
6661 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6663 unsigned int i;
6665 for (i = 0; i < input_signature->element_count; ++i)
6667 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6668 const char *semantic_name;
6669 UINT semantic_idx;
6670 char reg_mask[6];
6672 /* Unused */
6673 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6674 continue;
6676 semantic_name = input->semantic_name;
6677 semantic_idx = input->semantic_idx;
6678 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6680 if (args->vp_mode == vertexshader)
6682 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6684 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6685 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6687 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6689 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6691 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6693 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6694 input->register_idx, reg_mask);
6696 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6698 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6699 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6700 input->register_idx, reg_mask);
6701 else
6702 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6704 else
6706 if (input->sysval_semantic)
6707 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6708 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6709 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6710 shader_glsl_shader_input_name(gl_info),
6711 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6714 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6716 if (args->pointsprite)
6717 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6718 shader->u.ps.input_reg_map[input->register_idx]);
6719 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6720 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6721 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6722 needs_legacy_glsl_syntax(gl_info)
6723 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6724 else
6725 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6726 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6728 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6730 if (!semantic_idx)
6731 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6732 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6733 else if (semantic_idx == 1)
6734 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6735 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6736 else
6737 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6738 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6740 else
6742 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6743 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6748 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6750 struct glsl_program_key key;
6752 key.vs_id = entry->vs.id;
6753 key.hs_id = entry->hs.id;
6754 key.ds_id = entry->ds.id;
6755 key.gs_id = entry->gs.id;
6756 key.ps_id = entry->ps.id;
6757 key.cs_id = entry->cs.id;
6759 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6761 ERR("Failed to insert program entry.\n");
6765 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6766 const struct glsl_program_key *key)
6768 struct wine_rb_entry *entry;
6770 entry = wine_rb_get(&priv->program_lookup, key);
6771 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6774 /* Context activation is done by the caller. */
6775 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6776 struct glsl_shader_prog_link *entry)
6778 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6780 GL_EXTCALL(glDeleteProgram(entry->id));
6781 if (entry->vs.id)
6782 list_remove(&entry->vs.shader_entry);
6783 if (entry->hs.id)
6784 list_remove(&entry->hs.shader_entry);
6785 if (entry->ds.id)
6786 list_remove(&entry->ds.shader_entry);
6787 if (entry->gs.id)
6788 list_remove(&entry->gs.shader_entry);
6789 if (entry->ps.id)
6790 list_remove(&entry->ps.shader_entry);
6791 if (entry->cs.id)
6792 list_remove(&entry->cs.shader_entry);
6793 HeapFree(GetProcessHeap(), 0, entry);
6796 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6797 const struct wined3d_gl_info *gl_info, const DWORD *map,
6798 const struct wined3d_shader_signature *input_signature,
6799 const struct wined3d_shader_reg_maps *reg_maps_in,
6800 const struct wined3d_shader_signature *output_signature,
6801 const struct wined3d_shader_reg_maps *reg_maps_out)
6803 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6804 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6805 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6806 unsigned int in_count = vec4_varyings(3, gl_info);
6807 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6808 DWORD in_idx, *set = NULL;
6809 unsigned int i, j;
6810 char reg_mask[6];
6812 set = wined3d_calloc(max_varyings, sizeof(*set));
6814 for (i = 0; i < input_signature->element_count; ++i)
6816 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6818 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6819 continue;
6821 in_idx = map[input->register_idx];
6822 /* Declared, but not read register */
6823 if (in_idx == ~0u)
6824 continue;
6825 if (in_idx >= max_varyings)
6827 FIXME("More input varyings declared than supported, expect issues.\n");
6828 continue;
6831 if (in_idx == in_count)
6832 string_buffer_sprintf(destination, "gl_FrontColor");
6833 else if (in_idx == in_count + 1)
6834 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6835 else
6836 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6838 if (!set[in_idx])
6839 set[in_idx] = ~0u;
6841 for (j = 0; j < output_signature->element_count; ++j)
6843 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6844 DWORD mask;
6846 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6847 || input->semantic_idx != output->semantic_idx
6848 || strcmp(input->semantic_name, output->semantic_name)
6849 || !(mask = input->mask & output->mask))
6850 continue;
6852 if (set[in_idx] == ~0u)
6853 set[in_idx] = 0;
6854 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6855 shader_glsl_write_mask_to_str(mask, reg_mask);
6857 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6858 destination->buffer, reg_mask, output->register_idx, reg_mask);
6862 for (i = 0; i < max_varyings; ++i)
6864 unsigned int size;
6866 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6867 continue;
6869 if (set[i] == ~0u)
6870 set[i] = 0;
6872 size = 0;
6873 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6874 reg_mask[size++] = 'x';
6875 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6876 reg_mask[size++] = 'y';
6877 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6878 reg_mask[size++] = 'z';
6879 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6880 reg_mask[size++] = 'w';
6881 reg_mask[size] = '\0';
6883 if (i == in_count)
6884 string_buffer_sprintf(destination, "gl_FrontColor");
6885 else if (i == in_count + 1)
6886 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6887 else
6888 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6890 if (size == 1)
6891 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6892 else
6893 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6896 HeapFree(GetProcessHeap(), 0, set);
6897 string_buffer_release(&priv->string_buffers, destination);
6900 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6901 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6902 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6903 BOOL rasterizer_setup)
6905 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6906 char reg_mask[6];
6907 unsigned int i;
6909 for (i = 0; i < output_signature->element_count; ++i)
6911 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6913 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6914 continue;
6916 if (output->stream_idx)
6917 continue;
6919 if (output->register_idx >= input_count)
6920 continue;
6922 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6924 shader_addline(buffer,
6925 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
6926 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
6930 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6931 const struct wined3d_gl_info *gl_info, const DWORD *map,
6932 const struct wined3d_shader_signature *input_signature,
6933 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6934 const struct wined3d_shader_signature *output_signature,
6935 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6937 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6938 const char *semantic_name;
6939 UINT semantic_idx;
6940 char reg_mask[6];
6941 unsigned int i;
6943 /* First, sort out position and point size system values. */
6944 for (i = 0; i < output_signature->element_count; ++i)
6946 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6948 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6949 continue;
6951 if (output->stream_idx)
6952 continue;
6954 semantic_name = output->semantic_name;
6955 semantic_idx = output->semantic_idx;
6956 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6958 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6960 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
6961 reg_mask, output->register_idx, reg_mask);
6963 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6965 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
6966 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6968 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6970 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
6971 output->register_idx, reg_mask);
6973 else if (output->sysval_semantic)
6975 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6979 /* Then, setup the pixel shader input. */
6980 if (reg_maps_out->shader_version.major < 4)
6981 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6982 output_signature, reg_maps_out);
6983 else
6984 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
6987 /* Context activation is done by the caller. */
6988 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6989 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6990 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6992 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
6993 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6994 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6995 const char *semantic_name;
6996 UINT semantic_idx;
6997 char reg_mask[6];
6998 unsigned int i;
6999 GLuint ret;
7001 string_buffer_clear(buffer);
7003 shader_glsl_add_version_declaration(buffer, gl_info);
7005 if (per_vertex_point_size)
7007 shader_addline(buffer, "uniform struct\n{\n");
7008 shader_addline(buffer, " float size_min;\n");
7009 shader_addline(buffer, " float size_max;\n");
7010 shader_addline(buffer, "} ffp_point;\n");
7013 if (ps_major < 3)
7015 DWORD colors_written_mask[2] = {0};
7016 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7018 if (!legacy_syntax)
7020 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7021 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7022 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7023 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7026 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7028 for (i = 0; i < vs->output_signature.element_count; ++i)
7030 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7031 DWORD write_mask;
7033 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7034 continue;
7036 semantic_name = output->semantic_name;
7037 semantic_idx = output->semantic_idx;
7038 write_mask = output->mask;
7039 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7041 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7043 if (legacy_syntax)
7044 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7045 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7046 else
7047 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7048 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7050 colors_written_mask[semantic_idx] = write_mask;
7052 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7054 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7055 reg_mask, output->register_idx, reg_mask);
7057 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7059 if (semantic_idx < MAX_TEXTURES)
7061 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7062 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7063 semantic_idx, reg_mask, output->register_idx, reg_mask);
7064 texcoords_written_mask[semantic_idx] = write_mask;
7067 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7069 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7070 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7072 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7074 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7075 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7076 output->register_idx, reg_mask[1]);
7080 for (i = 0; i < 2; ++i)
7082 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7084 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7085 if (!i)
7086 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7087 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7088 reg_mask, reg_mask);
7089 else
7090 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7091 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7092 reg_mask, reg_mask);
7095 for (i = 0; i < MAX_TEXTURES; ++i)
7097 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7098 continue;
7100 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7102 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7103 && !texcoords_written_mask[i])
7104 continue;
7106 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7107 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7108 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7112 else
7114 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7116 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7117 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7118 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7119 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7122 shader_addline(buffer, "}\n");
7124 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7125 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7126 shader_glsl_compile(gl_info, ret, buffer->buffer);
7128 return ret;
7131 static void shader_glsl_generate_stream_output_setup(struct shader_glsl_priv *priv,
7132 const struct wined3d_shader *shader, const struct wined3d_stream_output_desc *so_desc)
7134 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7135 unsigned int i;
7137 shader_addline(buffer, "out shader_in_out\n{\n");
7138 for (i = 0; i < so_desc->element_count; ++i)
7140 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7142 if (e->stream_idx)
7144 FIXME("Unhandled stream %u.\n", e->stream_idx);
7145 continue;
7147 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7148 continue;
7150 if (e->component_idx || e->component_count != 4)
7152 if (e->component_count == 1)
7153 shader_addline(buffer, "float");
7154 else
7155 shader_addline(buffer, "vec%u", e->component_count);
7156 shader_addline(buffer, " reg%u_%u_%u;\n",
7157 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
7159 else
7161 shader_addline(buffer, "vec4 reg%u;\n", e->register_idx);
7164 shader_addline(buffer, "} shader_out;\n");
7166 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7167 shader->limits->packed_output);
7168 for (i = 0; i < so_desc->element_count; ++i)
7170 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7172 if (e->stream_idx)
7174 FIXME("Unhandled stream %u.\n", e->stream_idx);
7175 continue;
7177 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7178 continue;
7180 if (e->component_idx || e->component_count != 4)
7182 DWORD write_mask;
7183 char str_mask[6];
7185 write_mask = ((1u << e->component_count) - 1) << e->component_idx;
7186 shader_glsl_write_mask_to_str(write_mask, str_mask);
7187 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7188 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1,
7189 e->register_idx, str_mask);
7191 else
7193 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n",
7194 e->register_idx, e->register_idx);
7197 shader_addline(buffer, "}\n");
7200 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7201 const struct wined3d_shader *shader, unsigned int input_count,
7202 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7204 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7205 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7207 if (rasterizer_setup)
7208 input_count = min(vec4_varyings(4, gl_info), input_count);
7210 if (input_count)
7211 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7213 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7214 prefix, shader->limits->packed_output);
7216 if (rasterizer_setup)
7217 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7218 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7219 else
7220 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7221 &shader->reg_maps, "shader_out", rasterizer_setup);
7223 shader_addline(buffer, "}\n");
7226 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7227 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7228 const char *reg_mask)
7230 if (!constant->sysval_semantic)
7232 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7233 return;
7236 switch (constant->sysval_semantic)
7238 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7239 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7240 case WINED3D_SV_TESS_FACTOR_LINEDET:
7241 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7242 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7243 break;
7244 case WINED3D_SV_TESS_FACTOR_QUADINT:
7245 case WINED3D_SV_TESS_FACTOR_TRIINT:
7246 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7247 break;
7248 default:
7249 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7250 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7254 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7255 const struct wined3d_shader_signature *signature, BOOL input_setup)
7257 unsigned int i, register_count, user_constant_index, user_constant_count;
7259 register_count = user_constant_count = 0;
7260 for (i = 0; i < signature->element_count; ++i)
7262 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7263 register_count = max(constant->register_idx + 1, register_count);
7264 if (!constant->sysval_semantic)
7265 ++user_constant_count;
7268 if (user_constant_count)
7269 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7270 input_setup ? "in" : "out", user_constant_count);
7271 if (input_setup)
7272 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7274 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7275 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7277 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7278 char reg_mask[6];
7280 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7282 if (input_setup)
7283 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7284 else
7285 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7287 shader_addline(buffer, " = ");
7289 if (input_setup)
7290 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7291 else
7292 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7294 shader_addline(buffer, ";\n");
7296 shader_addline(buffer, "}\n");
7299 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7300 const struct wined3d_gl_info *gl_info)
7302 const char *output = get_fragment_output(gl_info);
7304 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7305 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7306 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7307 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7308 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7309 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7312 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7313 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7315 const char *output = get_fragment_output(gl_info);
7317 switch (mode)
7319 case WINED3D_FFP_PS_FOG_OFF:
7320 return;
7322 case WINED3D_FFP_PS_FOG_LINEAR:
7323 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7324 break;
7326 case WINED3D_FFP_PS_FOG_EXP:
7327 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7328 break;
7330 case WINED3D_FFP_PS_FOG_EXP2:
7331 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7332 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7333 break;
7335 default:
7336 ERR("Invalid fog mode %#x.\n", mode);
7337 return;
7340 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7341 output, output);
7344 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7345 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7347 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7348 * flipping all the operators here, just negate the comparison below. */
7349 static const char * const comparison_operator[] =
7351 "", /* WINED3D_CMP_NEVER */
7352 "<", /* WINED3D_CMP_LESS */
7353 "==", /* WINED3D_CMP_EQUAL */
7354 "<=", /* WINED3D_CMP_LESSEQUAL */
7355 ">", /* WINED3D_CMP_GREATER */
7356 "!=", /* WINED3D_CMP_NOTEQUAL */
7357 ">=", /* WINED3D_CMP_GREATEREQUAL */
7358 "" /* WINED3D_CMP_ALWAYS */
7361 if (alpha_func == WINED3D_CMP_ALWAYS)
7362 return;
7364 if (alpha_func != WINED3D_CMP_NEVER)
7365 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7366 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7367 shader_addline(buffer, " discard;\n");
7370 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7371 const struct wined3d_gl_info *gl_info)
7373 if (gl_info->supported[ARB_GPU_SHADER5])
7374 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7375 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7376 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7377 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7378 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7379 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7380 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7381 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7382 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7383 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7384 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7385 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7386 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7387 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7388 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7389 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7390 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7391 if (gl_info->supported[ARB_TEXTURE_GATHER])
7392 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7393 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7394 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7395 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7396 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7397 if (gl_info->supported[EXT_GPU_SHADER4])
7398 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7399 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7400 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7403 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7404 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7405 const struct ps_compile_args *args)
7407 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7409 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7410 if (reg_maps->shader_version.major < 2)
7411 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7413 if (args->srgb_correction)
7414 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7416 /* SM < 3 does not replace the fog stage. */
7417 if (reg_maps->shader_version.major < 3)
7418 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7420 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7423 /* Context activation is done by the caller. */
7424 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7425 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7426 const struct wined3d_shader *shader,
7427 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7429 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7430 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7431 const char *prefix = shader_glsl_get_prefix(version->type);
7432 const struct wined3d_gl_info *gl_info = context->gl_info;
7433 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7434 unsigned int i, extra_constants_needed = 0;
7435 struct shader_glsl_ctx_priv priv_ctx;
7436 GLuint shader_id;
7437 DWORD map;
7439 memset(&priv_ctx, 0, sizeof(priv_ctx));
7440 priv_ctx.cur_ps_args = args;
7441 priv_ctx.cur_np2fixup_info = np2fixup_info;
7442 priv_ctx.string_buffers = string_buffers;
7444 shader_glsl_add_version_declaration(buffer, gl_info);
7446 shader_glsl_enable_extensions(buffer, gl_info);
7447 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7448 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7449 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7450 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7451 if (shader_glsl_use_explicit_attrib_location(gl_info))
7452 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7453 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7454 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7455 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7456 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7457 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7458 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7459 /* The spec says that it doesn't have to be explicitly enabled, but the
7460 * nvidia drivers write a warning if we don't do so. */
7461 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7462 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7464 /* Base Declarations */
7465 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7467 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7469 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7470 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7471 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7472 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7475 /* Declare uniforms for NP2 texcoord fixup:
7476 * This is NOT done inside the loop that declares the texture samplers
7477 * since the NP2 fixup code is currently only used for the GeforceFX
7478 * series and when forcing the ARB_npot extension off. Modern cards just
7479 * skip the code anyway, so put it inside a separate loop. */
7480 if (args->np2_fixup)
7482 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7483 unsigned int cur = 0;
7485 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7486 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7487 * samplerNP2Fixup stores texture dimensions and is updated through
7488 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7490 for (i = 0; i < shader->limits->sampler; ++i)
7492 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7493 continue;
7495 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7497 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7498 continue;
7501 fixup->idx[i] = cur++;
7504 fixup->num_consts = (cur + 1) >> 1;
7505 fixup->active = args->np2_fixup;
7506 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7509 if (version->major < 3 || args->vp_mode != vertexshader)
7511 shader_addline(buffer, "uniform struct\n{\n");
7512 shader_addline(buffer, " vec4 color;\n");
7513 shader_addline(buffer, " float density;\n");
7514 shader_addline(buffer, " float end;\n");
7515 shader_addline(buffer, " float scale;\n");
7516 shader_addline(buffer, "} ffp_fog;\n");
7518 if (needs_legacy_glsl_syntax(gl_info))
7520 if (glsl_is_color_reg_read(shader, 0))
7521 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7522 if (glsl_is_color_reg_read(shader, 1))
7523 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7524 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7525 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7527 else
7529 if (glsl_is_color_reg_read(shader, 0))
7530 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7531 if (glsl_is_color_reg_read(shader, 1))
7532 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7533 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7534 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7535 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7539 if (version->major >= 3)
7541 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7543 if (args->vp_mode == vertexshader && reg_maps->input_registers)
7544 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7545 shader->u.ps.interpolation_mode, version->major >= 4);
7546 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7549 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7551 if (!(map & 1))
7552 continue;
7554 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7556 if (reg_maps->luminanceparams & (1u << i))
7558 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7559 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7560 extra_constants_needed++;
7563 extra_constants_needed++;
7566 if (args->srgb_correction)
7568 shader_addline(buffer, "const vec4 srgb_const0 = ");
7569 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7570 shader_addline(buffer, ";\n");
7571 shader_addline(buffer, "const vec4 srgb_const1 = ");
7572 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7573 shader_addline(buffer, ";\n");
7575 if (reg_maps->vpos || reg_maps->usesdsy)
7577 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7579 ++extra_constants_needed;
7580 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7582 if (reg_maps->vpos)
7584 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7586 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7587 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7588 args->render_offscreen ? "" : "origin_upper_left, ");
7589 else if (!args->render_offscreen)
7590 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7592 shader_addline(buffer, "vec4 vpos;\n");
7596 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7597 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7599 if (!needs_legacy_glsl_syntax(gl_info))
7601 if (shader_glsl_use_explicit_attrib_location(gl_info))
7602 shader_addline(buffer, "layout(location = 0) ");
7603 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
7606 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7607 FIXME("Insufficient uniforms to run this shader.\n");
7609 if (shader->u.ps.force_early_depth_stencil)
7610 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7612 shader_addline(buffer, "void main()\n{\n");
7614 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7615 * add approximately 0.5. This causes off-by-one problems as spotted by
7616 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7617 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7618 * causes precision troubles when we just subtract 0.5.
7620 * To deal with that, just floor() the position. This will eliminate the
7621 * fraction on all cards.
7623 * TODO: Test how this behaves with multisampling.
7625 * An advantage of floor is that it works even if the driver doesn't add
7626 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7627 * to return in gl_FragCoord, even though coordinates specify the pixel
7628 * centers instead of the pixel corners. This code will behave correctly
7629 * on drivers that returns integer values. */
7630 if (reg_maps->vpos)
7632 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7633 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7634 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7635 shader_addline(buffer,
7636 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7637 else
7638 shader_addline(buffer,
7639 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7642 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
7644 unsigned int i;
7645 WORD map = reg_maps->texcoord;
7647 if (legacy_syntax)
7649 if (glsl_is_color_reg_read(shader, 0))
7650 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7651 if (glsl_is_color_reg_read(shader, 1))
7652 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7655 for (i = 0; map; map >>= 1, ++i)
7657 if (map & 1)
7659 if (args->pointsprite)
7660 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7661 else if (args->texcoords_initialized & (1u << i))
7662 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7663 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7664 else
7665 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7666 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7670 if (legacy_syntax)
7671 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7674 /* Pack 3.0 inputs */
7675 if (reg_maps->shader_version.major >= 3)
7676 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7677 reg_maps->shader_version.major >= 4);
7679 /* Base Shader Body */
7680 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7681 return 0;
7683 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7684 if (reg_maps->shader_version.major < 4)
7685 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
7687 shader_addline(buffer, "}\n");
7689 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7690 TRACE("Compiling shader object %u.\n", shader_id);
7691 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7693 return shader_id;
7696 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7697 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7698 const struct vs_compile_args *args)
7700 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7701 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7702 unsigned int i;
7704 /* Unpack outputs. */
7705 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7707 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7708 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7709 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7710 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7712 if (reg_maps->shader_version.major < 3)
7714 if (args->fog_src == VS_FOG_Z)
7715 shader_addline(buffer, "%s = gl_Position.z;\n",
7716 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7717 else if (!reg_maps->fog)
7718 shader_addline(buffer, "%s = 0.0;\n",
7719 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7722 /* We always store the clipplanes without y inversion. */
7723 if (args->clip_enabled)
7725 if (legacy_syntax)
7726 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7727 else
7728 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7729 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7732 if (args->point_size && !args->per_vertex_point_size)
7733 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7735 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7736 shader_glsl_fixup_position(buffer);
7739 /* Context activation is done by the caller. */
7740 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
7741 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7743 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7744 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7745 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7746 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7747 const struct wined3d_gl_info *gl_info = context->gl_info;
7748 struct shader_glsl_ctx_priv priv_ctx;
7749 GLuint shader_id;
7750 unsigned int i;
7752 memset(&priv_ctx, 0, sizeof(priv_ctx));
7753 priv_ctx.cur_vs_args = args;
7754 priv_ctx.string_buffers = string_buffers;
7756 shader_glsl_add_version_declaration(buffer, gl_info);
7758 shader_glsl_enable_extensions(buffer, gl_info);
7759 if (gl_info->supported[ARB_DRAW_INSTANCED])
7760 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7761 if (shader_glsl_use_explicit_attrib_location(gl_info))
7762 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7764 /* Base Declarations */
7765 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7767 for (i = 0; i < shader->input_signature.element_count; ++i)
7768 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7770 if (args->point_size && !args->per_vertex_point_size)
7772 shader_addline(buffer, "uniform struct\n{\n");
7773 shader_addline(buffer, " float size;\n");
7774 shader_addline(buffer, " float size_min;\n");
7775 shader_addline(buffer, " float size_max;\n");
7776 shader_addline(buffer, "} ffp_point;\n");
7779 if (!needs_legacy_glsl_syntax(gl_info))
7781 if (args->clip_enabled)
7782 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7784 if (version->major < 3)
7786 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7787 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7788 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7789 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7793 if (version->major < 4)
7794 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
7796 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7797 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7799 if (reg_maps->shader_version.major >= 4)
7800 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
7801 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
7803 shader_addline(buffer, "void main()\n{\n");
7805 /* Base Shader Body */
7806 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7807 return 0;
7809 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7810 if (reg_maps->shader_version.major < 4)
7811 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7813 shader_addline(buffer, "}\n");
7815 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7816 TRACE("Compiling shader object %u.\n", shader_id);
7817 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7819 return shader_id;
7822 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
7823 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
7825 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7826 char reg_mask[6];
7827 unsigned int i;
7829 for (i = 0; i < output_signature->element_count; ++i)
7831 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7833 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7834 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
7835 output->register_idx, reg_mask, output->register_idx, reg_mask);
7839 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
7840 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
7841 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
7842 const char *phase_name, unsigned phase_idx)
7844 unsigned int i;
7845 HRESULT hr;
7847 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
7848 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
7849 for (i = 0; i < phase->temporary_count; ++i)
7850 shader_addline(buffer, "vec4 R%u;\n", i);
7851 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
7852 shader_addline(buffer, "}\n");
7853 return hr;
7856 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
7857 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
7859 if (phase->instance_count)
7861 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
7862 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
7863 shader_addline(buffer, "}\n");
7865 else
7867 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
7871 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
7872 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
7874 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7875 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7876 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7877 const struct wined3d_gl_info *gl_info = context->gl_info;
7878 const struct wined3d_hull_shader *hs = &shader->u.hs;
7879 const struct wined3d_shader_phase *phase;
7880 struct shader_glsl_ctx_priv priv_ctx;
7881 GLuint shader_id;
7882 unsigned int i;
7884 memset(&priv_ctx, 0, sizeof(priv_ctx));
7885 priv_ctx.string_buffers = string_buffers;
7887 shader_glsl_add_version_declaration(buffer, gl_info);
7889 shader_glsl_enable_extensions(buffer, gl_info);
7890 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7892 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7894 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
7896 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7897 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
7899 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
7901 if (hs->phases.control_point)
7903 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
7904 shader->limits->packed_output);
7905 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
7906 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
7907 shader_addline(buffer, "}\n");
7910 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
7911 if ((phase = hs->phases.control_point))
7913 for (i = 0; i < phase->temporary_count; ++i)
7914 shader_addline(buffer, "vec4 R%u;\n", i);
7915 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
7916 return 0;
7917 shader_addline(buffer, "setup_hs_output(hs_out);\n");
7919 else
7921 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
7923 shader_addline(buffer, "}\n");
7925 for (i = 0; i < hs->phases.fork_count; ++i)
7927 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7928 &hs->phases.fork[i], "fork", i)))
7929 return 0;
7932 for (i = 0; i < hs->phases.join_count; ++i)
7934 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7935 &hs->phases.join[i], "join", i)))
7936 return 0;
7939 shader_addline(buffer, "void main()\n{\n");
7940 shader_addline(buffer, "hs_control_point_phase();\n");
7941 if (reg_maps->vocp)
7942 shader_addline(buffer, "barrier();\n");
7943 for (i = 0; i < hs->phases.fork_count; ++i)
7944 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
7945 for (i = 0; i < hs->phases.join_count; ++i)
7946 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
7947 shader_addline(buffer, "setup_patch_constant_output();\n");
7948 shader_addline(buffer, "}\n");
7950 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
7951 TRACE("Compiling shader object %u.\n", shader_id);
7952 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7954 return shader_id;
7957 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
7958 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7959 const struct ds_compile_args *args)
7961 shader_addline(buffer, "setup_ds_output(ds_out);\n");
7963 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7964 shader_glsl_fixup_position(buffer);
7967 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
7968 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
7970 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7971 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7972 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7973 const struct wined3d_gl_info *gl_info = context->gl_info;
7974 struct shader_glsl_ctx_priv priv_ctx;
7975 GLuint shader_id;
7977 memset(&priv_ctx, 0, sizeof(priv_ctx));
7978 priv_ctx.cur_ds_args = args;
7979 priv_ctx.string_buffers = string_buffers;
7981 shader_glsl_add_version_declaration(buffer, gl_info);
7983 shader_glsl_enable_extensions(buffer, gl_info);
7984 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7986 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7988 shader_addline(buffer, "layout(");
7989 switch (shader->u.ds.tessellator_domain)
7991 case WINED3D_TESSELLATOR_DOMAIN_LINE:
7992 shader_addline(buffer, "isolines");
7993 break;
7994 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
7995 shader_addline(buffer, "quads");
7996 break;
7997 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
7998 shader_addline(buffer, "triangles");
7999 break;
8001 switch (args->tessellator_output_primitive)
8003 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8004 if (args->render_offscreen)
8005 shader_addline(buffer, ", ccw");
8006 else
8007 shader_addline(buffer, ", cw");
8008 break;
8009 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8010 if (args->render_offscreen)
8011 shader_addline(buffer, ", cw");
8012 else
8013 shader_addline(buffer, ", ccw");
8014 break;
8015 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8016 shader_addline(buffer, ", point_mode");
8017 break;
8018 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8019 break;
8021 switch (args->tessellator_partitioning)
8023 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8024 shader_addline(buffer, ", fractional_odd_spacing");
8025 break;
8026 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8027 shader_addline(buffer, ", fractional_even_spacing");
8028 break;
8029 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8030 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8031 shader_addline(buffer, ", equal_spacing");
8032 break;
8034 shader_addline(buffer, ") in;\n");
8036 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8038 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8039 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8041 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8042 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8043 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8045 shader_addline(buffer, "void main()\n{\n");
8046 shader_addline(buffer, "setup_patch_constant_input();\n");
8048 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8049 return 0;
8051 shader_addline(buffer, "}\n");
8053 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8054 TRACE("Compiling shader object %u.\n", shader_id);
8055 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8057 return shader_id;
8060 /* Context activation is done by the caller. */
8061 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
8062 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8064 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8065 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8066 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8067 const struct wined3d_gl_info *gl_info = context->gl_info;
8068 struct shader_glsl_ctx_priv priv_ctx;
8069 GLuint shader_id;
8071 memset(&priv_ctx, 0, sizeof(priv_ctx));
8072 priv_ctx.string_buffers = string_buffers;
8074 shader_glsl_add_version_declaration(buffer, gl_info);
8076 shader_glsl_enable_extensions(buffer, gl_info);
8078 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8080 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
8081 if (shader->u.gs.instance_count > 1)
8082 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8083 shader_addline(buffer, ") in;\n");
8084 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8085 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
8086 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8088 if (!gl_info->supported[ARB_CLIP_CONTROL])
8089 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8091 if (is_rasterization_disabled(shader))
8093 shader_glsl_generate_stream_output_setup(priv, shader, &shader->u.gs.so_desc);
8095 else
8097 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8098 gl_info, TRUE, args->interpolation_mode);
8100 shader_addline(buffer, "void main()\n{\n");
8101 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8102 return 0;
8103 shader_addline(buffer, "}\n");
8105 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8106 TRACE("Compiling shader object %u.\n", shader_id);
8107 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8109 return shader_id;
8112 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8114 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8115 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8116 struct wined3d_string_buffer *buffer = ctx->buffer;
8117 const struct wined3d_shader *shader = ctx->shader;
8119 switch (shader->reg_maps.shader_version.type)
8121 case WINED3D_SHADER_TYPE_PIXEL:
8122 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args);
8123 break;
8124 case WINED3D_SHADER_TYPE_VERTEX:
8125 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8126 break;
8127 case WINED3D_SHADER_TYPE_DOMAIN:
8128 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8129 break;
8130 case WINED3D_SHADER_TYPE_GEOMETRY:
8131 case WINED3D_SHADER_TYPE_COMPUTE:
8132 break;
8133 default:
8134 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8135 break;
8139 /* Context activation is done by the caller. */
8140 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8141 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8142 const struct wined3d_shader *shader)
8144 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8145 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8146 const struct wined3d_gl_info *gl_info = context->gl_info;
8147 struct shader_glsl_ctx_priv priv_ctx;
8148 GLuint shader_id;
8149 unsigned int i;
8151 memset(&priv_ctx, 0, sizeof(priv_ctx));
8152 priv_ctx.string_buffers = string_buffers;
8154 shader_glsl_add_version_declaration(buffer, gl_info);
8156 shader_glsl_enable_extensions(buffer, gl_info);
8157 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8159 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8161 for (i = 0; i < reg_maps->tgsm_count; ++i)
8163 if (reg_maps->tgsm[i].size)
8164 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8167 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8168 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8170 shader_addline(buffer, "void main()\n{\n");
8171 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8172 shader_addline(buffer, "}\n");
8174 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8175 TRACE("Compiling shader object %u.\n", shader_id);
8176 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8178 return shader_id;
8181 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8182 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8183 struct wined3d_shader *shader,
8184 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8186 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8187 struct glsl_shader_private *shader_data;
8188 struct ps_np2fixup_info *np2fixup;
8189 UINT i;
8190 DWORD new_size;
8191 GLuint ret;
8193 if (!shader->backend_data)
8195 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8196 if (!shader->backend_data)
8198 ERR("Failed to allocate backend data.\n");
8199 return 0;
8202 shader_data = shader->backend_data;
8203 gl_shaders = shader_data->gl_shaders.ps;
8205 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8206 * so a linear search is more performant than a hashmap or a binary search
8207 * (cache coherency etc)
8209 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8211 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8213 if (args->np2_fixup)
8214 *np2fixup_info = &gl_shaders[i].np2fixup;
8215 return gl_shaders[i].id;
8219 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8220 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8221 if (shader_data->num_gl_shaders)
8223 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8224 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
8225 new_size * sizeof(*gl_shaders));
8227 else
8229 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8230 new_size = 1;
8233 if(!new_array) {
8234 ERR("Out of memory\n");
8235 return 0;
8237 shader_data->gl_shaders.ps = new_array;
8238 shader_data->shader_array_size = new_size;
8239 gl_shaders = new_array;
8242 gl_shaders[shader_data->num_gl_shaders].args = *args;
8244 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8245 memset(np2fixup, 0, sizeof(*np2fixup));
8246 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8248 pixelshader_update_resource_types(shader, args->tex_types);
8250 string_buffer_clear(buffer);
8251 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8252 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8254 return ret;
8257 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8258 const DWORD use_map)
8260 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
8261 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
8262 if (stored->point_size != new->point_size)
8263 return FALSE;
8264 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8265 return FALSE;
8266 if (stored->flatshading != new->flatshading)
8267 return FALSE;
8268 if (stored->next_shader_type != new->next_shader_type)
8269 return FALSE;
8270 if (stored->next_shader_input_count != new->next_shader_input_count)
8271 return FALSE;
8272 return stored->fog_src == new->fog_src;
8275 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8276 struct wined3d_shader *shader, const struct vs_compile_args *args)
8278 UINT i;
8279 DWORD new_size;
8280 DWORD use_map = context->stream_info.use_map;
8281 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8282 struct glsl_shader_private *shader_data;
8283 GLuint ret;
8285 if (!shader->backend_data)
8287 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8288 if (!shader->backend_data)
8290 ERR("Failed to allocate backend data.\n");
8291 return 0;
8294 shader_data = shader->backend_data;
8295 gl_shaders = shader_data->gl_shaders.vs;
8297 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8298 * so a linear search is more performant than a hashmap or a binary search
8299 * (cache coherency etc)
8301 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8303 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8304 return gl_shaders[i].id;
8307 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8309 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8310 if (shader_data->num_gl_shaders)
8312 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8313 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
8314 new_size * sizeof(*gl_shaders));
8316 else
8318 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8319 new_size = 1;
8322 if(!new_array) {
8323 ERR("Out of memory\n");
8324 return 0;
8326 shader_data->gl_shaders.vs = new_array;
8327 shader_data->shader_array_size = new_size;
8328 gl_shaders = new_array;
8331 gl_shaders[shader_data->num_gl_shaders].args = *args;
8333 string_buffer_clear(&priv->shader_buffer);
8334 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8335 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8337 return ret;
8340 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8341 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8343 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8344 struct glsl_shader_private *shader_data;
8345 unsigned int new_size;
8346 GLuint ret;
8348 if (!shader->backend_data)
8350 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8352 ERR("Failed to allocate backend data.\n");
8353 return 0;
8356 shader_data = shader->backend_data;
8357 gl_shaders = shader_data->gl_shaders.hs;
8359 if (shader_data->num_gl_shaders > 0)
8361 assert(shader_data->num_gl_shaders == 1);
8362 return gl_shaders[0].id;
8365 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8367 assert(!shader_data->gl_shaders.hs);
8368 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8369 new_size = 1;
8370 if (!new_array)
8372 ERR("Failed to allocate GL shaders array.\n");
8373 return 0;
8375 shader_data->gl_shaders.hs = new_array;
8376 shader_data->shader_array_size = new_size;
8377 gl_shaders = new_array;
8379 string_buffer_clear(&priv->shader_buffer);
8380 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8381 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8383 return ret;
8386 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8387 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8389 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8390 struct glsl_shader_private *shader_data;
8391 unsigned int i, new_size;
8392 GLuint ret;
8394 if (!shader->backend_data)
8396 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8398 ERR("Failed to allocate backend data.\n");
8399 return 0;
8402 shader_data = shader->backend_data;
8403 gl_shaders = shader_data->gl_shaders.ds;
8405 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8407 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8408 return gl_shaders[i].id;
8411 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8413 if (shader_data->num_gl_shaders)
8415 new_size = shader_data->shader_array_size + 1;
8416 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ds,
8417 new_size * sizeof(*new_array));
8419 else
8421 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8422 new_size = 1;
8425 if (!new_array)
8427 ERR("Failed to allocate GL shaders array.\n");
8428 return 0;
8430 shader_data->gl_shaders.ds = new_array;
8431 shader_data->shader_array_size = new_size;
8432 gl_shaders = new_array;
8434 string_buffer_clear(&priv->shader_buffer);
8435 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8436 gl_shaders[shader_data->num_gl_shaders].args = *args;
8437 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8439 return ret;
8442 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8443 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8445 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8446 struct glsl_shader_private *shader_data;
8447 unsigned int i, new_size;
8448 GLuint ret;
8450 if (!shader->backend_data)
8452 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8454 ERR("Failed to allocate backend data.\n");
8455 return 0;
8458 shader_data = shader->backend_data;
8459 gl_shaders = shader_data->gl_shaders.gs;
8461 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8463 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8464 return gl_shaders[i].id;
8467 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8469 if (shader_data->num_gl_shaders)
8471 new_size = shader_data->shader_array_size + 1;
8472 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
8473 new_size * sizeof(*new_array));
8475 else
8477 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8478 new_size = 1;
8481 if (!new_array)
8483 ERR("Failed to allocate GL shaders array.\n");
8484 return 0;
8486 shader_data->gl_shaders.gs = new_array;
8487 shader_data->shader_array_size = new_size;
8488 gl_shaders = new_array;
8490 string_buffer_clear(&priv->shader_buffer);
8491 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8492 gl_shaders[shader_data->num_gl_shaders].args = *args;
8493 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8495 return ret;
8498 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8500 switch (mcs)
8502 case WINED3D_MCS_MATERIAL:
8503 return material;
8504 case WINED3D_MCS_COLOR1:
8505 return "ffp_attrib_diffuse";
8506 case WINED3D_MCS_COLOR2:
8507 return "ffp_attrib_specular";
8508 default:
8509 ERR("Invalid material color source %#x.\n", mcs);
8510 return "<invalid>";
8514 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8515 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8517 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8518 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8519 if (settings->localviewer)
8520 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8521 else
8522 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8523 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8524 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8527 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8528 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8530 const char *diffuse, *specular, *emissive, *ambient;
8531 unsigned int i, idx;
8533 if (!settings->lighting)
8535 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8536 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8537 return;
8540 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8541 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8542 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8543 shader_addline(buffer, "vec3 dir, dst;\n");
8544 shader_addline(buffer, "float att, t;\n");
8546 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8547 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8548 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8549 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8551 idx = 0;
8552 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8554 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8555 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8556 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8557 shader_addline(buffer, "dst.x = 1.0;\n");
8558 if (legacy_lighting)
8560 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8561 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8562 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8564 else
8566 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8568 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8569 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8570 if (!legacy_lighting)
8571 shader_addline(buffer, "att = 1.0 / att;\n");
8572 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8573 if (!settings->normal)
8575 shader_addline(buffer, "}\n");
8576 continue;
8578 shader_addline(buffer, "dir = normalize(dir);\n");
8579 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8580 shader_addline(buffer, "}\n");
8583 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8585 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8586 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8587 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8588 shader_addline(buffer, "dst.x = 1.0;\n");
8589 if (legacy_lighting)
8591 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8592 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8593 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8595 else
8597 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8599 shader_addline(buffer, "dir = normalize(dir);\n");
8600 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8601 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8602 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8603 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8604 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8605 idx, idx, idx, idx);
8606 if (legacy_lighting)
8607 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8608 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8609 idx, idx, idx);
8610 else
8611 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8612 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8613 idx, idx, idx);
8614 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8615 if (!settings->normal)
8617 shader_addline(buffer, "}\n");
8618 continue;
8620 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8621 shader_addline(buffer, "}\n");
8624 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8626 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8627 if (!settings->normal)
8628 continue;
8629 shader_addline(buffer, "att = 1.0;\n");
8630 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8631 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8634 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8636 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8637 if (!settings->normal)
8638 continue;
8639 shader_addline(buffer, "att = 1.0;\n");
8640 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8641 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8644 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8645 ambient, diffuse, emissive);
8646 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8647 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8650 /* Context activation is done by the caller. */
8651 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8652 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8654 static const struct attrib_info
8656 const char type[6];
8657 const char name[24];
8659 attrib_info[] =
8661 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8662 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8663 /* TODO: Indexed vertex blending */
8664 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8665 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8666 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8667 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8668 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8670 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8671 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8672 BOOL output_legacy_fogcoord = legacy_syntax;
8673 BOOL legacy_lighting = priv->legacy_lighting;
8674 GLuint shader_obj;
8675 unsigned int i;
8677 string_buffer_clear(buffer);
8679 shader_glsl_add_version_declaration(buffer, gl_info);
8681 if (shader_glsl_use_explicit_attrib_location(gl_info))
8682 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8684 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8686 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8688 if (shader_glsl_use_explicit_attrib_location(gl_info))
8689 shader_addline(buffer, "layout(location = %u) ", i);
8690 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8692 shader_addline(buffer, "\n");
8694 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8695 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8696 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8697 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
8699 shader_addline(buffer, "uniform struct\n{\n");
8700 shader_addline(buffer, " vec4 emissive;\n");
8701 shader_addline(buffer, " vec4 ambient;\n");
8702 shader_addline(buffer, " vec4 diffuse;\n");
8703 shader_addline(buffer, " vec4 specular;\n");
8704 shader_addline(buffer, " float shininess;\n");
8705 shader_addline(buffer, "} ffp_material;\n");
8707 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8708 shader_addline(buffer, "uniform struct\n{\n");
8709 shader_addline(buffer, " vec4 diffuse;\n");
8710 shader_addline(buffer, " vec4 specular;\n");
8711 shader_addline(buffer, " vec4 ambient;\n");
8712 shader_addline(buffer, " vec4 position;\n");
8713 shader_addline(buffer, " vec3 direction;\n");
8714 shader_addline(buffer, " float range;\n");
8715 shader_addline(buffer, " float falloff;\n");
8716 shader_addline(buffer, " float c_att;\n");
8717 shader_addline(buffer, " float l_att;\n");
8718 shader_addline(buffer, " float q_att;\n");
8719 shader_addline(buffer, " float cos_htheta;\n");
8720 shader_addline(buffer, " float cos_hphi;\n");
8721 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
8723 if (settings->point_size)
8725 shader_addline(buffer, "uniform struct\n{\n");
8726 shader_addline(buffer, " float size;\n");
8727 shader_addline(buffer, " float size_min;\n");
8728 shader_addline(buffer, " float size_max;\n");
8729 shader_addline(buffer, " float c_att;\n");
8730 shader_addline(buffer, " float l_att;\n");
8731 shader_addline(buffer, " float q_att;\n");
8732 shader_addline(buffer, "} ffp_point;\n");
8735 if (legacy_syntax)
8737 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8738 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8739 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8740 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8742 else
8744 if (settings->clipping)
8745 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8747 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8748 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8749 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8750 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8753 shader_addline(buffer, "\nvoid main()\n{\n");
8754 shader_addline(buffer, "float m;\n");
8755 shader_addline(buffer, "vec3 r;\n");
8757 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
8759 if (attrib_info[i].name[0])
8760 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
8761 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
8763 for (i = 0; i < MAX_TEXTURES; ++i)
8765 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
8766 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
8767 && settings->texcoords & (1u << i))
8768 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
8771 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
8773 if (settings->transformed)
8775 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
8776 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8777 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
8779 else
8781 for (i = 0; i < settings->vertexblends; ++i)
8782 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
8784 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
8785 for (i = 0; i < settings->vertexblends + 1; ++i)
8786 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
8788 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8789 if (settings->clipping)
8791 if (legacy_syntax)
8792 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
8793 else
8794 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8795 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
8797 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
8800 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
8801 if (settings->normal)
8803 if (!settings->vertexblends)
8805 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
8807 else
8809 for (i = 0; i < settings->vertexblends + 1; ++i)
8810 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
8813 if (settings->normalize)
8814 shader_addline(buffer, "normal = normalize(normal);\n");
8817 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
8818 if (legacy_syntax)
8820 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
8821 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
8823 else
8825 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
8826 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
8829 for (i = 0; i < MAX_TEXTURES; ++i)
8831 BOOL output_legacy_texcoord = legacy_syntax;
8833 switch (settings->texgen[i] & 0xffff0000)
8835 case WINED3DTSS_TCI_PASSTHRU:
8836 if (settings->texcoords & (1u << i))
8837 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
8838 i, i, i);
8839 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
8840 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
8841 else
8842 output_legacy_texcoord = FALSE;
8843 break;
8845 case WINED3DTSS_TCI_CAMERASPACENORMAL:
8846 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
8847 break;
8849 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
8850 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
8851 break;
8853 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
8854 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8855 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
8856 break;
8858 case WINED3DTSS_TCI_SPHEREMAP:
8859 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
8860 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
8861 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8862 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
8863 break;
8865 default:
8866 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
8867 break;
8869 if (output_legacy_texcoord)
8870 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
8873 switch (settings->fog_mode)
8875 case WINED3D_FFP_VS_FOG_OFF:
8876 output_legacy_fogcoord = FALSE;
8877 break;
8879 case WINED3D_FFP_VS_FOG_FOGCOORD:
8880 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
8881 break;
8883 case WINED3D_FFP_VS_FOG_RANGE:
8884 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
8885 break;
8887 case WINED3D_FFP_VS_FOG_DEPTH:
8888 if (settings->ortho_fog)
8890 if (gl_info->supported[ARB_CLIP_CONTROL])
8891 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
8892 else
8893 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
8894 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
8896 else if (settings->transformed)
8898 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
8900 else
8902 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
8904 break;
8906 default:
8907 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
8908 break;
8910 if (output_legacy_fogcoord)
8911 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
8913 if (settings->point_size)
8915 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
8916 " + ffp_point.l_att * length(ec_pos.xyz)"
8917 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
8918 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
8919 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
8922 shader_addline(buffer, "}\n");
8924 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8925 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
8927 return shader_obj;
8930 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
8931 DWORD argnum, unsigned int stage, DWORD arg)
8933 const char *ret;
8935 if (arg == ARG_UNUSED)
8936 return "<unused arg>";
8938 switch (arg & WINED3DTA_SELECTMASK)
8940 case WINED3DTA_DIFFUSE:
8941 ret = "ffp_varying_diffuse";
8942 break;
8944 case WINED3DTA_CURRENT:
8945 ret = "ret";
8946 break;
8948 case WINED3DTA_TEXTURE:
8949 switch (stage)
8951 case 0: ret = "tex0"; break;
8952 case 1: ret = "tex1"; break;
8953 case 2: ret = "tex2"; break;
8954 case 3: ret = "tex3"; break;
8955 case 4: ret = "tex4"; break;
8956 case 5: ret = "tex5"; break;
8957 case 6: ret = "tex6"; break;
8958 case 7: ret = "tex7"; break;
8959 default:
8960 ret = "<invalid texture>";
8961 break;
8963 break;
8965 case WINED3DTA_TFACTOR:
8966 ret = "tex_factor";
8967 break;
8969 case WINED3DTA_SPECULAR:
8970 ret = "ffp_varying_specular";
8971 break;
8973 case WINED3DTA_TEMP:
8974 ret = "temp_reg";
8975 break;
8977 case WINED3DTA_CONSTANT:
8978 switch (stage)
8980 case 0: ret = "tss_const0"; break;
8981 case 1: ret = "tss_const1"; break;
8982 case 2: ret = "tss_const2"; break;
8983 case 3: ret = "tss_const3"; break;
8984 case 4: ret = "tss_const4"; break;
8985 case 5: ret = "tss_const5"; break;
8986 case 6: ret = "tss_const6"; break;
8987 case 7: ret = "tss_const7"; break;
8988 default:
8989 ret = "<invalid constant>";
8990 break;
8992 break;
8994 default:
8995 return "<unhandled arg>";
8998 if (arg & WINED3DTA_COMPLEMENT)
9000 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9001 if (argnum == 0)
9002 ret = "arg0";
9003 else if (argnum == 1)
9004 ret = "arg1";
9005 else if (argnum == 2)
9006 ret = "arg2";
9009 if (arg & WINED3DTA_ALPHAREPLICATE)
9011 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9012 if (argnum == 0)
9013 ret = "arg0";
9014 else if (argnum == 1)
9015 ret = "arg1";
9016 else if (argnum == 2)
9017 ret = "arg2";
9020 return ret;
9023 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9024 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9026 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9028 if (color && alpha)
9029 dstmask = "";
9030 else if (color)
9031 dstmask = ".xyz";
9032 else
9033 dstmask = ".w";
9035 if (dst == tempreg)
9036 dstreg = "temp_reg";
9037 else
9038 dstreg = "ret";
9040 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9041 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9042 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9044 switch (op)
9046 case WINED3D_TOP_DISABLE:
9047 break;
9049 case WINED3D_TOP_SELECT_ARG1:
9050 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9051 break;
9053 case WINED3D_TOP_SELECT_ARG2:
9054 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9055 break;
9057 case WINED3D_TOP_MODULATE:
9058 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9059 break;
9061 case WINED3D_TOP_MODULATE_4X:
9062 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9063 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9064 break;
9066 case WINED3D_TOP_MODULATE_2X:
9067 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9068 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9069 break;
9071 case WINED3D_TOP_ADD:
9072 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9073 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9074 break;
9076 case WINED3D_TOP_ADD_SIGNED:
9077 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9078 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9079 break;
9081 case WINED3D_TOP_ADD_SIGNED_2X:
9082 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9083 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9084 break;
9086 case WINED3D_TOP_SUBTRACT:
9087 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9088 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9089 break;
9091 case WINED3D_TOP_ADD_SMOOTH:
9092 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9093 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9094 break;
9096 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9097 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9098 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9099 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9100 break;
9102 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9103 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9104 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9105 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9106 break;
9108 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9109 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9110 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9111 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9112 break;
9114 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9115 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9116 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9117 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9118 break;
9120 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9121 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9122 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9123 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9124 break;
9126 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9127 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9128 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9129 break;
9131 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9132 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9133 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9134 break;
9136 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9137 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9138 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9139 break;
9140 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9141 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9142 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9143 break;
9145 case WINED3D_TOP_BUMPENVMAP:
9146 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9147 /* These are handled in the first pass, nothing to do. */
9148 break;
9150 case WINED3D_TOP_DOTPRODUCT3:
9151 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9152 dstreg, dstmask, arg1, arg2, dstmask);
9153 break;
9155 case WINED3D_TOP_MULTIPLY_ADD:
9156 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9157 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9158 break;
9160 case WINED3D_TOP_LERP:
9161 /* MSDN isn't quite right here. */
9162 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9163 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9164 break;
9166 default:
9167 FIXME("Unhandled operation %#x.\n", op);
9168 break;
9172 /* Context activation is done by the caller. */
9173 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9174 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9176 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9177 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9178 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9179 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9180 const struct wined3d_gl_info *gl_info = context->gl_info;
9181 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9182 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9183 UINT lowest_disabled_stage;
9184 GLuint shader_id;
9185 DWORD arg0, arg1, arg2;
9186 unsigned int stage;
9188 string_buffer_clear(buffer);
9190 /* Find out which textures are read */
9191 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9193 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9194 break;
9196 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9197 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9198 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9200 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9201 || (stage == 0 && settings->color_key_enabled))
9202 tex_map |= 1u << stage;
9203 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9204 tfactor_used = TRUE;
9205 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9206 tempreg_used = TRUE;
9207 if (settings->op[stage].dst == tempreg)
9208 tempreg_used = TRUE;
9209 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9210 tss_const_map |= 1u << stage;
9212 switch (settings->op[stage].cop)
9214 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9215 lum_map |= 1u << stage;
9216 /* fall through */
9217 case WINED3D_TOP_BUMPENVMAP:
9218 bump_map |= 1u << stage;
9219 /* fall through */
9220 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9221 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9222 tex_map |= 1u << stage;
9223 break;
9225 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9226 tfactor_used = TRUE;
9227 break;
9229 default:
9230 break;
9233 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9234 continue;
9236 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9237 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9238 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9240 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9241 tex_map |= 1u << stage;
9242 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9243 tfactor_used = TRUE;
9244 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9245 tempreg_used = TRUE;
9246 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9247 tss_const_map |= 1u << stage;
9249 lowest_disabled_stage = stage;
9251 shader_glsl_add_version_declaration(buffer, gl_info);
9253 if (shader_glsl_use_explicit_attrib_location(gl_info))
9254 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9255 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9256 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9257 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9258 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9260 if (!needs_legacy_glsl_syntax(gl_info))
9262 if (shader_glsl_use_explicit_attrib_location(gl_info))
9263 shader_addline(buffer, "layout(location = 0) ");
9264 shader_addline(buffer, "out vec4 ps_out[1];\n");
9267 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9268 shader_addline(buffer, "vec4 ret;\n");
9269 if (tempreg_used || settings->sRGB_write)
9270 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9271 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9273 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9275 const char *sampler_type;
9277 if (tss_const_map & (1u << stage))
9278 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9280 if (!(tex_map & (1u << stage)))
9281 continue;
9283 switch (settings->op[stage].tex_type)
9285 case WINED3D_GL_RES_TYPE_TEX_1D:
9286 sampler_type = "1D";
9287 break;
9288 case WINED3D_GL_RES_TYPE_TEX_2D:
9289 sampler_type = "2D";
9290 break;
9291 case WINED3D_GL_RES_TYPE_TEX_3D:
9292 sampler_type = "3D";
9293 break;
9294 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9295 sampler_type = "Cube";
9296 break;
9297 case WINED3D_GL_RES_TYPE_TEX_RECT:
9298 sampler_type = "2DRect";
9299 break;
9300 default:
9301 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9302 sampler_type = NULL;
9303 break;
9305 if (sampler_type)
9307 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9308 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9309 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9312 shader_addline(buffer, "vec4 tex%u;\n", stage);
9314 if (!(bump_map & (1u << stage)))
9315 continue;
9316 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9318 if (!(lum_map & (1u << stage)))
9319 continue;
9320 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9321 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9323 if (tfactor_used)
9324 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9325 if (settings->color_key_enabled)
9326 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9327 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9329 if (settings->sRGB_write)
9331 shader_addline(buffer, "const vec4 srgb_const0 = ");
9332 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9333 shader_addline(buffer, ";\n");
9334 shader_addline(buffer, "const vec4 srgb_const1 = ");
9335 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9336 shader_addline(buffer, ";\n");
9339 shader_addline(buffer, "uniform struct\n{\n");
9340 shader_addline(buffer, " vec4 color;\n");
9341 shader_addline(buffer, " float density;\n");
9342 shader_addline(buffer, " float end;\n");
9343 shader_addline(buffer, " float scale;\n");
9344 shader_addline(buffer, "} ffp_fog;\n");
9346 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9347 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9349 if (legacy_syntax)
9351 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9352 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9353 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9354 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9355 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9357 else
9359 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9360 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9361 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9362 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9363 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9366 shader_addline(buffer, "void main()\n{\n");
9368 if (legacy_syntax)
9370 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9371 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9374 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9376 if (tex_map & (1u << stage))
9378 if (settings->pointsprite)
9379 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9380 else if (settings->texcoords_initialized & (1u << stage))
9381 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9382 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9383 else
9384 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9388 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9389 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9391 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9392 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9394 /* Generate texture sampling instructions */
9395 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9397 const char *texture_function, *coord_mask;
9398 BOOL proj;
9400 if (!(tex_map & (1u << stage)))
9401 continue;
9403 if (settings->op[stage].projected == proj_none)
9405 proj = FALSE;
9407 else if (settings->op[stage].projected == proj_count4
9408 || settings->op[stage].projected == proj_count3)
9410 proj = TRUE;
9412 else
9414 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9415 proj = TRUE;
9418 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9419 proj = FALSE;
9421 switch (settings->op[stage].tex_type)
9423 case WINED3D_GL_RES_TYPE_TEX_1D:
9424 if (proj)
9426 texture_function = "texture1DProj";
9427 coord_mask = "xw";
9429 else
9431 texture_function = "texture1D";
9432 coord_mask = "x";
9434 break;
9435 case WINED3D_GL_RES_TYPE_TEX_2D:
9436 if (proj)
9438 texture_function = "texture2DProj";
9439 coord_mask = "xyw";
9441 else
9443 texture_function = "texture2D";
9444 coord_mask = "xy";
9446 break;
9447 case WINED3D_GL_RES_TYPE_TEX_3D:
9448 if (proj)
9450 texture_function = "texture3DProj";
9451 coord_mask = "xyzw";
9453 else
9455 texture_function = "texture3D";
9456 coord_mask = "xyz";
9458 break;
9459 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9460 texture_function = "textureCube";
9461 coord_mask = "xyz";
9462 break;
9463 case WINED3D_GL_RES_TYPE_TEX_RECT:
9464 if (proj)
9466 texture_function = "texture2DRectProj";
9467 coord_mask = "xyw";
9469 else
9471 texture_function = "texture2DRect";
9472 coord_mask = "xy";
9474 break;
9475 default:
9476 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9477 texture_function = "";
9478 coord_mask = "xyzw";
9479 break;
9481 if (!legacy_syntax)
9482 texture_function = proj ? "textureProj" : "texture";
9484 if (stage > 0
9485 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9486 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9488 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9490 /* With projective textures, texbem only divides the static
9491 * texture coord, not the displacement, so multiply the
9492 * displacement with the dividing parameter before passing it to
9493 * TXP. */
9494 if (settings->op[stage].projected != proj_none)
9496 if (settings->op[stage].projected == proj_count4)
9498 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9499 stage, stage);
9500 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9502 else
9504 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9505 stage, stage);
9506 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9509 else
9511 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9514 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9515 stage, texture_function, stage, coord_mask);
9517 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9518 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9519 stage, stage - 1, stage - 1, stage - 1);
9521 else if (settings->op[stage].projected == proj_count3)
9523 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9524 stage, texture_function, stage, stage);
9526 else
9528 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9529 stage, texture_function, stage, stage, coord_mask);
9532 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9533 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9534 settings->op[stage].color_fixup);
9537 if (settings->color_key_enabled)
9539 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9540 shader_addline(buffer, " discard;\n");
9543 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9545 /* Generate the main shader */
9546 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9548 BOOL op_equal;
9550 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9551 break;
9553 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9554 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9555 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9556 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9557 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9558 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9559 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9560 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9561 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9562 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9563 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9564 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9565 else
9566 op_equal = settings->op[stage].aop == settings->op[stage].cop
9567 && settings->op[stage].carg0 == settings->op[stage].aarg0
9568 && settings->op[stage].carg1 == settings->op[stage].aarg1
9569 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9571 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9573 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9574 settings->op[stage].cop, settings->op[stage].carg0,
9575 settings->op[stage].carg1, settings->op[stage].carg2);
9577 else if (op_equal)
9579 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
9580 settings->op[stage].cop, settings->op[stage].carg0,
9581 settings->op[stage].carg1, settings->op[stage].carg2);
9583 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9584 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9586 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9587 settings->op[stage].cop, settings->op[stage].carg0,
9588 settings->op[stage].carg1, settings->op[stage].carg2);
9589 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
9590 settings->op[stage].aop, settings->op[stage].aarg0,
9591 settings->op[stage].aarg1, settings->op[stage].aarg2);
9595 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9596 get_fragment_output(gl_info));
9598 if (settings->sRGB_write)
9599 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9601 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9603 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9605 shader_addline(buffer, "}\n");
9607 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9608 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9610 string_buffer_release(&priv->string_buffers, tex_reg_name);
9611 return shader_id;
9614 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9615 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9617 struct glsl_ffp_vertex_shader *shader;
9618 const struct wine_rb_entry *entry;
9620 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9621 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9623 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
9624 return NULL;
9626 shader->desc.settings = *settings;
9627 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9628 list_init(&shader->linked_programs);
9629 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9630 ERR("Failed to insert ffp vertex shader.\n");
9632 return shader;
9635 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9636 const struct ffp_frag_settings *args, const struct wined3d_context *context)
9638 struct glsl_ffp_fragment_shader *glsl_desc;
9639 const struct ffp_frag_desc *desc;
9641 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9642 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9644 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
9645 return NULL;
9647 glsl_desc->entry.settings = *args;
9648 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
9649 list_init(&glsl_desc->linked_programs);
9650 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9652 return glsl_desc;
9656 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9657 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9659 unsigned int i;
9660 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9662 for (i = 0; i < vs_c_count; ++i)
9664 string_buffer_sprintf(name, "vs_c[%u]", i);
9665 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9667 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9669 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9671 string_buffer_sprintf(name, "vs_i[%u]", i);
9672 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9675 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9677 string_buffer_sprintf(name, "vs_b[%u]", i);
9678 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9681 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9683 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9685 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9686 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9688 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9689 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9690 for (i = 0; i < MAX_TEXTURES; ++i)
9692 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9693 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9695 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9696 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9697 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9698 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9699 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9700 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9701 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
9703 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9704 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9705 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9706 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9707 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9708 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9709 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9710 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9711 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9712 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9713 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9714 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9715 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9716 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9717 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9718 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9719 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9720 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9721 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9722 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9723 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9724 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9725 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9726 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9728 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
9729 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
9730 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
9731 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
9732 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
9733 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
9734 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
9736 string_buffer_release(&priv->string_buffers, name);
9739 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
9740 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
9742 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9745 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
9746 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
9748 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9751 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
9752 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
9754 unsigned int i;
9755 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9757 for (i = 0; i < ps_c_count; ++i)
9759 string_buffer_sprintf(name, "ps_c[%u]", i);
9760 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9762 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
9764 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9766 string_buffer_sprintf(name, "ps_i[%u]", i);
9767 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9770 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9772 string_buffer_sprintf(name, "ps_b[%u]", i);
9773 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9776 for (i = 0; i < MAX_TEXTURES; ++i)
9778 string_buffer_sprintf(name, "bumpenv_mat%u", i);
9779 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9780 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
9781 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9782 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
9783 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9784 string_buffer_sprintf(name, "tss_const%u", i);
9785 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9788 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
9789 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
9791 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
9792 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
9793 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
9794 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
9796 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
9798 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
9799 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
9800 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
9802 string_buffer_release(&priv->string_buffers, name);
9805 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
9806 const struct wined3d_context *context, struct wined3d_shader *shader)
9808 struct glsl_context_data *ctx_data = context->shader_backend_data;
9809 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9810 const struct wined3d_gl_info *gl_info = context->gl_info;
9811 struct glsl_cs_compiled_shader *gl_shaders;
9812 struct glsl_shader_private *shader_data;
9813 struct glsl_shader_prog_link *entry;
9814 GLuint shader_id, program_id;
9816 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
9818 ERR("Out of memory.\n");
9819 return E_OUTOFMEMORY;
9822 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
9824 ERR("Failed to allocate backend data.\n");
9825 HeapFree(GetProcessHeap(), 0, entry);
9826 return E_OUTOFMEMORY;
9828 shader_data = shader->backend_data;
9829 gl_shaders = shader_data->gl_shaders.cs;
9831 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
9833 ERR("Failed to allocate GL shader array.\n");
9834 HeapFree(GetProcessHeap(), 0, entry);
9835 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9836 shader->backend_data = NULL;
9837 return E_OUTOFMEMORY;
9839 shader_data->shader_array_size = 1;
9840 gl_shaders = shader_data->gl_shaders.cs;
9842 TRACE("Compiling compute shader %p.\n", shader);
9844 string_buffer_clear(buffer);
9845 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
9846 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
9848 program_id = GL_EXTCALL(glCreateProgram());
9849 TRACE("Created new GLSL shader program %u.\n", program_id);
9851 entry->id = program_id;
9852 entry->vs.id = 0;
9853 entry->hs.id = 0;
9854 entry->ds.id = 0;
9855 entry->gs.id = 0;
9856 entry->ps.id = 0;
9857 entry->cs.id = shader_id;
9858 entry->constant_version = 0;
9859 entry->ps.np2_fixup_info = NULL;
9860 add_glsl_program_entry(priv, entry);
9862 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
9863 GL_EXTCALL(glAttachShader(program_id, shader_id));
9864 checkGLcall("glAttachShader");
9866 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
9868 TRACE("Linking GLSL shader program %u.\n", program_id);
9869 GL_EXTCALL(glLinkProgram(program_id));
9870 shader_glsl_validate_link(gl_info, program_id);
9872 GL_EXTCALL(glUseProgram(program_id));
9873 checkGLcall("glUseProgram");
9874 shader_glsl_load_program_resources(context, priv, program_id, shader);
9875 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
9877 entry->constant_update_mask = 0;
9879 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
9880 checkGLcall("glUseProgram");
9881 return WINED3D_OK;
9884 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
9885 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
9887 struct glsl_shader_private *shader_data;
9889 if (!shader->backend_data)
9891 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
9892 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
9894 ERR("Failed to compile compute shader %p.\n", shader);
9895 return 0;
9898 shader_data = shader->backend_data;
9899 return shader_data->gl_shaders.cs[0].id;
9902 /* Context activation is done by the caller. */
9903 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
9904 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9906 struct glsl_shader_prog_link *entry;
9907 struct wined3d_shader *shader;
9908 struct glsl_program_key key;
9909 GLuint cs_id;
9911 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
9912 return;
9914 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
9916 WARN("Compute shader is NULL.\n");
9917 ctx_data->glsl_program = NULL;
9918 return;
9921 cs_id = find_glsl_compute_shader(context, priv, shader);
9922 memset(&key, 0, sizeof(key));
9923 key.cs_id = cs_id;
9924 if (!(entry = get_glsl_program_entry(priv, &key)))
9925 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
9926 ctx_data->glsl_program = entry;
9929 /* Context activation is done by the caller. */
9930 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
9931 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9933 const struct wined3d_gl_info *gl_info = context->gl_info;
9934 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
9935 const struct ps_np2fixup_info *np2fixup_info = NULL;
9936 struct glsl_shader_prog_link *entry = NULL;
9937 struct wined3d_shader *hshader, *dshader, *gshader;
9938 struct wined3d_shader *vshader = NULL;
9939 struct wined3d_shader *pshader = NULL;
9940 GLuint reorder_shader_id = 0;
9941 struct glsl_program_key key;
9942 GLuint program_id;
9943 unsigned int i;
9944 GLuint vs_id = 0;
9945 GLuint hs_id = 0;
9946 GLuint ds_id = 0;
9947 GLuint gs_id = 0;
9948 GLuint ps_id = 0;
9949 struct list *ps_list, *vs_list;
9950 WORD attribs_map;
9951 struct wined3d_string_buffer *tmp_name;
9953 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
9955 vs_id = ctx_data->glsl_program->vs.id;
9956 vs_list = &ctx_data->glsl_program->vs.shader_entry;
9958 if (use_vs(state))
9959 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
9961 else if (use_vs(state))
9963 struct vs_compile_args vs_compile_args;
9965 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
9967 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
9968 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
9969 vs_list = &vshader->linked_programs;
9971 else if (priv->vertex_pipe == &glsl_vertex_pipe)
9973 struct glsl_ffp_vertex_shader *ffp_shader;
9974 struct wined3d_ffp_vs_settings settings;
9976 wined3d_ffp_get_vs_settings(context, state, &settings);
9977 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
9978 vs_id = ffp_shader->id;
9979 vs_list = &ffp_shader->linked_programs;
9982 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
9983 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
9984 hs_id = ctx_data->glsl_program->hs.id;
9985 else if (hshader)
9986 hs_id = find_glsl_hull_shader(context, priv, hshader);
9988 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
9989 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
9991 ds_id = ctx_data->glsl_program->ds.id;
9993 else if (dshader)
9995 struct ds_compile_args args;
9997 find_ds_compile_args(state, dshader, &args, context);
9998 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
10001 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10002 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10004 gs_id = ctx_data->glsl_program->gs.id;
10006 else if (gshader)
10008 struct gs_compile_args args;
10010 find_gs_compile_args(state, gshader, &args, context);
10011 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
10014 /* A pixel shader is not used when rasterization is disabled. */
10015 if (is_rasterization_disabled(gshader))
10017 ps_id = 0;
10018 ps_list = NULL;
10020 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10022 ps_id = ctx_data->glsl_program->ps.id;
10023 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10025 if (use_ps(state))
10026 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10028 else if (use_ps(state))
10030 struct ps_compile_args ps_compile_args;
10031 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10032 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
10033 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
10034 pshader, &ps_compile_args, &np2fixup_info);
10035 ps_list = &pshader->linked_programs;
10037 else if (priv->fragment_pipe == &glsl_fragment_pipe
10038 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10040 struct glsl_ffp_fragment_shader *ffp_shader;
10041 struct ffp_frag_settings settings;
10043 gen_ffp_frag_op(context, state, &settings, FALSE);
10044 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
10045 ps_id = ffp_shader->id;
10046 ps_list = &ffp_shader->linked_programs;
10049 key.vs_id = vs_id;
10050 key.hs_id = hs_id;
10051 key.ds_id = ds_id;
10052 key.gs_id = gs_id;
10053 key.ps_id = ps_id;
10054 key.cs_id = 0;
10055 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10057 ctx_data->glsl_program = entry;
10058 return;
10061 /* If we get to this point, then no matching program exists, so we create one */
10062 program_id = GL_EXTCALL(glCreateProgram());
10063 TRACE("Created new GLSL shader program %u.\n", program_id);
10065 /* Create the entry */
10066 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
10067 entry->id = program_id;
10068 entry->vs.id = vs_id;
10069 entry->hs.id = hs_id;
10070 entry->ds.id = ds_id;
10071 entry->gs.id = gs_id;
10072 entry->ps.id = ps_id;
10073 entry->cs.id = 0;
10074 entry->constant_version = 0;
10075 entry->ps.np2_fixup_info = np2fixup_info;
10076 /* Add the hash table entry */
10077 add_glsl_program_entry(priv, entry);
10079 /* Set the current program */
10080 ctx_data->glsl_program = entry;
10082 /* Attach GLSL vshader */
10083 if (vs_id)
10085 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10086 GL_EXTCALL(glAttachShader(program_id, vs_id));
10087 checkGLcall("glAttachShader");
10089 list_add_head(vs_list, &entry->vs.shader_entry);
10092 if (vshader)
10094 attribs_map = vshader->reg_maps.input_registers;
10095 if (vshader->reg_maps.shader_version.major < 4)
10097 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10098 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10099 d3d_info->emulated_flatshading
10100 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10101 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10102 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10103 checkGLcall("glAttachShader");
10104 /* Flag the reorder function for deletion, it will be freed
10105 * automatically when the program is destroyed. */
10106 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10109 else
10111 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10114 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10116 /* Bind vertex attributes to a corresponding index number to match
10117 * the same index numbers as ARB_vertex_programs (makes loading
10118 * vertex attributes simpler). With this method, we can use the
10119 * exact same code to load the attributes later for both ARB and
10120 * GLSL shaders.
10122 * We have to do this here because we need to know the Program ID
10123 * in order to make the bindings work, and it has to be done prior
10124 * to linking the GLSL program. */
10125 tmp_name = string_buffer_get(&priv->string_buffers);
10126 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10128 if (!(attribs_map & 1))
10129 continue;
10131 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10132 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10133 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10135 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10136 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10137 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10138 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10141 checkGLcall("glBindAttribLocation");
10142 string_buffer_release(&priv->string_buffers, tmp_name);
10144 if (!needs_legacy_glsl_syntax(gl_info))
10146 GL_EXTCALL(glBindFragDataLocation(program_id, 0, "ps_out"));
10147 checkGLcall("glBindFragDataLocation");
10151 if (hshader)
10153 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10154 GL_EXTCALL(glAttachShader(program_id, hs_id));
10155 checkGLcall("glAttachShader");
10157 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10160 if (dshader)
10162 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10163 GL_EXTCALL(glAttachShader(program_id, ds_id));
10164 checkGLcall("glAttachShader");
10166 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10169 if (gshader)
10171 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10172 GL_EXTCALL(glAttachShader(program_id, gs_id));
10173 checkGLcall("glAttachShader");
10175 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10177 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10180 /* Attach GLSL pshader */
10181 if (ps_id)
10183 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10184 GL_EXTCALL(glAttachShader(program_id, ps_id));
10185 checkGLcall("glAttachShader");
10187 list_add_head(ps_list, &entry->ps.shader_entry);
10190 /* Link the program */
10191 TRACE("Linking GLSL shader program %u.\n", program_id);
10192 GL_EXTCALL(glLinkProgram(program_id));
10193 shader_glsl_validate_link(gl_info, program_id);
10195 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10196 vshader ? vshader->limits->constant_float : 0);
10197 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10198 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10199 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10200 pshader ? pshader->limits->constant_float : 0);
10201 checkGLcall("Find glsl program uniform locations");
10203 if (needs_legacy_glsl_syntax(gl_info))
10205 if (pshader && pshader->reg_maps.shader_version.major >= 3
10206 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10208 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10209 entry->vs.vertex_color_clamp = GL_FALSE;
10211 else
10213 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10216 else
10218 /* With core profile we never change vertex_color_clamp from
10219 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10220 * glClampColorARB(). */
10221 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10224 /* Set the shader to allow uniform loading on it */
10225 GL_EXTCALL(glUseProgram(program_id));
10226 checkGLcall("glUseProgram");
10228 entry->constant_update_mask = 0;
10229 if (vshader)
10231 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10232 if (vshader->reg_maps.integer_constants)
10233 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10234 if (vshader->reg_maps.boolean_constants)
10235 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10236 if (entry->vs.pos_fixup_location != -1)
10237 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10239 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10241 else
10243 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10244 | WINED3D_SHADER_CONST_FFP_PROJ;
10246 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10248 if (entry->vs.modelview_matrix_location[i] != -1)
10250 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10251 break;
10255 for (i = 0; i < MAX_TEXTURES; ++i)
10257 if (entry->vs.texture_matrix_location[i] != -1)
10259 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10260 break;
10263 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10264 || entry->vs.material_specular_location != -1
10265 || entry->vs.material_emissive_location != -1
10266 || entry->vs.material_shininess_location != -1)
10267 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10268 if (entry->vs.light_ambient_location != -1)
10269 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10271 if (entry->vs.clip_planes_location != -1)
10272 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10273 if (entry->vs.pointsize_min_location != -1)
10274 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10276 if (hshader)
10277 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10279 if (dshader)
10281 if (entry->ds.pos_fixup_location != -1)
10282 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10284 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10287 if (gshader)
10289 if (entry->gs.pos_fixup_location != -1)
10290 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10292 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10295 if (ps_id)
10297 if (pshader)
10299 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10300 if (pshader->reg_maps.integer_constants)
10301 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10302 if (pshader->reg_maps.boolean_constants)
10303 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10304 if (entry->ps.ycorrection_location != -1)
10305 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10307 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10308 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10310 else
10312 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10314 shader_glsl_load_samplers(context, priv, program_id, NULL);
10317 for (i = 0; i < MAX_TEXTURES; ++i)
10319 if (entry->ps.bumpenv_mat_location[i] != -1)
10321 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10322 break;
10326 if (entry->ps.fog_color_location != -1)
10327 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10328 if (entry->ps.alpha_test_ref_location != -1)
10329 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10330 if (entry->ps.np2_fixup_location != -1)
10331 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10332 if (entry->ps.color_key_location != -1)
10333 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10337 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10339 struct wined3d_device *device = shader->device;
10340 struct wined3d_context *context;
10342 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10344 context = context_acquire(device, NULL, 0);
10345 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10346 context_release(context);
10350 /* Context activation is done by the caller. */
10351 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10352 const struct wined3d_state *state)
10354 struct glsl_context_data *ctx_data = context->shader_backend_data;
10355 const struct wined3d_gl_info *gl_info = context->gl_info;
10356 struct shader_glsl_priv *priv = shader_priv;
10357 GLenum current_vertex_color_clamp;
10358 GLuint program_id, prev_id;
10360 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10361 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10363 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10365 set_glsl_shader_program(context, state, priv, ctx_data);
10367 if (ctx_data->glsl_program)
10369 program_id = ctx_data->glsl_program->id;
10370 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
10372 else
10374 program_id = 0;
10375 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10378 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10380 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10381 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10383 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10384 checkGLcall("glClampColorARB");
10386 else
10388 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10392 TRACE("Using GLSL program %u.\n", program_id);
10394 if (prev_id != program_id)
10396 GL_EXTCALL(glUseProgram(program_id));
10397 checkGLcall("glUseProgram");
10399 if (program_id)
10400 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
10403 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10406 /* Context activation is done by the caller. */
10407 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10408 const struct wined3d_state *state)
10410 struct glsl_context_data *ctx_data = context->shader_backend_data;
10411 const struct wined3d_gl_info *gl_info = context->gl_info;
10412 struct shader_glsl_priv *priv = shader_priv;
10413 GLuint program_id, prev_id;
10415 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10416 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10417 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10419 TRACE("Using GLSL program %u.\n", program_id);
10421 if (prev_id != program_id)
10423 GL_EXTCALL(glUseProgram(program_id));
10424 checkGLcall("glUseProgram");
10427 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10428 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10429 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10430 | (1u << WINED3D_SHADER_TYPE_HULL)
10431 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10434 /* "context" is not necessarily the currently active context. */
10435 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10437 struct glsl_context_data *ctx_data = context->shader_backend_data;
10439 ctx_data->glsl_program = NULL;
10440 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10441 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10442 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10443 | (1u << WINED3D_SHADER_TYPE_HULL)
10444 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10445 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10448 /* Context activation is done by the caller. */
10449 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10451 struct glsl_context_data *ctx_data = context->shader_backend_data;
10452 const struct wined3d_gl_info *gl_info = context->gl_info;
10453 struct shader_glsl_priv *priv = shader_priv;
10455 shader_glsl_invalidate_current_program(context);
10456 GL_EXTCALL(glUseProgram(0));
10457 checkGLcall("glUseProgram");
10459 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10460 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10462 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10464 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10465 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10466 checkGLcall("glClampColorARB");
10470 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10471 const struct glsl_shader_prog_link *program)
10473 const struct glsl_context_data *ctx_data;
10474 struct wined3d_context *context;
10475 unsigned int i;
10477 for (i = 0; i < device->context_count; ++i)
10479 context = device->contexts[i];
10480 ctx_data = context->shader_backend_data;
10482 if (ctx_data->glsl_program == program)
10483 shader_glsl_invalidate_current_program(context);
10487 static void shader_glsl_destroy(struct wined3d_shader *shader)
10489 struct glsl_shader_private *shader_data = shader->backend_data;
10490 struct wined3d_device *device = shader->device;
10491 struct shader_glsl_priv *priv = device->shader_priv;
10492 const struct wined3d_gl_info *gl_info;
10493 const struct list *linked_programs;
10494 struct wined3d_context *context;
10496 if (!shader_data || !shader_data->num_gl_shaders)
10498 HeapFree(GetProcessHeap(), 0, shader_data);
10499 shader->backend_data = NULL;
10500 return;
10503 context = context_acquire(device, NULL, 0);
10504 gl_info = context->gl_info;
10506 TRACE("Deleting linked programs.\n");
10507 linked_programs = &shader->linked_programs;
10508 if (linked_programs->next)
10510 struct glsl_shader_prog_link *entry, *entry2;
10511 UINT i;
10513 switch (shader->reg_maps.shader_version.type)
10515 case WINED3D_SHADER_TYPE_PIXEL:
10517 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10519 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10521 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10522 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10523 checkGLcall("glDeleteShader");
10525 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
10527 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10528 struct glsl_shader_prog_link, ps.shader_entry)
10530 shader_glsl_invalidate_contexts_program(device, entry);
10531 delete_glsl_program_entry(priv, gl_info, entry);
10534 break;
10537 case WINED3D_SHADER_TYPE_VERTEX:
10539 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10541 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10543 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10544 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10545 checkGLcall("glDeleteShader");
10547 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
10549 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10550 struct glsl_shader_prog_link, vs.shader_entry)
10552 shader_glsl_invalidate_contexts_program(device, entry);
10553 delete_glsl_program_entry(priv, gl_info, entry);
10556 break;
10559 case WINED3D_SHADER_TYPE_HULL:
10561 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10563 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10565 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10566 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10567 checkGLcall("glDeleteShader");
10569 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.hs);
10571 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10572 struct glsl_shader_prog_link, hs.shader_entry)
10574 shader_glsl_invalidate_contexts_program(device, entry);
10575 delete_glsl_program_entry(priv, gl_info, entry);
10578 break;
10581 case WINED3D_SHADER_TYPE_DOMAIN:
10583 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10585 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10587 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10588 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10589 checkGLcall("glDeleteShader");
10591 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ds);
10593 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10594 struct glsl_shader_prog_link, ds.shader_entry)
10596 shader_glsl_invalidate_contexts_program(device, entry);
10597 delete_glsl_program_entry(priv, gl_info, entry);
10600 break;
10603 case WINED3D_SHADER_TYPE_GEOMETRY:
10605 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10607 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10609 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10610 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10611 checkGLcall("glDeleteShader");
10613 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
10615 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10616 struct glsl_shader_prog_link, gs.shader_entry)
10618 shader_glsl_invalidate_contexts_program(device, entry);
10619 delete_glsl_program_entry(priv, gl_info, entry);
10622 break;
10625 case WINED3D_SHADER_TYPE_COMPUTE:
10627 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10629 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10631 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10632 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10633 checkGLcall("glDeleteShader");
10635 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
10637 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10638 struct glsl_shader_prog_link, cs.shader_entry)
10640 shader_glsl_invalidate_contexts_program(device, entry);
10641 delete_glsl_program_entry(priv, gl_info, entry);
10644 break;
10647 default:
10648 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10649 break;
10653 HeapFree(GetProcessHeap(), 0, shader->backend_data);
10654 shader->backend_data = NULL;
10656 context_release(context);
10659 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10661 const struct glsl_program_key *k = key;
10662 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10663 const struct glsl_shader_prog_link, program_lookup_entry);
10665 if (k->vs_id > prog->vs.id) return 1;
10666 else if (k->vs_id < prog->vs.id) return -1;
10668 if (k->gs_id > prog->gs.id) return 1;
10669 else if (k->gs_id < prog->gs.id) return -1;
10671 if (k->ps_id > prog->ps.id) return 1;
10672 else if (k->ps_id < prog->ps.id) return -1;
10674 if (k->hs_id > prog->hs.id) return 1;
10675 else if (k->hs_id < prog->hs.id) return -1;
10677 if (k->ds_id > prog->ds.id) return 1;
10678 else if (k->ds_id < prog->ds.id) return -1;
10680 if (k->cs_id > prog->cs.id) return 1;
10681 else if (k->cs_id < prog->cs.id) return -1;
10683 return 0;
10686 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10688 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10689 + constant_count * sizeof(*heap->contained)
10690 + constant_count * sizeof(*heap->positions);
10691 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
10693 if (!mem)
10695 ERR("Failed to allocate memory\n");
10696 return FALSE;
10699 heap->entries = mem;
10700 heap->entries[1].version = 0;
10701 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10702 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10703 heap->positions = (unsigned int *)(heap->contained + constant_count);
10704 heap->size = 1;
10706 return TRUE;
10709 static void constant_heap_free(struct constant_heap *heap)
10711 HeapFree(GetProcessHeap(), 0, heap->entries);
10714 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
10715 const struct fragment_pipeline *fragment_pipe)
10717 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
10718 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
10719 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
10720 struct fragment_caps fragment_caps;
10721 void *vertex_priv, *fragment_priv;
10723 string_buffer_list_init(&priv->string_buffers);
10725 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
10727 ERR("Failed to initialize vertex pipe.\n");
10728 HeapFree(GetProcessHeap(), 0, priv);
10729 return E_FAIL;
10732 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
10734 ERR("Failed to initialize fragment pipe.\n");
10735 vertex_pipe->vp_free(device);
10736 HeapFree(GetProcessHeap(), 0, priv);
10737 return E_FAIL;
10740 if (!string_buffer_init(&priv->shader_buffer))
10742 ERR("Failed to initialize shader buffer.\n");
10743 goto fail;
10746 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
10748 ERR("Failed to allocate memory.\n");
10749 goto fail;
10752 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
10754 ERR("Failed to initialize vertex shader constant heap\n");
10755 goto fail;
10758 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
10760 ERR("Failed to initialize pixel shader constant heap\n");
10761 goto fail;
10764 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
10766 priv->next_constant_version = 1;
10767 priv->vertex_pipe = vertex_pipe;
10768 priv->fragment_pipe = fragment_pipe;
10769 fragment_pipe->get_caps(gl_info, &fragment_caps);
10770 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
10771 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
10773 device->vertex_priv = vertex_priv;
10774 device->fragment_priv = fragment_priv;
10775 device->shader_priv = priv;
10777 return WINED3D_OK;
10779 fail:
10780 constant_heap_free(&priv->pconst_heap);
10781 constant_heap_free(&priv->vconst_heap);
10782 HeapFree(GetProcessHeap(), 0, priv->stack);
10783 string_buffer_free(&priv->shader_buffer);
10784 fragment_pipe->free_private(device);
10785 vertex_pipe->vp_free(device);
10786 HeapFree(GetProcessHeap(), 0, priv);
10787 return E_OUTOFMEMORY;
10790 /* Context activation is done by the caller. */
10791 static void shader_glsl_free(struct wined3d_device *device)
10793 struct shader_glsl_priv *priv = device->shader_priv;
10795 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
10796 constant_heap_free(&priv->pconst_heap);
10797 constant_heap_free(&priv->vconst_heap);
10798 HeapFree(GetProcessHeap(), 0, priv->stack);
10799 string_buffer_list_cleanup(&priv->string_buffers);
10800 string_buffer_free(&priv->shader_buffer);
10801 priv->fragment_pipe->free_private(device);
10802 priv->vertex_pipe->vp_free(device);
10804 HeapFree(GetProcessHeap(), 0, device->shader_priv);
10805 device->shader_priv = NULL;
10808 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
10810 struct glsl_context_data *ctx_data;
10811 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
10812 return FALSE;
10813 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10814 context->shader_backend_data = ctx_data;
10815 return TRUE;
10818 static void shader_glsl_free_context_data(struct wined3d_context *context)
10820 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
10823 static void shader_glsl_init_context_state(struct wined3d_context *context)
10825 const struct wined3d_gl_info *gl_info = context->gl_info;
10827 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
10828 checkGLcall("GL_PROGRAM_POINT_SIZE");
10831 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
10833 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
10834 && gl_info->supported[WINED3D_GL_VERSION_3_2]
10835 && gl_info->supported[ARB_SAMPLER_OBJECTS]
10836 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
10837 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
10839 if (shader_model_4
10840 && gl_info->supported[ARB_COMPUTE_SHADER]
10841 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
10842 && gl_info->supported[ARB_DRAW_INDIRECT]
10843 && gl_info->supported[ARB_GPU_SHADER5]
10844 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
10845 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
10846 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
10847 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
10848 && gl_info->supported[ARB_TESSELLATION_SHADER]
10849 && gl_info->supported[ARB_TEXTURE_GATHER]
10850 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
10851 return 5;
10853 if (shader_model_4)
10854 return 4;
10856 /* Support for texldd and texldl instructions in pixel shaders is required
10857 * for SM3. */
10858 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
10859 return 3;
10861 return 2;
10864 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
10866 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
10868 TRACE("Shader model %u.\n", shader_model);
10870 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
10871 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
10872 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
10873 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
10874 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
10875 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
10877 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
10878 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
10880 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
10881 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
10882 caps->varying_count = gl_info->limits.glsl_varyings;
10884 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
10885 * Direct3D minimum requirement.
10887 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
10888 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
10890 * The problem is that the refrast clamps temporary results in the shader to
10891 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
10892 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
10893 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
10894 * offer a way to query this.
10896 if (shader_model >= 4)
10897 caps->ps_1x_max_value = FLT_MAX;
10898 else
10899 caps->ps_1x_max_value = 1024.0f;
10901 /* Ideally we'd only set caps like sRGB writes here if supported by both
10902 * the shader backend and the fragment pipe, but we can get called before
10903 * shader_glsl_alloc(). */
10904 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
10905 | WINED3D_SHADER_CAP_SRGB_WRITE;
10908 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
10910 /* We support everything except YUV conversions. */
10911 return !is_complex_fixup(fixup);
10914 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
10916 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
10917 /* WINED3DSIH_ADD */ shader_glsl_binop,
10918 /* WINED3DSIH_AND */ shader_glsl_binop,
10919 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
10920 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
10921 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
10922 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
10923 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
10924 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
10925 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
10926 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
10927 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
10928 /* WINED3DSIH_BEM */ shader_glsl_bem,
10929 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
10930 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
10931 /* WINED3DSIH_BREAK */ shader_glsl_break,
10932 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
10933 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
10934 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
10935 /* WINED3DSIH_CALL */ shader_glsl_call,
10936 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
10937 /* WINED3DSIH_CASE */ shader_glsl_case,
10938 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
10939 /* WINED3DSIH_CND */ shader_glsl_cnd,
10940 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
10941 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
10942 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
10943 /* WINED3DSIH_CRS */ shader_glsl_cross,
10944 /* WINED3DSIH_CUT */ shader_glsl_cut,
10945 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
10946 /* WINED3DSIH_DCL */ shader_glsl_nop,
10947 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
10948 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
10949 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
10950 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
10951 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
10952 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
10953 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
10954 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
10955 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
10956 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
10957 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
10958 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
10959 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
10960 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
10961 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
10962 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
10963 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
10964 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
10965 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
10966 /* WINED3DSIH_DCL_INTERFACE */ NULL,
10967 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
10968 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
10969 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
10970 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
10971 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
10972 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
10973 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
10974 /* WINED3DSIH_DCL_STREAM */ NULL,
10975 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
10976 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
10977 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
10978 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
10979 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
10980 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
10981 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
10982 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
10983 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
10984 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
10985 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
10986 /* WINED3DSIH_DEF */ shader_glsl_nop,
10987 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
10988 /* WINED3DSIH_DEFB */ shader_glsl_nop,
10989 /* WINED3DSIH_DEFI */ shader_glsl_nop,
10990 /* WINED3DSIH_DIV */ shader_glsl_binop,
10991 /* WINED3DSIH_DP2 */ shader_glsl_dot,
10992 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
10993 /* WINED3DSIH_DP3 */ shader_glsl_dot,
10994 /* WINED3DSIH_DP4 */ shader_glsl_dot,
10995 /* WINED3DSIH_DST */ shader_glsl_dst,
10996 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
10997 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
10998 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
10999 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11000 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11001 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11002 /* WINED3DSIH_ELSE */ shader_glsl_else,
11003 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11004 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11005 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11006 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11007 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11008 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11009 /* WINED3DSIH_EQ */ shader_glsl_relop,
11010 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11011 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11012 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11013 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11014 /* WINED3DSIH_FCALL */ NULL,
11015 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11016 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11017 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11018 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11019 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11020 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11021 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11022 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11023 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11024 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11025 /* WINED3DSIH_GE */ shader_glsl_relop,
11026 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11027 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11028 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11029 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11030 /* WINED3DSIH_IADD */ shader_glsl_binop,
11031 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11032 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11033 /* WINED3DSIH_IF */ shader_glsl_if,
11034 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11035 /* WINED3DSIH_IGE */ shader_glsl_relop,
11036 /* WINED3DSIH_ILT */ shader_glsl_relop,
11037 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11038 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11039 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11040 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11041 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11042 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11043 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11044 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11045 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11046 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11047 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11048 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11049 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11050 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11051 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11052 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11053 /* WINED3DSIH_INE */ shader_glsl_relop,
11054 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11055 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11056 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11057 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11058 /* WINED3DSIH_LABEL */ shader_glsl_label,
11059 /* WINED3DSIH_LD */ shader_glsl_ld,
11060 /* WINED3DSIH_LD2DMS */ NULL,
11061 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11062 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11063 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11064 /* WINED3DSIH_LIT */ shader_glsl_lit,
11065 /* WINED3DSIH_LOD */ NULL,
11066 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11067 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11068 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11069 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11070 /* WINED3DSIH_LT */ shader_glsl_relop,
11071 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11072 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11073 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11074 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11075 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11076 /* WINED3DSIH_MAD */ shader_glsl_mad,
11077 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11078 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11079 /* WINED3DSIH_MOV */ shader_glsl_mov,
11080 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11081 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11082 /* WINED3DSIH_MUL */ shader_glsl_binop,
11083 /* WINED3DSIH_NE */ shader_glsl_relop,
11084 /* WINED3DSIH_NOP */ shader_glsl_nop,
11085 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11086 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11087 /* WINED3DSIH_OR */ shader_glsl_binop,
11088 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11089 /* WINED3DSIH_POW */ shader_glsl_pow,
11090 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11091 /* WINED3DSIH_REP */ shader_glsl_rep,
11092 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11093 /* WINED3DSIH_RET */ shader_glsl_ret,
11094 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11095 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11096 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11097 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11098 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11099 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11100 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11101 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11102 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11103 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11104 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11105 /* WINED3DSIH_SAMPLE_INFO */ NULL,
11106 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11107 /* WINED3DSIH_SAMPLE_POS */ NULL,
11108 /* WINED3DSIH_SETP */ NULL,
11109 /* WINED3DSIH_SGE */ shader_glsl_compare,
11110 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11111 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11112 /* WINED3DSIH_SLT */ shader_glsl_compare,
11113 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11114 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11115 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11116 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11117 /* WINED3DSIH_SUB */ shader_glsl_binop,
11118 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11119 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11120 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11121 /* WINED3DSIH_TEX */ shader_glsl_tex,
11122 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11123 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11124 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11125 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11126 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11127 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11128 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11129 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11130 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11131 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11132 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11133 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11134 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11135 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11136 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11137 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11138 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11139 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11140 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11141 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11142 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11143 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11144 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11145 /* WINED3DSIH_UGE */ shader_glsl_relop,
11146 /* WINED3DSIH_ULT */ shader_glsl_relop,
11147 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11148 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11149 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11150 /* WINED3DSIH_USHR */ shader_glsl_binop,
11151 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11152 /* WINED3DSIH_XOR */ shader_glsl_binop,
11155 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11156 SHADER_HANDLER hw_fct;
11158 /* Select handler */
11159 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11161 /* Unhandled opcode */
11162 if (!hw_fct)
11164 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11165 return;
11167 hw_fct(ins);
11169 shader_glsl_add_instruction_modifiers(ins);
11172 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11174 struct shader_glsl_priv *priv = shader_priv;
11176 return priv->ffp_proj_control;
11179 const struct wined3d_shader_backend_ops glsl_shader_backend =
11181 shader_glsl_handle_instruction,
11182 shader_glsl_precompile,
11183 shader_glsl_select,
11184 shader_glsl_select_compute,
11185 shader_glsl_disable,
11186 shader_glsl_update_float_vertex_constants,
11187 shader_glsl_update_float_pixel_constants,
11188 shader_glsl_load_constants,
11189 shader_glsl_destroy,
11190 shader_glsl_alloc,
11191 shader_glsl_free,
11192 shader_glsl_allocate_context_data,
11193 shader_glsl_free_context_data,
11194 shader_glsl_init_context_state,
11195 shader_glsl_get_caps,
11196 shader_glsl_color_fixup_supported,
11197 shader_glsl_has_ffp_proj_control,
11200 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11202 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11204 caps->xyzrhw = TRUE;
11205 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11206 caps->ffp_generic_attributes = TRUE;
11207 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11208 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11209 caps->max_vertex_blend_matrix_index = 0;
11210 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11211 | WINED3DVTXPCAPS_MATERIALSOURCE7
11212 | WINED3DVTXPCAPS_VERTEXFOG
11213 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11214 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11215 | WINED3DVTXPCAPS_LOCALVIEWER
11216 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11217 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11218 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11219 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11222 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11224 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11225 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11226 return 0;
11229 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11231 struct shader_glsl_priv *priv;
11233 if (shader_backend == &glsl_shader_backend)
11235 priv = shader_priv;
11236 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11237 return priv;
11240 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11242 return NULL;
11245 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11247 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11248 struct glsl_ffp_vertex_shader, desc.entry);
11249 struct glsl_shader_prog_link *program, *program2;
11250 struct glsl_ffp_destroy_ctx *ctx = context;
11252 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11253 struct glsl_shader_prog_link, vs.shader_entry)
11255 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11257 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11258 HeapFree(GetProcessHeap(), 0, shader);
11261 /* Context activation is done by the caller. */
11262 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11264 struct shader_glsl_priv *priv = device->vertex_priv;
11265 struct glsl_ffp_destroy_ctx ctx;
11267 ctx.priv = priv;
11268 ctx.gl_info = &device->adapter->gl_info;
11269 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11272 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11273 const struct wined3d_state *state, DWORD state_id) {}
11275 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11276 const struct wined3d_state *state, DWORD state_id)
11278 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11281 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11282 const struct wined3d_state *state, DWORD state_id)
11284 const struct wined3d_gl_info *gl_info = context->gl_info;
11285 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11286 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11287 BOOL transformed = context->stream_info.position_transformed;
11288 BOOL wasrhw = context->last_was_rhw;
11289 unsigned int i;
11291 context->last_was_rhw = transformed;
11293 /* If the vertex declaration contains a transformed position attribute,
11294 * the draw uses the fixed function vertex pipeline regardless of any
11295 * vertex shader set by the application. */
11296 if (transformed != wasrhw
11297 || context->stream_info.swizzle_map != context->last_swizzle_map)
11298 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11300 context->last_swizzle_map = context->stream_info.swizzle_map;
11302 if (!use_vs(state))
11304 if (context->last_was_vshader)
11306 if (legacy_clip_planes)
11307 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11308 clipplane(context, state, STATE_CLIPPLANE(i));
11309 else
11310 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11313 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11315 /* Because of settings->texcoords, we have to regenerate the vertex
11316 * shader on a vdecl change if there aren't enough varyings to just
11317 * always output all the texture coordinates. */
11318 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11319 || normal != context->last_was_normal)
11320 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11322 if (use_ps(state)
11323 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11324 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11325 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11327 else
11329 if (!context->last_was_vshader)
11331 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11332 if (legacy_clip_planes)
11333 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11334 clipplane(context, state, STATE_CLIPPLANE(i));
11335 else
11336 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11340 context->last_was_vshader = use_vs(state);
11341 context->last_was_normal = normal;
11344 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11345 const struct wined3d_state *state, DWORD state_id)
11347 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11348 /* Different vertex shaders potentially require a different vertex attributes setup. */
11349 if (!isStateDirty(context, STATE_VDECL))
11350 context_apply_state(context, state, STATE_VDECL);
11353 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11354 const struct wined3d_state *state, DWORD state_id)
11356 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11357 * winding) are defined in Hull Shaders, while in GLSL those are
11358 * specified in Tessellation Evaluation Shaders. */
11359 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11361 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11362 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11365 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11366 const struct wined3d_state *state, DWORD state_id)
11368 struct glsl_context_data *ctx_data = context->shader_backend_data;
11369 BOOL rasterization_disabled;
11371 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11372 if (ctx_data->rasterization_disabled != rasterization_disabled)
11373 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11374 ctx_data->rasterization_disabled = rasterization_disabled;
11376 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11377 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11378 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11379 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11380 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11383 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11384 const struct wined3d_state *state, DWORD state_id)
11386 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11387 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11388 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11389 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11390 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11391 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11392 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11395 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11396 const struct wined3d_state *state, DWORD state_id)
11398 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11401 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11402 const struct wined3d_state *state, DWORD state_id)
11404 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11407 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11409 const struct wined3d_gl_info *gl_info = context->gl_info;
11410 unsigned int k;
11412 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11413 | WINED3D_SHADER_CONST_FFP_LIGHTS
11414 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11416 if (needs_legacy_glsl_syntax(gl_info))
11418 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11420 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11421 clipplane(context, state, STATE_CLIPPLANE(k));
11424 else
11426 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11430 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11431 const struct wined3d_state *state, DWORD state_id)
11433 /* Table fog behavior depends on the projection matrix. */
11434 if (state->render_states[WINED3D_RS_FOGENABLE]
11435 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11436 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11437 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11440 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11441 const struct wined3d_state *state, DWORD state_id)
11443 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11444 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11445 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11446 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11447 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11448 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11451 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11452 const struct wined3d_state *state, DWORD state_id)
11454 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11457 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11458 const struct wined3d_state *state, DWORD state_id)
11460 DWORD sampler = state_id - STATE_SAMPLER(0);
11461 const struct wined3d_texture *texture = state->textures[sampler];
11462 BOOL np2;
11464 if (!texture)
11465 return;
11467 if (sampler >= MAX_TEXTURES)
11468 return;
11470 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11471 || context->lastWasPow2Texture & (1u << sampler))
11473 if (np2)
11474 context->lastWasPow2Texture |= 1u << sampler;
11475 else
11476 context->lastWasPow2Texture &= ~(1u << sampler);
11478 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11482 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11483 const struct wined3d_state *state, DWORD state_id)
11485 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11488 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11489 const struct wined3d_state *state, DWORD state_id)
11491 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11494 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11495 const struct wined3d_state *state, DWORD state_id)
11497 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11500 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11501 const struct wined3d_state *state, DWORD state_id)
11503 if (!use_vs(state))
11504 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11507 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11508 const struct wined3d_state *state, DWORD state_id)
11510 static unsigned int once;
11512 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11513 FIXME("Non-point sprite points not supported in core profile.\n");
11516 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11517 const struct wined3d_state *state, DWORD state_id)
11519 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11522 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11523 const struct wined3d_state *state, DWORD state_id)
11525 const struct wined3d_gl_info *gl_info = context->gl_info;
11526 UINT index = state_id - STATE_CLIPPLANE(0);
11528 if (index >= gl_info->limits.user_clip_distances)
11529 return;
11531 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11534 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11536 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11537 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11538 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11539 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11540 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11541 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11542 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11543 /* Clip planes */
11544 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11545 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11546 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11547 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11548 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11549 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11550 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11551 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11552 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11553 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11554 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11555 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11556 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11557 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11558 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11559 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11560 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11561 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
11562 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11563 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
11564 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11565 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
11566 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11567 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
11568 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11569 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
11570 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11571 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
11572 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11573 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
11574 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11575 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
11576 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11577 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
11578 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11579 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
11580 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11581 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
11582 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11583 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
11584 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11585 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
11586 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11587 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
11588 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11589 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
11590 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11591 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
11592 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11593 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
11594 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11595 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
11596 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11597 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
11598 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11599 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
11600 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11601 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
11602 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11603 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
11604 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11605 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
11606 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11607 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
11608 /* Lights */
11609 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11610 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11611 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11612 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11613 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11614 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11615 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11616 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11617 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11618 /* Viewport */
11619 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11620 /* Transform states */
11621 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11622 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11623 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11624 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11625 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11626 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11627 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11628 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11629 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11630 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11631 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11632 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11633 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11634 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11635 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11636 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11637 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11638 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11639 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11640 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11641 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11642 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11643 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11644 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11645 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11646 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11647 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11648 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11649 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11650 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11651 /* Fog */
11652 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11653 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11654 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11655 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11656 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11657 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11658 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11659 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11660 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11661 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11662 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11663 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11664 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11665 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11666 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11667 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11668 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11669 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11670 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11671 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11672 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11673 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11674 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11675 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11676 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11677 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11678 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11679 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11680 /* NP2 texture matrix fixups. They are not needed if
11681 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11682 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11683 * matrix. */
11684 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11685 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11686 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11687 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11688 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11689 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11690 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11691 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11692 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11693 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11694 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11695 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11696 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11697 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11698 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11699 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11700 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11701 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11702 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11703 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11704 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11705 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11706 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11707 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11708 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11709 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11710 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11711 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11714 /* TODO:
11715 * - Implement vertex tweening. */
11716 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11718 glsl_vertex_pipe_vp_enable,
11719 glsl_vertex_pipe_vp_get_caps,
11720 glsl_vertex_pipe_vp_get_emul_mask,
11721 glsl_vertex_pipe_vp_alloc,
11722 glsl_vertex_pipe_vp_free,
11723 glsl_vertex_pipe_vp_states,
11726 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
11728 /* Nothing to do. */
11731 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
11733 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11734 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11735 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11736 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11737 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11738 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11739 | WINED3DTEXOPCAPS_SELECTARG1
11740 | WINED3DTEXOPCAPS_SELECTARG2
11741 | WINED3DTEXOPCAPS_MODULATE4X
11742 | WINED3DTEXOPCAPS_MODULATE2X
11743 | WINED3DTEXOPCAPS_MODULATE
11744 | WINED3DTEXOPCAPS_ADDSIGNED2X
11745 | WINED3DTEXOPCAPS_ADDSIGNED
11746 | WINED3DTEXOPCAPS_ADD
11747 | WINED3DTEXOPCAPS_SUBTRACT
11748 | WINED3DTEXOPCAPS_ADDSMOOTH
11749 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11750 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11751 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11752 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11753 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
11754 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
11755 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
11756 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
11757 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
11758 | WINED3DTEXOPCAPS_DOTPRODUCT3
11759 | WINED3DTEXOPCAPS_MULTIPLYADD
11760 | WINED3DTEXOPCAPS_LERP
11761 | WINED3DTEXOPCAPS_BUMPENVMAP
11762 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
11763 caps->MaxTextureBlendStages = MAX_TEXTURES;
11764 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
11767 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
11769 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11770 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11771 return 0;
11774 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11776 struct shader_glsl_priv *priv;
11778 if (shader_backend == &glsl_shader_backend)
11780 priv = shader_priv;
11781 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
11782 return priv;
11785 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
11787 return NULL;
11790 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
11792 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11793 struct glsl_ffp_fragment_shader, entry.entry);
11794 struct glsl_shader_prog_link *program, *program2;
11795 struct glsl_ffp_destroy_ctx *ctx = context;
11797 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11798 struct glsl_shader_prog_link, ps.shader_entry)
11800 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11802 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11803 HeapFree(GetProcessHeap(), 0, shader);
11806 /* Context activation is done by the caller. */
11807 static void glsl_fragment_pipe_free(struct wined3d_device *device)
11809 struct shader_glsl_priv *priv = device->fragment_priv;
11810 struct glsl_ffp_destroy_ctx ctx;
11812 ctx.priv = priv;
11813 ctx.gl_info = &device->adapter->gl_info;
11814 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
11817 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
11818 const struct wined3d_state *state, DWORD state_id)
11820 context->last_was_pshader = use_ps(state);
11822 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11825 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
11826 const struct wined3d_state *state, DWORD state_id)
11828 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11831 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
11832 const struct wined3d_state *state, DWORD state_id)
11834 BOOL use_vshader = use_vs(state);
11835 enum fogsource new_source;
11836 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
11837 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
11839 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11841 if (!state->render_states[WINED3D_RS_FOGENABLE])
11842 return;
11844 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
11846 if (use_vshader)
11847 new_source = FOGSOURCE_VS;
11848 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
11849 new_source = FOGSOURCE_COORD;
11850 else
11851 new_source = FOGSOURCE_FFP;
11853 else
11855 new_source = FOGSOURCE_FFP;
11858 if (new_source != context->fog_source || fogstart == fogend)
11860 context->fog_source = new_source;
11861 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11865 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
11866 const struct wined3d_state *state, DWORD state_id)
11868 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11869 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11870 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11872 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
11873 glsl_fragment_pipe_fog(context, state, state_id);
11876 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
11877 const struct wined3d_state *state, DWORD state_id)
11879 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11880 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11881 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11884 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
11885 const struct wined3d_state *state, DWORD state_id)
11887 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11890 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
11891 const struct wined3d_state *state, DWORD state_id)
11893 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
11896 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
11897 const struct wined3d_state *state, DWORD state_id)
11899 const struct wined3d_gl_info *gl_info = context->gl_info;
11900 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
11901 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
11903 if (func)
11905 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
11906 checkGLcall("glAlphaFunc");
11910 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
11911 const struct wined3d_state *state, DWORD state_id)
11913 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11916 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
11917 const struct wined3d_state *state, DWORD state_id)
11919 const struct wined3d_gl_info *gl_info = context->gl_info;
11921 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
11923 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
11924 checkGLcall("glEnable(GL_ALPHA_TEST)");
11926 else
11928 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
11929 checkGLcall("glDisable(GL_ALPHA_TEST)");
11933 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
11934 const struct wined3d_state *state, DWORD state_id)
11936 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
11939 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
11940 const struct wined3d_state *state, DWORD state_id)
11942 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
11945 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
11946 const struct wined3d_state *state, DWORD state_id)
11948 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11951 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
11953 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11954 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
11955 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11956 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11957 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11958 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11959 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11960 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11961 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11962 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11963 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11964 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11965 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11966 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11967 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11968 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11969 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11970 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11971 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11972 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11973 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11974 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11975 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11976 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11977 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11978 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11979 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11980 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11981 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11982 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11983 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11984 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11985 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11986 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11987 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11988 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11989 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11990 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11991 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11992 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11993 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11994 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11995 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11996 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11997 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11998 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11999 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12000 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12001 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12002 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12003 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12004 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12005 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12006 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12007 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12008 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12009 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12010 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12011 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12012 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12013 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12014 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12015 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12016 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12017 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12018 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12019 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12020 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12021 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12022 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12023 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12024 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12025 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12026 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12027 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12028 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12029 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12030 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12031 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12032 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12033 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12034 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12035 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12036 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12037 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12038 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12039 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12040 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12041 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12042 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12043 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12044 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12045 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12046 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12047 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12048 {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 },
12049 {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 },
12050 {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 },
12051 {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 },
12052 {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 },
12053 {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 },
12054 {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 },
12055 {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 },
12056 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12057 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12058 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12059 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12060 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12061 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12062 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12063 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12064 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12065 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12066 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12067 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12068 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12071 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12073 return TRUE;
12076 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12080 const struct fragment_pipeline glsl_fragment_pipe =
12082 glsl_fragment_pipe_enable,
12083 glsl_fragment_pipe_get_caps,
12084 glsl_fragment_pipe_get_emul_mask,
12085 glsl_fragment_pipe_alloc,
12086 glsl_fragment_pipe_free,
12087 glsl_fragment_pipe_alloc_context_data,
12088 glsl_fragment_pipe_free_context_data,
12089 shader_glsl_color_fixup_supported,
12090 glsl_fragment_pipe_state_template,