wined3d: Disable pixel shader when rasterization is disabled.
[wine.git] / dlls / wined3d / glsl_shader.c
blob11d91462d2d638c24d44ba58f3316bc8da12035b
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;
271 struct glsl_ps_compiled_shader
273 struct ps_compile_args args;
274 struct ps_np2fixup_info np2fixup;
275 GLuint id;
278 struct glsl_vs_compiled_shader
280 struct vs_compile_args args;
281 GLuint id;
284 struct glsl_hs_compiled_shader
286 GLuint id;
289 struct glsl_ds_compiled_shader
291 struct ds_compile_args args;
292 GLuint id;
295 struct glsl_gs_compiled_shader
297 struct gs_compile_args args;
298 GLuint id;
301 struct glsl_cs_compiled_shader
303 GLuint id;
306 struct glsl_shader_private
308 union
310 struct glsl_vs_compiled_shader *vs;
311 struct glsl_hs_compiled_shader *hs;
312 struct glsl_ds_compiled_shader *ds;
313 struct glsl_gs_compiled_shader *gs;
314 struct glsl_ps_compiled_shader *ps;
315 struct glsl_cs_compiled_shader *cs;
316 } gl_shaders;
317 unsigned int num_gl_shaders, shader_array_size;
320 struct glsl_ffp_vertex_shader
322 struct wined3d_ffp_vs_desc desc;
323 GLuint id;
324 struct list linked_programs;
327 struct glsl_ffp_fragment_shader
329 struct ffp_frag_desc entry;
330 GLuint id;
331 struct list linked_programs;
334 struct glsl_ffp_destroy_ctx
336 struct shader_glsl_priv *priv;
337 const struct wined3d_gl_info *gl_info;
340 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
342 static const char *debug_gl_shader_type(GLenum type)
344 switch (type)
346 #define WINED3D_TO_STR(u) case u: return #u
347 WINED3D_TO_STR(GL_VERTEX_SHADER);
348 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
349 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
350 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
351 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
352 WINED3D_TO_STR(GL_COMPUTE_SHADER);
353 #undef WINED3D_TO_STR
354 default:
355 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
359 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
361 switch (type)
363 case WINED3D_SHADER_TYPE_VERTEX:
364 return "vs";
366 case WINED3D_SHADER_TYPE_HULL:
367 return "hs";
369 case WINED3D_SHADER_TYPE_DOMAIN:
370 return "ds";
372 case WINED3D_SHADER_TYPE_GEOMETRY:
373 return "gs";
375 case WINED3D_SHADER_TYPE_PIXEL:
376 return "ps";
378 case WINED3D_SHADER_TYPE_COMPUTE:
379 return "cs";
381 default:
382 FIXME("Unhandled shader type %#x.\n", type);
383 return "unknown";
387 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
389 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
390 return 440;
391 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
392 return 150;
393 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
394 return 130;
395 else
396 return 120;
399 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
400 const struct wined3d_gl_info *gl_info)
402 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
405 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
407 char str[4][17];
409 wined3d_ftoa(values[0], str[0]);
410 wined3d_ftoa(values[1], str[1]);
411 wined3d_ftoa(values[2], str[2]);
412 wined3d_ftoa(values[3], str[3]);
413 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
416 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
417 const int *values, unsigned int size)
419 int i;
421 if (!size || size > 4)
423 ERR("Invalid vector size %u.\n", size);
424 return;
427 if (size > 1)
428 shader_addline(buffer, "ivec%u(", size);
430 for (i = 0; i < size; ++i)
431 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
433 if (size > 1)
434 shader_addline(buffer, ")");
437 static const char *get_info_log_line(const char **ptr)
439 const char *p, *q;
441 p = *ptr;
442 if (!(q = strstr(p, "\n")))
444 if (!*p) return NULL;
445 *ptr += strlen(p);
446 return p;
448 *ptr = q + 1;
450 return p;
453 /* Context activation is done by the caller. */
454 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
456 int length = 0;
457 char *log;
459 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
460 return;
462 if (program)
463 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
464 else
465 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
467 /* A size of 1 is just a null-terminated string, so the log should be bigger than
468 * that if there are errors. */
469 if (length > 1)
471 const char *ptr, *line;
473 log = HeapAlloc(GetProcessHeap(), 0, length);
474 /* The info log is supposed to be zero-terminated, but at least some
475 * versions of fglrx don't terminate the string properly. The reported
476 * length does include the terminator, so explicitly set it to zero
477 * here. */
478 log[length - 1] = 0;
479 if (program)
480 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
481 else
482 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
484 ptr = log;
485 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
487 WARN("Info log received from GLSL shader #%u:\n", id);
488 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
490 else
492 FIXME("Info log received from GLSL shader #%u:\n", id);
493 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
495 HeapFree(GetProcessHeap(), 0, log);
499 /* Context activation is done by the caller. */
500 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
502 const char *ptr, *line;
504 TRACE("Compiling shader object %u.\n", shader);
506 if (TRACE_ON(d3d_shader))
508 ptr = src;
509 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
512 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
513 checkGLcall("glShaderSource");
514 GL_EXTCALL(glCompileShader(shader));
515 checkGLcall("glCompileShader");
516 print_glsl_info_log(gl_info, shader, FALSE);
519 /* Context activation is done by the caller. */
520 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
522 GLint i, shader_count, source_size = -1;
523 GLuint *shaders;
524 char *source = NULL;
526 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
527 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
529 ERR("Failed to allocate shader array memory.\n");
530 return;
533 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
534 for (i = 0; i < shader_count; ++i)
536 const char *ptr, *line;
537 GLint tmp;
539 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
541 if (source_size < tmp)
543 HeapFree(GetProcessHeap(), 0, source);
545 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
546 if (!source)
548 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
549 HeapFree(GetProcessHeap(), 0, shaders);
550 return;
552 source_size = tmp;
555 FIXME("Shader %u:\n", shaders[i]);
556 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
557 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
558 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
559 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
560 FIXME("\n");
562 ptr = source;
563 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
564 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
565 FIXME("\n");
568 HeapFree(GetProcessHeap(), 0, source);
569 HeapFree(GetProcessHeap(), 0, shaders);
572 /* Context activation is done by the caller. */
573 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
575 GLint tmp;
577 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
578 return;
580 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
581 if (!tmp)
583 FIXME("Program %u link status invalid.\n", program);
584 shader_glsl_dump_program_source(gl_info, program);
587 print_glsl_info_log(gl_info, program, TRUE);
590 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
592 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
593 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
594 * versions. */
595 return shader_glsl_get_version(gl_info) >= 140;
598 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
600 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
603 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
604 struct shader_glsl_priv *priv, GLuint program_id,
605 const struct wined3d_shader_reg_maps *reg_maps)
607 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
608 struct wined3d_string_buffer *name;
609 unsigned int i, base, count;
610 GLuint block_idx;
612 if (shader_glsl_use_layout_binding_qualifier(gl_info))
613 return;
615 name = string_buffer_get(&priv->string_buffers);
616 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
617 for (i = 0; i < count; ++i)
619 if (!reg_maps->cb_sizes[i])
620 continue;
622 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
623 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
624 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
626 checkGLcall("glUniformBlockBinding");
627 string_buffer_release(&priv->string_buffers, name);
630 /* Context activation is done by the caller. */
631 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
632 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
633 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
635 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
636 unsigned int i, mapped_unit;
637 GLint name_loc;
639 for (i = 0; i < count; ++i)
641 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
642 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
643 if (name_loc == -1)
644 continue;
646 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
647 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
649 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
650 continue;
653 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
654 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
656 checkGLcall("Load sampler bindings");
657 string_buffer_release(&priv->string_buffers, sampler_name);
660 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
661 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
663 const DWORD *tex_unit_map;
664 unsigned int base, count;
666 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
667 if (sampler_idx >= count)
668 return WINED3D_UNMAPPED_STAGE;
669 if (!tex_unit_map)
670 return base + sampler_idx;
671 return tex_unit_map[base + sampler_idx];
674 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
675 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
676 unsigned int sampler_idx)
678 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
679 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
680 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
681 else
682 ERR("Unmapped sampler %u.\n", sampler_idx);
685 /* Context activation is done by the caller. */
686 static void shader_glsl_load_samplers(const struct wined3d_context *context,
687 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
689 const struct wined3d_gl_info *gl_info = context->gl_info;
690 const struct wined3d_shader_version *shader_version;
691 const DWORD *tex_unit_map;
692 unsigned int base, count;
693 const char *prefix;
695 if (shader_glsl_use_layout_binding_qualifier(gl_info))
696 return;
698 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
699 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
700 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
701 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
704 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
705 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
707 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
709 if (icb)
711 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
712 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
713 GLint icb_location;
715 string_buffer_sprintf(icb_name, "%s_icb", prefix);
716 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
717 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
718 checkGLcall("Load immediate constant buffer");
720 string_buffer_release(&priv->string_buffers, icb_name);
724 /* Context activation is done by the caller. */
725 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
726 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
728 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
729 struct wined3d_string_buffer *name;
730 GLint location;
731 unsigned int i;
733 if (shader_glsl_use_layout_binding_qualifier(gl_info))
734 return;
736 name = string_buffer_get(&priv->string_buffers);
737 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
739 if (!reg_maps->uav_resource_info[i].type)
740 continue;
742 string_buffer_sprintf(name, "%s_image%u", prefix, i);
743 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
744 if (location == -1)
745 continue;
747 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
748 GL_EXTCALL(glUniform1i(location, i));
750 checkGLcall("Load image bindings");
751 string_buffer_release(&priv->string_buffers, name);
754 /* Context activation is done by the caller. */
755 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
756 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
758 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
760 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
761 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
762 /* Texture unit mapping is set up to be the same each time the shader
763 * program is used so we can hardcode the sampler uniform values. */
764 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
767 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
768 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
770 if (varyings && *strings)
772 char *ptr = *strings;
774 varyings[*varying_count] = ptr;
776 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
777 ptr += buffer->content_size + 1;
779 *strings = ptr;
782 *strings_length += buffer->content_size + 1;
783 ++(*varying_count);
786 static void append_transform_feedback_skip_components(const char **varyings,
787 unsigned int *varying_count, char **strings, unsigned int *strings_length,
788 struct wined3d_string_buffer *buffer, unsigned int component_count)
790 unsigned int j;
792 for (j = 0; j < component_count / 4; ++j)
794 string_buffer_sprintf(buffer, "gl_SkipComponents4");
795 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
797 if (component_count % 4)
799 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
800 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
804 static void shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
805 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
806 char *strings, unsigned int *strings_length, GLenum buffer_mode)
808 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
810 count = length = 0;
811 highest_output_slot = 0;
812 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
814 stride = 0;
816 for (i = 0; i < so_desc->element_count; ++i)
818 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
820 highest_output_slot = max(highest_output_slot, e->output_slot);
821 if (e->output_slot != buffer_idx)
822 continue;
824 if (e->stream_idx)
826 FIXME("Unhandled stream %u.\n", e->stream_idx);
827 continue;
830 stride += e->component_count;
832 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
834 append_transform_feedback_skip_components(varyings, &count,
835 &strings, &length, buffer, e->component_count);
836 continue;
839 if (e->component_idx || e->component_count != 4)
841 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
842 append_transform_feedback_skip_components(varyings, &count,
843 &strings, &length, buffer, e->component_count);
844 continue;
847 string_buffer_sprintf(buffer, "shader_in_out.reg%u", e->register_idx);
848 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
851 if (buffer_idx < so_desc->buffer_stride_count
852 && stride < so_desc->buffer_strides[buffer_idx] / 4)
854 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
855 append_transform_feedback_skip_components(varyings, &count,
856 &strings, &length, buffer, component_count);
859 if (highest_output_slot <= buffer_idx)
860 break;
862 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
864 string_buffer_sprintf(buffer, "gl_NextBuffer");
865 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
869 if (varying_count)
870 *varying_count = count;
871 if (strings_length)
872 *strings_length = length;
875 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
876 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
878 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
879 const struct wined3d_gl_info *gl_info = context->gl_info;
880 struct wined3d_string_buffer *buffer;
881 unsigned int i, count, length;
882 const char **varyings;
883 char *strings;
884 GLenum mode;
886 if (!so_desc->element_count)
887 return;
889 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
891 mode = GL_INTERLEAVED_ATTRIBS;
893 else
895 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
897 for (i = 0; i < so_desc->element_count; ++i)
899 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
901 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
902 return;
904 ++element_count[so_desc->elements[i].output_slot];
907 if (element_count[0] == so_desc->element_count)
909 mode = GL_INTERLEAVED_ATTRIBS;
911 else
913 mode = GL_SEPARATE_ATTRIBS;
914 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
916 if (element_count[i] != 1)
917 break;
919 for (; i < ARRAY_SIZE(element_count); ++i)
921 if (element_count[i])
923 FIXME("Only single element per buffer is allowed in separate mode.\n");
924 return;
930 buffer = string_buffer_get(&priv->string_buffers);
932 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode);
934 if (!(varyings = wined3d_calloc(count, sizeof(*varyings))))
936 ERR("Out of memory.\n");
937 string_buffer_release(&priv->string_buffers, buffer);
938 return;
940 if (!(strings = wined3d_calloc(length, sizeof(*strings))))
942 ERR("Out of memory.\n");
943 HeapFree(GetProcessHeap(), 0, varyings);
944 string_buffer_release(&priv->string_buffers, buffer);
945 return;
948 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
949 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
950 checkGLcall("glTransformFeedbackVaryings");
952 HeapFree(GetProcessHeap(), 0, varyings);
953 HeapFree(GetProcessHeap(), 0, strings);
954 string_buffer_release(&priv->string_buffers, buffer);
957 /* Context activation is done by the caller. */
958 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
959 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
961 unsigned int start = ~0U, end = 0;
962 int stack_idx = 0;
963 unsigned int heap_idx = 1;
964 unsigned int idx;
966 if (heap->entries[heap_idx].version <= version) return;
968 idx = heap->entries[heap_idx].idx;
969 if (constant_locations[idx] != -1)
970 start = end = idx;
971 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
973 while (stack_idx >= 0)
975 /* Note that we fall through to the next case statement. */
976 switch(stack[stack_idx])
978 case HEAP_NODE_TRAVERSE_LEFT:
980 unsigned int left_idx = heap_idx << 1;
981 if (left_idx < heap->size && heap->entries[left_idx].version > version)
983 heap_idx = left_idx;
984 idx = heap->entries[heap_idx].idx;
985 if (constant_locations[idx] != -1)
987 if (start > idx)
988 start = idx;
989 if (end < idx)
990 end = idx;
993 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
994 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
995 break;
999 case HEAP_NODE_TRAVERSE_RIGHT:
1001 unsigned int right_idx = (heap_idx << 1) + 1;
1002 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1004 heap_idx = right_idx;
1005 idx = heap->entries[heap_idx].idx;
1006 if (constant_locations[idx] != -1)
1008 if (start > idx)
1009 start = idx;
1010 if (end < idx)
1011 end = idx;
1014 stack[stack_idx++] = HEAP_NODE_POP;
1015 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1016 break;
1020 case HEAP_NODE_POP:
1021 heap_idx >>= 1;
1022 --stack_idx;
1023 break;
1026 if (start <= end)
1027 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1028 checkGLcall("walk_constant_heap()");
1031 /* Context activation is done by the caller. */
1032 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1033 GLint location, const struct wined3d_vec4 *data)
1035 GLfloat clamped_constant[4];
1037 if (location == -1) return;
1039 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1040 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1041 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1042 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1044 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1047 /* Context activation is done by the caller. */
1048 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1049 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1050 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1052 int stack_idx = 0;
1053 unsigned int heap_idx = 1;
1054 unsigned int idx;
1056 if (heap->entries[heap_idx].version <= version) return;
1058 idx = heap->entries[heap_idx].idx;
1059 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1060 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1062 while (stack_idx >= 0)
1064 /* Note that we fall through to the next case statement. */
1065 switch(stack[stack_idx])
1067 case HEAP_NODE_TRAVERSE_LEFT:
1069 unsigned int left_idx = heap_idx << 1;
1070 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1072 heap_idx = left_idx;
1073 idx = heap->entries[heap_idx].idx;
1074 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1076 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1077 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1078 break;
1082 case HEAP_NODE_TRAVERSE_RIGHT:
1084 unsigned int right_idx = (heap_idx << 1) + 1;
1085 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1087 heap_idx = right_idx;
1088 idx = heap->entries[heap_idx].idx;
1089 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1091 stack[stack_idx++] = HEAP_NODE_POP;
1092 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1093 break;
1097 case HEAP_NODE_POP:
1098 heap_idx >>= 1;
1099 --stack_idx;
1100 break;
1103 checkGLcall("walk_constant_heap_clamped()");
1106 /* Context activation is done by the caller. */
1107 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1108 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1109 unsigned char *stack, unsigned int version)
1111 const struct wined3d_shader_lconst *lconst;
1113 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1114 if (shader->reg_maps.shader_version.major == 1
1115 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1116 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1117 else
1118 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1120 if (!shader->load_local_constsF)
1122 TRACE("No need to load local float constants for this shader.\n");
1123 return;
1126 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1127 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1129 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1131 checkGLcall("glUniform4fv()");
1134 /* Context activation is done by the caller. */
1135 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1136 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1138 unsigned int i;
1139 struct list* ptr;
1141 for (i = 0; constants_set; constants_set >>= 1, ++i)
1143 if (!(constants_set & 1)) continue;
1145 /* We found this uniform name in the program - go ahead and send the data */
1146 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1149 /* Load immediate constants */
1150 ptr = list_head(&shader->constantsI);
1151 while (ptr)
1153 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1154 unsigned int idx = lconst->idx;
1155 const GLint *values = (const GLint *)lconst->value;
1157 /* We found this uniform name in the program - go ahead and send the data */
1158 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1159 ptr = list_next(&shader->constantsI, ptr);
1161 checkGLcall("glUniform4iv()");
1164 /* Context activation is done by the caller. */
1165 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1166 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1168 unsigned int i;
1169 struct list* ptr;
1171 for (i = 0; constants_set; constants_set >>= 1, ++i)
1173 if (!(constants_set & 1)) continue;
1175 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1178 /* Load immediate constants */
1179 ptr = list_head(&shader->constantsB);
1180 while (ptr)
1182 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1183 unsigned int idx = lconst->idx;
1184 const GLint *values = (const GLint *)lconst->value;
1186 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1187 ptr = list_next(&shader->constantsB, ptr);
1189 checkGLcall("glUniform1iv()");
1192 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1194 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1197 /* Context activation is done by the caller (state handler). */
1198 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1199 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1201 struct
1203 float sx, sy;
1205 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1206 UINT fixup = ps->np2_fixup_info->active;
1207 UINT i;
1209 for (i = 0; fixup; fixup >>= 1, ++i)
1211 const struct wined3d_texture *tex = state->textures[i];
1212 unsigned char idx = ps->np2_fixup_info->idx[i];
1214 if (!tex)
1216 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1217 continue;
1220 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1221 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1224 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1227 /* Taken and adapted from Mesa. */
1228 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1230 float pos, neg, t, det;
1231 struct wined3d_matrix temp;
1233 /* Calculate the determinant of upper left 3x3 submatrix and
1234 * determine if the matrix is singular. */
1235 pos = neg = 0.0f;
1236 t = in->_11 * in->_22 * in->_33;
1237 if (t >= 0.0f)
1238 pos += t;
1239 else
1240 neg += t;
1242 t = in->_21 * in->_32 * in->_13;
1243 if (t >= 0.0f)
1244 pos += t;
1245 else
1246 neg += t;
1247 t = in->_31 * in->_12 * in->_23;
1248 if (t >= 0.0f)
1249 pos += t;
1250 else
1251 neg += t;
1253 t = -in->_31 * in->_22 * in->_13;
1254 if (t >= 0.0f)
1255 pos += t;
1256 else
1257 neg += t;
1258 t = -in->_21 * in->_12 * in->_33;
1259 if (t >= 0.0f)
1260 pos += t;
1261 else
1262 neg += t;
1264 t = -in->_11 * in->_32 * in->_23;
1265 if (t >= 0.0f)
1266 pos += t;
1267 else
1268 neg += t;
1270 det = pos + neg;
1272 if (fabsf(det) < 1e-25f)
1273 return FALSE;
1275 det = 1.0f / det;
1276 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1277 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1278 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1279 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1280 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1281 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1282 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1283 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1284 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1286 *out = temp;
1287 return TRUE;
1290 static void swap_rows(float **a, float **b)
1292 float *tmp = *a;
1294 *a = *b;
1295 *b = tmp;
1298 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
1300 float wtmp[4][8];
1301 float m0, m1, m2, m3, s;
1302 float *r0, *r1, *r2, *r3;
1304 r0 = wtmp[0];
1305 r1 = wtmp[1];
1306 r2 = wtmp[2];
1307 r3 = wtmp[3];
1309 r0[0] = m->_11;
1310 r0[1] = m->_12;
1311 r0[2] = m->_13;
1312 r0[3] = m->_14;
1313 r0[4] = 1.0f;
1314 r0[5] = r0[6] = r0[7] = 0.0f;
1316 r1[0] = m->_21;
1317 r1[1] = m->_22;
1318 r1[2] = m->_23;
1319 r1[3] = m->_24;
1320 r1[5] = 1.0f;
1321 r1[4] = r1[6] = r1[7] = 0.0f;
1323 r2[0] = m->_31;
1324 r2[1] = m->_32;
1325 r2[2] = m->_33;
1326 r2[3] = m->_34;
1327 r2[6] = 1.0f;
1328 r2[4] = r2[5] = r2[7] = 0.0f;
1330 r3[0] = m->_41;
1331 r3[1] = m->_42;
1332 r3[2] = m->_43;
1333 r3[3] = m->_44;
1334 r3[7] = 1.0f;
1335 r3[4] = r3[5] = r3[6] = 0.0f;
1337 /* Choose pivot - or die. */
1338 if (fabsf(r3[0]) > fabsf(r2[0]))
1339 swap_rows(&r3, &r2);
1340 if (fabsf(r2[0]) > fabsf(r1[0]))
1341 swap_rows(&r2, &r1);
1342 if (fabsf(r1[0]) > fabsf(r0[0]))
1343 swap_rows(&r1, &r0);
1344 if (r0[0] == 0.0f)
1345 return FALSE;
1347 /* Eliminate first variable. */
1348 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1349 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1350 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1351 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1352 s = r0[4];
1353 if (s != 0.0f)
1355 r1[4] -= m1 * s;
1356 r2[4] -= m2 * s;
1357 r3[4] -= m3 * s;
1359 s = r0[5];
1360 if (s != 0.0f)
1362 r1[5] -= m1 * s;
1363 r2[5] -= m2 * s;
1364 r3[5] -= m3 * s;
1366 s = r0[6];
1367 if (s != 0.0f)
1369 r1[6] -= m1 * s;
1370 r2[6] -= m2 * s;
1371 r3[6] -= m3 * s;
1373 s = r0[7];
1374 if (s != 0.0f)
1376 r1[7] -= m1 * s;
1377 r2[7] -= m2 * s;
1378 r3[7] -= m3 * s;
1381 /* Choose pivot - or die. */
1382 if (fabsf(r3[1]) > fabsf(r2[1]))
1383 swap_rows(&r3, &r2);
1384 if (fabsf(r2[1]) > fabsf(r1[1]))
1385 swap_rows(&r2, &r1);
1386 if (r1[1] == 0.0f)
1387 return FALSE;
1389 /* Eliminate second variable. */
1390 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1391 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1392 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1393 s = r1[4];
1394 if (s != 0.0f)
1396 r2[4] -= m2 * s;
1397 r3[4] -= m3 * s;
1399 s = r1[5];
1400 if (s != 0.0f)
1402 r2[5] -= m2 * s;
1403 r3[5] -= m3 * s;
1405 s = r1[6];
1406 if (s != 0.0f)
1408 r2[6] -= m2 * s;
1409 r3[6] -= m3 * s;
1411 s = r1[7];
1412 if (s != 0.0f)
1414 r2[7] -= m2 * s;
1415 r3[7] -= m3 * s;
1418 /* Choose pivot - or die. */
1419 if (fabsf(r3[2]) > fabsf(r2[2]))
1420 swap_rows(&r3, &r2);
1421 if (r2[2] == 0.0f)
1422 return FALSE;
1424 /* Eliminate third variable. */
1425 m3 = r3[2] / r2[2];
1426 r3[3] -= m3 * r2[3];
1427 r3[4] -= m3 * r2[4];
1428 r3[5] -= m3 * r2[5];
1429 r3[6] -= m3 * r2[6];
1430 r3[7] -= m3 * r2[7];
1432 /* Last check. */
1433 if (r3[3] == 0.0f)
1434 return FALSE;
1436 /* Back substitute row 3. */
1437 s = 1.0f / r3[3];
1438 r3[4] *= s;
1439 r3[5] *= s;
1440 r3[6] *= s;
1441 r3[7] *= s;
1443 /* Back substitute row 2. */
1444 m2 = r2[3];
1445 s = 1.0f / r2[2];
1446 r2[4] = s * (r2[4] - r3[4] * m2);
1447 r2[5] = s * (r2[5] - r3[5] * m2);
1448 r2[6] = s * (r2[6] - r3[6] * m2);
1449 r2[7] = s * (r2[7] - r3[7] * m2);
1450 m1 = r1[3];
1451 r1[4] -= r3[4] * m1;
1452 r1[5] -= r3[5] * m1;
1453 r1[6] -= r3[6] * m1;
1454 r1[7] -= r3[7] * m1;
1455 m0 = r0[3];
1456 r0[4] -= r3[4] * m0;
1457 r0[5] -= r3[5] * m0;
1458 r0[6] -= r3[6] * m0;
1459 r0[7] -= r3[7] * m0;
1461 /* Back substitute row 1. */
1462 m1 = r1[2];
1463 s = 1.0f / r1[1];
1464 r1[4] = s * (r1[4] - r2[4] * m1);
1465 r1[5] = s * (r1[5] - r2[5] * m1);
1466 r1[6] = s * (r1[6] - r2[6] * m1);
1467 r1[7] = s * (r1[7] - r2[7] * m1);
1468 m0 = r0[2];
1469 r0[4] -= r2[4] * m0;
1470 r0[5] -= r2[5] * m0;
1471 r0[6] -= r2[6] * m0;
1472 r0[7] -= r2[7] * m0;
1474 /* Back substitute row 0. */
1475 m0 = r0[1];
1476 s = 1.0f / r0[0];
1477 r0[4] = s * (r0[4] - r1[4] * m0);
1478 r0[5] = s * (r0[5] - r1[5] * m0);
1479 r0[6] = s * (r0[6] - r1[6] * m0);
1480 r0[7] = s * (r0[7] - r1[7] * m0);
1482 out->_11 = r0[4];
1483 out->_12 = r0[5];
1484 out->_13 = r0[6];
1485 out->_14 = r0[7];
1486 out->_21 = r1[4];
1487 out->_22 = r1[5];
1488 out->_23 = r1[6];
1489 out->_24 = r1[7];
1490 out->_31 = r2[4];
1491 out->_32 = r2[5];
1492 out->_33 = r2[6];
1493 out->_34 = r2[7];
1494 out->_41 = r3[4];
1495 out->_42 = r3[5];
1496 out->_43 = r3[6];
1497 out->_44 = r3[7];
1499 return TRUE;
1502 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1503 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1505 const struct wined3d_gl_info *gl_info = context->gl_info;
1506 float mat[3 * 3];
1507 struct wined3d_matrix mv;
1508 unsigned int i, j;
1510 if (prog->vs.normal_matrix_location == -1)
1511 return;
1513 get_modelview_matrix(context, state, 0, &mv);
1514 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1515 invert_matrix_3d(&mv, &mv);
1516 else
1517 invert_matrix(&mv, &mv);
1518 /* Tests show that singular modelview matrices are used unchanged as normal
1519 * matrices on D3D3 and older. There seems to be no clearly consistent
1520 * behavior on newer D3D versions so always follow older ddraw behavior. */
1521 for (i = 0; i < 3; ++i)
1522 for (j = 0; j < 3; ++j)
1523 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1525 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1526 checkGLcall("glUniformMatrix3fv");
1529 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1530 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1532 const struct wined3d_gl_info *gl_info = context->gl_info;
1533 struct wined3d_matrix mat;
1535 if (tex >= MAX_TEXTURES)
1536 return;
1537 if (prog->vs.texture_matrix_location[tex] == -1)
1538 return;
1540 get_texture_matrix(context, state, tex, &mat);
1541 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1542 checkGLcall("glUniformMatrix4fv");
1545 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1546 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1548 const struct wined3d_gl_info *gl_info = context->gl_info;
1550 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1552 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1553 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1555 else
1557 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1559 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1561 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1562 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1563 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1564 checkGLcall("setting FFP material uniforms");
1567 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1568 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1570 const struct wined3d_gl_info *gl_info = context->gl_info;
1571 struct wined3d_color color;
1573 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1574 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1575 checkGLcall("glUniform3fv");
1578 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1579 const struct wined3d_matrix *src2)
1581 struct wined3d_vec4 temp;
1583 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1584 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1585 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1586 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1588 *dest = temp;
1591 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1592 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1593 struct glsl_shader_prog_link *prog)
1595 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1596 const struct wined3d_gl_info *gl_info = context->gl_info;
1597 struct wined3d_vec4 vec4;
1599 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1600 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1601 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1603 switch (light_info->OriginalParms.type)
1605 case WINED3D_LIGHT_POINT:
1606 multiply_vector_matrix(&vec4, &light_info->position, view);
1607 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1608 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1609 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1610 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1611 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1612 break;
1614 case WINED3D_LIGHT_SPOT:
1615 multiply_vector_matrix(&vec4, &light_info->position, view);
1616 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1618 multiply_vector_matrix(&vec4, &light_info->direction, view);
1619 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1621 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1622 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1623 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1624 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1625 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1626 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1627 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1628 break;
1630 case WINED3D_LIGHT_DIRECTIONAL:
1631 multiply_vector_matrix(&vec4, &light_info->direction, view);
1632 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1633 break;
1635 case WINED3D_LIGHT_PARALLELPOINT:
1636 multiply_vector_matrix(&vec4, &light_info->position, view);
1637 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1638 break;
1640 default:
1641 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1643 checkGLcall("setting FFP lights uniforms");
1646 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1647 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1649 const struct wined3d_gl_info *gl_info = context->gl_info;
1650 float min, max;
1651 float size, att[3];
1653 get_pointsize_minmax(context, state, &min, &max);
1655 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1656 checkGLcall("glUniform1f");
1657 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1658 checkGLcall("glUniform1f");
1660 get_pointsize(context, state, &size, att);
1662 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1663 checkGLcall("glUniform1f");
1664 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1665 checkGLcall("glUniform1f");
1666 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1667 checkGLcall("glUniform1f");
1668 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1669 checkGLcall("glUniform1f");
1672 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1673 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1675 const struct wined3d_gl_info *gl_info = context->gl_info;
1676 struct wined3d_color color;
1677 float start, end, scale;
1678 union
1680 DWORD d;
1681 float f;
1682 } tmpvalue;
1684 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1685 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1686 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1687 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1688 get_fog_start_end(context, state, &start, &end);
1689 scale = 1.0f / (end - start);
1690 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1691 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1692 checkGLcall("fog emulation uniforms");
1695 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1696 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1698 const struct wined3d_gl_info *gl_info = context->gl_info;
1699 struct wined3d_vec4 plane;
1701 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1702 if (!use_vs(state))
1703 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1704 else
1705 plane = state->clip_planes[index];
1707 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1710 /* Context activation is done by the caller (state handler). */
1711 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1712 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1714 struct wined3d_color float_key[2];
1715 const struct wined3d_texture *texture = state->textures[0];
1717 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1718 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1721 /* Context activation is done by the caller (state handler). */
1722 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1723 const struct wined3d_state *state)
1725 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1726 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1727 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1728 const struct wined3d_gl_info *gl_info = context->gl_info;
1729 struct shader_glsl_priv *priv = shader_priv;
1730 float position_fixup[4];
1731 DWORD update_mask;
1733 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1734 UINT constant_version;
1735 int i;
1737 if (!prog) {
1738 /* No GLSL program set - nothing to do. */
1739 return;
1741 constant_version = prog->constant_version;
1742 update_mask = context->constant_update_mask & prog->constant_update_mask;
1744 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1745 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1746 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1748 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1749 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1750 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1752 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1753 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1754 vshader->reg_maps.boolean_constants);
1756 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1758 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1759 shader_glsl_clip_plane_uniform(context, state, i, prog);
1762 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1763 shader_glsl_pointsize_uniform(context, state, prog);
1765 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1767 shader_get_position_fixup(context, state, position_fixup);
1768 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1769 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1770 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1771 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1772 else
1773 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1774 checkGLcall("glUniform4fv");
1777 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1779 struct wined3d_matrix mat;
1781 get_modelview_matrix(context, state, 0, &mat);
1782 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1783 checkGLcall("glUniformMatrix4fv");
1785 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1788 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1790 struct wined3d_matrix mat;
1792 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1794 if (prog->vs.modelview_matrix_location[i] == -1)
1795 break;
1797 get_modelview_matrix(context, state, i, &mat);
1798 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1799 checkGLcall("glUniformMatrix4fv");
1803 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1805 struct wined3d_matrix projection;
1807 get_projection_matrix(context, state, &projection);
1808 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1809 checkGLcall("glUniformMatrix4fv");
1812 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1814 for (i = 0; i < MAX_TEXTURES; ++i)
1815 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1818 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1819 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1821 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1823 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1824 DWORD point_count = 0;
1825 DWORD spot_count = 0;
1826 DWORD directional_count = 0;
1827 DWORD parallel_point_count = 0;
1829 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1831 if (!state->lights[i])
1832 continue;
1834 switch (state->lights[i]->OriginalParms.type)
1836 case WINED3D_LIGHT_POINT:
1837 ++point_count;
1838 break;
1839 case WINED3D_LIGHT_SPOT:
1840 ++spot_count;
1841 break;
1842 case WINED3D_LIGHT_DIRECTIONAL:
1843 ++directional_count;
1844 break;
1845 case WINED3D_LIGHT_PARALLELPOINT:
1846 ++parallel_point_count;
1847 break;
1848 default:
1849 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1850 break;
1853 point_idx = 0;
1854 spot_idx = point_idx + point_count;
1855 directional_idx = spot_idx + spot_count;
1856 parallel_point_idx = directional_idx + directional_count;
1858 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1859 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1861 const struct wined3d_light_info *light_info = state->lights[i];
1862 unsigned int idx;
1864 if (!light_info)
1865 continue;
1867 switch (light_info->OriginalParms.type)
1869 case WINED3D_LIGHT_POINT:
1870 idx = point_idx++;
1871 break;
1872 case WINED3D_LIGHT_SPOT:
1873 idx = spot_idx++;
1874 break;
1875 case WINED3D_LIGHT_DIRECTIONAL:
1876 idx = directional_idx++;
1877 break;
1878 case WINED3D_LIGHT_PARALLELPOINT:
1879 idx = parallel_point_idx++;
1880 break;
1881 default:
1882 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1883 continue;
1885 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1889 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1890 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1891 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1893 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1894 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1895 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1897 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1898 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1899 pshader->reg_maps.boolean_constants);
1901 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1903 for (i = 0; i < MAX_TEXTURES; ++i)
1905 if (prog->ps.bumpenv_mat_location[i] == -1)
1906 continue;
1908 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1909 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1911 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1913 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1914 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1915 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1916 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1920 checkGLcall("bump env uniforms");
1923 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1925 const struct wined3d_vec4 correction_params =
1927 /* Position is relative to the framebuffer, not the viewport. */
1928 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1929 context->render_offscreen ? 1.0f : -1.0f,
1930 0.0f,
1931 0.0f,
1934 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1937 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1938 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1939 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1940 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1942 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1944 struct wined3d_color color;
1946 if (prog->ps.tex_factor_location != -1)
1948 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1949 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1952 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1953 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1954 else
1955 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1957 for (i = 0; i < MAX_TEXTURES; ++i)
1959 if (prog->ps.tss_constant_location[i] == -1)
1960 continue;
1962 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1963 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1966 checkGLcall("fixed function uniforms");
1969 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1970 shader_glsl_load_fog_uniform(context, state, prog);
1972 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1974 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1976 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1977 checkGLcall("alpha test emulation uniform");
1980 if (priv->next_constant_version == UINT_MAX)
1982 TRACE("Max constant version reached, resetting to 0.\n");
1983 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1984 priv->next_constant_version = 1;
1986 else
1988 prog->constant_version = priv->next_constant_version++;
1992 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1994 struct constant_entry *entries = heap->entries;
1995 unsigned int *positions = heap->positions;
1996 unsigned int heap_idx, parent_idx;
1998 if (!heap->contained[idx])
2000 heap_idx = heap->size++;
2001 heap->contained[idx] = TRUE;
2003 else
2005 heap_idx = positions[idx];
2008 while (heap_idx > 1)
2010 parent_idx = heap_idx >> 1;
2012 if (new_version <= entries[parent_idx].version) break;
2014 entries[heap_idx] = entries[parent_idx];
2015 positions[entries[parent_idx].idx] = heap_idx;
2016 heap_idx = parent_idx;
2019 entries[heap_idx].version = new_version;
2020 entries[heap_idx].idx = idx;
2021 positions[idx] = heap_idx;
2024 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2026 struct shader_glsl_priv *priv = device->shader_priv;
2027 struct constant_heap *heap = &priv->vconst_heap;
2028 UINT i;
2030 for (i = start; i < count + start; ++i)
2032 update_heap_entry(heap, i, priv->next_constant_version);
2036 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2038 struct shader_glsl_priv *priv = device->shader_priv;
2039 struct constant_heap *heap = &priv->pconst_heap;
2040 UINT i;
2042 for (i = start; i < count + start; ++i)
2044 update_heap_entry(heap, i, priv->next_constant_version);
2048 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2050 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2051 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2052 if(shader_major > 3) return ret;
2054 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2055 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2056 return ret;
2059 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2061 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2064 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2066 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2067 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2070 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2072 return shader_glsl_get_version(gl_info) >= 150;
2075 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2077 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2080 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2081 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2083 va_list args;
2084 int ret;
2086 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2087 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2088 for (;;)
2090 va_start(args, format);
2091 ret = shader_vaddline(buffer, format, args);
2092 va_end(args);
2093 if (!ret)
2094 return;
2095 if (!string_buffer_resize(buffer, ret))
2096 return;
2100 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2101 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2103 va_list args;
2104 int ret;
2106 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2107 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2108 for (;;)
2110 va_start(args, format);
2111 ret = shader_vaddline(buffer, format, args);
2112 va_end(args);
2113 if (!ret)
2114 return;
2115 if (!string_buffer_resize(buffer, ret))
2116 return;
2120 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2122 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2125 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2127 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2130 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2132 switch (mode)
2134 case WINED3DSIM_CONSTANT:
2135 return "flat";
2136 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2137 return "noperspective";
2138 default:
2139 FIXME("Unhandled interpolation mode %#x.\n", mode);
2140 case WINED3DSIM_NONE:
2141 case WINED3DSIM_LINEAR:
2142 return "";
2146 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2147 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2149 return wined3d_extract_bits(packed_interpolation_mode,
2150 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2153 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2154 struct wined3d_string_buffer *buffer, unsigned int element_count,
2155 const DWORD *interpolation_mode, BOOL unroll)
2157 enum wined3d_shader_interpolation_mode mode;
2158 unsigned int i;
2160 if (shader_glsl_use_interface_blocks(gl_info))
2162 if (unroll)
2164 shader_addline(buffer, "in shader_in_out {\n");
2165 for (i = 0; i < element_count; ++i)
2167 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2168 shader_addline(buffer, "%s vec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2170 shader_addline(buffer, "} shader_in;\n");
2172 else
2174 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2177 else
2179 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2183 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2184 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2185 const DWORD *interpolation_mode)
2187 enum wined3d_shader_interpolation_mode mode;
2188 unsigned int i;
2190 if (shader_glsl_use_interface_blocks(gl_info))
2192 if (rasterizer_setup)
2194 shader_addline(buffer, "out shader_in_out {\n");
2195 for (i = 0; i < element_count; ++i)
2197 const char *interpolation_qualifiers = "";
2198 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2200 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2201 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2203 shader_addline(buffer, "%s vec4 reg%u;\n", interpolation_qualifiers, i);
2205 shader_addline(buffer, "} shader_out;\n");
2207 else
2209 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2212 else
2214 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2218 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2220 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2223 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2225 switch (primitive_type)
2227 case WINED3D_PT_POINTLIST:
2228 return "points";
2230 case WINED3D_PT_LINELIST:
2231 return "lines";
2233 case WINED3D_PT_LINESTRIP:
2234 return "line_strip";
2236 case WINED3D_PT_TRIANGLELIST:
2237 return "triangles";
2239 case WINED3D_PT_TRIANGLESTRIP:
2240 return "triangle_strip";
2242 case WINED3D_PT_LINELIST_ADJ:
2243 return "lines_adjacency";
2245 case WINED3D_PT_TRIANGLELIST_ADJ:
2246 return "triangles_adjacency";
2248 default:
2249 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2250 return "";
2254 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2256 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2257 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2258 DWORD input_reg_used = shader->u.ps.input_reg_used;
2259 unsigned int i;
2261 if (reg_maps->shader_version.major < 3)
2262 return input_reg_used & (1u << idx);
2264 for (i = 0; i < input_signature->element_count; ++i)
2266 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2268 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2269 continue;
2271 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2272 && input->semantic_idx == idx)
2273 return input_reg_used & (1u << input->register_idx);
2275 return FALSE;
2278 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2279 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2281 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2283 if (version->major >= 4)
2284 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2285 else
2286 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2289 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2290 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2291 unsigned int index)
2293 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2294 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2295 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2296 index, scalar_type, scalar_type, index);
2299 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2300 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2302 unsigned int index = e->register_idx;
2304 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2306 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2307 index);
2308 return;
2310 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2312 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2313 index);
2314 return;
2316 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2317 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2319 if (shader_glsl_use_explicit_attrib_location(gl_info))
2320 shader_addline(buffer, "layout(location = %u) ", index);
2322 switch (e->component_type)
2324 case WINED3D_TYPE_UINT:
2325 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
2326 break;
2327 case WINED3D_TYPE_INT:
2328 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
2329 break;
2331 default:
2332 FIXME("Unhandled type %#x.\n", e->component_type);
2333 /* Fall through. */
2334 case WINED3D_TYPE_UNKNOWN:
2335 case WINED3D_TYPE_FLOAT:
2336 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2337 break;
2341 /** Generate the variable & register declarations for the GLSL output target */
2342 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2343 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2344 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2346 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2347 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2348 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2349 const struct wined3d_gl_info *gl_info = context->gl_info;
2350 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2351 unsigned int uniform_block_base, uniform_block_count;
2352 const struct wined3d_shader_lconst *lconst;
2353 const char *prefix;
2354 unsigned int i;
2355 DWORD map;
2357 prefix = shader_glsl_get_prefix(version->type);
2359 /* Prototype the subroutines */
2360 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2362 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2365 /* Declare the constants (aka uniforms) */
2366 if (shader->limits->constant_float > 0)
2368 unsigned max_constantsF;
2370 /* Unless the shader uses indirect addressing, always declare the
2371 * maximum array size and ignore that we need some uniforms privately.
2372 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2373 * and immediate values, still declare VC[256]. If the shader needs
2374 * more uniforms than we have it won't work in any case. If it uses
2375 * less, the compiler will figure out which uniforms are really used
2376 * and strip them out. This allows a shader to use c255 on a dx9 card,
2377 * as long as it doesn't also use all the other constants.
2379 * If the shader uses indirect addressing the compiler must assume
2380 * that all declared uniforms are used. In this case, declare only the
2381 * amount that we're assured to have.
2383 * Thus we run into problems in these two cases:
2384 * 1) The shader really uses more uniforms than supported.
2385 * 2) The shader uses indirect addressing, less constants than
2386 * supported, but uses a constant index > #supported consts. */
2387 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2389 /* No indirect addressing here. */
2390 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2392 else
2394 if (reg_maps->usesrelconstF)
2396 /* Subtract the other potential uniforms from the max
2397 * available (bools, ints, and 1 row of projection matrix).
2398 * Subtract another uniform for immediate values, which have
2399 * to be loaded via uniform by the driver as well. The shader
2400 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2401 * shader code, so one vec4 should be enough. (Unfortunately
2402 * the Nvidia driver doesn't store 128 and -128 in one float).
2404 * Writing gl_ClipVertex requires one uniform for each
2405 * clipplane as well. */
2406 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2407 if (vs_args->clip_enabled)
2408 max_constantsF -= gl_info->limits.user_clip_distances;
2409 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2410 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2411 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2412 * for now take this into account when calculating the number of available constants
2414 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2415 /* Set by driver quirks in directx.c */
2416 max_constantsF -= gl_info->reserved_glsl_constants;
2418 if (max_constantsF < shader->limits->constant_float)
2420 static unsigned int once;
2422 if (!once++)
2423 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2424 " it may not render correctly.\n");
2425 else
2426 WARN("The hardware does not support enough uniform components to run this shader.\n");
2429 else
2431 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2434 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2435 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2438 /* Always declare the full set of constants, the compiler can remove the
2439 * unused ones because d3d doesn't (yet) support indirect int and bool
2440 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2441 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2442 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2444 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2445 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2447 /* Declare immediate constant buffer */
2448 if (reg_maps->icb)
2449 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2451 /* Declare constant buffers */
2452 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2453 &uniform_block_base, &uniform_block_count);
2454 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2456 if (reg_maps->cb_sizes[i])
2458 shader_addline(buffer, "layout(std140");
2459 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2460 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2461 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2462 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2466 /* Declare texture samplers */
2467 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2469 struct wined3d_shader_sampler_map_entry *entry;
2470 const char *sampler_type_prefix, *sampler_type;
2471 BOOL shadow_sampler, tex_rect;
2473 entry = &reg_maps->sampler_map.entries[i];
2475 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2477 ERR("Invalid resource index %u.\n", entry->resource_idx);
2478 continue;
2481 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2483 case WINED3D_DATA_FLOAT:
2484 case WINED3D_DATA_UNORM:
2485 case WINED3D_DATA_SNORM:
2486 sampler_type_prefix = "";
2487 break;
2489 case WINED3D_DATA_INT:
2490 sampler_type_prefix = "i";
2491 break;
2493 case WINED3D_DATA_UINT:
2494 sampler_type_prefix = "u";
2495 break;
2497 default:
2498 sampler_type_prefix = "";
2499 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2500 break;
2503 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2504 switch (reg_maps->resource_info[entry->resource_idx].type)
2506 case WINED3D_SHADER_RESOURCE_BUFFER:
2507 sampler_type = "samplerBuffer";
2508 break;
2510 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2511 if (shadow_sampler)
2512 sampler_type = "sampler1DShadow";
2513 else
2514 sampler_type = "sampler1D";
2515 break;
2517 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2518 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2519 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2520 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2521 if (shadow_sampler)
2523 if (tex_rect)
2524 sampler_type = "sampler2DRectShadow";
2525 else
2526 sampler_type = "sampler2DShadow";
2528 else
2530 if (tex_rect)
2531 sampler_type = "sampler2DRect";
2532 else
2533 sampler_type = "sampler2D";
2535 break;
2537 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2538 if (shadow_sampler)
2539 FIXME("Unsupported 3D shadow sampler.\n");
2540 sampler_type = "sampler3D";
2541 break;
2543 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2544 if (shadow_sampler)
2545 sampler_type = "samplerCubeShadow";
2546 else
2547 sampler_type = "samplerCube";
2548 break;
2550 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2551 if (shadow_sampler)
2552 sampler_type = "sampler2DArrayShadow";
2553 else
2554 sampler_type = "sampler2DArray";
2555 break;
2557 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2558 if (shadow_sampler)
2559 sampler_type = "samplerCubeArrayShadow";
2560 else
2561 sampler_type = "samplerCubeArray";
2562 break;
2564 default:
2565 sampler_type = "unsupported_sampler";
2566 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2567 break;
2570 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2571 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2572 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2573 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2576 /* Declare images */
2577 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2579 const char *image_type_prefix, *image_type, *read_format;
2581 if (!reg_maps->uav_resource_info[i].type)
2582 continue;
2584 switch (reg_maps->uav_resource_info[i].data_type)
2586 case WINED3D_DATA_FLOAT:
2587 case WINED3D_DATA_UNORM:
2588 case WINED3D_DATA_SNORM:
2589 image_type_prefix = "";
2590 read_format = "r32f";
2591 break;
2593 case WINED3D_DATA_INT:
2594 image_type_prefix = "i";
2595 read_format = "r32i";
2596 break;
2598 case WINED3D_DATA_UINT:
2599 image_type_prefix = "u";
2600 read_format = "r32ui";
2601 break;
2603 default:
2604 image_type_prefix = "";
2605 read_format = "";
2606 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2607 break;
2610 switch (reg_maps->uav_resource_info[i].type)
2612 case WINED3D_SHADER_RESOURCE_BUFFER:
2613 image_type = "imageBuffer";
2614 break;
2616 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2617 image_type = "image2D";
2618 break;
2620 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2621 image_type = "image3D";
2622 break;
2624 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2625 image_type = "image2DArray";
2626 break;
2628 default:
2629 image_type = "unsupported_image";
2630 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2631 break;
2634 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2635 shader_addline(buffer, "layout(binding = %u)\n", i);
2636 if (reg_maps->uav_read_mask & (1u << i))
2637 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2638 read_format, image_type_prefix, image_type, prefix, i);
2639 else
2640 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2641 image_type_prefix, image_type, prefix, i);
2643 if (reg_maps->uav_counter_mask & (1u << i))
2644 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2645 i, prefix, i);
2648 /* Declare address variables */
2649 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2651 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2654 /* Declare output register temporaries */
2655 if (shader->limits->packed_output)
2656 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2658 /* Declare temporary variables */
2659 if (reg_maps->temporary_count)
2661 for (i = 0; i < reg_maps->temporary_count; ++i)
2662 shader_addline(buffer, "vec4 R%u;\n", i);
2664 else if (version->major < 4)
2666 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2668 if (map & 1)
2669 shader_addline(buffer, "vec4 R%u;\n", i);
2673 /* Declare indexable temporary variables */
2674 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2676 if (idx_temp_reg->component_count != 4)
2677 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2678 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2681 /* Declare loop registers aLx */
2682 if (version->major < 4)
2684 for (i = 0; i < reg_maps->loop_depth; ++i)
2686 shader_addline(buffer, "int aL%u;\n", i);
2687 shader_addline(buffer, "int tmpInt%u;\n", i);
2691 /* Temporary variables for matrix operations */
2692 shader_addline(buffer, "vec4 tmp0;\n");
2693 shader_addline(buffer, "vec4 tmp1;\n");
2695 if (!shader->load_local_constsF)
2697 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2699 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2700 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2701 shader_addline(buffer, ";\n");
2706 /*****************************************************************************
2707 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2709 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2710 ****************************************************************************/
2712 /* Prototypes */
2713 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2714 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2716 /** Used for opcode modifiers - They multiply the result by the specified amount */
2717 static const char * const shift_glsl_tab[] = {
2718 "", /* 0 (none) */
2719 "2.0 * ", /* 1 (x2) */
2720 "4.0 * ", /* 2 (x4) */
2721 "8.0 * ", /* 3 (x8) */
2722 "16.0 * ", /* 4 (x16) */
2723 "32.0 * ", /* 5 (x32) */
2724 "", /* 6 (x64) */
2725 "", /* 7 (x128) */
2726 "", /* 8 (d256) */
2727 "", /* 9 (d128) */
2728 "", /* 10 (d64) */
2729 "", /* 11 (d32) */
2730 "0.0625 * ", /* 12 (d16) */
2731 "0.125 * ", /* 13 (d8) */
2732 "0.25 * ", /* 14 (d4) */
2733 "0.5 * " /* 15 (d2) */
2736 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2737 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2738 const char *in_reg, const char *in_regswizzle, char *out_str)
2740 switch (src_modifier)
2742 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2743 case WINED3DSPSM_DW:
2744 case WINED3DSPSM_NONE:
2745 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2746 break;
2747 case WINED3DSPSM_NEG:
2748 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2749 break;
2750 case WINED3DSPSM_NOT:
2751 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2752 break;
2753 case WINED3DSPSM_BIAS:
2754 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2755 break;
2756 case WINED3DSPSM_BIASNEG:
2757 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2758 break;
2759 case WINED3DSPSM_SIGN:
2760 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2761 break;
2762 case WINED3DSPSM_SIGNNEG:
2763 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2764 break;
2765 case WINED3DSPSM_COMP:
2766 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2767 break;
2768 case WINED3DSPSM_X2:
2769 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2770 break;
2771 case WINED3DSPSM_X2NEG:
2772 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2773 break;
2774 case WINED3DSPSM_ABS:
2775 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2776 break;
2777 case WINED3DSPSM_ABSNEG:
2778 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2779 break;
2780 default:
2781 FIXME("Unhandled modifier %u\n", src_modifier);
2782 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2786 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2787 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2789 /* The ARB_shading_language_420pack extension allows swizzle operations on
2790 * scalars. */
2791 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2792 sprintf(register_name, "%s", glsl_variable);
2793 else
2794 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2797 /** Writes the GLSL variable name that corresponds to the register that the
2798 * DX opcode parameter is trying to access */
2799 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2800 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2801 const struct wined3d_shader_instruction *ins)
2803 /* oPos, oFog and oPts in D3D */
2804 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2806 const struct wined3d_shader *shader = ins->ctx->shader;
2807 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2808 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2809 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2810 const char *prefix = shader_glsl_get_prefix(version->type);
2811 struct glsl_src_param rel_param0, rel_param1;
2812 char imm_str[4][17];
2814 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2815 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2816 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2817 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2818 *is_color = FALSE;
2820 switch (reg->type)
2822 case WINED3DSPR_TEMP:
2823 sprintf(register_name, "R%u", reg->idx[0].offset);
2824 break;
2826 case WINED3DSPR_INPUT:
2827 case WINED3DSPR_INCONTROLPOINT:
2828 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2830 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2832 if (reg->idx[0].rel_addr)
2833 FIXME("VS3+ input registers relative addressing.\n");
2834 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2835 *is_color = TRUE;
2836 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2837 break;
2840 if (version->type == WINED3D_SHADER_TYPE_HULL
2841 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2842 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2844 if (reg->idx[0].rel_addr)
2846 if (reg->idx[1].rel_addr)
2847 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2848 rel_param0.param_str, reg->idx[0].offset,
2849 rel_param1.param_str, reg->idx[1].offset);
2850 else
2851 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2852 rel_param0.param_str, reg->idx[0].offset,
2853 reg->idx[1].offset);
2855 else if (reg->idx[1].rel_addr)
2856 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2857 rel_param1.param_str, reg->idx[1].offset);
2858 else
2859 sprintf(register_name, "shader_in[%u].reg[%u]",
2860 reg->idx[0].offset, reg->idx[1].offset);
2861 break;
2864 /* pixel shaders >= 3.0 */
2865 if (version->major >= 3)
2867 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2868 unsigned int in_count = vec4_varyings(version->major, gl_info);
2870 if (reg->idx[0].rel_addr)
2872 /* Removing a + 0 would be an obvious optimization, but
2873 * OS X doesn't see the NOP operation there. */
2874 if (idx)
2876 if (needs_legacy_glsl_syntax(gl_info)
2877 && shader->u.ps.declared_in_count > in_count)
2879 sprintf(register_name,
2880 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2881 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2882 prefix, rel_param0.param_str, idx);
2884 else
2886 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2889 else
2891 if (needs_legacy_glsl_syntax(gl_info)
2892 && shader->u.ps.declared_in_count > in_count)
2894 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2895 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2896 prefix, rel_param0.param_str);
2898 else
2900 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2904 else
2906 if (idx == in_count) sprintf(register_name, "gl_Color");
2907 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2908 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2911 else
2913 if (!reg->idx[0].offset)
2914 strcpy(register_name, "ffp_varying_diffuse");
2915 else
2916 strcpy(register_name, "ffp_varying_specular");
2917 break;
2919 break;
2921 case WINED3DSPR_CONST:
2923 /* Relative addressing */
2924 if (reg->idx[0].rel_addr)
2926 if (wined3d_settings.check_float_constants)
2927 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2928 rel_param0.param_str, reg->idx[0].offset,
2929 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2930 prefix, rel_param0.param_str, reg->idx[0].offset);
2931 else if (reg->idx[0].offset)
2932 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2933 else
2934 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2936 else
2938 if (shader_constant_is_local(shader, reg->idx[0].offset))
2939 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2940 else
2941 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2944 break;
2946 case WINED3DSPR_CONSTINT:
2947 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2948 break;
2950 case WINED3DSPR_CONSTBOOL:
2951 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2952 break;
2954 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2955 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2956 sprintf(register_name, "T%u", reg->idx[0].offset);
2957 else
2958 sprintf(register_name, "A%u", reg->idx[0].offset);
2959 break;
2961 case WINED3DSPR_LOOP:
2962 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2963 break;
2965 case WINED3DSPR_SAMPLER:
2966 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2967 break;
2969 case WINED3DSPR_COLOROUT:
2970 if (reg->idx[0].offset >= gl_info->limits.buffers)
2971 WARN("Write to render target %u, only %d supported.\n",
2972 reg->idx[0].offset, gl_info->limits.buffers);
2974 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2975 break;
2977 case WINED3DSPR_RASTOUT:
2978 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2979 break;
2981 case WINED3DSPR_DEPTHOUT:
2982 case WINED3DSPR_DEPTHOUTGE:
2983 case WINED3DSPR_DEPTHOUTLE:
2984 sprintf(register_name, "gl_FragDepth");
2985 break;
2987 case WINED3DSPR_ATTROUT:
2988 if (!reg->idx[0].offset)
2989 sprintf(register_name, "%s_out[8]", prefix);
2990 else
2991 sprintf(register_name, "%s_out[9]", prefix);
2992 break;
2994 case WINED3DSPR_TEXCRDOUT:
2995 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2996 if (reg->idx[0].rel_addr)
2997 sprintf(register_name, "%s_out[%s + %u]",
2998 prefix, rel_param0.param_str, reg->idx[0].offset);
2999 else
3000 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3001 break;
3003 case WINED3DSPR_MISCTYPE:
3004 if (!reg->idx[0].offset)
3006 /* vPos */
3007 sprintf(register_name, "vpos");
3009 else if (reg->idx[0].offset == 1)
3011 /* Note that gl_FrontFacing is a bool, while vFace is
3012 * a float for which the sign determines front/back */
3013 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3015 else
3017 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3018 sprintf(register_name, "unrecognized_register");
3020 break;
3022 case WINED3DSPR_IMMCONST:
3023 switch (reg->immconst_type)
3025 case WINED3D_IMMCONST_SCALAR:
3026 switch (data_type)
3028 case WINED3D_DATA_FLOAT:
3029 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3030 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3031 else
3032 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3033 break;
3034 case WINED3D_DATA_INT:
3035 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3036 break;
3037 case WINED3D_DATA_RESOURCE:
3038 case WINED3D_DATA_SAMPLER:
3039 case WINED3D_DATA_UINT:
3040 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3041 break;
3042 default:
3043 sprintf(register_name, "<unhandled data type %#x>", data_type);
3044 break;
3046 break;
3048 case WINED3D_IMMCONST_VEC4:
3049 switch (data_type)
3051 case WINED3D_DATA_FLOAT:
3052 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3054 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3055 reg->u.immconst_data[0], reg->u.immconst_data[1],
3056 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3058 else
3060 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3061 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3062 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3063 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3064 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3065 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3067 break;
3068 case WINED3D_DATA_INT:
3069 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3070 reg->u.immconst_data[0], reg->u.immconst_data[1],
3071 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3072 break;
3073 case WINED3D_DATA_RESOURCE:
3074 case WINED3D_DATA_SAMPLER:
3075 case WINED3D_DATA_UINT:
3076 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3077 reg->u.immconst_data[0], reg->u.immconst_data[1],
3078 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3079 break;
3080 default:
3081 sprintf(register_name, "<unhandled data type %#x>", data_type);
3082 break;
3084 break;
3086 default:
3087 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3088 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3090 break;
3092 case WINED3DSPR_CONSTBUFFER:
3093 if (reg->idx[1].rel_addr)
3094 sprintf(register_name, "%s_cb%u[%s + %u]",
3095 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3096 else
3097 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3098 break;
3100 case WINED3DSPR_IMMCONSTBUFFER:
3101 if (reg->idx[0].rel_addr)
3102 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3103 else
3104 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3105 break;
3107 case WINED3DSPR_PRIMID:
3108 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3109 sprintf(register_name, "gl_PrimitiveIDIn");
3110 else
3111 sprintf(register_name, "gl_PrimitiveID");
3112 break;
3114 case WINED3DSPR_IDXTEMP:
3115 if (reg->idx[1].rel_addr)
3116 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3117 else
3118 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3119 break;
3121 case WINED3DSPR_LOCALTHREADINDEX:
3122 shader_glsl_fixup_scalar_register_variable(register_name,
3123 "int(gl_LocalInvocationIndex)", gl_info);
3124 break;
3126 case WINED3DSPR_GSINSTID:
3127 case WINED3DSPR_OUTPOINTID:
3128 shader_glsl_fixup_scalar_register_variable(register_name,
3129 "gl_InvocationID", gl_info);
3130 break;
3132 case WINED3DSPR_THREADID:
3133 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3134 break;
3136 case WINED3DSPR_THREADGROUPID:
3137 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3138 break;
3140 case WINED3DSPR_LOCALTHREADID:
3141 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3142 break;
3144 case WINED3DSPR_FORKINSTID:
3145 case WINED3DSPR_JOININSTID:
3146 shader_glsl_fixup_scalar_register_variable(register_name,
3147 "phase_instance_id", gl_info);
3148 break;
3150 case WINED3DSPR_TESSCOORD:
3151 sprintf(register_name, "gl_TessCoord");
3152 break;
3154 case WINED3DSPR_OUTCONTROLPOINT:
3155 if (reg->idx[0].rel_addr)
3157 if (reg->idx[1].rel_addr)
3158 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3159 rel_param0.param_str, reg->idx[0].offset,
3160 rel_param1.param_str, reg->idx[1].offset);
3161 else
3162 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3163 rel_param0.param_str, reg->idx[0].offset,
3164 reg->idx[1].offset);
3166 else if (reg->idx[1].rel_addr)
3168 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3169 reg->idx[0].offset, rel_param1.param_str,
3170 reg->idx[1].offset);
3172 else
3174 sprintf(register_name, "shader_out[%u].reg[%u]",
3175 reg->idx[0].offset, reg->idx[1].offset);
3177 break;
3179 case WINED3DSPR_PATCHCONST:
3180 if (version->type == WINED3D_SHADER_TYPE_HULL)
3181 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3182 else
3183 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3184 break;
3186 default:
3187 FIXME("Unhandled register type %#x.\n", reg->type);
3188 sprintf(register_name, "unrecognized_register");
3189 break;
3193 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3195 *str++ = '.';
3196 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3197 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3198 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3199 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3200 *str = '\0';
3203 /* Get the GLSL write mask for the destination register */
3204 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3206 DWORD mask = param->write_mask;
3208 if (shader_is_scalar(&param->reg))
3210 mask = WINED3DSP_WRITEMASK_0;
3211 *write_mask = '\0';
3213 else
3215 shader_glsl_write_mask_to_str(mask, write_mask);
3218 return mask;
3221 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3223 unsigned int size = 0;
3225 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3226 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3227 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3228 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3230 return size;
3233 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3234 unsigned int component_idx)
3236 /* swizzle bits fields: wwzzyyxx */
3237 return (swizzle >> (2 * component_idx)) & 0x3;
3240 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3242 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3243 * but addressed as "rgba". To fix this we need to swap the register's x
3244 * and z components. */
3245 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3246 unsigned int i;
3248 *str++ = '.';
3249 for (i = 0; i < 4; ++i)
3251 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3252 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3254 *str = '\0';
3257 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3258 BOOL fixup, DWORD mask, char *swizzle_str)
3260 if (shader_is_scalar(&param->reg))
3261 *swizzle_str = '\0';
3262 else
3263 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3266 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3267 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3269 if (dst_data_type == src_data_type)
3271 string_buffer_sprintf(dst_param, "%s", src_param);
3272 return;
3275 if (src_data_type == WINED3D_DATA_FLOAT)
3277 switch (dst_data_type)
3279 case WINED3D_DATA_INT:
3280 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3281 return;
3282 case WINED3D_DATA_RESOURCE:
3283 case WINED3D_DATA_SAMPLER:
3284 case WINED3D_DATA_UINT:
3285 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3286 return;
3287 default:
3288 break;
3292 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3294 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3295 return;
3298 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3300 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3301 return;
3304 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3305 string_buffer_sprintf(dst_param, "%s", src_param);
3308 /* From a given parameter token, generate the corresponding GLSL string.
3309 * Also, return the actual register name and swizzle in case the
3310 * caller needs this information as well. */
3311 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3312 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3313 enum wined3d_data_type data_type)
3315 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3316 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3317 enum wined3d_data_type param_data_type;
3318 BOOL is_color = FALSE;
3319 char swizzle_str[6];
3321 glsl_src->reg_name[0] = '\0';
3322 glsl_src->param_str[0] = '\0';
3323 swizzle_str[0] = '\0';
3325 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ins);
3326 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3328 switch (wined3d_src->reg.type)
3330 case WINED3DSPR_IMMCONST:
3331 param_data_type = data_type;
3332 break;
3333 case WINED3DSPR_FORKINSTID:
3334 case WINED3DSPR_GSINSTID:
3335 case WINED3DSPR_JOININSTID:
3336 case WINED3DSPR_LOCALTHREADID:
3337 case WINED3DSPR_LOCALTHREADINDEX:
3338 case WINED3DSPR_OUTPOINTID:
3339 case WINED3DSPR_PRIMID:
3340 case WINED3DSPR_THREADGROUPID:
3341 case WINED3DSPR_THREADID:
3342 param_data_type = WINED3D_DATA_INT;
3343 break;
3344 default:
3345 param_data_type = WINED3D_DATA_FLOAT;
3346 break;
3349 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3350 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3352 string_buffer_release(priv->string_buffers, reg_name);
3355 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3356 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3358 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3361 /* From a given parameter token, generate the corresponding GLSL string.
3362 * Also, return the actual register name and swizzle in case the
3363 * caller needs this information as well. */
3364 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3365 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3367 BOOL is_color = FALSE;
3369 glsl_dst->mask_str[0] = '\0';
3370 glsl_dst->reg_name[0] = '\0';
3372 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3373 glsl_dst->reg_name, &is_color, ins);
3374 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3377 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3378 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3379 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3380 enum wined3d_data_type data_type)
3382 struct glsl_dst_param glsl_dst;
3383 DWORD mask;
3385 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3387 switch (data_type)
3389 case WINED3D_DATA_FLOAT:
3390 shader_addline(buffer, "%s%s = %s(",
3391 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3392 break;
3393 case WINED3D_DATA_INT:
3394 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3395 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3396 break;
3397 case WINED3D_DATA_RESOURCE:
3398 case WINED3D_DATA_SAMPLER:
3399 case WINED3D_DATA_UINT:
3400 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3401 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3402 break;
3403 default:
3404 FIXME("Unhandled data type %#x.\n", data_type);
3405 shader_addline(buffer, "%s%s = %s(",
3406 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3407 break;
3411 return mask;
3414 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3415 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3417 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3420 /** Process GLSL instruction modifiers */
3421 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3423 struct glsl_dst_param dst_param;
3424 DWORD modifiers;
3426 if (!ins->dst_count) return;
3428 modifiers = ins->dst[0].modifiers;
3429 if (!modifiers) return;
3431 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3433 if (modifiers & WINED3DSPDM_SATURATE)
3435 /* _SAT means to clamp the value of the register to between 0 and 1 */
3436 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3437 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3440 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3442 FIXME("_centroid modifier not handled\n");
3445 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3447 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3451 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3453 switch (op)
3455 case WINED3D_SHADER_REL_OP_GT: return ">";
3456 case WINED3D_SHADER_REL_OP_EQ: return "==";
3457 case WINED3D_SHADER_REL_OP_GE: return ">=";
3458 case WINED3D_SHADER_REL_OP_LT: return "<";
3459 case WINED3D_SHADER_REL_OP_NE: return "!=";
3460 case WINED3D_SHADER_REL_OP_LE: return "<=";
3461 default:
3462 FIXME("Unrecognized operator %#x.\n", op);
3463 return "(\?\?)";
3467 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3469 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3472 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3473 unsigned int *coord_size, unsigned int *deriv_size)
3475 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3476 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3478 *coord_size = resource_type_info[resource_type].coord_size;
3479 *deriv_size = *coord_size;
3480 if (is_array)
3481 --(*deriv_size);
3484 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3485 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3487 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3488 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3489 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3490 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3491 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3492 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3493 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3494 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3495 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3496 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3497 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3498 const char *base = "texture", *type_part = "", *suffix = "";
3499 unsigned int coord_size, deriv_size;
3501 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3503 if (resource_type >= ARRAY_SIZE(resource_type_info))
3505 ERR("Unexpected resource type %#x.\n", resource_type);
3506 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3509 /* Note that there's no such thing as a projected cube texture. */
3510 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3511 projected = FALSE;
3513 if (needs_legacy_glsl_syntax(gl_info))
3515 if (shadow)
3516 base = "shadow";
3518 type_part = resource_type_info[resource_type].type_part;
3519 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3520 type_part = "2DRect";
3521 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3522 FIXME("Unhandled resource type %#x.\n", resource_type);
3524 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3526 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3527 suffix = "ARB";
3528 else
3529 FIXME("Unsupported grad function.\n");
3533 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3535 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3536 if (flags & ~texel_fetch_flags)
3537 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3539 base = "texelFetch";
3540 type_part = "";
3543 sample_function->name = string_buffer_get(priv->string_buffers);
3544 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3545 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3547 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3548 if (shadow)
3549 ++coord_size;
3550 sample_function->offset_size = offset ? deriv_size : 0;
3551 sample_function->coord_mask = (1u << coord_size) - 1;
3552 sample_function->deriv_mask = (1u << deriv_size) - 1;
3553 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3556 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3557 struct glsl_sample_function *sample_function)
3559 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3561 string_buffer_release(priv->string_buffers, sample_function->name);
3564 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3565 BOOL sign_fixup, enum fixup_channel_source channel_source)
3567 switch(channel_source)
3569 case CHANNEL_SOURCE_ZERO:
3570 strcat(arguments, "0.0");
3571 break;
3573 case CHANNEL_SOURCE_ONE:
3574 strcat(arguments, "1.0");
3575 break;
3577 case CHANNEL_SOURCE_X:
3578 strcat(arguments, reg_name);
3579 strcat(arguments, ".x");
3580 break;
3582 case CHANNEL_SOURCE_Y:
3583 strcat(arguments, reg_name);
3584 strcat(arguments, ".y");
3585 break;
3587 case CHANNEL_SOURCE_Z:
3588 strcat(arguments, reg_name);
3589 strcat(arguments, ".z");
3590 break;
3592 case CHANNEL_SOURCE_W:
3593 strcat(arguments, reg_name);
3594 strcat(arguments, ".w");
3595 break;
3597 default:
3598 FIXME("Unhandled channel source %#x\n", channel_source);
3599 strcat(arguments, "undefined");
3600 break;
3603 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3606 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3607 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3609 unsigned int mask_size, remaining;
3610 DWORD fixup_mask = 0;
3611 char arguments[256];
3612 char mask_str[6];
3614 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3615 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3616 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3617 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3618 if (!(mask &= fixup_mask))
3619 return;
3621 if (is_complex_fixup(fixup))
3623 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3624 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3625 return;
3628 shader_glsl_write_mask_to_str(mask, mask_str);
3629 mask_size = shader_glsl_get_write_mask_size(mask);
3631 arguments[0] = '\0';
3632 remaining = mask_size;
3633 if (mask & WINED3DSP_WRITEMASK_0)
3635 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3636 if (--remaining) strcat(arguments, ", ");
3638 if (mask & WINED3DSP_WRITEMASK_1)
3640 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3641 if (--remaining) strcat(arguments, ", ");
3643 if (mask & WINED3DSP_WRITEMASK_2)
3645 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3646 if (--remaining) strcat(arguments, ", ");
3648 if (mask & WINED3DSP_WRITEMASK_3)
3650 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3651 if (--remaining) strcat(arguments, ", ");
3654 if (mask_size > 1)
3655 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3656 else
3657 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3660 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3662 char reg_name[256];
3663 BOOL is_color;
3665 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins);
3666 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3669 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3670 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3671 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3672 const char *coord_reg_fmt, ...)
3674 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3675 char dst_swizzle[6];
3676 struct color_fixup_desc fixup;
3677 BOOL np2_fixup = FALSE;
3678 va_list args;
3679 int ret;
3681 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3683 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3684 * We actually rely on it for vertex shaders and SM4+. */
3685 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3687 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3688 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3690 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3691 np2_fixup = TRUE;
3693 else
3695 fixup = COLOR_FIXUP_IDENTITY;
3698 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3700 if (sample_function->output_single_component)
3701 shader_addline(ins->ctx->buffer, "vec4(");
3703 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3704 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3706 for (;;)
3708 va_start(args, coord_reg_fmt);
3709 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3710 va_end(args);
3711 if (!ret)
3712 break;
3713 if (!string_buffer_resize(ins->ctx->buffer, ret))
3714 break;
3717 if (np2_fixup)
3719 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3720 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3722 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3724 case 1:
3725 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3726 idx >> 1, (idx % 2) ? "z" : "x");
3727 break;
3728 case 2:
3729 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3730 idx >> 1, (idx % 2) ? "zw" : "xy");
3731 break;
3732 case 3:
3733 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3734 idx >> 1, (idx % 2) ? "zw" : "xy");
3735 break;
3736 case 4:
3737 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3738 idx >> 1, (idx % 2) ? "zw" : "xy");
3739 break;
3742 if (dx && dy)
3743 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3744 else if (bias)
3745 shader_addline(ins->ctx->buffer, ", %s", bias);
3746 if (sample_function->offset_size)
3748 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3749 shader_addline(ins->ctx->buffer, ", ");
3750 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3752 shader_addline(ins->ctx->buffer, ")");
3754 if (sample_function->output_single_component)
3755 shader_addline(ins->ctx->buffer, ")");
3757 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3759 if (!is_identity_fixup(fixup))
3760 shader_glsl_color_correction(ins, fixup);
3763 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3765 /* Write the final position.
3767 * OpenGL coordinates specify the center of the pixel while D3D coords
3768 * specify the corner. The offsets are stored in z and w in
3769 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3770 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3771 * a MAD. */
3772 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3773 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3775 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3776 * in utils.c
3778 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3779 * shaders are run before the homogeneous divide, so we have to take the w
3780 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3781 * z = z * 2 - w. */
3782 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3785 /*****************************************************************************
3786 * Begin processing individual instruction opcodes
3787 ****************************************************************************/
3789 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3791 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3792 struct glsl_src_param src0_param;
3793 struct glsl_src_param src1_param;
3794 DWORD write_mask;
3795 const char *op;
3797 /* Determine the GLSL operator to use based on the opcode */
3798 switch (ins->handler_idx)
3800 case WINED3DSIH_ADD: op = "+"; break;
3801 case WINED3DSIH_AND: op = "&"; break;
3802 case WINED3DSIH_DIV: op = "/"; break;
3803 case WINED3DSIH_IADD: op = "+"; break;
3804 case WINED3DSIH_ISHL: op = "<<"; break;
3805 case WINED3DSIH_ISHR: op = ">>"; break;
3806 case WINED3DSIH_MUL: op = "*"; break;
3807 case WINED3DSIH_OR: op = "|"; break;
3808 case WINED3DSIH_SUB: op = "-"; break;
3809 case WINED3DSIH_USHR: op = ">>"; break;
3810 case WINED3DSIH_XOR: op = "^"; break;
3811 default:
3812 op = "<unhandled operator>";
3813 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3814 break;
3817 write_mask = shader_glsl_append_dst(buffer, ins);
3818 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3819 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3820 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3823 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3825 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3826 struct glsl_src_param src0_param;
3827 struct glsl_src_param src1_param;
3828 unsigned int mask_size;
3829 DWORD write_mask;
3830 const char *op;
3832 write_mask = shader_glsl_append_dst(buffer, ins);
3833 mask_size = shader_glsl_get_write_mask_size(write_mask);
3834 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3835 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3837 if (mask_size > 1)
3839 switch (ins->handler_idx)
3841 case WINED3DSIH_EQ: op = "equal"; break;
3842 case WINED3DSIH_IEQ: op = "equal"; break;
3843 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3844 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3845 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3846 case WINED3DSIH_LT: op = "lessThan"; break;
3847 case WINED3DSIH_ILT: op = "lessThan"; break;
3848 case WINED3DSIH_ULT: op = "lessThan"; break;
3849 case WINED3DSIH_NE: op = "notEqual"; break;
3850 case WINED3DSIH_INE: op = "notEqual"; break;
3851 default:
3852 op = "<unhandled operator>";
3853 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3854 break;
3857 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3858 mask_size, op, src0_param.param_str, src1_param.param_str);
3860 else
3862 switch (ins->handler_idx)
3864 case WINED3DSIH_EQ: op = "=="; break;
3865 case WINED3DSIH_IEQ: op = "=="; break;
3866 case WINED3DSIH_GE: op = ">="; break;
3867 case WINED3DSIH_IGE: op = ">="; break;
3868 case WINED3DSIH_UGE: op = ">="; break;
3869 case WINED3DSIH_LT: op = "<"; break;
3870 case WINED3DSIH_ILT: op = "<"; break;
3871 case WINED3DSIH_ULT: op = "<"; break;
3872 case WINED3DSIH_NE: op = "!="; break;
3873 case WINED3DSIH_INE: op = "!="; break;
3874 default:
3875 op = "<unhandled operator>";
3876 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3877 break;
3880 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3881 src0_param.param_str, op, src1_param.param_str);
3885 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3887 struct glsl_src_param src_param;
3888 DWORD write_mask;
3889 const char *op;
3891 switch (ins->handler_idx)
3893 case WINED3DSIH_INEG: op = "-"; break;
3894 case WINED3DSIH_NOT: op = "~"; break;
3895 default:
3896 op = "<unhandled operator>";
3897 ERR("Unhandled opcode %s.\n",
3898 debug_d3dshaderinstructionhandler(ins->handler_idx));
3899 break;
3902 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3903 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3904 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3907 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3909 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3910 struct glsl_src_param src0_param;
3911 struct glsl_src_param src1_param;
3912 DWORD write_mask;
3914 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3915 * If not, we can emulate it. */
3916 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3917 FIXME("64-bit integer multiplies not implemented.\n");
3919 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3921 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3922 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3923 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3925 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3926 src0_param.param_str, src1_param.param_str);
3930 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3932 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3933 struct glsl_src_param src0_param, src1_param;
3934 DWORD write_mask;
3936 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3938 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3940 char dst_mask[6];
3942 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3943 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3944 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3945 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3946 dst_mask, src0_param.param_str, src1_param.param_str);
3948 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3949 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3950 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3951 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3953 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3954 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3956 else
3958 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3959 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3960 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3961 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3964 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3966 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].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);
3973 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3974 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3976 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3977 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3978 struct glsl_src_param src0_param;
3979 DWORD write_mask;
3981 write_mask = shader_glsl_append_dst(buffer, ins);
3982 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3984 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3985 * shader versions WINED3DSIO_MOVA is used for this. */
3986 if (ins->ctx->reg_maps->shader_version.major == 1
3987 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3988 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3990 /* This is a simple floor() */
3991 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3992 if (mask_size > 1) {
3993 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3994 } else {
3995 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3998 else if (ins->handler_idx == WINED3DSIH_MOVA)
4000 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4002 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4004 if (mask_size > 1)
4005 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4006 else
4007 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4009 else
4011 if (mask_size > 1)
4012 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4013 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4014 else
4015 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4016 src0_param.param_str, src0_param.param_str);
4019 else
4021 shader_addline(buffer, "%s);\n", src0_param.param_str);
4025 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4026 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4028 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4029 struct glsl_src_param src0_param;
4030 struct glsl_src_param src1_param;
4031 DWORD dst_write_mask, src_write_mask;
4032 unsigned int dst_size;
4034 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4035 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4037 /* dp4 works on vec4, dp3 on vec3, etc. */
4038 if (ins->handler_idx == WINED3DSIH_DP4)
4039 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4040 else if (ins->handler_idx == WINED3DSIH_DP3)
4041 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4042 else
4043 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4045 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4046 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4048 if (dst_size > 1) {
4049 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4050 } else {
4051 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4055 /* Note that this instruction has some restrictions. The destination write mask
4056 * can't contain the w component, and the source swizzles have to be .xyzw */
4057 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4059 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4060 struct glsl_src_param src0_param;
4061 struct glsl_src_param src1_param;
4062 char dst_mask[6];
4064 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4065 shader_glsl_append_dst(ins->ctx->buffer, ins);
4066 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4067 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4068 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4071 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4073 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4075 if (!stream)
4076 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4077 else
4078 FIXME("Unhandled primitive stream %u.\n", stream);
4081 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4082 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4083 * GLSL uses the value as-is. */
4084 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4086 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4087 struct glsl_src_param src0_param;
4088 struct glsl_src_param src1_param;
4089 DWORD dst_write_mask;
4090 unsigned int dst_size;
4092 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4093 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4095 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4096 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4098 if (dst_size > 1)
4100 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4101 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4103 else
4105 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4106 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4110 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4111 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4113 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4114 struct glsl_src_param src_param;
4115 const char *instruction;
4116 DWORD write_mask;
4117 unsigned i;
4119 /* Determine the GLSL function to use based on the opcode */
4120 /* TODO: Possibly make this a table for faster lookups */
4121 switch (ins->handler_idx)
4123 case WINED3DSIH_ABS: instruction = "abs"; break;
4124 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4125 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4126 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4127 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4128 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4129 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4130 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4131 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4132 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4133 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4134 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4135 case WINED3DSIH_FRC: instruction = "fract"; break;
4136 case WINED3DSIH_IMAX: instruction = "max"; break;
4137 case WINED3DSIH_IMIN: instruction = "min"; break;
4138 case WINED3DSIH_MAX: instruction = "max"; break;
4139 case WINED3DSIH_MIN: instruction = "min"; break;
4140 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4141 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4142 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4143 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4144 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4145 case WINED3DSIH_UMAX: instruction = "max"; break;
4146 case WINED3DSIH_UMIN: instruction = "min"; break;
4147 default: instruction = "";
4148 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4149 break;
4152 write_mask = shader_glsl_append_dst(buffer, ins);
4154 /* In D3D bits are numbered from the most significant bit. */
4155 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4156 shader_addline(buffer, "31 - ");
4157 shader_addline(buffer, "%s(", instruction);
4159 if (ins->src_count)
4161 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4162 shader_addline(buffer, "%s", src_param.param_str);
4163 for (i = 1; i < ins->src_count; ++i)
4165 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4166 shader_addline(buffer, ", %s", src_param.param_str);
4170 shader_addline(buffer, "));\n");
4173 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4175 struct wined3d_shader_dst_param dst;
4176 struct glsl_src_param src;
4177 DWORD write_mask;
4178 const char *fmt;
4179 unsigned int i;
4181 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4182 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4184 dst = ins->dst[0];
4185 for (i = 0; i < 4; ++i)
4187 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4188 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4189 &dst, dst.reg.data_type)))
4190 continue;
4192 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4193 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4197 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4199 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4200 struct wined3d_shader_dst_param dst;
4201 struct glsl_src_param src[4];
4202 const char *instruction;
4203 BOOL tmp_dst = FALSE;
4204 char mask_char[6];
4205 unsigned int i, j;
4206 DWORD write_mask;
4208 switch (ins->handler_idx)
4210 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4211 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4212 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4213 default:
4214 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4215 return;
4218 for (i = 0; i < ins->src_count; ++i)
4220 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4221 && ins->dst[0].reg.type == ins->src[i].reg.type)
4222 tmp_dst = TRUE;
4225 dst = ins->dst[0];
4226 for (i = 0; i < 4; ++i)
4228 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4229 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4230 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4231 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4232 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4233 continue;
4235 for (j = 0; j < ins->src_count; ++j)
4236 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4237 shader_addline(buffer, "%s(", instruction);
4238 for (j = 0; j < ins->src_count - 2; ++j)
4239 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4240 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4243 if (tmp_dst)
4245 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4246 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4247 shader_addline(buffer, "tmp0%s);\n", mask_char);
4251 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4253 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4255 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4256 struct glsl_src_param src_param;
4257 unsigned int mask_size;
4258 DWORD write_mask;
4259 char dst_mask[6];
4261 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4262 mask_size = shader_glsl_get_write_mask_size(write_mask);
4263 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4265 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4266 src_param.param_str, src_param.param_str);
4267 shader_glsl_append_dst(buffer, ins);
4269 if (mask_size > 1)
4271 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4272 mask_size, src_param.param_str);
4274 else
4276 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4277 src_param.param_str);
4281 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4283 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4284 ins->ctx->reg_maps->shader_version.minor);
4285 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4286 struct glsl_src_param src0_param;
4287 const char *prefix, *suffix;
4288 unsigned int dst_size;
4289 DWORD dst_write_mask;
4291 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4292 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4294 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4295 dst_write_mask = WINED3DSP_WRITEMASK_3;
4297 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4299 switch (ins->handler_idx)
4301 case WINED3DSIH_EXP:
4302 case WINED3DSIH_EXPP:
4303 prefix = "exp2(";
4304 suffix = ")";
4305 break;
4307 case WINED3DSIH_LOG:
4308 case WINED3DSIH_LOGP:
4309 prefix = "log2(abs(";
4310 suffix = "))";
4311 break;
4313 case WINED3DSIH_RCP:
4314 prefix = "1.0 / ";
4315 suffix = "";
4316 break;
4318 case WINED3DSIH_RSQ:
4319 prefix = "inversesqrt(abs(";
4320 suffix = "))";
4321 break;
4323 default:
4324 prefix = "";
4325 suffix = "";
4326 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4327 break;
4330 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4331 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4332 else
4333 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4336 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4337 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4338 * dst.x = 2^(floor(src))
4339 * dst.y = src - floor(src)
4340 * dst.z = 2^src (partial precision is allowed, but optional)
4341 * dst.w = 1.0;
4342 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4343 * dst = 2^src; (partial precision is allowed, but optional)
4345 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4347 if (ins->ctx->reg_maps->shader_version.major < 2)
4349 struct glsl_src_param src_param;
4350 char dst_mask[6];
4352 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4354 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4355 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4356 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4357 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4359 shader_glsl_append_dst(ins->ctx->buffer, ins);
4360 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4361 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4362 return;
4365 shader_glsl_scalar_op(ins);
4368 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4369 const char *vector_constructor, const char *scalar_constructor)
4371 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4372 struct glsl_src_param src_param;
4373 unsigned int mask_size;
4374 DWORD write_mask;
4376 write_mask = shader_glsl_append_dst(buffer, ins);
4377 mask_size = shader_glsl_get_write_mask_size(write_mask);
4378 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4380 if (mask_size > 1)
4381 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4382 else
4383 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4386 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4388 shader_glsl_cast(ins, "ivec", "int");
4391 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4393 shader_glsl_cast(ins, "uvec", "uint");
4396 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4398 shader_glsl_cast(ins, "vec", "float");
4401 /** Process signed comparison opcodes in GLSL. */
4402 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4404 struct glsl_src_param src0_param;
4405 struct glsl_src_param src1_param;
4406 DWORD write_mask;
4407 unsigned int mask_size;
4409 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4410 mask_size = shader_glsl_get_write_mask_size(write_mask);
4411 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4412 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4414 if (mask_size > 1) {
4415 const char *compare;
4417 switch(ins->handler_idx)
4419 case WINED3DSIH_SLT: compare = "lessThan"; break;
4420 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4421 default: compare = "";
4422 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4425 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4426 src0_param.param_str, src1_param.param_str);
4427 } else {
4428 switch(ins->handler_idx)
4430 case WINED3DSIH_SLT:
4431 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4432 * to return 0.0 but step returns 1.0 because step is not < x
4433 * An alternative is a bvec compare padded with an unused second component.
4434 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4435 * issue. Playing with not() is not possible either because not() does not accept
4436 * a scalar.
4438 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4439 src0_param.param_str, src1_param.param_str);
4440 break;
4441 case WINED3DSIH_SGE:
4442 /* Here we can use the step() function and safe a conditional */
4443 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4444 break;
4445 default:
4446 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4452 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4454 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4455 struct wined3d_shader_dst_param dst[2];
4456 struct glsl_src_param src[3];
4457 unsigned int i, j, k;
4458 char mask_char[6];
4459 DWORD write_mask;
4460 BOOL tmp_dst[2];
4462 for (i = 0; i < ins->dst_count; ++i)
4464 tmp_dst[i] = FALSE;
4465 for (j = 0; j < ins->src_count; ++j)
4467 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4468 && ins->dst[i].reg.type == ins->src[j].reg.type)
4469 tmp_dst[i] = TRUE;
4473 dst[0] = ins->dst[0];
4474 dst[1] = ins->dst[1];
4475 for (i = 0; i < 4; ++i)
4477 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4479 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4480 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4481 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4482 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4483 continue;
4485 for (k = 0; k < ARRAY_SIZE(src); ++k)
4486 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4488 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4489 src[0].param_str, src[1].param_str, src[2].param_str);
4493 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4495 if (tmp_dst[i])
4497 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4498 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4499 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4504 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4506 const char *condition_prefix, *condition_suffix;
4507 struct wined3d_shader_dst_param dst;
4508 struct glsl_src_param src0_param;
4509 struct glsl_src_param src1_param;
4510 struct glsl_src_param src2_param;
4511 BOOL temp_destination = FALSE;
4512 DWORD cmp_channel = 0;
4513 unsigned int i, j;
4514 char mask_char[6];
4515 DWORD write_mask;
4517 switch (ins->handler_idx)
4519 case WINED3DSIH_CMP:
4520 condition_prefix = "";
4521 condition_suffix = " >= 0.0";
4522 break;
4524 case WINED3DSIH_CND:
4525 condition_prefix = "";
4526 condition_suffix = " > 0.5";
4527 break;
4529 case WINED3DSIH_MOVC:
4530 condition_prefix = "bool(";
4531 condition_suffix = ")";
4532 break;
4534 default:
4535 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4536 condition_prefix = "<unhandled prefix>";
4537 condition_suffix = "<unhandled suffix>";
4538 break;
4541 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4543 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4544 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4545 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4546 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4548 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4549 condition_prefix, src0_param.param_str, condition_suffix,
4550 src1_param.param_str, src2_param.param_str);
4551 return;
4554 dst = ins->dst[0];
4556 /* Splitting the instruction up in multiple lines imposes a problem:
4557 * The first lines may overwrite source parameters of the following lines.
4558 * Deal with that by using a temporary destination register if needed. */
4559 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4560 && ins->src[0].reg.type == dst.reg.type)
4561 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4562 && ins->src[1].reg.type == dst.reg.type)
4563 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4564 && ins->src[2].reg.type == dst.reg.type))
4565 temp_destination = TRUE;
4567 /* Cycle through all source0 channels. */
4568 for (i = 0; i < 4; ++i)
4570 write_mask = 0;
4571 /* Find the destination channels which use the current source0 channel. */
4572 for (j = 0; j < 4; ++j)
4574 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4576 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4577 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4580 dst.write_mask = ins->dst[0].write_mask & write_mask;
4582 if (temp_destination)
4584 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4585 continue;
4586 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4588 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4589 continue;
4591 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4592 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4593 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4595 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4596 condition_prefix, src0_param.param_str, condition_suffix,
4597 src1_param.param_str, src2_param.param_str);
4600 if (temp_destination)
4602 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4603 shader_glsl_append_dst(ins->ctx->buffer, ins);
4604 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4608 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4609 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4610 * the compare is done per component of src0. */
4611 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4613 struct glsl_src_param src0_param;
4614 struct glsl_src_param src1_param;
4615 struct glsl_src_param src2_param;
4616 DWORD write_mask;
4617 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4618 ins->ctx->reg_maps->shader_version.minor);
4620 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4622 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4623 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4624 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4625 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4627 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4628 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4629 else
4630 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4631 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4632 return;
4635 shader_glsl_conditional_move(ins);
4638 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4639 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4641 struct glsl_src_param src0_param;
4642 struct glsl_src_param src1_param;
4643 struct glsl_src_param src2_param;
4644 DWORD write_mask;
4646 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4647 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4648 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4649 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4650 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4651 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4654 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4655 Vertex shaders to GLSL codes */
4656 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4658 int i;
4659 int nComponents = 0;
4660 struct wined3d_shader_dst_param tmp_dst = {{0}};
4661 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4662 struct wined3d_shader_instruction tmp_ins;
4664 memset(&tmp_ins, 0, sizeof(tmp_ins));
4666 /* Set constants for the temporary argument */
4667 tmp_ins.ctx = ins->ctx;
4668 tmp_ins.dst_count = 1;
4669 tmp_ins.dst = &tmp_dst;
4670 tmp_ins.src_count = 2;
4671 tmp_ins.src = tmp_src;
4673 switch(ins->handler_idx)
4675 case WINED3DSIH_M4x4:
4676 nComponents = 4;
4677 tmp_ins.handler_idx = WINED3DSIH_DP4;
4678 break;
4679 case WINED3DSIH_M4x3:
4680 nComponents = 3;
4681 tmp_ins.handler_idx = WINED3DSIH_DP4;
4682 break;
4683 case WINED3DSIH_M3x4:
4684 nComponents = 4;
4685 tmp_ins.handler_idx = WINED3DSIH_DP3;
4686 break;
4687 case WINED3DSIH_M3x3:
4688 nComponents = 3;
4689 tmp_ins.handler_idx = WINED3DSIH_DP3;
4690 break;
4691 case WINED3DSIH_M3x2:
4692 nComponents = 2;
4693 tmp_ins.handler_idx = WINED3DSIH_DP3;
4694 break;
4695 default:
4696 break;
4699 tmp_dst = ins->dst[0];
4700 tmp_src[0] = ins->src[0];
4701 tmp_src[1] = ins->src[1];
4702 for (i = 0; i < nComponents; ++i)
4704 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4705 shader_glsl_dot(&tmp_ins);
4706 ++tmp_src[1].reg.idx[0].offset;
4711 The LRP instruction performs a component-wise linear interpolation
4712 between the second and third operands using the first operand as the
4713 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4714 This is equivalent to mix(src2, src1, src0);
4716 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4718 struct glsl_src_param src0_param;
4719 struct glsl_src_param src1_param;
4720 struct glsl_src_param src2_param;
4721 DWORD write_mask;
4723 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4725 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4726 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4727 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4729 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4730 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4733 /** Process the WINED3DSIO_LIT instruction in GLSL:
4734 * dst.x = dst.w = 1.0
4735 * dst.y = (src0.x > 0) ? src0.x
4736 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4737 * where src.w is clamped at +- 128
4739 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4741 struct glsl_src_param src0_param;
4742 struct glsl_src_param src1_param;
4743 struct glsl_src_param src3_param;
4744 char dst_mask[6];
4746 shader_glsl_append_dst(ins->ctx->buffer, ins);
4747 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4749 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4750 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4751 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4753 /* The sdk specifies the instruction like this
4754 * dst.x = 1.0;
4755 * if(src.x > 0.0) dst.y = src.x
4756 * else dst.y = 0.0.
4757 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4758 * else dst.z = 0.0;
4759 * dst.w = 1.0;
4760 * (where power = src.w clamped between -128 and 128)
4762 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4763 * dst.x = 1.0 ... No further explanation needed
4764 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4765 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4766 * dst.w = 1.0. ... Nothing fancy.
4768 * So we still have one conditional in there. So do this:
4769 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4771 * 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),
4772 * 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.
4773 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4775 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4776 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4777 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4779 shader_addline(ins->ctx->buffer,
4780 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4781 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4782 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4783 src0_param.param_str, src3_param.param_str, dst_mask);
4786 /** Process the WINED3DSIO_DST instruction in GLSL:
4787 * dst.x = 1.0
4788 * dst.y = src0.x * src0.y
4789 * dst.z = src0.z
4790 * dst.w = src1.w
4792 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4794 struct glsl_src_param src0y_param;
4795 struct glsl_src_param src0z_param;
4796 struct glsl_src_param src1y_param;
4797 struct glsl_src_param src1w_param;
4798 char dst_mask[6];
4800 shader_glsl_append_dst(ins->ctx->buffer, ins);
4801 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4803 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4804 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4805 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4806 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4808 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4809 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4812 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4813 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4814 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4816 * dst.x = cos(src0.?)
4817 * dst.y = sin(src0.?)
4818 * dst.z = dst.z
4819 * dst.w = dst.w
4821 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4823 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4824 struct glsl_src_param src0_param;
4825 DWORD write_mask;
4827 if (ins->ctx->reg_maps->shader_version.major < 4)
4829 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4831 write_mask = shader_glsl_append_dst(buffer, ins);
4832 switch (write_mask)
4834 case WINED3DSP_WRITEMASK_0:
4835 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4836 break;
4838 case WINED3DSP_WRITEMASK_1:
4839 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4840 break;
4842 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4843 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4844 src0_param.param_str, src0_param.param_str);
4845 break;
4847 default:
4848 ERR("Write mask should be .x, .y or .xy\n");
4849 break;
4852 return;
4855 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4858 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4860 char dst_mask[6];
4862 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4863 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4864 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4866 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4867 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4868 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4870 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4871 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4873 else
4875 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4876 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4877 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4880 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4882 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4883 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4884 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4888 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4889 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4890 * generate invalid code
4892 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4894 struct glsl_src_param src0_param;
4895 DWORD write_mask;
4897 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4898 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4900 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4903 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4904 * Start a for() loop where src1.y is the initial value of aL,
4905 * increment aL by src1.z for a total of src1.x iterations.
4906 * Need to use a temporary variable for this operation.
4908 /* FIXME: I don't think nested loops will work correctly this way. */
4909 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4911 struct wined3d_shader_parser_state *state = ins->ctx->state;
4912 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4913 const struct wined3d_shader *shader = ins->ctx->shader;
4914 const struct wined3d_shader_lconst *constant;
4915 struct glsl_src_param src1_param;
4916 const DWORD *control_values = NULL;
4918 if (ins->ctx->reg_maps->shader_version.major < 4)
4920 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4922 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4923 * class hardware doesn't support real varying indexing, but Microsoft
4924 * designed this feature for Shader model 2.x+. If the loop control is
4925 * known at compile time, the GLSL compiler can unroll the loop, and
4926 * replace indirect addressing with direct addressing. */
4927 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4929 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4931 if (constant->idx == ins->src[1].reg.idx[0].offset)
4933 control_values = constant->value;
4934 break;
4939 if (control_values)
4941 struct wined3d_shader_loop_control loop_control;
4942 loop_control.count = control_values[0];
4943 loop_control.start = control_values[1];
4944 loop_control.step = (int)control_values[2];
4946 if (loop_control.step > 0)
4948 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4949 state->current_loop_depth, loop_control.start,
4950 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4951 state->current_loop_depth, loop_control.step);
4953 else if (loop_control.step < 0)
4955 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4956 state->current_loop_depth, loop_control.start,
4957 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4958 state->current_loop_depth, loop_control.step);
4960 else
4962 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4963 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4964 state->current_loop_depth, loop_control.count,
4965 state->current_loop_depth);
4968 else
4970 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4971 state->current_loop_depth, state->current_loop_reg,
4972 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4973 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4976 ++state->current_loop_reg;
4978 else
4980 shader_addline(buffer, "for (;;)\n{\n");
4983 ++state->current_loop_depth;
4986 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4988 struct wined3d_shader_parser_state *state = ins->ctx->state;
4990 shader_addline(ins->ctx->buffer, "}\n");
4992 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4994 --state->current_loop_depth;
4995 --state->current_loop_reg;
4998 if (ins->handler_idx == WINED3DSIH_ENDREP)
5000 --state->current_loop_depth;
5004 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5006 struct wined3d_shader_parser_state *state = ins->ctx->state;
5007 const struct wined3d_shader *shader = ins->ctx->shader;
5008 const struct wined3d_shader_lconst *constant;
5009 struct glsl_src_param src0_param;
5010 const DWORD *control_values = NULL;
5012 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5013 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5015 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5017 if (constant->idx == ins->src[0].reg.idx[0].offset)
5019 control_values = constant->value;
5020 break;
5025 if (control_values)
5027 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5028 state->current_loop_depth, state->current_loop_depth,
5029 control_values[0], state->current_loop_depth);
5031 else
5033 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5034 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5035 state->current_loop_depth, state->current_loop_depth,
5036 src0_param.param_str, state->current_loop_depth);
5039 ++state->current_loop_depth;
5042 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5044 struct glsl_src_param src0_param;
5046 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5047 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5050 static void shader_glsl_case(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, "case %s:\n", src0_param.param_str);
5058 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5060 shader_addline(ins->ctx->buffer, "default:\n");
5063 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5065 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
5066 struct glsl_src_param src0_param;
5068 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5069 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
5072 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5074 struct glsl_src_param src0_param;
5075 struct glsl_src_param src1_param;
5077 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5078 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5080 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5081 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5084 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5086 shader_addline(ins->ctx->buffer, "} else {\n");
5089 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5091 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5093 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5094 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5095 shader_glsl_fixup_position(ins->ctx->buffer);
5097 if (!stream)
5098 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5099 else
5100 FIXME("Unhandled primitive stream %u.\n", stream);
5103 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5105 shader_addline(ins->ctx->buffer, "break;\n");
5108 /* FIXME: According to MSDN the compare is done per component. */
5109 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5111 struct glsl_src_param src0_param;
5112 struct glsl_src_param src1_param;
5114 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5115 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5117 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5118 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5121 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5123 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
5124 struct glsl_src_param src_param;
5125 const char *op;
5127 switch (ins->handler_idx)
5129 case WINED3DSIH_BREAKP: op = "break"; break;
5130 case WINED3DSIH_CONTINUEP: op = "continue"; break;
5131 case WINED3DSIH_RETP: op = "return"; break;
5132 default:
5133 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5134 return;
5137 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5138 shader_addline(ins->ctx->buffer, "if (%s(%s)) %s;\n", condition, src_param.param_str, op);
5141 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5143 shader_addline(ins->ctx->buffer, "continue;\n");
5146 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5148 shader_addline(ins->ctx->buffer, "}\n");
5149 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5151 /* Subroutines appear at the end of the shader. */
5152 ins->ctx->state->in_subroutine = TRUE;
5155 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5157 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5160 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5162 struct glsl_src_param src1_param;
5164 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5165 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5166 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5169 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5171 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5173 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5175 shader_glsl_generate_shader_epilogue(ins->ctx);
5176 shader_addline(ins->ctx->buffer, "return;\n");
5180 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5182 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5183 ins->ctx->reg_maps->shader_version.minor);
5184 struct glsl_sample_function sample_function;
5185 DWORD sample_flags = 0;
5186 DWORD resource_idx;
5187 DWORD mask = 0, swizzle;
5188 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5190 /* 1.0-1.4: Use destination register as sampler source.
5191 * 2.0+: Use provided sampler source. */
5192 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5193 resource_idx = ins->dst[0].reg.idx[0].offset;
5194 else
5195 resource_idx = ins->src[1].reg.idx[0].offset;
5197 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5199 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5200 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5201 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5203 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5204 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5206 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5207 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5209 case WINED3D_TTFF_COUNT1:
5210 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5211 break;
5212 case WINED3D_TTFF_COUNT2:
5213 mask = WINED3DSP_WRITEMASK_1;
5214 break;
5215 case WINED3D_TTFF_COUNT3:
5216 mask = WINED3DSP_WRITEMASK_2;
5217 break;
5218 case WINED3D_TTFF_COUNT4:
5219 case WINED3D_TTFF_DISABLE:
5220 mask = WINED3DSP_WRITEMASK_3;
5221 break;
5225 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5227 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5229 if (src_mod == WINED3DSPSM_DZ) {
5230 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5231 mask = WINED3DSP_WRITEMASK_2;
5232 } else if (src_mod == WINED3DSPSM_DW) {
5233 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5234 mask = WINED3DSP_WRITEMASK_3;
5237 else
5239 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5240 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5242 /* ps 2.0 texldp instruction always divides by the fourth component. */
5243 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5244 mask = WINED3DSP_WRITEMASK_3;
5248 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5249 mask |= sample_function.coord_mask;
5250 sample_function.coord_mask = mask;
5252 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5253 else swizzle = ins->src[1].swizzle;
5255 /* 1.0-1.3: Use destination register as coordinate source.
5256 1.4+: Use provided coordinate source register. */
5257 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5259 char coord_mask[6];
5260 shader_glsl_write_mask_to_str(mask, coord_mask);
5261 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5262 "T%u%s", resource_idx, coord_mask);
5264 else
5266 struct glsl_src_param coord_param;
5267 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5268 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5270 struct glsl_src_param bias;
5271 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5272 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5273 NULL, "%s", coord_param.param_str);
5274 } else {
5275 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5276 "%s", coord_param.param_str);
5279 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5282 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5284 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5285 struct glsl_src_param coord_param, dx_param, dy_param;
5286 struct glsl_sample_function sample_function;
5287 DWORD sampler_idx;
5288 DWORD swizzle = ins->src[1].swizzle;
5290 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5292 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5293 shader_glsl_tex(ins);
5294 return;
5297 sampler_idx = ins->src[1].reg.idx[0].offset;
5299 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5300 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5301 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5302 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5304 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5305 NULL, NULL, "%s", coord_param.param_str);
5306 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5309 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5311 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5312 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5313 struct glsl_src_param coord_param, lod_param;
5314 struct glsl_sample_function sample_function;
5315 DWORD swizzle = ins->src[1].swizzle;
5316 DWORD sampler_idx;
5318 sampler_idx = ins->src[1].reg.idx[0].offset;
5320 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5321 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5323 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5325 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5326 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5328 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5329 * However, the NVIDIA drivers allow them in fragment shaders as well,
5330 * even without the appropriate extension. */
5331 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5333 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5334 "%s", coord_param.param_str);
5335 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5338 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5339 unsigned int resource_idx, unsigned int sampler_idx)
5341 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5342 unsigned int i;
5344 for (i = 0; i < sampler_map->count; ++i)
5346 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5347 return entries[i].bind_idx;
5350 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5352 return ~0u;
5355 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5357 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5358 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5359 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5360 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5361 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5362 struct glsl_src_param structure_idx, offset, data, data2;
5363 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5364 enum wined3d_shader_resource_type resource_type;
5365 struct wined3d_string_buffer *address;
5366 enum wined3d_data_type data_type;
5367 unsigned int resource_idx, stride;
5368 const char *op, *resource;
5369 DWORD coord_mask;
5370 BOOL is_tgsm;
5372 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5373 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5374 if (is_tgsm)
5376 if (resource_idx >= reg_maps->tgsm_count)
5378 ERR("Invalid TGSM index %u.\n", resource_idx);
5379 return;
5381 resource = "g";
5382 data_type = WINED3D_DATA_UINT;
5383 coord_mask = 1;
5384 stride = reg_maps->tgsm[resource_idx].stride;
5386 else
5388 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5390 ERR("Invalid UAV index %u.\n", resource_idx);
5391 return;
5393 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5394 if (resource_type >= ARRAY_SIZE(resource_type_info))
5396 ERR("Unexpected resource type %#x.\n", resource_type);
5397 return;
5399 resource = "image";
5400 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5401 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5402 stride = reg_maps->uav_resource_info[resource_idx].stride;
5405 switch (ins->handler_idx)
5407 case WINED3DSIH_ATOMIC_AND:
5408 case WINED3DSIH_IMM_ATOMIC_AND:
5409 if (is_tgsm)
5410 op = "atomicAnd";
5411 else
5412 op = "imageAtomicAnd";
5413 break;
5414 case WINED3DSIH_ATOMIC_CMP_STORE:
5415 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5416 if (is_tgsm)
5417 op = "atomicCompSwap";
5418 else
5419 op = "imageAtomicCompSwap";
5420 break;
5421 case WINED3DSIH_ATOMIC_IADD:
5422 case WINED3DSIH_IMM_ATOMIC_IADD:
5423 if (is_tgsm)
5424 op = "atomicAdd";
5425 else
5426 op = "imageAtomicAdd";
5427 break;
5428 case WINED3DSIH_ATOMIC_IMAX:
5429 case WINED3DSIH_IMM_ATOMIC_IMAX:
5430 if (is_tgsm)
5431 op = "atomicMax";
5432 else
5433 op = "imageAtomicMax";
5434 if (data_type != WINED3D_DATA_INT)
5436 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5437 return;
5439 break;
5440 case WINED3DSIH_ATOMIC_IMIN:
5441 case WINED3DSIH_IMM_ATOMIC_IMIN:
5442 if (is_tgsm)
5443 op = "atomicMin";
5444 else
5445 op = "imageAtomicMin";
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_OR:
5453 case WINED3DSIH_IMM_ATOMIC_OR:
5454 if (is_tgsm)
5455 op = "atomicOr";
5456 else
5457 op = "imageAtomicOr";
5458 break;
5459 case WINED3DSIH_ATOMIC_UMAX:
5460 case WINED3DSIH_IMM_ATOMIC_UMAX:
5461 if (is_tgsm)
5462 op = "atomicMax";
5463 else
5464 op = "imageAtomicMax";
5465 if (data_type != WINED3D_DATA_UINT)
5467 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5468 return;
5470 break;
5471 case WINED3DSIH_ATOMIC_UMIN:
5472 case WINED3DSIH_IMM_ATOMIC_UMIN:
5473 if (is_tgsm)
5474 op = "atomicMin";
5475 else
5476 op = "imageAtomicMin";
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_XOR:
5484 case WINED3DSIH_IMM_ATOMIC_XOR:
5485 if (is_tgsm)
5486 op = "atomicXor";
5487 else
5488 op = "imageAtomicXor";
5489 break;
5490 case WINED3DSIH_IMM_ATOMIC_EXCH:
5491 if (is_tgsm)
5492 op = "atomicExchange";
5493 else
5494 op = "imageAtomicExchange";
5495 break;
5496 default:
5497 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5498 return;
5501 address = string_buffer_get(priv->string_buffers);
5502 if (stride)
5504 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5505 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5506 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5508 else
5510 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5511 string_buffer_sprintf(address, "%s", offset.param_str);
5512 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5513 shader_addline(address, "/ 4");
5516 if (is_imm_instruction)
5517 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5519 if (is_tgsm)
5520 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5521 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5522 else
5523 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5524 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5526 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5527 shader_addline(buffer, "%s", data.param_str);
5528 if (ins->src_count >= 3)
5530 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5531 shader_addline(buffer, ", %s", data2.param_str);
5534 if (is_imm_instruction)
5535 shader_addline(buffer, ")");
5536 shader_addline(buffer, ");\n");
5538 string_buffer_release(priv->string_buffers, address);
5541 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5543 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5544 const char *op;
5546 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5547 op = "atomicCounterIncrement";
5548 else
5549 op = "atomicCounterDecrement";
5551 shader_glsl_append_dst(ins->ctx->buffer, ins);
5552 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5555 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5557 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5558 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5559 enum wined3d_shader_resource_type resource_type;
5560 struct glsl_src_param image_coord_param;
5561 enum wined3d_data_type data_type;
5562 DWORD coord_mask, write_mask;
5563 unsigned int uav_idx;
5564 char dst_swizzle[6];
5566 uav_idx = ins->src[1].reg.idx[0].offset;
5567 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5569 ERR("Invalid UAV index %u.\n", uav_idx);
5570 return;
5572 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5573 if (resource_type >= ARRAY_SIZE(resource_type_info))
5575 ERR("Unexpected resource type %#x.\n", resource_type);
5576 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5578 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5579 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5581 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5582 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5584 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5585 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5586 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5589 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5591 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5592 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5593 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5594 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5595 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5596 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5597 struct glsl_src_param structure_idx, offset;
5598 struct wined3d_string_buffer *address;
5599 struct wined3d_shader_dst_param dst;
5600 const char *function, *resource;
5602 resource_idx = src->reg.idx[0].offset;
5603 if (src->reg.type == WINED3DSPR_RESOURCE)
5605 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5607 ERR("Invalid resource index %u.\n", resource_idx);
5608 return;
5610 stride = reg_maps->resource_info[resource_idx].stride;
5611 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5612 function = "texelFetch";
5613 resource = "sampler";
5615 else if (src->reg.type == WINED3DSPR_UAV)
5617 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5619 ERR("Invalid UAV index %u.\n", resource_idx);
5620 return;
5622 stride = reg_maps->uav_resource_info[resource_idx].stride;
5623 bind_idx = resource_idx;
5624 function = "imageLoad";
5625 resource = "image";
5627 else
5629 if (resource_idx >= reg_maps->tgsm_count)
5631 ERR("Invalid TGSM index %u.\n", resource_idx);
5632 return;
5634 stride = reg_maps->tgsm[resource_idx].stride;
5635 bind_idx = resource_idx;
5636 function = NULL;
5637 resource = "g";
5640 address = string_buffer_get(priv->string_buffers);
5641 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5643 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5644 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5646 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5647 shader_addline(address, "%s / 4", offset.param_str);
5649 dst = ins->dst[0];
5650 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5652 /* The instruction is split into multiple lines. The first lines may
5653 * overwrite source parameters of the following lines. */
5654 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5655 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5658 for (i = 0; i < 4; ++i)
5660 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5661 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5662 continue;
5664 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5665 if (function)
5666 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5667 function, prefix, resource, bind_idx, address->buffer, swizzle);
5668 else
5669 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5670 prefix, resource, bind_idx, address->buffer, swizzle);
5673 string_buffer_release(priv->string_buffers, address);
5676 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5678 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5679 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5680 struct glsl_src_param image_coord_param, image_data_param;
5681 enum wined3d_shader_resource_type resource_type;
5682 enum wined3d_data_type data_type;
5683 unsigned int uav_idx;
5684 DWORD coord_mask;
5686 uav_idx = ins->dst[0].reg.idx[0].offset;
5687 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5689 ERR("Invalid UAV index %u.\n", uav_idx);
5690 return;
5692 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5693 if (resource_type >= ARRAY_SIZE(resource_type_info))
5695 ERR("Unexpected resource type %#x.\n", resource_type);
5696 return;
5698 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5699 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5701 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5702 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5703 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5704 shader_glsl_get_prefix(version->type), uav_idx,
5705 image_coord_param.param_str, image_data_param.param_str);
5708 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5710 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5711 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5712 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5713 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5714 struct glsl_src_param structure_idx, offset, data;
5715 unsigned int i, resource_idx, stride, src_idx = 0;
5716 struct wined3d_string_buffer *address;
5717 DWORD write_mask;
5718 BOOL is_tgsm;
5720 resource_idx = ins->dst[0].reg.idx[0].offset;
5721 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5722 if (is_tgsm)
5724 if (resource_idx >= reg_maps->tgsm_count)
5726 ERR("Invalid TGSM index %u.\n", resource_idx);
5727 return;
5729 stride = reg_maps->tgsm[resource_idx].stride;
5731 else
5733 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5735 ERR("Invalid UAV index %u.\n", resource_idx);
5736 return;
5738 stride = reg_maps->uav_resource_info[resource_idx].stride;
5741 address = string_buffer_get(priv->string_buffers);
5742 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5744 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5745 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5747 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5748 shader_addline(address, "%s / 4", offset.param_str);
5750 for (i = 0; i < 4; ++i)
5752 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5753 continue;
5755 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5757 if (is_tgsm)
5758 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5759 prefix, resource_idx, address->buffer, i, data.param_str);
5760 else
5761 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5762 prefix, resource_idx, address->buffer, i, data.param_str);
5765 string_buffer_release(priv->string_buffers, address);
5768 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5770 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5771 unsigned int sync_flags = ins->flags;
5773 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5775 shader_addline(buffer, "barrier();\n");
5776 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5779 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5781 shader_addline(buffer, "memoryBarrierShared();\n");
5782 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5785 if (sync_flags)
5786 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5789 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5790 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5792 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5793 unsigned int idx = reg->idx[0].offset;
5795 if (reg->type == WINED3DSPR_RESOURCE)
5797 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5799 ERR("Invalid resource index %u.\n", idx);
5800 return NULL;
5802 return &reg_maps->resource_info[idx];
5805 if (reg->type == WINED3DSPR_UAV)
5807 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5809 ERR("Invalid UAV index %u.\n", idx);
5810 return NULL;
5812 return &reg_maps->uav_resource_info[idx];
5815 FIXME("Unhandled register type %#x.\n", reg->type);
5816 return NULL;
5819 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5821 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5822 const struct wined3d_shader_resource_info *resource_info;
5823 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5824 unsigned int resource_idx;
5825 char dst_swizzle[6];
5826 DWORD write_mask;
5828 write_mask = shader_glsl_append_dst(buffer, ins);
5829 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5831 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5832 return;
5833 resource_idx = ins->src[0].reg.idx[0].offset;
5835 shader_addline(buffer, "ivec2(");
5836 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5838 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5839 resource_idx, WINED3D_SAMPLER_DEFAULT);
5840 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5842 else
5844 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5846 if (resource_info->stride)
5847 shader_addline(buffer, " / %u", resource_info->stride);
5848 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5849 shader_addline(buffer, " * 4");
5850 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5853 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5855 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5856 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5857 enum wined3d_shader_resource_type resource_type;
5858 enum wined3d_shader_register_type reg_type;
5859 unsigned int resource_idx, bind_idx, i;
5860 enum wined3d_data_type dst_data_type;
5861 struct glsl_src_param lod_param;
5862 char dst_swizzle[6];
5863 DWORD write_mask;
5865 dst_data_type = ins->dst[0].reg.data_type;
5866 if (ins->flags == WINED3DSI_RESINFO_UINT)
5867 dst_data_type = WINED3D_DATA_UINT;
5868 else if (ins->flags)
5869 FIXME("Unhandled flags %#x.\n", ins->flags);
5871 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5872 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5874 reg_type = ins->src[1].reg.type;
5875 resource_idx = ins->src[1].reg.idx[0].offset;
5876 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5877 if (reg_type == WINED3DSPR_RESOURCE)
5879 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5880 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5881 resource_idx, WINED3D_SAMPLER_DEFAULT);
5883 else
5885 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5886 bind_idx = resource_idx;
5889 if (resource_type >= ARRAY_SIZE(resource_type_info))
5891 ERR("Unexpected resource type %#x.\n", resource_type);
5892 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5895 if (dst_data_type == WINED3D_DATA_UINT)
5896 shader_addline(ins->ctx->buffer, "uvec4(");
5897 else
5898 shader_addline(ins->ctx->buffer, "vec4(");
5900 if (reg_type == WINED3DSPR_RESOURCE)
5902 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5903 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5905 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5906 shader_addline(ins->ctx->buffer, "0, ");
5908 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5910 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5911 shader_glsl_get_prefix(version->type), bind_idx);
5913 else
5915 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5916 shader_addline(ins->ctx->buffer, "1");
5919 else
5921 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5922 shader_glsl_get_prefix(version->type), bind_idx);
5924 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5925 shader_addline(ins->ctx->buffer, "0, ");
5927 /* For UAVs the returned miplevel count is always 1. */
5928 shader_addline(ins->ctx->buffer, "1");
5931 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5934 /* FIXME: The current implementation does not handle multisample textures correctly. */
5935 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5937 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5938 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5939 struct glsl_src_param coord_param, lod_param;
5940 struct glsl_sample_function sample_function;
5941 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5942 BOOL has_lod_param;
5944 if (wined3d_shader_instruction_has_texel_offset(ins))
5945 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5947 resource_idx = ins->src[1].reg.idx[0].offset;
5948 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5950 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5952 ERR("Invalid resource index %u.\n", resource_idx);
5953 return;
5955 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5957 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5958 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5959 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5960 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5961 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5962 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5963 "%s", coord_param.param_str);
5964 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5967 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5969 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5970 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5971 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5972 struct glsl_sample_function sample_function;
5973 DWORD flags = 0;
5975 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5976 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5977 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5978 flags |= WINED3D_GLSL_SAMPLE_LOD;
5979 if (wined3d_shader_instruction_has_texel_offset(ins))
5980 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5982 resource_idx = ins->src[1].reg.idx[0].offset;
5983 sampler_idx = ins->src[2].reg.idx[0].offset;
5985 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5986 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5988 switch (ins->handler_idx)
5990 case WINED3DSIH_SAMPLE:
5991 break;
5992 case WINED3DSIH_SAMPLE_B:
5993 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5994 lod_param_str = lod_param.param_str;
5995 break;
5996 case WINED3DSIH_SAMPLE_GRAD:
5997 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5998 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5999 dx_param_str = dx_param.param_str;
6000 dy_param_str = dy_param.param_str;
6001 break;
6002 case WINED3DSIH_SAMPLE_LOD:
6003 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6004 lod_param_str = lod_param.param_str;
6005 break;
6006 default:
6007 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6008 break;
6011 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6012 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6013 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6014 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6017 /* GLSL doesn't provide a function to sample from level zero with depth
6018 * comparison for array textures and cube textures. We use textureGrad*()
6019 * to implement sample_c_lz.
6021 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6022 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6023 unsigned int coord_size, const char *coord_param, const char *ref_param)
6025 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6026 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6027 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6028 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6029 char dst_swizzle[6];
6031 WARN("Emitting textureGrad() for sample_c_lz.\n");
6033 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6034 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6035 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6036 sample_function->offset_size ? "Offset" : "",
6037 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6038 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6039 if (sample_function->offset_size)
6041 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6042 shader_addline(buffer, ", ");
6043 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6045 shader_addline(buffer, "))%s);\n", dst_swizzle);
6048 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6050 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6051 const struct wined3d_shader_resource_info *resource_info;
6052 struct glsl_src_param coord_param, compare_param;
6053 struct glsl_sample_function sample_function;
6054 const char *lod_param = NULL;
6055 unsigned int coord_size;
6056 DWORD flags = 0;
6058 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6060 lod_param = "0";
6061 flags |= WINED3D_GLSL_SAMPLE_LOD;
6064 if (wined3d_shader_instruction_has_texel_offset(ins))
6065 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6067 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6068 return;
6069 resource_idx = ins->src[1].reg.idx[0].offset;
6070 sampler_idx = ins->src[2].reg.idx[0].offset;
6072 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6073 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6074 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6075 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6076 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6077 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6078 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6079 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6081 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6082 coord_size, coord_param.param_str, compare_param.param_str);
6084 else
6086 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6087 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6088 coord_size, coord_param.param_str, compare_param.param_str);
6090 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6093 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6095 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6096 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6097 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6098 struct glsl_src_param coord_param, compare_param, offset_param;
6099 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6100 const struct wined3d_shader_resource_info *resource_info;
6101 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6102 unsigned int coord_size, offset_size;
6103 char dst_swizzle[6];
6104 BOOL has_offset;
6106 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6108 FIXME("OpenGL implementation does not support textureGather.\n");
6109 return;
6112 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6113 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6114 || wined3d_shader_instruction_has_texel_offset(ins);
6116 resource_param_idx =
6117 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6118 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6119 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6120 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6121 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6123 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6124 return;
6126 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6128 ERR("Unexpected resource type %#x.\n", resource_info->type);
6129 return;
6131 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6133 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6134 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6136 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6138 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6139 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6140 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6142 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6143 shader_addline(buffer, ", %s", compare_param.param_str);
6145 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6147 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6148 shader_addline(buffer, ", %s", offset_param.param_str);
6150 else if (has_offset)
6152 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6153 shader_addline(buffer, ", ");
6154 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6156 if (component_idx)
6157 shader_addline(buffer, ", %u", component_idx);
6159 shader_addline(buffer, ")%s);\n", dst_swizzle);
6162 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6164 /* FIXME: Make this work for more than just 2D textures */
6165 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6166 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6168 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6170 char dst_mask[6];
6172 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6173 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6174 ins->dst[0].reg.idx[0].offset, dst_mask);
6176 else
6178 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6179 DWORD reg = ins->src[0].reg.idx[0].offset;
6180 char dst_swizzle[6];
6182 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6184 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6186 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6187 struct glsl_src_param div_param;
6188 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6190 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6192 if (mask_size > 1)
6193 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6194 else
6195 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6197 else
6199 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6204 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6205 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6206 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6207 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6209 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6210 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6211 struct glsl_sample_function sample_function;
6212 struct glsl_src_param src0_param;
6213 UINT mask_size;
6215 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6217 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6218 * scalar, and projected sampling would require 4.
6220 * It is a dependent read - not valid with conditional NP2 textures
6222 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6223 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6225 switch(mask_size)
6227 case 1:
6228 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6229 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6230 break;
6232 case 2:
6233 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6234 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6235 break;
6237 case 3:
6238 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6239 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6240 break;
6242 default:
6243 FIXME("Unexpected mask size %u\n", mask_size);
6244 break;
6246 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6249 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6250 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6251 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6253 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6254 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6255 struct glsl_src_param src0_param;
6256 DWORD dst_mask;
6257 unsigned int mask_size;
6259 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6260 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6261 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6263 if (mask_size > 1) {
6264 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6265 } else {
6266 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6270 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6271 * Calculate the depth as dst.x / dst.y */
6272 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6274 struct glsl_dst_param dst_param;
6276 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6278 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6279 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6280 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6281 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6282 * >= 1.0 or < 0.0
6284 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6285 dst_param.reg_name, dst_param.reg_name);
6288 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6289 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6290 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6291 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6293 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6295 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6296 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6297 struct glsl_src_param src0_param;
6299 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6301 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6302 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6305 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6306 * Calculate the 1st of a 2-row matrix multiplication. */
6307 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6309 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6310 DWORD reg = ins->dst[0].reg.idx[0].offset;
6311 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6312 struct glsl_src_param src0_param;
6314 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6315 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6318 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6319 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6320 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6322 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6323 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6324 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6325 DWORD reg = ins->dst[0].reg.idx[0].offset;
6326 struct glsl_src_param src0_param;
6328 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6329 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6330 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6333 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6335 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6336 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6337 struct glsl_sample_function sample_function;
6338 DWORD reg = ins->dst[0].reg.idx[0].offset;
6339 struct glsl_src_param src0_param;
6341 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6342 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6344 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6346 /* Sample the texture using the calculated coordinates */
6347 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6348 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6351 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6352 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6353 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6355 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6356 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6357 struct glsl_sample_function sample_function;
6358 DWORD reg = ins->dst[0].reg.idx[0].offset;
6359 struct glsl_src_param src0_param;
6361 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6362 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6364 /* Dependent read, not valid with conditional NP2 */
6365 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6367 /* Sample the texture using the calculated coordinates */
6368 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6369 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6371 tex_mx->current_row = 0;
6374 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6375 * Perform the 3rd row of a 3x3 matrix multiply */
6376 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6378 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6379 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6380 DWORD reg = ins->dst[0].reg.idx[0].offset;
6381 struct glsl_src_param src0_param;
6382 char dst_mask[6];
6384 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6386 shader_glsl_append_dst(ins->ctx->buffer, ins);
6387 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6388 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6390 tex_mx->current_row = 0;
6393 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6394 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6395 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6397 struct glsl_src_param src0_param;
6398 struct glsl_src_param src1_param;
6399 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6400 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6401 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6402 struct glsl_sample_function sample_function;
6403 DWORD reg = ins->dst[0].reg.idx[0].offset;
6404 char coord_mask[6];
6406 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6407 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6409 /* Perform the last matrix multiply operation */
6410 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6411 /* Reflection calculation */
6412 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6414 /* Dependent read, not valid with conditional NP2 */
6415 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6416 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6418 /* Sample the texture */
6419 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6420 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6421 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6423 tex_mx->current_row = 0;
6426 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6427 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6428 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6430 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6431 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6432 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6433 struct glsl_sample_function sample_function;
6434 DWORD reg = ins->dst[0].reg.idx[0].offset;
6435 struct glsl_src_param src0_param;
6436 char coord_mask[6];
6438 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6440 /* Perform the last matrix multiply operation */
6441 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6443 /* Construct the eye-ray vector from w coordinates */
6444 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6445 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6446 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6448 /* Dependent read, not valid with conditional NP2 */
6449 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6450 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6452 /* Sample the texture using the calculated coordinates */
6453 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6454 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6455 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6457 tex_mx->current_row = 0;
6460 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6461 * Apply a fake bump map transform.
6462 * texbem is pshader <= 1.3 only, this saves a few version checks
6464 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6466 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6467 struct glsl_sample_function sample_function;
6468 struct glsl_src_param coord_param;
6469 DWORD sampler_idx;
6470 DWORD mask;
6471 DWORD flags;
6472 char coord_mask[6];
6474 sampler_idx = ins->dst[0].reg.idx[0].offset;
6475 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6476 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6478 /* Dependent read, not valid with conditional NP2 */
6479 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6480 mask = sample_function.coord_mask;
6482 shader_glsl_write_mask_to_str(mask, coord_mask);
6484 /* With projected textures, texbem only divides the static texture coord,
6485 * not the displacement, so we can't let GL handle this. */
6486 if (flags & WINED3D_PSARGS_PROJECTED)
6488 DWORD div_mask=0;
6489 char coord_div_mask[3];
6490 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6492 case WINED3D_TTFF_COUNT1:
6493 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6494 break;
6495 case WINED3D_TTFF_COUNT2:
6496 div_mask = WINED3DSP_WRITEMASK_1;
6497 break;
6498 case WINED3D_TTFF_COUNT3:
6499 div_mask = WINED3DSP_WRITEMASK_2;
6500 break;
6501 case WINED3D_TTFF_COUNT4:
6502 case WINED3D_TTFF_DISABLE:
6503 div_mask = WINED3DSP_WRITEMASK_3;
6504 break;
6506 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6507 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6510 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6512 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6513 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6514 coord_param.param_str, coord_mask);
6516 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6518 struct glsl_src_param luminance_param;
6519 struct glsl_dst_param dst_param;
6521 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6522 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6524 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6525 dst_param.reg_name, dst_param.mask_str,
6526 luminance_param.param_str, sampler_idx, sampler_idx);
6528 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6531 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6533 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6534 struct glsl_src_param src0_param, src1_param;
6536 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6537 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6539 shader_glsl_append_dst(ins->ctx->buffer, ins);
6540 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6541 src0_param.param_str, sampler_idx, src1_param.param_str);
6544 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6545 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6546 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6548 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6549 struct glsl_sample_function sample_function;
6550 struct glsl_src_param src0_param;
6552 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6554 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6555 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6556 "%s.wx", src0_param.reg_name);
6557 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6560 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6561 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6562 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6564 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6565 struct glsl_sample_function sample_function;
6566 struct glsl_src_param src0_param;
6568 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6570 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6571 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6572 "%s.yz", src0_param.reg_name);
6573 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6576 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6577 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6578 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6580 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6581 struct glsl_sample_function sample_function;
6582 struct glsl_src_param src0_param;
6584 /* Dependent read, not valid with conditional NP2 */
6585 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6586 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6588 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6589 "%s", src0_param.param_str);
6590 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6593 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6594 * If any of the first 3 components are < 0, discard this pixel */
6595 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6597 if (ins->ctx->reg_maps->shader_version.major >= 4)
6599 struct glsl_src_param src_param;
6601 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
6602 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
6604 else
6606 struct glsl_dst_param dst_param;
6608 /* The argument is a destination parameter, and no writemasks are allowed */
6609 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6611 /* 2.0 shaders compare all 4 components in texkill. */
6612 if (ins->ctx->reg_maps->shader_version.major >= 2)
6613 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6614 /* 1.x shaders only compare the first 3 components, probably due to
6615 * the nature of the texkill instruction as a tex* instruction, and
6616 * phase, which kills all .w components. Even if all 4 components are
6617 * defined, only the first 3 are used. */
6618 else
6619 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6623 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6624 * dst = dot2(src0, src1) + src2 */
6625 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6627 struct glsl_src_param src0_param;
6628 struct glsl_src_param src1_param;
6629 struct glsl_src_param src2_param;
6630 DWORD write_mask;
6631 unsigned int mask_size;
6633 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6634 mask_size = shader_glsl_get_write_mask_size(write_mask);
6636 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6637 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6638 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6640 if (mask_size > 1) {
6641 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6642 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6643 } else {
6644 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6645 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6649 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6650 const struct wined3d_shader_signature *input_signature,
6651 const struct wined3d_shader_reg_maps *reg_maps,
6652 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6654 unsigned int i;
6656 for (i = 0; i < input_signature->element_count; ++i)
6658 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6659 const char *semantic_name;
6660 UINT semantic_idx;
6661 char reg_mask[6];
6663 /* Unused */
6664 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6665 continue;
6667 semantic_name = input->semantic_name;
6668 semantic_idx = input->semantic_idx;
6669 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6671 if (args->vp_mode == vertexshader)
6673 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6675 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6676 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6678 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6680 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6682 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6684 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6685 input->register_idx, reg_mask);
6687 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6689 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6690 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6691 input->register_idx, reg_mask);
6692 else
6693 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6695 else
6697 if (input->sysval_semantic)
6698 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6699 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6700 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6701 shader_glsl_shader_input_name(gl_info),
6702 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6705 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6707 if (args->pointsprite)
6708 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6709 shader->u.ps.input_reg_map[input->register_idx]);
6710 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6711 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6712 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6713 needs_legacy_glsl_syntax(gl_info)
6714 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6715 else
6716 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6717 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6719 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6721 if (!semantic_idx)
6722 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6723 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6724 else if (semantic_idx == 1)
6725 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6726 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6727 else
6728 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6729 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6731 else
6733 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6734 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6739 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6741 struct glsl_program_key key;
6743 key.vs_id = entry->vs.id;
6744 key.hs_id = entry->hs.id;
6745 key.ds_id = entry->ds.id;
6746 key.gs_id = entry->gs.id;
6747 key.ps_id = entry->ps.id;
6748 key.cs_id = entry->cs.id;
6750 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6752 ERR("Failed to insert program entry.\n");
6756 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6757 const struct glsl_program_key *key)
6759 struct wine_rb_entry *entry;
6761 entry = wine_rb_get(&priv->program_lookup, key);
6762 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6765 /* Context activation is done by the caller. */
6766 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6767 struct glsl_shader_prog_link *entry)
6769 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6771 GL_EXTCALL(glDeleteProgram(entry->id));
6772 if (entry->vs.id)
6773 list_remove(&entry->vs.shader_entry);
6774 if (entry->hs.id)
6775 list_remove(&entry->hs.shader_entry);
6776 if (entry->ds.id)
6777 list_remove(&entry->ds.shader_entry);
6778 if (entry->gs.id)
6779 list_remove(&entry->gs.shader_entry);
6780 if (entry->ps.id)
6781 list_remove(&entry->ps.shader_entry);
6782 if (entry->cs.id)
6783 list_remove(&entry->cs.shader_entry);
6784 HeapFree(GetProcessHeap(), 0, entry);
6787 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6788 const struct wined3d_gl_info *gl_info, const DWORD *map,
6789 const struct wined3d_shader_signature *input_signature,
6790 const struct wined3d_shader_reg_maps *reg_maps_in,
6791 const struct wined3d_shader_signature *output_signature,
6792 const struct wined3d_shader_reg_maps *reg_maps_out)
6794 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6795 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6796 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6797 unsigned int in_count = vec4_varyings(3, gl_info);
6798 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6799 DWORD in_idx, *set = NULL;
6800 unsigned int i, j;
6801 char reg_mask[6];
6803 set = wined3d_calloc(max_varyings, sizeof(*set));
6805 for (i = 0; i < input_signature->element_count; ++i)
6807 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6809 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6810 continue;
6812 in_idx = map[input->register_idx];
6813 /* Declared, but not read register */
6814 if (in_idx == ~0u)
6815 continue;
6816 if (in_idx >= max_varyings)
6818 FIXME("More input varyings declared than supported, expect issues.\n");
6819 continue;
6822 if (in_idx == in_count)
6823 string_buffer_sprintf(destination, "gl_FrontColor");
6824 else if (in_idx == in_count + 1)
6825 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6826 else
6827 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6829 if (!set[in_idx])
6830 set[in_idx] = ~0u;
6832 for (j = 0; j < output_signature->element_count; ++j)
6834 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6835 DWORD mask;
6837 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6838 || input->semantic_idx != output->semantic_idx
6839 || strcmp(input->semantic_name, output->semantic_name)
6840 || !(mask = input->mask & output->mask))
6841 continue;
6843 if (set[in_idx] == ~0u)
6844 set[in_idx] = 0;
6845 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6846 shader_glsl_write_mask_to_str(mask, reg_mask);
6848 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6849 destination->buffer, reg_mask, output->register_idx, reg_mask);
6853 for (i = 0; i < max_varyings; ++i)
6855 unsigned int size;
6857 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6858 continue;
6860 if (set[i] == ~0u)
6861 set[i] = 0;
6863 size = 0;
6864 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6865 reg_mask[size++] = 'x';
6866 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6867 reg_mask[size++] = 'y';
6868 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6869 reg_mask[size++] = 'z';
6870 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6871 reg_mask[size++] = 'w';
6872 reg_mask[size] = '\0';
6874 if (i == in_count)
6875 string_buffer_sprintf(destination, "gl_FrontColor");
6876 else if (i == in_count + 1)
6877 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6878 else
6879 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6881 if (size == 1)
6882 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6883 else
6884 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6887 HeapFree(GetProcessHeap(), 0, set);
6888 string_buffer_release(&priv->string_buffers, destination);
6891 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6892 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6893 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6894 BOOL rasterizer_setup)
6896 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6897 char reg_mask[6];
6898 unsigned int i;
6900 for (i = 0; i < output_signature->element_count; ++i)
6902 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6904 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6905 continue;
6907 if (output->stream_idx)
6908 continue;
6910 if (output->register_idx >= input_count)
6911 continue;
6913 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6915 shader_addline(buffer,
6916 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
6917 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
6921 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6922 const struct wined3d_gl_info *gl_info, const DWORD *map,
6923 const struct wined3d_shader_signature *input_signature,
6924 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6925 const struct wined3d_shader_signature *output_signature,
6926 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6928 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6929 const char *semantic_name;
6930 UINT semantic_idx;
6931 char reg_mask[6];
6932 unsigned int i;
6934 /* First, sort out position and point size system values. */
6935 for (i = 0; i < output_signature->element_count; ++i)
6937 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6939 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6940 continue;
6942 if (output->stream_idx)
6943 continue;
6945 semantic_name = output->semantic_name;
6946 semantic_idx = output->semantic_idx;
6947 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6949 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6951 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
6952 reg_mask, output->register_idx, reg_mask);
6954 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6956 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
6957 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6959 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6961 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
6962 output->register_idx, reg_mask);
6964 else if (output->sysval_semantic)
6966 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6970 /* Then, setup the pixel shader input. */
6971 if (reg_maps_out->shader_version.major < 4)
6972 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6973 output_signature, reg_maps_out);
6974 else
6975 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
6978 /* Context activation is done by the caller. */
6979 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6980 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6981 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6983 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
6984 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6985 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6986 const char *semantic_name;
6987 UINT semantic_idx;
6988 char reg_mask[6];
6989 unsigned int i;
6990 GLuint ret;
6992 string_buffer_clear(buffer);
6994 shader_glsl_add_version_declaration(buffer, gl_info);
6996 if (per_vertex_point_size)
6998 shader_addline(buffer, "uniform struct\n{\n");
6999 shader_addline(buffer, " float size_min;\n");
7000 shader_addline(buffer, " float size_max;\n");
7001 shader_addline(buffer, "} ffp_point;\n");
7004 if (ps_major < 3)
7006 DWORD colors_written_mask[2] = {0};
7007 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7009 if (!legacy_syntax)
7011 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7012 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7013 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7014 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7017 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7019 for (i = 0; i < vs->output_signature.element_count; ++i)
7021 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7022 DWORD write_mask;
7024 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7025 continue;
7027 semantic_name = output->semantic_name;
7028 semantic_idx = output->semantic_idx;
7029 write_mask = output->mask;
7030 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7032 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7034 if (legacy_syntax)
7035 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7036 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7037 else
7038 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7039 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7041 colors_written_mask[semantic_idx] = write_mask;
7043 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7045 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7046 reg_mask, output->register_idx, reg_mask);
7048 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7050 if (semantic_idx < MAX_TEXTURES)
7052 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7053 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7054 semantic_idx, reg_mask, output->register_idx, reg_mask);
7055 texcoords_written_mask[semantic_idx] = write_mask;
7058 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7060 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7061 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7063 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7065 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7066 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7067 output->register_idx, reg_mask[1]);
7071 for (i = 0; i < 2; ++i)
7073 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7075 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7076 if (!i)
7077 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7078 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7079 reg_mask, reg_mask);
7080 else
7081 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7082 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7083 reg_mask, reg_mask);
7086 for (i = 0; i < MAX_TEXTURES; ++i)
7088 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7089 continue;
7091 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7093 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7094 && !texcoords_written_mask[i])
7095 continue;
7097 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7098 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7099 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7103 else
7105 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7107 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7108 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7109 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7110 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7113 shader_addline(buffer, "}\n");
7115 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7116 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7117 shader_glsl_compile(gl_info, ret, buffer->buffer);
7119 return ret;
7122 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7123 const struct wined3d_shader *shader, unsigned int input_count,
7124 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7126 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7127 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7129 if (rasterizer_setup)
7130 input_count = min(vec4_varyings(4, gl_info), input_count);
7132 if (input_count)
7133 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7135 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7136 prefix, shader->limits->packed_output);
7138 if (rasterizer_setup)
7139 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7140 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7141 else
7142 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7143 &shader->reg_maps, "shader_out", rasterizer_setup);
7145 shader_addline(buffer, "}\n");
7148 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7149 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7150 const char *reg_mask)
7152 if (!constant->sysval_semantic)
7154 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7155 return;
7158 switch (constant->sysval_semantic)
7160 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7161 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7162 case WINED3D_SV_TESS_FACTOR_LINEDET:
7163 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7164 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7165 break;
7166 case WINED3D_SV_TESS_FACTOR_QUADINT:
7167 case WINED3D_SV_TESS_FACTOR_TRIINT:
7168 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7169 break;
7170 default:
7171 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7172 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7176 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7177 const struct wined3d_shader_signature *signature, BOOL input_setup)
7179 unsigned int i, register_count, user_constant_index, user_constant_count;
7181 register_count = user_constant_count = 0;
7182 for (i = 0; i < signature->element_count; ++i)
7184 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7185 register_count = max(constant->register_idx + 1, register_count);
7186 if (!constant->sysval_semantic)
7187 ++user_constant_count;
7190 if (user_constant_count)
7191 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7192 input_setup ? "in" : "out", user_constant_count);
7193 if (input_setup)
7194 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7196 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7197 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7199 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7200 char reg_mask[6];
7202 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7204 if (input_setup)
7205 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7206 else
7207 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7209 shader_addline(buffer, " = ");
7211 if (input_setup)
7212 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7213 else
7214 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7216 shader_addline(buffer, ";\n");
7218 shader_addline(buffer, "}\n");
7221 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7222 const struct wined3d_gl_info *gl_info)
7224 const char *output = get_fragment_output(gl_info);
7226 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7227 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7228 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7229 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7230 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7231 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7234 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7235 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7237 const char *output = get_fragment_output(gl_info);
7239 switch (mode)
7241 case WINED3D_FFP_PS_FOG_OFF:
7242 return;
7244 case WINED3D_FFP_PS_FOG_LINEAR:
7245 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7246 break;
7248 case WINED3D_FFP_PS_FOG_EXP:
7249 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7250 break;
7252 case WINED3D_FFP_PS_FOG_EXP2:
7253 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7254 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7255 break;
7257 default:
7258 ERR("Invalid fog mode %#x.\n", mode);
7259 return;
7262 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7263 output, output);
7266 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7267 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7269 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7270 * flipping all the operators here, just negate the comparison below. */
7271 static const char * const comparison_operator[] =
7273 "", /* WINED3D_CMP_NEVER */
7274 "<", /* WINED3D_CMP_LESS */
7275 "==", /* WINED3D_CMP_EQUAL */
7276 "<=", /* WINED3D_CMP_LESSEQUAL */
7277 ">", /* WINED3D_CMP_GREATER */
7278 "!=", /* WINED3D_CMP_NOTEQUAL */
7279 ">=", /* WINED3D_CMP_GREATEREQUAL */
7280 "" /* WINED3D_CMP_ALWAYS */
7283 if (alpha_func == WINED3D_CMP_ALWAYS)
7284 return;
7286 if (alpha_func != WINED3D_CMP_NEVER)
7287 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7288 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7289 shader_addline(buffer, " discard;\n");
7292 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7293 const struct wined3d_gl_info *gl_info)
7295 if (gl_info->supported[ARB_GPU_SHADER5])
7296 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7297 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7298 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7299 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7300 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7301 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7302 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7303 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7304 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7305 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7306 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7307 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7308 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7309 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7310 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7311 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7312 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7313 if (gl_info->supported[ARB_TEXTURE_GATHER])
7314 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7315 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7316 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7317 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7318 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7319 if (gl_info->supported[EXT_GPU_SHADER4])
7320 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7321 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7322 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7325 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7326 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7327 const struct ps_compile_args *args)
7329 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7331 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7332 if (reg_maps->shader_version.major < 2)
7333 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7335 if (args->srgb_correction)
7336 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7338 /* SM < 3 does not replace the fog stage. */
7339 if (reg_maps->shader_version.major < 3)
7340 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7342 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7345 /* Context activation is done by the caller. */
7346 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7347 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7348 const struct wined3d_shader *shader,
7349 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7351 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7352 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7353 const char *prefix = shader_glsl_get_prefix(version->type);
7354 const struct wined3d_gl_info *gl_info = context->gl_info;
7355 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7356 unsigned int i, extra_constants_needed = 0;
7357 struct shader_glsl_ctx_priv priv_ctx;
7358 GLuint shader_id;
7359 DWORD map;
7361 memset(&priv_ctx, 0, sizeof(priv_ctx));
7362 priv_ctx.cur_ps_args = args;
7363 priv_ctx.cur_np2fixup_info = np2fixup_info;
7364 priv_ctx.string_buffers = string_buffers;
7366 shader_glsl_add_version_declaration(buffer, gl_info);
7368 shader_glsl_enable_extensions(buffer, gl_info);
7369 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7370 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7371 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7372 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7373 if (shader_glsl_use_explicit_attrib_location(gl_info))
7374 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7375 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7376 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7377 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7378 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7379 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7380 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7381 /* The spec says that it doesn't have to be explicitly enabled, but the
7382 * nvidia drivers write a warning if we don't do so. */
7383 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7384 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7386 /* Base Declarations */
7387 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7389 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7391 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7392 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7393 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7394 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7397 /* Declare uniforms for NP2 texcoord fixup:
7398 * This is NOT done inside the loop that declares the texture samplers
7399 * since the NP2 fixup code is currently only used for the GeforceFX
7400 * series and when forcing the ARB_npot extension off. Modern cards just
7401 * skip the code anyway, so put it inside a separate loop. */
7402 if (args->np2_fixup)
7404 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7405 unsigned int cur = 0;
7407 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7408 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7409 * samplerNP2Fixup stores texture dimensions and is updated through
7410 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7412 for (i = 0; i < shader->limits->sampler; ++i)
7414 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7415 continue;
7417 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7419 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7420 continue;
7423 fixup->idx[i] = cur++;
7426 fixup->num_consts = (cur + 1) >> 1;
7427 fixup->active = args->np2_fixup;
7428 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7431 if (version->major < 3 || args->vp_mode != vertexshader)
7433 shader_addline(buffer, "uniform struct\n{\n");
7434 shader_addline(buffer, " vec4 color;\n");
7435 shader_addline(buffer, " float density;\n");
7436 shader_addline(buffer, " float end;\n");
7437 shader_addline(buffer, " float scale;\n");
7438 shader_addline(buffer, "} ffp_fog;\n");
7440 if (needs_legacy_glsl_syntax(gl_info))
7442 if (glsl_is_color_reg_read(shader, 0))
7443 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7444 if (glsl_is_color_reg_read(shader, 1))
7445 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7446 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7447 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7449 else
7451 if (glsl_is_color_reg_read(shader, 0))
7452 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7453 if (glsl_is_color_reg_read(shader, 1))
7454 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7455 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7456 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7457 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7461 if (version->major >= 3)
7463 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7465 if (args->vp_mode == vertexshader && reg_maps->input_registers)
7466 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7467 shader->u.ps.interpolation_mode, version->major >= 4);
7468 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7471 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7473 if (!(map & 1))
7474 continue;
7476 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7478 if (reg_maps->luminanceparams & (1u << i))
7480 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7481 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7482 extra_constants_needed++;
7485 extra_constants_needed++;
7488 if (args->srgb_correction)
7490 shader_addline(buffer, "const vec4 srgb_const0 = ");
7491 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7492 shader_addline(buffer, ";\n");
7493 shader_addline(buffer, "const vec4 srgb_const1 = ");
7494 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7495 shader_addline(buffer, ";\n");
7497 if (reg_maps->vpos || reg_maps->usesdsy)
7499 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7501 ++extra_constants_needed;
7502 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7504 if (reg_maps->vpos)
7506 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7508 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7509 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7510 args->render_offscreen ? "" : "origin_upper_left, ");
7511 else if (!args->render_offscreen)
7512 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7514 shader_addline(buffer, "vec4 vpos;\n");
7518 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7519 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7521 if (!needs_legacy_glsl_syntax(gl_info))
7523 if (shader_glsl_use_explicit_attrib_location(gl_info))
7524 shader_addline(buffer, "layout(location = 0) ");
7525 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
7528 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7529 FIXME("Insufficient uniforms to run this shader.\n");
7531 if (shader->u.ps.force_early_depth_stencil)
7532 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7534 shader_addline(buffer, "void main()\n{\n");
7536 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7537 * add approximately 0.5. This causes off-by-one problems as spotted by
7538 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7539 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7540 * causes precision troubles when we just subtract 0.5.
7542 * To deal with that, just floor() the position. This will eliminate the
7543 * fraction on all cards.
7545 * TODO: Test how this behaves with multisampling.
7547 * An advantage of floor is that it works even if the driver doesn't add
7548 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7549 * to return in gl_FragCoord, even though coordinates specify the pixel
7550 * centers instead of the pixel corners. This code will behave correctly
7551 * on drivers that returns integer values. */
7552 if (reg_maps->vpos)
7554 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7555 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7556 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7557 shader_addline(buffer,
7558 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7559 else
7560 shader_addline(buffer,
7561 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7564 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
7566 unsigned int i;
7567 WORD map = reg_maps->texcoord;
7569 if (legacy_syntax)
7571 if (glsl_is_color_reg_read(shader, 0))
7572 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7573 if (glsl_is_color_reg_read(shader, 1))
7574 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7577 for (i = 0; map; map >>= 1, ++i)
7579 if (map & 1)
7581 if (args->pointsprite)
7582 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7583 else if (args->texcoords_initialized & (1u << i))
7584 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7585 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7586 else
7587 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7588 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7592 if (legacy_syntax)
7593 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7596 /* Pack 3.0 inputs */
7597 if (reg_maps->shader_version.major >= 3)
7598 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7599 reg_maps->shader_version.major >= 4);
7601 /* Base Shader Body */
7602 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7603 return 0;
7605 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7606 if (reg_maps->shader_version.major < 4)
7607 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
7609 shader_addline(buffer, "}\n");
7611 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7612 TRACE("Compiling shader object %u.\n", shader_id);
7613 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7615 return shader_id;
7618 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7619 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7620 const struct vs_compile_args *args)
7622 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7623 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7624 unsigned int i;
7626 /* Unpack outputs. */
7627 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7629 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7630 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7631 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7632 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7634 if (reg_maps->shader_version.major < 3)
7636 if (args->fog_src == VS_FOG_Z)
7637 shader_addline(buffer, "%s = gl_Position.z;\n",
7638 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7639 else if (!reg_maps->fog)
7640 shader_addline(buffer, "%s = 0.0;\n",
7641 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7644 /* We always store the clipplanes without y inversion. */
7645 if (args->clip_enabled)
7647 if (legacy_syntax)
7648 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7649 else
7650 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7651 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7654 if (args->point_size && !args->per_vertex_point_size)
7655 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7657 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7658 shader_glsl_fixup_position(buffer);
7661 /* Context activation is done by the caller. */
7662 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
7663 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7665 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7666 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7667 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7668 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7669 const struct wined3d_gl_info *gl_info = context->gl_info;
7670 struct shader_glsl_ctx_priv priv_ctx;
7671 GLuint shader_id;
7672 unsigned int i;
7674 memset(&priv_ctx, 0, sizeof(priv_ctx));
7675 priv_ctx.cur_vs_args = args;
7676 priv_ctx.string_buffers = string_buffers;
7678 shader_glsl_add_version_declaration(buffer, gl_info);
7680 shader_glsl_enable_extensions(buffer, gl_info);
7681 if (gl_info->supported[ARB_DRAW_INSTANCED])
7682 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7683 if (shader_glsl_use_explicit_attrib_location(gl_info))
7684 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7686 /* Base Declarations */
7687 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7689 for (i = 0; i < shader->input_signature.element_count; ++i)
7690 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7692 if (args->point_size && !args->per_vertex_point_size)
7694 shader_addline(buffer, "uniform struct\n{\n");
7695 shader_addline(buffer, " float size;\n");
7696 shader_addline(buffer, " float size_min;\n");
7697 shader_addline(buffer, " float size_max;\n");
7698 shader_addline(buffer, "} ffp_point;\n");
7701 if (!needs_legacy_glsl_syntax(gl_info))
7703 if (args->clip_enabled)
7704 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7706 if (version->major < 3)
7708 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7709 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7710 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7711 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7715 if (version->major < 4)
7716 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
7718 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7719 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7721 if (reg_maps->shader_version.major >= 4)
7722 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
7723 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
7725 shader_addline(buffer, "void main()\n{\n");
7727 /* Base Shader Body */
7728 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7729 return 0;
7731 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7732 if (reg_maps->shader_version.major < 4)
7733 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7735 shader_addline(buffer, "}\n");
7737 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7738 TRACE("Compiling shader object %u.\n", shader_id);
7739 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7741 return shader_id;
7744 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
7745 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
7747 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7748 char reg_mask[6];
7749 unsigned int i;
7751 for (i = 0; i < output_signature->element_count; ++i)
7753 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7755 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7756 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
7757 output->register_idx, reg_mask, output->register_idx, reg_mask);
7761 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
7762 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
7763 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
7764 const char *phase_name, unsigned phase_idx)
7766 unsigned int i;
7767 HRESULT hr;
7769 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
7770 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
7771 for (i = 0; i < phase->temporary_count; ++i)
7772 shader_addline(buffer, "vec4 R%u;\n", i);
7773 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
7774 shader_addline(buffer, "}\n");
7775 return hr;
7778 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
7779 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
7781 if (phase->instance_count)
7783 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
7784 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
7785 shader_addline(buffer, "}\n");
7787 else
7789 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
7793 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
7794 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
7796 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7797 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7798 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7799 const struct wined3d_gl_info *gl_info = context->gl_info;
7800 const struct wined3d_hull_shader *hs = &shader->u.hs;
7801 const struct wined3d_shader_phase *phase;
7802 struct shader_glsl_ctx_priv priv_ctx;
7803 GLuint shader_id;
7804 unsigned int i;
7806 memset(&priv_ctx, 0, sizeof(priv_ctx));
7807 priv_ctx.string_buffers = string_buffers;
7809 shader_glsl_add_version_declaration(buffer, gl_info);
7811 shader_glsl_enable_extensions(buffer, gl_info);
7812 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7814 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7816 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
7818 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7819 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
7821 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
7823 if (hs->phases.control_point)
7825 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
7826 shader->limits->packed_output);
7827 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
7828 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
7829 shader_addline(buffer, "}\n");
7832 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
7833 if ((phase = hs->phases.control_point))
7835 for (i = 0; i < phase->temporary_count; ++i)
7836 shader_addline(buffer, "vec4 R%u;\n", i);
7837 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
7838 return 0;
7839 shader_addline(buffer, "setup_hs_output(hs_out);\n");
7841 else
7843 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
7845 shader_addline(buffer, "}\n");
7847 for (i = 0; i < hs->phases.fork_count; ++i)
7849 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7850 &hs->phases.fork[i], "fork", i)))
7851 return 0;
7854 for (i = 0; i < hs->phases.join_count; ++i)
7856 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7857 &hs->phases.join[i], "join", i)))
7858 return 0;
7861 shader_addline(buffer, "void main()\n{\n");
7862 shader_addline(buffer, "hs_control_point_phase();\n");
7863 if (reg_maps->vocp)
7864 shader_addline(buffer, "barrier();\n");
7865 for (i = 0; i < hs->phases.fork_count; ++i)
7866 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
7867 for (i = 0; i < hs->phases.join_count; ++i)
7868 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
7869 shader_addline(buffer, "setup_patch_constant_output();\n");
7870 shader_addline(buffer, "}\n");
7872 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
7873 TRACE("Compiling shader object %u.\n", shader_id);
7874 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7876 return shader_id;
7879 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
7880 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7881 const struct ds_compile_args *args)
7883 shader_addline(buffer, "setup_ds_output(ds_out);\n");
7885 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7886 shader_glsl_fixup_position(buffer);
7889 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
7890 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
7892 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7893 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7894 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7895 const struct wined3d_gl_info *gl_info = context->gl_info;
7896 struct shader_glsl_ctx_priv priv_ctx;
7897 GLuint shader_id;
7899 memset(&priv_ctx, 0, sizeof(priv_ctx));
7900 priv_ctx.cur_ds_args = args;
7901 priv_ctx.string_buffers = string_buffers;
7903 shader_glsl_add_version_declaration(buffer, gl_info);
7905 shader_glsl_enable_extensions(buffer, gl_info);
7906 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7908 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7910 shader_addline(buffer, "layout(");
7911 switch (shader->u.ds.tessellator_domain)
7913 case WINED3D_TESSELLATOR_DOMAIN_LINE:
7914 shader_addline(buffer, "isolines");
7915 break;
7916 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
7917 shader_addline(buffer, "quads");
7918 break;
7919 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
7920 shader_addline(buffer, "triangles");
7921 break;
7923 switch (args->tessellator_output_primitive)
7925 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
7926 if (args->render_offscreen)
7927 shader_addline(buffer, ", ccw");
7928 else
7929 shader_addline(buffer, ", cw");
7930 break;
7931 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
7932 if (args->render_offscreen)
7933 shader_addline(buffer, ", cw");
7934 else
7935 shader_addline(buffer, ", ccw");
7936 break;
7937 case WINED3D_TESSELLATOR_OUTPUT_POINT:
7938 shader_addline(buffer, ", point_mode");
7939 break;
7940 case WINED3D_TESSELLATOR_OUTPUT_LINE:
7941 break;
7943 switch (args->tessellator_partitioning)
7945 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
7946 shader_addline(buffer, ", fractional_odd_spacing");
7947 break;
7948 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
7949 shader_addline(buffer, ", fractional_even_spacing");
7950 break;
7951 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
7952 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
7953 shader_addline(buffer, ", equal_spacing");
7954 break;
7956 shader_addline(buffer, ") in;\n");
7958 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7960 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7961 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7963 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
7964 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
7965 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
7967 shader_addline(buffer, "void main()\n{\n");
7968 shader_addline(buffer, "setup_patch_constant_input();\n");
7970 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7971 return 0;
7973 shader_addline(buffer, "}\n");
7975 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
7976 TRACE("Compiling shader object %u.\n", shader_id);
7977 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7979 return shader_id;
7982 /* Context activation is done by the caller. */
7983 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
7984 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
7986 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7987 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7988 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7989 const struct wined3d_gl_info *gl_info = context->gl_info;
7990 struct shader_glsl_ctx_priv priv_ctx;
7991 GLuint shader_id;
7993 memset(&priv_ctx, 0, sizeof(priv_ctx));
7994 priv_ctx.string_buffers = string_buffers;
7996 shader_glsl_add_version_declaration(buffer, gl_info);
7998 shader_glsl_enable_extensions(buffer, gl_info);
8000 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8002 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
8003 if (shader->u.gs.instance_count > 1)
8004 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8005 shader_addline(buffer, ") in;\n");
8006 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8007 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
8008 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8010 if (!gl_info->supported[ARB_CLIP_CONTROL])
8011 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8013 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8014 gl_info, TRUE, args->interpolation_mode);
8015 shader_addline(buffer, "void main()\n{\n");
8016 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8017 return 0;
8018 shader_addline(buffer, "}\n");
8020 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8021 TRACE("Compiling shader object %u.\n", shader_id);
8022 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8024 return shader_id;
8027 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8029 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8030 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8031 struct wined3d_string_buffer *buffer = ctx->buffer;
8032 const struct wined3d_shader *shader = ctx->shader;
8034 switch (shader->reg_maps.shader_version.type)
8036 case WINED3D_SHADER_TYPE_PIXEL:
8037 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args);
8038 break;
8039 case WINED3D_SHADER_TYPE_VERTEX:
8040 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8041 break;
8042 case WINED3D_SHADER_TYPE_DOMAIN:
8043 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8044 break;
8045 case WINED3D_SHADER_TYPE_GEOMETRY:
8046 case WINED3D_SHADER_TYPE_COMPUTE:
8047 break;
8048 default:
8049 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8050 break;
8054 /* Context activation is done by the caller. */
8055 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8056 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8057 const struct wined3d_shader *shader)
8059 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8060 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8061 const struct wined3d_gl_info *gl_info = context->gl_info;
8062 struct shader_glsl_ctx_priv priv_ctx;
8063 GLuint shader_id;
8064 unsigned int i;
8066 memset(&priv_ctx, 0, sizeof(priv_ctx));
8067 priv_ctx.string_buffers = string_buffers;
8069 shader_glsl_add_version_declaration(buffer, gl_info);
8071 shader_glsl_enable_extensions(buffer, gl_info);
8072 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8074 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8076 for (i = 0; i < reg_maps->tgsm_count; ++i)
8078 if (reg_maps->tgsm[i].size)
8079 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8082 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8083 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8085 shader_addline(buffer, "void main()\n{\n");
8086 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8087 shader_addline(buffer, "}\n");
8089 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8090 TRACE("Compiling shader object %u.\n", shader_id);
8091 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8093 return shader_id;
8096 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8097 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8098 struct wined3d_shader *shader,
8099 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8101 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8102 struct glsl_shader_private *shader_data;
8103 struct ps_np2fixup_info *np2fixup;
8104 UINT i;
8105 DWORD new_size;
8106 GLuint ret;
8108 if (!shader->backend_data)
8110 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8111 if (!shader->backend_data)
8113 ERR("Failed to allocate backend data.\n");
8114 return 0;
8117 shader_data = shader->backend_data;
8118 gl_shaders = shader_data->gl_shaders.ps;
8120 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8121 * so a linear search is more performant than a hashmap or a binary search
8122 * (cache coherency etc)
8124 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8126 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8128 if (args->np2_fixup)
8129 *np2fixup_info = &gl_shaders[i].np2fixup;
8130 return gl_shaders[i].id;
8134 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8135 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8136 if (shader_data->num_gl_shaders)
8138 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8139 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
8140 new_size * sizeof(*gl_shaders));
8142 else
8144 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8145 new_size = 1;
8148 if(!new_array) {
8149 ERR("Out of memory\n");
8150 return 0;
8152 shader_data->gl_shaders.ps = new_array;
8153 shader_data->shader_array_size = new_size;
8154 gl_shaders = new_array;
8157 gl_shaders[shader_data->num_gl_shaders].args = *args;
8159 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8160 memset(np2fixup, 0, sizeof(*np2fixup));
8161 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8163 pixelshader_update_resource_types(shader, args->tex_types);
8165 string_buffer_clear(buffer);
8166 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8167 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8169 return ret;
8172 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8173 const DWORD use_map)
8175 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
8176 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
8177 if (stored->point_size != new->point_size)
8178 return FALSE;
8179 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8180 return FALSE;
8181 if (stored->flatshading != new->flatshading)
8182 return FALSE;
8183 if (stored->next_shader_type != new->next_shader_type)
8184 return FALSE;
8185 if (stored->next_shader_input_count != new->next_shader_input_count)
8186 return FALSE;
8187 return stored->fog_src == new->fog_src;
8190 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8191 struct wined3d_shader *shader, const struct vs_compile_args *args)
8193 UINT i;
8194 DWORD new_size;
8195 DWORD use_map = context->stream_info.use_map;
8196 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8197 struct glsl_shader_private *shader_data;
8198 GLuint ret;
8200 if (!shader->backend_data)
8202 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8203 if (!shader->backend_data)
8205 ERR("Failed to allocate backend data.\n");
8206 return 0;
8209 shader_data = shader->backend_data;
8210 gl_shaders = shader_data->gl_shaders.vs;
8212 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8213 * so a linear search is more performant than a hashmap or a binary search
8214 * (cache coherency etc)
8216 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8218 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8219 return gl_shaders[i].id;
8222 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8224 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8225 if (shader_data->num_gl_shaders)
8227 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8228 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
8229 new_size * sizeof(*gl_shaders));
8231 else
8233 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8234 new_size = 1;
8237 if(!new_array) {
8238 ERR("Out of memory\n");
8239 return 0;
8241 shader_data->gl_shaders.vs = new_array;
8242 shader_data->shader_array_size = new_size;
8243 gl_shaders = new_array;
8246 gl_shaders[shader_data->num_gl_shaders].args = *args;
8248 string_buffer_clear(&priv->shader_buffer);
8249 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8250 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8252 return ret;
8255 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8256 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8258 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8259 struct glsl_shader_private *shader_data;
8260 unsigned int new_size;
8261 GLuint ret;
8263 if (!shader->backend_data)
8265 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8267 ERR("Failed to allocate backend data.\n");
8268 return 0;
8271 shader_data = shader->backend_data;
8272 gl_shaders = shader_data->gl_shaders.hs;
8274 if (shader_data->num_gl_shaders > 0)
8276 assert(shader_data->num_gl_shaders == 1);
8277 return gl_shaders[0].id;
8280 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8282 assert(!shader_data->gl_shaders.hs);
8283 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8284 new_size = 1;
8285 if (!new_array)
8287 ERR("Failed to allocate GL shaders array.\n");
8288 return 0;
8290 shader_data->gl_shaders.hs = new_array;
8291 shader_data->shader_array_size = new_size;
8292 gl_shaders = new_array;
8294 string_buffer_clear(&priv->shader_buffer);
8295 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8296 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8298 return ret;
8301 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8302 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8304 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8305 struct glsl_shader_private *shader_data;
8306 unsigned int i, new_size;
8307 GLuint ret;
8309 if (!shader->backend_data)
8311 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8313 ERR("Failed to allocate backend data.\n");
8314 return 0;
8317 shader_data = shader->backend_data;
8318 gl_shaders = shader_data->gl_shaders.ds;
8320 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8322 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8323 return gl_shaders[i].id;
8326 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8328 if (shader_data->num_gl_shaders)
8330 new_size = shader_data->shader_array_size + 1;
8331 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ds,
8332 new_size * sizeof(*new_array));
8334 else
8336 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8337 new_size = 1;
8340 if (!new_array)
8342 ERR("Failed to allocate GL shaders array.\n");
8343 return 0;
8345 shader_data->gl_shaders.ds = new_array;
8346 shader_data->shader_array_size = new_size;
8347 gl_shaders = new_array;
8349 string_buffer_clear(&priv->shader_buffer);
8350 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8351 gl_shaders[shader_data->num_gl_shaders].args = *args;
8352 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8354 return ret;
8357 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8358 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8360 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8361 struct glsl_shader_private *shader_data;
8362 unsigned int i, new_size;
8363 GLuint ret;
8365 if (!shader->backend_data)
8367 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8369 ERR("Failed to allocate backend data.\n");
8370 return 0;
8373 shader_data = shader->backend_data;
8374 gl_shaders = shader_data->gl_shaders.gs;
8376 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8378 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8379 return gl_shaders[i].id;
8382 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8384 if (shader_data->num_gl_shaders)
8386 new_size = shader_data->shader_array_size + 1;
8387 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
8388 new_size * sizeof(*new_array));
8390 else
8392 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8393 new_size = 1;
8396 if (!new_array)
8398 ERR("Failed to allocate GL shaders array.\n");
8399 return 0;
8401 shader_data->gl_shaders.gs = new_array;
8402 shader_data->shader_array_size = new_size;
8403 gl_shaders = new_array;
8405 string_buffer_clear(&priv->shader_buffer);
8406 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8407 gl_shaders[shader_data->num_gl_shaders].args = *args;
8408 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8410 return ret;
8413 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8415 switch (mcs)
8417 case WINED3D_MCS_MATERIAL:
8418 return material;
8419 case WINED3D_MCS_COLOR1:
8420 return "ffp_attrib_diffuse";
8421 case WINED3D_MCS_COLOR2:
8422 return "ffp_attrib_specular";
8423 default:
8424 ERR("Invalid material color source %#x.\n", mcs);
8425 return "<invalid>";
8429 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8430 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8432 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8433 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8434 if (settings->localviewer)
8435 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8436 else
8437 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8438 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8439 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8442 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8443 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8445 const char *diffuse, *specular, *emissive, *ambient;
8446 unsigned int i, idx;
8448 if (!settings->lighting)
8450 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8451 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8452 return;
8455 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8456 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8457 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8458 shader_addline(buffer, "vec3 dir, dst;\n");
8459 shader_addline(buffer, "float att, t;\n");
8461 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8462 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8463 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8464 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8466 idx = 0;
8467 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8469 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8470 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8471 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8472 shader_addline(buffer, "dst.x = 1.0;\n");
8473 if (legacy_lighting)
8475 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8476 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8477 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8479 else
8481 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8483 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8484 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8485 if (!legacy_lighting)
8486 shader_addline(buffer, "att = 1.0 / att;\n");
8487 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8488 if (!settings->normal)
8490 shader_addline(buffer, "}\n");
8491 continue;
8493 shader_addline(buffer, "dir = normalize(dir);\n");
8494 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8495 shader_addline(buffer, "}\n");
8498 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8500 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8501 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8502 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8503 shader_addline(buffer, "dst.x = 1.0;\n");
8504 if (legacy_lighting)
8506 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8507 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8508 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8510 else
8512 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8514 shader_addline(buffer, "dir = normalize(dir);\n");
8515 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8516 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8517 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8518 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8519 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8520 idx, idx, idx, idx);
8521 if (legacy_lighting)
8522 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8523 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8524 idx, idx, idx);
8525 else
8526 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8527 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8528 idx, idx, idx);
8529 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8530 if (!settings->normal)
8532 shader_addline(buffer, "}\n");
8533 continue;
8535 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8536 shader_addline(buffer, "}\n");
8539 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8541 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8542 if (!settings->normal)
8543 continue;
8544 shader_addline(buffer, "att = 1.0;\n");
8545 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8546 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8549 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8551 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8552 if (!settings->normal)
8553 continue;
8554 shader_addline(buffer, "att = 1.0;\n");
8555 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8556 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8559 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8560 ambient, diffuse, emissive);
8561 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8562 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8565 /* Context activation is done by the caller. */
8566 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8567 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8569 static const struct attrib_info
8571 const char type[6];
8572 const char name[24];
8574 attrib_info[] =
8576 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8577 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8578 /* TODO: Indexed vertex blending */
8579 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8580 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8581 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8582 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8583 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8585 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8586 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8587 BOOL output_legacy_fogcoord = legacy_syntax;
8588 BOOL legacy_lighting = priv->legacy_lighting;
8589 GLuint shader_obj;
8590 unsigned int i;
8592 string_buffer_clear(buffer);
8594 shader_glsl_add_version_declaration(buffer, gl_info);
8596 if (shader_glsl_use_explicit_attrib_location(gl_info))
8597 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8599 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8601 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8603 if (shader_glsl_use_explicit_attrib_location(gl_info))
8604 shader_addline(buffer, "layout(location = %u) ", i);
8605 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8607 shader_addline(buffer, "\n");
8609 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8610 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8611 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8612 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
8614 shader_addline(buffer, "uniform struct\n{\n");
8615 shader_addline(buffer, " vec4 emissive;\n");
8616 shader_addline(buffer, " vec4 ambient;\n");
8617 shader_addline(buffer, " vec4 diffuse;\n");
8618 shader_addline(buffer, " vec4 specular;\n");
8619 shader_addline(buffer, " float shininess;\n");
8620 shader_addline(buffer, "} ffp_material;\n");
8622 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8623 shader_addline(buffer, "uniform struct\n{\n");
8624 shader_addline(buffer, " vec4 diffuse;\n");
8625 shader_addline(buffer, " vec4 specular;\n");
8626 shader_addline(buffer, " vec4 ambient;\n");
8627 shader_addline(buffer, " vec4 position;\n");
8628 shader_addline(buffer, " vec3 direction;\n");
8629 shader_addline(buffer, " float range;\n");
8630 shader_addline(buffer, " float falloff;\n");
8631 shader_addline(buffer, " float c_att;\n");
8632 shader_addline(buffer, " float l_att;\n");
8633 shader_addline(buffer, " float q_att;\n");
8634 shader_addline(buffer, " float cos_htheta;\n");
8635 shader_addline(buffer, " float cos_hphi;\n");
8636 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
8638 if (settings->point_size)
8640 shader_addline(buffer, "uniform struct\n{\n");
8641 shader_addline(buffer, " float size;\n");
8642 shader_addline(buffer, " float size_min;\n");
8643 shader_addline(buffer, " float size_max;\n");
8644 shader_addline(buffer, " float c_att;\n");
8645 shader_addline(buffer, " float l_att;\n");
8646 shader_addline(buffer, " float q_att;\n");
8647 shader_addline(buffer, "} ffp_point;\n");
8650 if (legacy_syntax)
8652 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8653 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8654 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8655 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8657 else
8659 if (settings->clipping)
8660 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8662 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8663 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8664 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8665 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8668 shader_addline(buffer, "\nvoid main()\n{\n");
8669 shader_addline(buffer, "float m;\n");
8670 shader_addline(buffer, "vec3 r;\n");
8672 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
8674 if (attrib_info[i].name[0])
8675 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
8676 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
8678 for (i = 0; i < MAX_TEXTURES; ++i)
8680 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
8681 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
8682 && settings->texcoords & (1u << i))
8683 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
8686 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
8688 if (settings->transformed)
8690 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
8691 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8692 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
8694 else
8696 for (i = 0; i < settings->vertexblends; ++i)
8697 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
8699 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
8700 for (i = 0; i < settings->vertexblends + 1; ++i)
8701 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
8703 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8704 if (settings->clipping)
8706 if (legacy_syntax)
8707 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
8708 else
8709 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8710 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
8712 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
8715 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
8716 if (settings->normal)
8718 if (!settings->vertexblends)
8720 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
8722 else
8724 for (i = 0; i < settings->vertexblends + 1; ++i)
8725 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
8728 if (settings->normalize)
8729 shader_addline(buffer, "normal = normalize(normal);\n");
8732 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
8733 if (legacy_syntax)
8735 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
8736 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
8738 else
8740 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
8741 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
8744 for (i = 0; i < MAX_TEXTURES; ++i)
8746 BOOL output_legacy_texcoord = legacy_syntax;
8748 switch (settings->texgen[i] & 0xffff0000)
8750 case WINED3DTSS_TCI_PASSTHRU:
8751 if (settings->texcoords & (1u << i))
8752 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
8753 i, i, i);
8754 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
8755 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
8756 else
8757 output_legacy_texcoord = FALSE;
8758 break;
8760 case WINED3DTSS_TCI_CAMERASPACENORMAL:
8761 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
8762 break;
8764 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
8765 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
8766 break;
8768 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
8769 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8770 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
8771 break;
8773 case WINED3DTSS_TCI_SPHEREMAP:
8774 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
8775 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
8776 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8777 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
8778 break;
8780 default:
8781 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
8782 break;
8784 if (output_legacy_texcoord)
8785 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
8788 switch (settings->fog_mode)
8790 case WINED3D_FFP_VS_FOG_OFF:
8791 output_legacy_fogcoord = FALSE;
8792 break;
8794 case WINED3D_FFP_VS_FOG_FOGCOORD:
8795 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
8796 break;
8798 case WINED3D_FFP_VS_FOG_RANGE:
8799 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
8800 break;
8802 case WINED3D_FFP_VS_FOG_DEPTH:
8803 if (settings->ortho_fog)
8805 if (gl_info->supported[ARB_CLIP_CONTROL])
8806 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
8807 else
8808 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
8809 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
8811 else if (settings->transformed)
8813 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
8815 else
8817 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
8819 break;
8821 default:
8822 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
8823 break;
8825 if (output_legacy_fogcoord)
8826 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
8828 if (settings->point_size)
8830 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
8831 " + ffp_point.l_att * length(ec_pos.xyz)"
8832 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
8833 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
8834 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
8837 shader_addline(buffer, "}\n");
8839 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8840 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
8842 return shader_obj;
8845 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
8846 DWORD argnum, unsigned int stage, DWORD arg)
8848 const char *ret;
8850 if (arg == ARG_UNUSED)
8851 return "<unused arg>";
8853 switch (arg & WINED3DTA_SELECTMASK)
8855 case WINED3DTA_DIFFUSE:
8856 ret = "ffp_varying_diffuse";
8857 break;
8859 case WINED3DTA_CURRENT:
8860 ret = "ret";
8861 break;
8863 case WINED3DTA_TEXTURE:
8864 switch (stage)
8866 case 0: ret = "tex0"; break;
8867 case 1: ret = "tex1"; break;
8868 case 2: ret = "tex2"; break;
8869 case 3: ret = "tex3"; break;
8870 case 4: ret = "tex4"; break;
8871 case 5: ret = "tex5"; break;
8872 case 6: ret = "tex6"; break;
8873 case 7: ret = "tex7"; break;
8874 default:
8875 ret = "<invalid texture>";
8876 break;
8878 break;
8880 case WINED3DTA_TFACTOR:
8881 ret = "tex_factor";
8882 break;
8884 case WINED3DTA_SPECULAR:
8885 ret = "ffp_varying_specular";
8886 break;
8888 case WINED3DTA_TEMP:
8889 ret = "temp_reg";
8890 break;
8892 case WINED3DTA_CONSTANT:
8893 switch (stage)
8895 case 0: ret = "tss_const0"; break;
8896 case 1: ret = "tss_const1"; break;
8897 case 2: ret = "tss_const2"; break;
8898 case 3: ret = "tss_const3"; break;
8899 case 4: ret = "tss_const4"; break;
8900 case 5: ret = "tss_const5"; break;
8901 case 6: ret = "tss_const6"; break;
8902 case 7: ret = "tss_const7"; break;
8903 default:
8904 ret = "<invalid constant>";
8905 break;
8907 break;
8909 default:
8910 return "<unhandled arg>";
8913 if (arg & WINED3DTA_COMPLEMENT)
8915 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
8916 if (argnum == 0)
8917 ret = "arg0";
8918 else if (argnum == 1)
8919 ret = "arg1";
8920 else if (argnum == 2)
8921 ret = "arg2";
8924 if (arg & WINED3DTA_ALPHAREPLICATE)
8926 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
8927 if (argnum == 0)
8928 ret = "arg0";
8929 else if (argnum == 1)
8930 ret = "arg1";
8931 else if (argnum == 2)
8932 ret = "arg2";
8935 return ret;
8938 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
8939 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
8941 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
8943 if (color && alpha)
8944 dstmask = "";
8945 else if (color)
8946 dstmask = ".xyz";
8947 else
8948 dstmask = ".w";
8950 if (dst == tempreg)
8951 dstreg = "temp_reg";
8952 else
8953 dstreg = "ret";
8955 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
8956 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
8957 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
8959 switch (op)
8961 case WINED3D_TOP_DISABLE:
8962 break;
8964 case WINED3D_TOP_SELECT_ARG1:
8965 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
8966 break;
8968 case WINED3D_TOP_SELECT_ARG2:
8969 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
8970 break;
8972 case WINED3D_TOP_MODULATE:
8973 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8974 break;
8976 case WINED3D_TOP_MODULATE_4X:
8977 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
8978 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8979 break;
8981 case WINED3D_TOP_MODULATE_2X:
8982 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
8983 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8984 break;
8986 case WINED3D_TOP_ADD:
8987 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
8988 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8989 break;
8991 case WINED3D_TOP_ADD_SIGNED:
8992 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
8993 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8994 break;
8996 case WINED3D_TOP_ADD_SIGNED_2X:
8997 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
8998 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8999 break;
9001 case WINED3D_TOP_SUBTRACT:
9002 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9003 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9004 break;
9006 case WINED3D_TOP_ADD_SMOOTH:
9007 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9008 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9009 break;
9011 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9012 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9013 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9014 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9015 break;
9017 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9018 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9019 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9020 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9021 break;
9023 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9024 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9025 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9026 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9027 break;
9029 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9030 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9031 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9032 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9033 break;
9035 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9036 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9037 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9038 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9039 break;
9041 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9042 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9043 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9044 break;
9046 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9047 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9048 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9049 break;
9051 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9052 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9053 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9054 break;
9055 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9056 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9057 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9058 break;
9060 case WINED3D_TOP_BUMPENVMAP:
9061 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9062 /* These are handled in the first pass, nothing to do. */
9063 break;
9065 case WINED3D_TOP_DOTPRODUCT3:
9066 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9067 dstreg, dstmask, arg1, arg2, dstmask);
9068 break;
9070 case WINED3D_TOP_MULTIPLY_ADD:
9071 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9072 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9073 break;
9075 case WINED3D_TOP_LERP:
9076 /* MSDN isn't quite right here. */
9077 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9078 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9079 break;
9081 default:
9082 FIXME("Unhandled operation %#x.\n", op);
9083 break;
9087 /* Context activation is done by the caller. */
9088 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9089 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9091 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9092 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9093 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9094 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9095 const struct wined3d_gl_info *gl_info = context->gl_info;
9096 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9097 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9098 UINT lowest_disabled_stage;
9099 GLuint shader_id;
9100 DWORD arg0, arg1, arg2;
9101 unsigned int stage;
9103 string_buffer_clear(buffer);
9105 /* Find out which textures are read */
9106 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9108 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9109 break;
9111 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9112 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9113 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9115 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9116 || (stage == 0 && settings->color_key_enabled))
9117 tex_map |= 1u << stage;
9118 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9119 tfactor_used = TRUE;
9120 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9121 tempreg_used = TRUE;
9122 if (settings->op[stage].dst == tempreg)
9123 tempreg_used = TRUE;
9124 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9125 tss_const_map |= 1u << stage;
9127 switch (settings->op[stage].cop)
9129 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9130 lum_map |= 1u << stage;
9131 /* fall through */
9132 case WINED3D_TOP_BUMPENVMAP:
9133 bump_map |= 1u << stage;
9134 /* fall through */
9135 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9136 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9137 tex_map |= 1u << stage;
9138 break;
9140 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9141 tfactor_used = TRUE;
9142 break;
9144 default:
9145 break;
9148 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9149 continue;
9151 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9152 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9153 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9155 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9156 tex_map |= 1u << stage;
9157 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9158 tfactor_used = TRUE;
9159 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9160 tempreg_used = TRUE;
9161 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9162 tss_const_map |= 1u << stage;
9164 lowest_disabled_stage = stage;
9166 shader_glsl_add_version_declaration(buffer, gl_info);
9168 if (shader_glsl_use_explicit_attrib_location(gl_info))
9169 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9170 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9171 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9172 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9173 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9175 if (!needs_legacy_glsl_syntax(gl_info))
9177 if (shader_glsl_use_explicit_attrib_location(gl_info))
9178 shader_addline(buffer, "layout(location = 0) ");
9179 shader_addline(buffer, "out vec4 ps_out[1];\n");
9182 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9183 shader_addline(buffer, "vec4 ret;\n");
9184 if (tempreg_used || settings->sRGB_write)
9185 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9186 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9188 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9190 const char *sampler_type;
9192 if (tss_const_map & (1u << stage))
9193 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9195 if (!(tex_map & (1u << stage)))
9196 continue;
9198 switch (settings->op[stage].tex_type)
9200 case WINED3D_GL_RES_TYPE_TEX_1D:
9201 sampler_type = "1D";
9202 break;
9203 case WINED3D_GL_RES_TYPE_TEX_2D:
9204 sampler_type = "2D";
9205 break;
9206 case WINED3D_GL_RES_TYPE_TEX_3D:
9207 sampler_type = "3D";
9208 break;
9209 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9210 sampler_type = "Cube";
9211 break;
9212 case WINED3D_GL_RES_TYPE_TEX_RECT:
9213 sampler_type = "2DRect";
9214 break;
9215 default:
9216 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9217 sampler_type = NULL;
9218 break;
9220 if (sampler_type)
9222 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9223 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9224 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9227 shader_addline(buffer, "vec4 tex%u;\n", stage);
9229 if (!(bump_map & (1u << stage)))
9230 continue;
9231 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9233 if (!(lum_map & (1u << stage)))
9234 continue;
9235 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9236 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9238 if (tfactor_used)
9239 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9240 if (settings->color_key_enabled)
9241 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9242 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9244 if (settings->sRGB_write)
9246 shader_addline(buffer, "const vec4 srgb_const0 = ");
9247 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9248 shader_addline(buffer, ";\n");
9249 shader_addline(buffer, "const vec4 srgb_const1 = ");
9250 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9251 shader_addline(buffer, ";\n");
9254 shader_addline(buffer, "uniform struct\n{\n");
9255 shader_addline(buffer, " vec4 color;\n");
9256 shader_addline(buffer, " float density;\n");
9257 shader_addline(buffer, " float end;\n");
9258 shader_addline(buffer, " float scale;\n");
9259 shader_addline(buffer, "} ffp_fog;\n");
9261 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9262 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9264 if (legacy_syntax)
9266 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9267 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9268 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9269 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9270 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9272 else
9274 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9275 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9276 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9277 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9278 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9281 shader_addline(buffer, "void main()\n{\n");
9283 if (legacy_syntax)
9285 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9286 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9289 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9291 if (tex_map & (1u << stage))
9293 if (settings->pointsprite)
9294 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9295 else if (settings->texcoords_initialized & (1u << stage))
9296 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9297 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9298 else
9299 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9303 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9304 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9306 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9307 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9309 /* Generate texture sampling instructions */
9310 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9312 const char *texture_function, *coord_mask;
9313 BOOL proj;
9315 if (!(tex_map & (1u << stage)))
9316 continue;
9318 if (settings->op[stage].projected == proj_none)
9320 proj = FALSE;
9322 else if (settings->op[stage].projected == proj_count4
9323 || settings->op[stage].projected == proj_count3)
9325 proj = TRUE;
9327 else
9329 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9330 proj = TRUE;
9333 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9334 proj = FALSE;
9336 switch (settings->op[stage].tex_type)
9338 case WINED3D_GL_RES_TYPE_TEX_1D:
9339 if (proj)
9341 texture_function = "texture1DProj";
9342 coord_mask = "xw";
9344 else
9346 texture_function = "texture1D";
9347 coord_mask = "x";
9349 break;
9350 case WINED3D_GL_RES_TYPE_TEX_2D:
9351 if (proj)
9353 texture_function = "texture2DProj";
9354 coord_mask = "xyw";
9356 else
9358 texture_function = "texture2D";
9359 coord_mask = "xy";
9361 break;
9362 case WINED3D_GL_RES_TYPE_TEX_3D:
9363 if (proj)
9365 texture_function = "texture3DProj";
9366 coord_mask = "xyzw";
9368 else
9370 texture_function = "texture3D";
9371 coord_mask = "xyz";
9373 break;
9374 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9375 texture_function = "textureCube";
9376 coord_mask = "xyz";
9377 break;
9378 case WINED3D_GL_RES_TYPE_TEX_RECT:
9379 if (proj)
9381 texture_function = "texture2DRectProj";
9382 coord_mask = "xyw";
9384 else
9386 texture_function = "texture2DRect";
9387 coord_mask = "xy";
9389 break;
9390 default:
9391 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9392 texture_function = "";
9393 coord_mask = "xyzw";
9394 break;
9396 if (!legacy_syntax)
9397 texture_function = proj ? "textureProj" : "texture";
9399 if (stage > 0
9400 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9401 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9403 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9405 /* With projective textures, texbem only divides the static
9406 * texture coord, not the displacement, so multiply the
9407 * displacement with the dividing parameter before passing it to
9408 * TXP. */
9409 if (settings->op[stage].projected != proj_none)
9411 if (settings->op[stage].projected == proj_count4)
9413 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9414 stage, stage);
9415 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9417 else
9419 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9420 stage, stage);
9421 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9424 else
9426 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9429 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9430 stage, texture_function, stage, coord_mask);
9432 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9433 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9434 stage, stage - 1, stage - 1, stage - 1);
9436 else if (settings->op[stage].projected == proj_count3)
9438 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9439 stage, texture_function, stage, stage);
9441 else
9443 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9444 stage, texture_function, stage, stage, coord_mask);
9447 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9448 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9449 settings->op[stage].color_fixup);
9452 if (settings->color_key_enabled)
9454 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9455 shader_addline(buffer, " discard;\n");
9458 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9460 /* Generate the main shader */
9461 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9463 BOOL op_equal;
9465 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9466 break;
9468 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9469 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9470 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9471 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9472 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9473 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9474 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9475 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9476 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9477 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9478 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9479 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9480 else
9481 op_equal = settings->op[stage].aop == settings->op[stage].cop
9482 && settings->op[stage].carg0 == settings->op[stage].aarg0
9483 && settings->op[stage].carg1 == settings->op[stage].aarg1
9484 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9486 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9488 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9489 settings->op[stage].cop, settings->op[stage].carg0,
9490 settings->op[stage].carg1, settings->op[stage].carg2);
9492 else if (op_equal)
9494 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
9495 settings->op[stage].cop, settings->op[stage].carg0,
9496 settings->op[stage].carg1, settings->op[stage].carg2);
9498 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9499 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9501 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9502 settings->op[stage].cop, settings->op[stage].carg0,
9503 settings->op[stage].carg1, settings->op[stage].carg2);
9504 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
9505 settings->op[stage].aop, settings->op[stage].aarg0,
9506 settings->op[stage].aarg1, settings->op[stage].aarg2);
9510 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9511 get_fragment_output(gl_info));
9513 if (settings->sRGB_write)
9514 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9516 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9518 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9520 shader_addline(buffer, "}\n");
9522 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9523 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9525 string_buffer_release(&priv->string_buffers, tex_reg_name);
9526 return shader_id;
9529 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9530 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9532 struct glsl_ffp_vertex_shader *shader;
9533 const struct wine_rb_entry *entry;
9535 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9536 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9538 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
9539 return NULL;
9541 shader->desc.settings = *settings;
9542 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9543 list_init(&shader->linked_programs);
9544 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9545 ERR("Failed to insert ffp vertex shader.\n");
9547 return shader;
9550 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9551 const struct ffp_frag_settings *args, const struct wined3d_context *context)
9553 struct glsl_ffp_fragment_shader *glsl_desc;
9554 const struct ffp_frag_desc *desc;
9556 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9557 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9559 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
9560 return NULL;
9562 glsl_desc->entry.settings = *args;
9563 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
9564 list_init(&glsl_desc->linked_programs);
9565 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9567 return glsl_desc;
9571 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9572 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9574 unsigned int i;
9575 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9577 for (i = 0; i < vs_c_count; ++i)
9579 string_buffer_sprintf(name, "vs_c[%u]", i);
9580 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9582 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9584 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9586 string_buffer_sprintf(name, "vs_i[%u]", i);
9587 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9590 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9592 string_buffer_sprintf(name, "vs_b[%u]", i);
9593 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9596 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9598 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9600 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9601 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9603 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9604 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9605 for (i = 0; i < MAX_TEXTURES; ++i)
9607 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9608 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9610 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9611 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9612 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9613 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9614 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9615 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9616 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
9618 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9619 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9620 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9621 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9622 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9623 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9624 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9625 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9626 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9627 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9628 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9629 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9630 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9631 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9632 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9633 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9634 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9635 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9636 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9637 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9638 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9639 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9640 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9641 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9643 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
9644 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
9645 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
9646 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
9647 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
9648 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
9649 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
9651 string_buffer_release(&priv->string_buffers, name);
9654 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
9655 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
9657 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9660 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
9661 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
9663 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9666 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
9667 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
9669 unsigned int i;
9670 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9672 for (i = 0; i < ps_c_count; ++i)
9674 string_buffer_sprintf(name, "ps_c[%u]", i);
9675 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9677 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
9679 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9681 string_buffer_sprintf(name, "ps_i[%u]", i);
9682 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9685 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9687 string_buffer_sprintf(name, "ps_b[%u]", i);
9688 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9691 for (i = 0; i < MAX_TEXTURES; ++i)
9693 string_buffer_sprintf(name, "bumpenv_mat%u", i);
9694 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9695 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
9696 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9697 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
9698 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9699 string_buffer_sprintf(name, "tss_const%u", i);
9700 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9703 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
9704 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
9706 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
9707 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
9708 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
9709 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
9711 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
9713 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
9714 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
9715 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
9717 string_buffer_release(&priv->string_buffers, name);
9720 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
9721 const struct wined3d_context *context, struct wined3d_shader *shader)
9723 struct glsl_context_data *ctx_data = context->shader_backend_data;
9724 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9725 const struct wined3d_gl_info *gl_info = context->gl_info;
9726 struct glsl_cs_compiled_shader *gl_shaders;
9727 struct glsl_shader_private *shader_data;
9728 struct glsl_shader_prog_link *entry;
9729 GLuint shader_id, program_id;
9731 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
9733 ERR("Out of memory.\n");
9734 return E_OUTOFMEMORY;
9737 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
9739 ERR("Failed to allocate backend data.\n");
9740 HeapFree(GetProcessHeap(), 0, entry);
9741 return E_OUTOFMEMORY;
9743 shader_data = shader->backend_data;
9744 gl_shaders = shader_data->gl_shaders.cs;
9746 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
9748 ERR("Failed to allocate GL shader array.\n");
9749 HeapFree(GetProcessHeap(), 0, entry);
9750 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9751 shader->backend_data = NULL;
9752 return E_OUTOFMEMORY;
9754 shader_data->shader_array_size = 1;
9755 gl_shaders = shader_data->gl_shaders.cs;
9757 TRACE("Compiling compute shader %p.\n", shader);
9759 string_buffer_clear(buffer);
9760 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
9761 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
9763 program_id = GL_EXTCALL(glCreateProgram());
9764 TRACE("Created new GLSL shader program %u.\n", program_id);
9766 entry->id = program_id;
9767 entry->vs.id = 0;
9768 entry->hs.id = 0;
9769 entry->ds.id = 0;
9770 entry->gs.id = 0;
9771 entry->ps.id = 0;
9772 entry->cs.id = shader_id;
9773 entry->constant_version = 0;
9774 entry->ps.np2_fixup_info = NULL;
9775 add_glsl_program_entry(priv, entry);
9777 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
9778 GL_EXTCALL(glAttachShader(program_id, shader_id));
9779 checkGLcall("glAttachShader");
9781 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
9783 TRACE("Linking GLSL shader program %u.\n", program_id);
9784 GL_EXTCALL(glLinkProgram(program_id));
9785 shader_glsl_validate_link(gl_info, program_id);
9787 GL_EXTCALL(glUseProgram(program_id));
9788 checkGLcall("glUseProgram");
9789 shader_glsl_load_program_resources(context, priv, program_id, shader);
9790 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
9792 entry->constant_update_mask = 0;
9794 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
9795 checkGLcall("glUseProgram");
9796 return WINED3D_OK;
9799 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
9800 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
9802 struct glsl_shader_private *shader_data;
9804 if (!shader->backend_data)
9806 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
9807 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
9809 ERR("Failed to compile compute shader %p.\n", shader);
9810 return 0;
9813 shader_data = shader->backend_data;
9814 return shader_data->gl_shaders.cs[0].id;
9817 /* Context activation is done by the caller. */
9818 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
9819 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9821 struct glsl_shader_prog_link *entry;
9822 struct wined3d_shader *shader;
9823 struct glsl_program_key key;
9824 GLuint cs_id;
9826 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
9827 return;
9829 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
9831 WARN("Compute shader is NULL.\n");
9832 ctx_data->glsl_program = NULL;
9833 return;
9836 cs_id = find_glsl_compute_shader(context, priv, shader);
9837 memset(&key, 0, sizeof(key));
9838 key.cs_id = cs_id;
9839 if (!(entry = get_glsl_program_entry(priv, &key)))
9840 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
9841 ctx_data->glsl_program = entry;
9844 /* Context activation is done by the caller. */
9845 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
9846 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9848 const struct wined3d_gl_info *gl_info = context->gl_info;
9849 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
9850 const struct ps_np2fixup_info *np2fixup_info = NULL;
9851 struct glsl_shader_prog_link *entry = NULL;
9852 struct wined3d_shader *hshader, *dshader, *gshader;
9853 struct wined3d_shader *vshader = NULL;
9854 struct wined3d_shader *pshader = NULL;
9855 GLuint reorder_shader_id = 0;
9856 struct glsl_program_key key;
9857 GLuint program_id;
9858 unsigned int i;
9859 GLuint vs_id = 0;
9860 GLuint hs_id = 0;
9861 GLuint ds_id = 0;
9862 GLuint gs_id = 0;
9863 GLuint ps_id = 0;
9864 struct list *ps_list, *vs_list;
9865 WORD attribs_map;
9866 struct wined3d_string_buffer *tmp_name;
9868 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
9870 vs_id = ctx_data->glsl_program->vs.id;
9871 vs_list = &ctx_data->glsl_program->vs.shader_entry;
9873 if (use_vs(state))
9874 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
9876 else if (use_vs(state))
9878 struct vs_compile_args vs_compile_args;
9880 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
9882 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
9883 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
9884 vs_list = &vshader->linked_programs;
9886 else if (priv->vertex_pipe == &glsl_vertex_pipe)
9888 struct glsl_ffp_vertex_shader *ffp_shader;
9889 struct wined3d_ffp_vs_settings settings;
9891 wined3d_ffp_get_vs_settings(context, state, &settings);
9892 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
9893 vs_id = ffp_shader->id;
9894 vs_list = &ffp_shader->linked_programs;
9897 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
9898 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
9899 hs_id = ctx_data->glsl_program->hs.id;
9900 else if (hshader)
9901 hs_id = find_glsl_hull_shader(context, priv, hshader);
9903 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
9904 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
9906 ds_id = ctx_data->glsl_program->ds.id;
9908 else if (dshader)
9910 struct ds_compile_args args;
9912 find_ds_compile_args(state, dshader, &args, context);
9913 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
9916 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
9917 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
9919 gs_id = ctx_data->glsl_program->gs.id;
9921 else if (gshader)
9923 struct gs_compile_args args;
9925 find_gs_compile_args(state, gshader, &args, context);
9926 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
9929 /* A pixel shader is not used when rasterization is disabled. */
9930 if (gshader && gshader->u.gs.so_desc.rasterizer_stream_idx == WINED3D_NO_RASTERIZER_STREAM)
9932 ps_id = 0;
9933 ps_list = NULL;
9935 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
9937 ps_id = ctx_data->glsl_program->ps.id;
9938 ps_list = &ctx_data->glsl_program->ps.shader_entry;
9940 if (use_ps(state))
9941 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
9943 else if (use_ps(state))
9945 struct ps_compile_args ps_compile_args;
9946 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
9947 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
9948 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
9949 pshader, &ps_compile_args, &np2fixup_info);
9950 ps_list = &pshader->linked_programs;
9952 else if (priv->fragment_pipe == &glsl_fragment_pipe
9953 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
9955 struct glsl_ffp_fragment_shader *ffp_shader;
9956 struct ffp_frag_settings settings;
9958 gen_ffp_frag_op(context, state, &settings, FALSE);
9959 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
9960 ps_id = ffp_shader->id;
9961 ps_list = &ffp_shader->linked_programs;
9964 key.vs_id = vs_id;
9965 key.hs_id = hs_id;
9966 key.ds_id = ds_id;
9967 key.gs_id = gs_id;
9968 key.ps_id = ps_id;
9969 key.cs_id = 0;
9970 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
9972 ctx_data->glsl_program = entry;
9973 return;
9976 /* If we get to this point, then no matching program exists, so we create one */
9977 program_id = GL_EXTCALL(glCreateProgram());
9978 TRACE("Created new GLSL shader program %u.\n", program_id);
9980 /* Create the entry */
9981 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
9982 entry->id = program_id;
9983 entry->vs.id = vs_id;
9984 entry->hs.id = hs_id;
9985 entry->ds.id = ds_id;
9986 entry->gs.id = gs_id;
9987 entry->ps.id = ps_id;
9988 entry->cs.id = 0;
9989 entry->constant_version = 0;
9990 entry->ps.np2_fixup_info = np2fixup_info;
9991 /* Add the hash table entry */
9992 add_glsl_program_entry(priv, entry);
9994 /* Set the current program */
9995 ctx_data->glsl_program = entry;
9997 /* Attach GLSL vshader */
9998 if (vs_id)
10000 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10001 GL_EXTCALL(glAttachShader(program_id, vs_id));
10002 checkGLcall("glAttachShader");
10004 list_add_head(vs_list, &entry->vs.shader_entry);
10007 if (vshader)
10009 attribs_map = vshader->reg_maps.input_registers;
10010 if (vshader->reg_maps.shader_version.major < 4)
10012 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10013 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10014 d3d_info->emulated_flatshading
10015 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10016 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10017 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10018 checkGLcall("glAttachShader");
10019 /* Flag the reorder function for deletion, it will be freed
10020 * automatically when the program is destroyed. */
10021 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10024 else
10026 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10029 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10031 /* Bind vertex attributes to a corresponding index number to match
10032 * the same index numbers as ARB_vertex_programs (makes loading
10033 * vertex attributes simpler). With this method, we can use the
10034 * exact same code to load the attributes later for both ARB and
10035 * GLSL shaders.
10037 * We have to do this here because we need to know the Program ID
10038 * in order to make the bindings work, and it has to be done prior
10039 * to linking the GLSL program. */
10040 tmp_name = string_buffer_get(&priv->string_buffers);
10041 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10043 if (!(attribs_map & 1))
10044 continue;
10046 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10047 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10048 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10050 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10051 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10052 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10053 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10056 checkGLcall("glBindAttribLocation");
10057 string_buffer_release(&priv->string_buffers, tmp_name);
10059 if (!needs_legacy_glsl_syntax(gl_info))
10061 GL_EXTCALL(glBindFragDataLocation(program_id, 0, "ps_out"));
10062 checkGLcall("glBindFragDataLocation");
10066 if (hshader)
10068 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10069 GL_EXTCALL(glAttachShader(program_id, hs_id));
10070 checkGLcall("glAttachShader");
10072 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10075 if (dshader)
10077 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10078 GL_EXTCALL(glAttachShader(program_id, ds_id));
10079 checkGLcall("glAttachShader");
10081 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10084 if (gshader)
10086 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10087 GL_EXTCALL(glAttachShader(program_id, gs_id));
10088 checkGLcall("glAttachShader");
10090 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10092 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10095 /* Attach GLSL pshader */
10096 if (ps_id)
10098 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10099 GL_EXTCALL(glAttachShader(program_id, ps_id));
10100 checkGLcall("glAttachShader");
10102 list_add_head(ps_list, &entry->ps.shader_entry);
10105 /* Link the program */
10106 TRACE("Linking GLSL shader program %u.\n", program_id);
10107 GL_EXTCALL(glLinkProgram(program_id));
10108 shader_glsl_validate_link(gl_info, program_id);
10110 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10111 vshader ? vshader->limits->constant_float : 0);
10112 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10113 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10114 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10115 pshader ? pshader->limits->constant_float : 0);
10116 checkGLcall("Find glsl program uniform locations");
10118 if (needs_legacy_glsl_syntax(gl_info))
10120 if (pshader && pshader->reg_maps.shader_version.major >= 3
10121 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10123 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10124 entry->vs.vertex_color_clamp = GL_FALSE;
10126 else
10128 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10131 else
10133 /* With core profile we never change vertex_color_clamp from
10134 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10135 * glClampColorARB(). */
10136 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10139 /* Set the shader to allow uniform loading on it */
10140 GL_EXTCALL(glUseProgram(program_id));
10141 checkGLcall("glUseProgram");
10143 entry->constant_update_mask = 0;
10144 if (vshader)
10146 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10147 if (vshader->reg_maps.integer_constants)
10148 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10149 if (vshader->reg_maps.boolean_constants)
10150 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10151 if (entry->vs.pos_fixup_location != -1)
10152 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10154 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10156 else
10158 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10159 | WINED3D_SHADER_CONST_FFP_PROJ;
10161 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10163 if (entry->vs.modelview_matrix_location[i] != -1)
10165 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10166 break;
10170 for (i = 0; i < MAX_TEXTURES; ++i)
10172 if (entry->vs.texture_matrix_location[i] != -1)
10174 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10175 break;
10178 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10179 || entry->vs.material_specular_location != -1
10180 || entry->vs.material_emissive_location != -1
10181 || entry->vs.material_shininess_location != -1)
10182 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10183 if (entry->vs.light_ambient_location != -1)
10184 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10186 if (entry->vs.clip_planes_location != -1)
10187 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10188 if (entry->vs.pointsize_min_location != -1)
10189 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10191 if (hshader)
10192 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10194 if (dshader)
10196 if (entry->ds.pos_fixup_location != -1)
10197 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10199 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10202 if (gshader)
10204 if (entry->gs.pos_fixup_location != -1)
10205 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10207 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10210 if (ps_id)
10212 if (pshader)
10214 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10215 if (pshader->reg_maps.integer_constants)
10216 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10217 if (pshader->reg_maps.boolean_constants)
10218 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10219 if (entry->ps.ycorrection_location != -1)
10220 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10222 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10223 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10225 else
10227 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10229 shader_glsl_load_samplers(context, priv, program_id, NULL);
10232 for (i = 0; i < MAX_TEXTURES; ++i)
10234 if (entry->ps.bumpenv_mat_location[i] != -1)
10236 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10237 break;
10241 if (entry->ps.fog_color_location != -1)
10242 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10243 if (entry->ps.alpha_test_ref_location != -1)
10244 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10245 if (entry->ps.np2_fixup_location != -1)
10246 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10247 if (entry->ps.color_key_location != -1)
10248 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10252 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10254 struct wined3d_device *device = shader->device;
10255 struct wined3d_context *context;
10257 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10259 context = context_acquire(device, NULL, 0);
10260 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10261 context_release(context);
10265 /* Context activation is done by the caller. */
10266 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10267 const struct wined3d_state *state)
10269 struct glsl_context_data *ctx_data = context->shader_backend_data;
10270 const struct wined3d_gl_info *gl_info = context->gl_info;
10271 struct shader_glsl_priv *priv = shader_priv;
10272 GLenum current_vertex_color_clamp;
10273 GLuint program_id, prev_id;
10275 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10276 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10278 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10280 set_glsl_shader_program(context, state, priv, ctx_data);
10282 if (ctx_data->glsl_program)
10284 program_id = ctx_data->glsl_program->id;
10285 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
10287 else
10289 program_id = 0;
10290 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10293 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10295 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10296 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10298 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10299 checkGLcall("glClampColorARB");
10301 else
10303 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10307 TRACE("Using GLSL program %u.\n", program_id);
10309 if (prev_id != program_id)
10311 GL_EXTCALL(glUseProgram(program_id));
10312 checkGLcall("glUseProgram");
10314 if (program_id)
10315 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
10318 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10321 /* Context activation is done by the caller. */
10322 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10323 const struct wined3d_state *state)
10325 struct glsl_context_data *ctx_data = context->shader_backend_data;
10326 const struct wined3d_gl_info *gl_info = context->gl_info;
10327 struct shader_glsl_priv *priv = shader_priv;
10328 GLuint program_id, prev_id;
10330 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10331 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10332 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10334 TRACE("Using GLSL program %u.\n", program_id);
10336 if (prev_id != program_id)
10338 GL_EXTCALL(glUseProgram(program_id));
10339 checkGLcall("glUseProgram");
10342 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10343 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10344 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10345 | (1u << WINED3D_SHADER_TYPE_HULL)
10346 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10349 /* "context" is not necessarily the currently active context. */
10350 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10352 struct glsl_context_data *ctx_data = context->shader_backend_data;
10354 ctx_data->glsl_program = NULL;
10355 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10356 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10357 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10358 | (1u << WINED3D_SHADER_TYPE_HULL)
10359 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10360 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10363 /* Context activation is done by the caller. */
10364 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10366 struct glsl_context_data *ctx_data = context->shader_backend_data;
10367 const struct wined3d_gl_info *gl_info = context->gl_info;
10368 struct shader_glsl_priv *priv = shader_priv;
10370 shader_glsl_invalidate_current_program(context);
10371 GL_EXTCALL(glUseProgram(0));
10372 checkGLcall("glUseProgram");
10374 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10375 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10377 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10379 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10380 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10381 checkGLcall("glClampColorARB");
10385 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10386 const struct glsl_shader_prog_link *program)
10388 const struct glsl_context_data *ctx_data;
10389 struct wined3d_context *context;
10390 unsigned int i;
10392 for (i = 0; i < device->context_count; ++i)
10394 context = device->contexts[i];
10395 ctx_data = context->shader_backend_data;
10397 if (ctx_data->glsl_program == program)
10398 shader_glsl_invalidate_current_program(context);
10402 static void shader_glsl_destroy(struct wined3d_shader *shader)
10404 struct glsl_shader_private *shader_data = shader->backend_data;
10405 struct wined3d_device *device = shader->device;
10406 struct shader_glsl_priv *priv = device->shader_priv;
10407 const struct wined3d_gl_info *gl_info;
10408 const struct list *linked_programs;
10409 struct wined3d_context *context;
10411 if (!shader_data || !shader_data->num_gl_shaders)
10413 HeapFree(GetProcessHeap(), 0, shader_data);
10414 shader->backend_data = NULL;
10415 return;
10418 context = context_acquire(device, NULL, 0);
10419 gl_info = context->gl_info;
10421 TRACE("Deleting linked programs.\n");
10422 linked_programs = &shader->linked_programs;
10423 if (linked_programs->next)
10425 struct glsl_shader_prog_link *entry, *entry2;
10426 UINT i;
10428 switch (shader->reg_maps.shader_version.type)
10430 case WINED3D_SHADER_TYPE_PIXEL:
10432 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10434 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10436 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10437 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10438 checkGLcall("glDeleteShader");
10440 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
10442 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10443 struct glsl_shader_prog_link, ps.shader_entry)
10445 shader_glsl_invalidate_contexts_program(device, entry);
10446 delete_glsl_program_entry(priv, gl_info, entry);
10449 break;
10452 case WINED3D_SHADER_TYPE_VERTEX:
10454 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10456 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10458 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10459 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10460 checkGLcall("glDeleteShader");
10462 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
10464 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10465 struct glsl_shader_prog_link, vs.shader_entry)
10467 shader_glsl_invalidate_contexts_program(device, entry);
10468 delete_glsl_program_entry(priv, gl_info, entry);
10471 break;
10474 case WINED3D_SHADER_TYPE_HULL:
10476 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10478 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10480 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10481 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10482 checkGLcall("glDeleteShader");
10484 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.hs);
10486 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10487 struct glsl_shader_prog_link, hs.shader_entry)
10489 shader_glsl_invalidate_contexts_program(device, entry);
10490 delete_glsl_program_entry(priv, gl_info, entry);
10493 break;
10496 case WINED3D_SHADER_TYPE_DOMAIN:
10498 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10500 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10502 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10503 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10504 checkGLcall("glDeleteShader");
10506 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ds);
10508 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10509 struct glsl_shader_prog_link, ds.shader_entry)
10511 shader_glsl_invalidate_contexts_program(device, entry);
10512 delete_glsl_program_entry(priv, gl_info, entry);
10515 break;
10518 case WINED3D_SHADER_TYPE_GEOMETRY:
10520 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10522 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10524 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10525 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10526 checkGLcall("glDeleteShader");
10528 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
10530 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10531 struct glsl_shader_prog_link, gs.shader_entry)
10533 shader_glsl_invalidate_contexts_program(device, entry);
10534 delete_glsl_program_entry(priv, gl_info, entry);
10537 break;
10540 case WINED3D_SHADER_TYPE_COMPUTE:
10542 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10544 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10546 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10547 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10548 checkGLcall("glDeleteShader");
10550 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
10552 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10553 struct glsl_shader_prog_link, cs.shader_entry)
10555 shader_glsl_invalidate_contexts_program(device, entry);
10556 delete_glsl_program_entry(priv, gl_info, entry);
10559 break;
10562 default:
10563 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10564 break;
10568 HeapFree(GetProcessHeap(), 0, shader->backend_data);
10569 shader->backend_data = NULL;
10571 context_release(context);
10574 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10576 const struct glsl_program_key *k = key;
10577 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10578 const struct glsl_shader_prog_link, program_lookup_entry);
10580 if (k->vs_id > prog->vs.id) return 1;
10581 else if (k->vs_id < prog->vs.id) return -1;
10583 if (k->gs_id > prog->gs.id) return 1;
10584 else if (k->gs_id < prog->gs.id) return -1;
10586 if (k->ps_id > prog->ps.id) return 1;
10587 else if (k->ps_id < prog->ps.id) return -1;
10589 if (k->hs_id > prog->hs.id) return 1;
10590 else if (k->hs_id < prog->hs.id) return -1;
10592 if (k->ds_id > prog->ds.id) return 1;
10593 else if (k->ds_id < prog->ds.id) return -1;
10595 if (k->cs_id > prog->cs.id) return 1;
10596 else if (k->cs_id < prog->cs.id) return -1;
10598 return 0;
10601 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10603 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10604 + constant_count * sizeof(*heap->contained)
10605 + constant_count * sizeof(*heap->positions);
10606 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
10608 if (!mem)
10610 ERR("Failed to allocate memory\n");
10611 return FALSE;
10614 heap->entries = mem;
10615 heap->entries[1].version = 0;
10616 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10617 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10618 heap->positions = (unsigned int *)(heap->contained + constant_count);
10619 heap->size = 1;
10621 return TRUE;
10624 static void constant_heap_free(struct constant_heap *heap)
10626 HeapFree(GetProcessHeap(), 0, heap->entries);
10629 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
10630 const struct fragment_pipeline *fragment_pipe)
10632 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
10633 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
10634 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
10635 struct fragment_caps fragment_caps;
10636 void *vertex_priv, *fragment_priv;
10638 string_buffer_list_init(&priv->string_buffers);
10640 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
10642 ERR("Failed to initialize vertex pipe.\n");
10643 HeapFree(GetProcessHeap(), 0, priv);
10644 return E_FAIL;
10647 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
10649 ERR("Failed to initialize fragment pipe.\n");
10650 vertex_pipe->vp_free(device);
10651 HeapFree(GetProcessHeap(), 0, priv);
10652 return E_FAIL;
10655 if (!string_buffer_init(&priv->shader_buffer))
10657 ERR("Failed to initialize shader buffer.\n");
10658 goto fail;
10661 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
10663 ERR("Failed to allocate memory.\n");
10664 goto fail;
10667 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
10669 ERR("Failed to initialize vertex shader constant heap\n");
10670 goto fail;
10673 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
10675 ERR("Failed to initialize pixel shader constant heap\n");
10676 goto fail;
10679 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
10681 priv->next_constant_version = 1;
10682 priv->vertex_pipe = vertex_pipe;
10683 priv->fragment_pipe = fragment_pipe;
10684 fragment_pipe->get_caps(gl_info, &fragment_caps);
10685 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
10686 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
10688 device->vertex_priv = vertex_priv;
10689 device->fragment_priv = fragment_priv;
10690 device->shader_priv = priv;
10692 return WINED3D_OK;
10694 fail:
10695 constant_heap_free(&priv->pconst_heap);
10696 constant_heap_free(&priv->vconst_heap);
10697 HeapFree(GetProcessHeap(), 0, priv->stack);
10698 string_buffer_free(&priv->shader_buffer);
10699 fragment_pipe->free_private(device);
10700 vertex_pipe->vp_free(device);
10701 HeapFree(GetProcessHeap(), 0, priv);
10702 return E_OUTOFMEMORY;
10705 /* Context activation is done by the caller. */
10706 static void shader_glsl_free(struct wined3d_device *device)
10708 struct shader_glsl_priv *priv = device->shader_priv;
10710 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
10711 constant_heap_free(&priv->pconst_heap);
10712 constant_heap_free(&priv->vconst_heap);
10713 HeapFree(GetProcessHeap(), 0, priv->stack);
10714 string_buffer_list_cleanup(&priv->string_buffers);
10715 string_buffer_free(&priv->shader_buffer);
10716 priv->fragment_pipe->free_private(device);
10717 priv->vertex_pipe->vp_free(device);
10719 HeapFree(GetProcessHeap(), 0, device->shader_priv);
10720 device->shader_priv = NULL;
10723 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
10725 struct glsl_context_data *ctx_data;
10726 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
10727 return FALSE;
10728 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10729 context->shader_backend_data = ctx_data;
10730 return TRUE;
10733 static void shader_glsl_free_context_data(struct wined3d_context *context)
10735 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
10738 static void shader_glsl_init_context_state(struct wined3d_context *context)
10740 const struct wined3d_gl_info *gl_info = context->gl_info;
10742 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
10743 checkGLcall("GL_PROGRAM_POINT_SIZE");
10746 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
10748 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
10749 && gl_info->supported[WINED3D_GL_VERSION_3_2]
10750 && gl_info->supported[ARB_SAMPLER_OBJECTS]
10751 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
10752 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
10754 if (shader_model_4
10755 && gl_info->supported[ARB_COMPUTE_SHADER]
10756 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
10757 && gl_info->supported[ARB_DRAW_INDIRECT]
10758 && gl_info->supported[ARB_GPU_SHADER5]
10759 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
10760 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
10761 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
10762 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
10763 && gl_info->supported[ARB_TESSELLATION_SHADER]
10764 && gl_info->supported[ARB_TEXTURE_GATHER]
10765 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
10766 return 5;
10768 if (shader_model_4)
10769 return 4;
10771 /* Support for texldd and texldl instructions in pixel shaders is required
10772 * for SM3. */
10773 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
10774 return 3;
10776 return 2;
10779 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
10781 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
10783 TRACE("Shader model %u.\n", shader_model);
10785 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
10786 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
10787 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
10788 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
10789 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
10790 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
10792 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
10793 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
10795 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
10796 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
10797 caps->varying_count = gl_info->limits.glsl_varyings;
10799 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
10800 * Direct3D minimum requirement.
10802 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
10803 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
10805 * The problem is that the refrast clamps temporary results in the shader to
10806 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
10807 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
10808 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
10809 * offer a way to query this.
10811 if (shader_model >= 4)
10812 caps->ps_1x_max_value = FLT_MAX;
10813 else
10814 caps->ps_1x_max_value = 1024.0f;
10816 /* Ideally we'd only set caps like sRGB writes here if supported by both
10817 * the shader backend and the fragment pipe, but we can get called before
10818 * shader_glsl_alloc(). */
10819 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
10820 | WINED3D_SHADER_CAP_SRGB_WRITE;
10823 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
10825 /* We support everything except YUV conversions. */
10826 return !is_complex_fixup(fixup);
10829 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
10831 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
10832 /* WINED3DSIH_ADD */ shader_glsl_binop,
10833 /* WINED3DSIH_AND */ shader_glsl_binop,
10834 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
10835 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
10836 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
10837 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
10838 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
10839 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
10840 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
10841 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
10842 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
10843 /* WINED3DSIH_BEM */ shader_glsl_bem,
10844 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
10845 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
10846 /* WINED3DSIH_BREAK */ shader_glsl_break,
10847 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
10848 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
10849 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
10850 /* WINED3DSIH_CALL */ shader_glsl_call,
10851 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
10852 /* WINED3DSIH_CASE */ shader_glsl_case,
10853 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
10854 /* WINED3DSIH_CND */ shader_glsl_cnd,
10855 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
10856 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
10857 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
10858 /* WINED3DSIH_CRS */ shader_glsl_cross,
10859 /* WINED3DSIH_CUT */ shader_glsl_cut,
10860 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
10861 /* WINED3DSIH_DCL */ shader_glsl_nop,
10862 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
10863 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
10864 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
10865 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
10866 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
10867 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
10868 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
10869 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
10870 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
10871 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
10872 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
10873 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
10874 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
10875 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
10876 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
10877 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
10878 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
10879 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
10880 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
10881 /* WINED3DSIH_DCL_INTERFACE */ NULL,
10882 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
10883 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
10884 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
10885 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
10886 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
10887 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
10888 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
10889 /* WINED3DSIH_DCL_STREAM */ NULL,
10890 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
10891 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
10892 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
10893 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
10894 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
10895 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
10896 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
10897 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
10898 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
10899 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
10900 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
10901 /* WINED3DSIH_DEF */ shader_glsl_nop,
10902 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
10903 /* WINED3DSIH_DEFB */ shader_glsl_nop,
10904 /* WINED3DSIH_DEFI */ shader_glsl_nop,
10905 /* WINED3DSIH_DIV */ shader_glsl_binop,
10906 /* WINED3DSIH_DP2 */ shader_glsl_dot,
10907 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
10908 /* WINED3DSIH_DP3 */ shader_glsl_dot,
10909 /* WINED3DSIH_DP4 */ shader_glsl_dot,
10910 /* WINED3DSIH_DST */ shader_glsl_dst,
10911 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
10912 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
10913 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
10914 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
10915 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
10916 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
10917 /* WINED3DSIH_ELSE */ shader_glsl_else,
10918 /* WINED3DSIH_EMIT */ shader_glsl_emit,
10919 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
10920 /* WINED3DSIH_ENDIF */ shader_glsl_end,
10921 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
10922 /* WINED3DSIH_ENDREP */ shader_glsl_end,
10923 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
10924 /* WINED3DSIH_EQ */ shader_glsl_relop,
10925 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
10926 /* WINED3DSIH_EXPP */ shader_glsl_expp,
10927 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
10928 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
10929 /* WINED3DSIH_FCALL */ NULL,
10930 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
10931 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
10932 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
10933 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
10934 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
10935 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
10936 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
10937 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
10938 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
10939 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
10940 /* WINED3DSIH_GE */ shader_glsl_relop,
10941 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
10942 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
10943 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
10944 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
10945 /* WINED3DSIH_IADD */ shader_glsl_binop,
10946 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
10947 /* WINED3DSIH_IEQ */ shader_glsl_relop,
10948 /* WINED3DSIH_IF */ shader_glsl_if,
10949 /* WINED3DSIH_IFC */ shader_glsl_ifc,
10950 /* WINED3DSIH_IGE */ shader_glsl_relop,
10951 /* WINED3DSIH_ILT */ shader_glsl_relop,
10952 /* WINED3DSIH_IMAD */ shader_glsl_mad,
10953 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
10954 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
10955 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
10956 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
10957 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
10958 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
10959 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
10960 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
10961 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
10962 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
10963 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
10964 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
10965 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
10966 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
10967 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
10968 /* WINED3DSIH_INE */ shader_glsl_relop,
10969 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
10970 /* WINED3DSIH_ISHL */ shader_glsl_binop,
10971 /* WINED3DSIH_ISHR */ shader_glsl_binop,
10972 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
10973 /* WINED3DSIH_LABEL */ shader_glsl_label,
10974 /* WINED3DSIH_LD */ shader_glsl_ld,
10975 /* WINED3DSIH_LD2DMS */ NULL,
10976 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
10977 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
10978 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
10979 /* WINED3DSIH_LIT */ shader_glsl_lit,
10980 /* WINED3DSIH_LOD */ NULL,
10981 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
10982 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
10983 /* WINED3DSIH_LOOP */ shader_glsl_loop,
10984 /* WINED3DSIH_LRP */ shader_glsl_lrp,
10985 /* WINED3DSIH_LT */ shader_glsl_relop,
10986 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
10987 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
10988 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
10989 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
10990 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
10991 /* WINED3DSIH_MAD */ shader_glsl_mad,
10992 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
10993 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
10994 /* WINED3DSIH_MOV */ shader_glsl_mov,
10995 /* WINED3DSIH_MOVA */ shader_glsl_mov,
10996 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
10997 /* WINED3DSIH_MUL */ shader_glsl_binop,
10998 /* WINED3DSIH_NE */ shader_glsl_relop,
10999 /* WINED3DSIH_NOP */ shader_glsl_nop,
11000 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11001 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11002 /* WINED3DSIH_OR */ shader_glsl_binop,
11003 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11004 /* WINED3DSIH_POW */ shader_glsl_pow,
11005 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11006 /* WINED3DSIH_REP */ shader_glsl_rep,
11007 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11008 /* WINED3DSIH_RET */ shader_glsl_ret,
11009 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11010 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11011 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11012 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11013 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11014 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11015 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11016 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11017 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11018 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11019 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11020 /* WINED3DSIH_SAMPLE_INFO */ NULL,
11021 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11022 /* WINED3DSIH_SAMPLE_POS */ NULL,
11023 /* WINED3DSIH_SETP */ NULL,
11024 /* WINED3DSIH_SGE */ shader_glsl_compare,
11025 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11026 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11027 /* WINED3DSIH_SLT */ shader_glsl_compare,
11028 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11029 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11030 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11031 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11032 /* WINED3DSIH_SUB */ shader_glsl_binop,
11033 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11034 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11035 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11036 /* WINED3DSIH_TEX */ shader_glsl_tex,
11037 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11038 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11039 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11040 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11041 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11042 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11043 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11044 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11045 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11046 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11047 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11048 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11049 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11050 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11051 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11052 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11053 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11054 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11055 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11056 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11057 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11058 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11059 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11060 /* WINED3DSIH_UGE */ shader_glsl_relop,
11061 /* WINED3DSIH_ULT */ shader_glsl_relop,
11062 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11063 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11064 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11065 /* WINED3DSIH_USHR */ shader_glsl_binop,
11066 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11067 /* WINED3DSIH_XOR */ shader_glsl_binop,
11070 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11071 SHADER_HANDLER hw_fct;
11073 /* Select handler */
11074 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11076 /* Unhandled opcode */
11077 if (!hw_fct)
11079 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11080 return;
11082 hw_fct(ins);
11084 shader_glsl_add_instruction_modifiers(ins);
11087 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11089 struct shader_glsl_priv *priv = shader_priv;
11091 return priv->ffp_proj_control;
11094 const struct wined3d_shader_backend_ops glsl_shader_backend =
11096 shader_glsl_handle_instruction,
11097 shader_glsl_precompile,
11098 shader_glsl_select,
11099 shader_glsl_select_compute,
11100 shader_glsl_disable,
11101 shader_glsl_update_float_vertex_constants,
11102 shader_glsl_update_float_pixel_constants,
11103 shader_glsl_load_constants,
11104 shader_glsl_destroy,
11105 shader_glsl_alloc,
11106 shader_glsl_free,
11107 shader_glsl_allocate_context_data,
11108 shader_glsl_free_context_data,
11109 shader_glsl_init_context_state,
11110 shader_glsl_get_caps,
11111 shader_glsl_color_fixup_supported,
11112 shader_glsl_has_ffp_proj_control,
11115 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11117 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11119 caps->xyzrhw = TRUE;
11120 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11121 caps->ffp_generic_attributes = TRUE;
11122 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11123 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11124 caps->max_vertex_blend_matrix_index = 0;
11125 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11126 | WINED3DVTXPCAPS_MATERIALSOURCE7
11127 | WINED3DVTXPCAPS_VERTEXFOG
11128 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11129 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11130 | WINED3DVTXPCAPS_LOCALVIEWER
11131 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11132 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11133 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11134 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11137 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11139 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11140 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11141 return 0;
11144 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11146 struct shader_glsl_priv *priv;
11148 if (shader_backend == &glsl_shader_backend)
11150 priv = shader_priv;
11151 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11152 return priv;
11155 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11157 return NULL;
11160 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11162 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11163 struct glsl_ffp_vertex_shader, desc.entry);
11164 struct glsl_shader_prog_link *program, *program2;
11165 struct glsl_ffp_destroy_ctx *ctx = context;
11167 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11168 struct glsl_shader_prog_link, vs.shader_entry)
11170 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11172 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11173 HeapFree(GetProcessHeap(), 0, shader);
11176 /* Context activation is done by the caller. */
11177 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11179 struct shader_glsl_priv *priv = device->vertex_priv;
11180 struct glsl_ffp_destroy_ctx ctx;
11182 ctx.priv = priv;
11183 ctx.gl_info = &device->adapter->gl_info;
11184 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11187 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11188 const struct wined3d_state *state, DWORD state_id) {}
11190 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11191 const struct wined3d_state *state, DWORD state_id)
11193 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11196 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11197 const struct wined3d_state *state, DWORD state_id)
11199 const struct wined3d_gl_info *gl_info = context->gl_info;
11200 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11201 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11202 BOOL transformed = context->stream_info.position_transformed;
11203 BOOL wasrhw = context->last_was_rhw;
11204 unsigned int i;
11206 context->last_was_rhw = transformed;
11208 /* If the vertex declaration contains a transformed position attribute,
11209 * the draw uses the fixed function vertex pipeline regardless of any
11210 * vertex shader set by the application. */
11211 if (transformed != wasrhw
11212 || context->stream_info.swizzle_map != context->last_swizzle_map)
11213 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11215 context->last_swizzle_map = context->stream_info.swizzle_map;
11217 if (!use_vs(state))
11219 if (context->last_was_vshader)
11221 if (legacy_clip_planes)
11222 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11223 clipplane(context, state, STATE_CLIPPLANE(i));
11224 else
11225 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11228 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11230 /* Because of settings->texcoords, we have to regenerate the vertex
11231 * shader on a vdecl change if there aren't enough varyings to just
11232 * always output all the texture coordinates. */
11233 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11234 || normal != context->last_was_normal)
11235 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11237 if (use_ps(state)
11238 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11239 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11240 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11242 else
11244 if (!context->last_was_vshader)
11246 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11247 if (legacy_clip_planes)
11248 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11249 clipplane(context, state, STATE_CLIPPLANE(i));
11250 else
11251 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11255 context->last_was_vshader = use_vs(state);
11256 context->last_was_normal = normal;
11259 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11260 const struct wined3d_state *state, DWORD state_id)
11262 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11263 /* Different vertex shaders potentially require a different vertex attributes setup. */
11264 if (!isStateDirty(context, STATE_VDECL))
11265 context_apply_state(context, state, STATE_VDECL);
11268 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11269 const struct wined3d_state *state, DWORD state_id)
11271 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11272 * winding) are defined in Hull Shaders, while in GLSL those are
11273 * specified in Tessellation Evaluation Shaders. */
11274 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11276 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11277 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11280 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11281 const struct wined3d_state *state, DWORD state_id)
11283 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11285 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11286 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11287 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11288 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11289 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11292 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11293 const struct wined3d_state *state, DWORD state_id)
11295 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11296 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11297 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11298 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11299 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11300 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11301 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11304 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11305 const struct wined3d_state *state, DWORD state_id)
11307 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11310 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11311 const struct wined3d_state *state, DWORD state_id)
11313 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11316 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11318 const struct wined3d_gl_info *gl_info = context->gl_info;
11319 unsigned int k;
11321 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11322 | WINED3D_SHADER_CONST_FFP_LIGHTS
11323 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11325 if (needs_legacy_glsl_syntax(gl_info))
11327 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11329 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11330 clipplane(context, state, STATE_CLIPPLANE(k));
11333 else
11335 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11339 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11340 const struct wined3d_state *state, DWORD state_id)
11342 /* Table fog behavior depends on the projection matrix. */
11343 if (state->render_states[WINED3D_RS_FOGENABLE]
11344 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11345 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11346 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11349 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11350 const struct wined3d_state *state, DWORD state_id)
11352 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11353 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11354 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11355 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11356 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11357 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11360 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11361 const struct wined3d_state *state, DWORD state_id)
11363 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11366 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11367 const struct wined3d_state *state, DWORD state_id)
11369 DWORD sampler = state_id - STATE_SAMPLER(0);
11370 const struct wined3d_texture *texture = state->textures[sampler];
11371 BOOL np2;
11373 if (!texture)
11374 return;
11376 if (sampler >= MAX_TEXTURES)
11377 return;
11379 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11380 || context->lastWasPow2Texture & (1u << sampler))
11382 if (np2)
11383 context->lastWasPow2Texture |= 1u << sampler;
11384 else
11385 context->lastWasPow2Texture &= ~(1u << sampler);
11387 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11391 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11392 const struct wined3d_state *state, DWORD state_id)
11394 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11397 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11398 const struct wined3d_state *state, DWORD state_id)
11400 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11403 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11404 const struct wined3d_state *state, DWORD state_id)
11406 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11409 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11410 const struct wined3d_state *state, DWORD state_id)
11412 if (!use_vs(state))
11413 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11416 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11417 const struct wined3d_state *state, DWORD state_id)
11419 static unsigned int once;
11421 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11422 FIXME("Non-point sprite points not supported in core profile.\n");
11425 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11426 const struct wined3d_state *state, DWORD state_id)
11428 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11431 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11432 const struct wined3d_state *state, DWORD state_id)
11434 const struct wined3d_gl_info *gl_info = context->gl_info;
11435 UINT index = state_id - STATE_CLIPPLANE(0);
11437 if (index >= gl_info->limits.user_clip_distances)
11438 return;
11440 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11443 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11445 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11446 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11447 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11448 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11449 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11450 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11451 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11452 /* Clip planes */
11453 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11454 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11455 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11456 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11457 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11458 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11459 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11460 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11461 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11462 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11463 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11464 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11465 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11466 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11467 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11468 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11469 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11470 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
11471 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11472 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
11473 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11474 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
11475 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11476 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
11477 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11478 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
11479 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11480 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
11481 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11482 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
11483 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11484 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
11485 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11486 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
11487 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11488 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
11489 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11490 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
11491 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11492 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
11493 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11494 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
11495 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11496 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
11497 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11498 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
11499 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11500 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
11501 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11502 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
11503 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11504 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
11505 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11506 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
11507 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11508 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
11509 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11510 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
11511 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11512 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
11513 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11514 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
11515 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11516 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
11517 /* Lights */
11518 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11519 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11520 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11521 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11522 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11523 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11524 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11525 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11526 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11527 /* Viewport */
11528 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11529 /* Transform states */
11530 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11531 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11532 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11533 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11534 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11535 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11536 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11537 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11538 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11539 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11540 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11541 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11542 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11543 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11544 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11545 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11546 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11547 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11548 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11549 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11550 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11551 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11552 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11553 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11554 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11555 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11556 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11557 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11558 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11559 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11560 /* Fog */
11561 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11562 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11563 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11564 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11565 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11566 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11567 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11568 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11569 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11570 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11571 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11572 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11573 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11574 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11575 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11576 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11577 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11578 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11579 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11580 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11581 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11582 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11583 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11584 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11585 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11586 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11587 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11588 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11589 /* NP2 texture matrix fixups. They are not needed if
11590 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11591 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11592 * matrix. */
11593 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11594 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11595 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11596 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11597 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11598 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11599 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11600 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11601 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11602 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11603 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11604 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11605 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11606 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11607 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11608 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11609 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11610 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11611 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11612 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11613 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11614 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11615 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11616 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11617 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11618 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11619 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11620 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11623 /* TODO:
11624 * - Implement vertex tweening. */
11625 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11627 glsl_vertex_pipe_vp_enable,
11628 glsl_vertex_pipe_vp_get_caps,
11629 glsl_vertex_pipe_vp_get_emul_mask,
11630 glsl_vertex_pipe_vp_alloc,
11631 glsl_vertex_pipe_vp_free,
11632 glsl_vertex_pipe_vp_states,
11635 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
11637 /* Nothing to do. */
11640 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
11642 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11643 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11644 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11645 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11646 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11647 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11648 | WINED3DTEXOPCAPS_SELECTARG1
11649 | WINED3DTEXOPCAPS_SELECTARG2
11650 | WINED3DTEXOPCAPS_MODULATE4X
11651 | WINED3DTEXOPCAPS_MODULATE2X
11652 | WINED3DTEXOPCAPS_MODULATE
11653 | WINED3DTEXOPCAPS_ADDSIGNED2X
11654 | WINED3DTEXOPCAPS_ADDSIGNED
11655 | WINED3DTEXOPCAPS_ADD
11656 | WINED3DTEXOPCAPS_SUBTRACT
11657 | WINED3DTEXOPCAPS_ADDSMOOTH
11658 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11659 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11660 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11661 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11662 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
11663 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
11664 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
11665 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
11666 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
11667 | WINED3DTEXOPCAPS_DOTPRODUCT3
11668 | WINED3DTEXOPCAPS_MULTIPLYADD
11669 | WINED3DTEXOPCAPS_LERP
11670 | WINED3DTEXOPCAPS_BUMPENVMAP
11671 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
11672 caps->MaxTextureBlendStages = MAX_TEXTURES;
11673 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
11676 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
11678 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11679 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11680 return 0;
11683 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11685 struct shader_glsl_priv *priv;
11687 if (shader_backend == &glsl_shader_backend)
11689 priv = shader_priv;
11690 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
11691 return priv;
11694 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
11696 return NULL;
11699 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
11701 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11702 struct glsl_ffp_fragment_shader, entry.entry);
11703 struct glsl_shader_prog_link *program, *program2;
11704 struct glsl_ffp_destroy_ctx *ctx = context;
11706 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11707 struct glsl_shader_prog_link, ps.shader_entry)
11709 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11711 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11712 HeapFree(GetProcessHeap(), 0, shader);
11715 /* Context activation is done by the caller. */
11716 static void glsl_fragment_pipe_free(struct wined3d_device *device)
11718 struct shader_glsl_priv *priv = device->fragment_priv;
11719 struct glsl_ffp_destroy_ctx ctx;
11721 ctx.priv = priv;
11722 ctx.gl_info = &device->adapter->gl_info;
11723 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
11726 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
11727 const struct wined3d_state *state, DWORD state_id)
11729 context->last_was_pshader = use_ps(state);
11731 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11734 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
11735 const struct wined3d_state *state, DWORD state_id)
11737 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11740 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
11741 const struct wined3d_state *state, DWORD state_id)
11743 BOOL use_vshader = use_vs(state);
11744 enum fogsource new_source;
11745 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
11746 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
11748 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11750 if (!state->render_states[WINED3D_RS_FOGENABLE])
11751 return;
11753 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
11755 if (use_vshader)
11756 new_source = FOGSOURCE_VS;
11757 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
11758 new_source = FOGSOURCE_COORD;
11759 else
11760 new_source = FOGSOURCE_FFP;
11762 else
11764 new_source = FOGSOURCE_FFP;
11767 if (new_source != context->fog_source || fogstart == fogend)
11769 context->fog_source = new_source;
11770 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11774 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
11775 const struct wined3d_state *state, DWORD state_id)
11777 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11778 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11779 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11781 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
11782 glsl_fragment_pipe_fog(context, state, state_id);
11785 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
11786 const struct wined3d_state *state, DWORD state_id)
11788 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11789 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11790 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11793 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
11794 const struct wined3d_state *state, DWORD state_id)
11796 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11799 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
11800 const struct wined3d_state *state, DWORD state_id)
11802 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
11805 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
11806 const struct wined3d_state *state, DWORD state_id)
11808 const struct wined3d_gl_info *gl_info = context->gl_info;
11809 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
11810 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
11812 if (func)
11814 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
11815 checkGLcall("glAlphaFunc");
11819 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
11820 const struct wined3d_state *state, DWORD state_id)
11822 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11825 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
11826 const struct wined3d_state *state, DWORD state_id)
11828 const struct wined3d_gl_info *gl_info = context->gl_info;
11830 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
11832 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
11833 checkGLcall("glEnable(GL_ALPHA_TEST)");
11835 else
11837 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
11838 checkGLcall("glDisable(GL_ALPHA_TEST)");
11842 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
11843 const struct wined3d_state *state, DWORD state_id)
11845 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
11848 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
11849 const struct wined3d_state *state, DWORD state_id)
11851 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
11854 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
11855 const struct wined3d_state *state, DWORD state_id)
11857 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11860 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
11862 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11863 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
11864 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11865 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11866 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11867 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11868 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11869 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11870 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11871 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11872 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11873 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11874 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11875 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11876 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11877 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11878 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11879 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11880 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11881 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11882 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11883 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11884 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11885 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11886 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11887 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11888 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11889 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11890 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11891 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11892 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11893 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11894 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11895 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11896 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11897 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11898 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11899 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11900 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11901 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11902 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11903 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11904 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11905 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11906 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11907 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11908 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11909 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11910 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11911 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11912 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11913 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11914 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11915 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11916 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11917 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11918 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11919 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11920 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11921 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11922 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11923 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11924 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11925 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11926 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11927 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11928 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11929 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11930 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11931 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11932 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11933 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11934 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11935 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11936 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11937 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
11938 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
11939 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
11940 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
11941 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
11942 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
11943 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
11944 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11945 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
11946 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
11947 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11948 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11949 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
11950 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
11951 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
11952 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11953 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
11954 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
11955 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
11956 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
11957 {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 },
11958 {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 },
11959 {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 },
11960 {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 },
11961 {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 },
11962 {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 },
11963 {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 },
11964 {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 },
11965 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11966 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11967 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11968 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11969 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11970 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11971 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11972 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11973 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11974 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
11975 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
11976 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
11977 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
11980 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
11982 return TRUE;
11985 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
11989 const struct fragment_pipeline glsl_fragment_pipe =
11991 glsl_fragment_pipe_enable,
11992 glsl_fragment_pipe_get_caps,
11993 glsl_fragment_pipe_get_emul_mask,
11994 glsl_fragment_pipe_alloc,
11995 glsl_fragment_pipe_free,
11996 glsl_fragment_pipe_alloc_context_data,
11997 glsl_fragment_pipe_free_context_data,
11998 shader_glsl_color_fixup_supported,
11999 glsl_fragment_pipe_state_template,