wined3d: Handle stream output components when rasterization is disabled.
[wine.git] / dlls / wined3d / glsl_shader.c
blob6d0e6a19ef90b9300268651668cf1d0c86642887
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 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
843 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
844 append_transform_feedback_skip_components(varyings, &count,
845 &strings, &length, buffer, e->component_count);
846 continue;
849 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
850 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
851 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
853 else
855 string_buffer_sprintf(buffer, "shader_in_out.reg%u", e->register_idx);
856 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
860 if (buffer_idx < so_desc->buffer_stride_count
861 && stride < so_desc->buffer_strides[buffer_idx] / 4)
863 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
864 append_transform_feedback_skip_components(varyings, &count,
865 &strings, &length, buffer, component_count);
868 if (highest_output_slot <= buffer_idx)
869 break;
871 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
873 string_buffer_sprintf(buffer, "gl_NextBuffer");
874 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
878 if (varying_count)
879 *varying_count = count;
880 if (strings_length)
881 *strings_length = length;
884 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
885 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
887 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
888 const struct wined3d_gl_info *gl_info = context->gl_info;
889 struct wined3d_string_buffer *buffer;
890 unsigned int i, count, length;
891 const char **varyings;
892 char *strings;
893 GLenum mode;
895 if (!so_desc->element_count)
896 return;
898 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
900 mode = GL_INTERLEAVED_ATTRIBS;
902 else
904 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
906 for (i = 0; i < so_desc->element_count; ++i)
908 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
910 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
911 return;
913 ++element_count[so_desc->elements[i].output_slot];
916 if (element_count[0] == so_desc->element_count)
918 mode = GL_INTERLEAVED_ATTRIBS;
920 else
922 mode = GL_SEPARATE_ATTRIBS;
923 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
925 if (element_count[i] != 1)
926 break;
928 for (; i < ARRAY_SIZE(element_count); ++i)
930 if (element_count[i])
932 FIXME("Only single element per buffer is allowed in separate mode.\n");
933 return;
939 buffer = string_buffer_get(&priv->string_buffers);
941 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode);
943 if (!(varyings = wined3d_calloc(count, sizeof(*varyings))))
945 ERR("Out of memory.\n");
946 string_buffer_release(&priv->string_buffers, buffer);
947 return;
949 if (!(strings = wined3d_calloc(length, sizeof(*strings))))
951 ERR("Out of memory.\n");
952 HeapFree(GetProcessHeap(), 0, varyings);
953 string_buffer_release(&priv->string_buffers, buffer);
954 return;
957 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
958 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
959 checkGLcall("glTransformFeedbackVaryings");
961 HeapFree(GetProcessHeap(), 0, varyings);
962 HeapFree(GetProcessHeap(), 0, strings);
963 string_buffer_release(&priv->string_buffers, buffer);
966 /* Context activation is done by the caller. */
967 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
968 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
970 unsigned int start = ~0U, end = 0;
971 int stack_idx = 0;
972 unsigned int heap_idx = 1;
973 unsigned int idx;
975 if (heap->entries[heap_idx].version <= version) return;
977 idx = heap->entries[heap_idx].idx;
978 if (constant_locations[idx] != -1)
979 start = end = idx;
980 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
982 while (stack_idx >= 0)
984 /* Note that we fall through to the next case statement. */
985 switch(stack[stack_idx])
987 case HEAP_NODE_TRAVERSE_LEFT:
989 unsigned int left_idx = heap_idx << 1;
990 if (left_idx < heap->size && heap->entries[left_idx].version > version)
992 heap_idx = left_idx;
993 idx = heap->entries[heap_idx].idx;
994 if (constant_locations[idx] != -1)
996 if (start > idx)
997 start = idx;
998 if (end < idx)
999 end = idx;
1002 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1003 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1004 break;
1008 case HEAP_NODE_TRAVERSE_RIGHT:
1010 unsigned int right_idx = (heap_idx << 1) + 1;
1011 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1013 heap_idx = right_idx;
1014 idx = heap->entries[heap_idx].idx;
1015 if (constant_locations[idx] != -1)
1017 if (start > idx)
1018 start = idx;
1019 if (end < idx)
1020 end = idx;
1023 stack[stack_idx++] = HEAP_NODE_POP;
1024 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1025 break;
1029 case HEAP_NODE_POP:
1030 heap_idx >>= 1;
1031 --stack_idx;
1032 break;
1035 if (start <= end)
1036 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1037 checkGLcall("walk_constant_heap()");
1040 /* Context activation is done by the caller. */
1041 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1042 GLint location, const struct wined3d_vec4 *data)
1044 GLfloat clamped_constant[4];
1046 if (location == -1) return;
1048 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1049 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1050 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1051 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1053 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1056 /* Context activation is done by the caller. */
1057 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1058 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1059 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1061 int stack_idx = 0;
1062 unsigned int heap_idx = 1;
1063 unsigned int idx;
1065 if (heap->entries[heap_idx].version <= version) return;
1067 idx = heap->entries[heap_idx].idx;
1068 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1069 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1071 while (stack_idx >= 0)
1073 /* Note that we fall through to the next case statement. */
1074 switch(stack[stack_idx])
1076 case HEAP_NODE_TRAVERSE_LEFT:
1078 unsigned int left_idx = heap_idx << 1;
1079 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1081 heap_idx = left_idx;
1082 idx = heap->entries[heap_idx].idx;
1083 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1085 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1086 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1087 break;
1091 case HEAP_NODE_TRAVERSE_RIGHT:
1093 unsigned int right_idx = (heap_idx << 1) + 1;
1094 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1096 heap_idx = right_idx;
1097 idx = heap->entries[heap_idx].idx;
1098 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1100 stack[stack_idx++] = HEAP_NODE_POP;
1101 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1102 break;
1106 case HEAP_NODE_POP:
1107 heap_idx >>= 1;
1108 --stack_idx;
1109 break;
1112 checkGLcall("walk_constant_heap_clamped()");
1115 /* Context activation is done by the caller. */
1116 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1117 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1118 unsigned char *stack, unsigned int version)
1120 const struct wined3d_shader_lconst *lconst;
1122 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1123 if (shader->reg_maps.shader_version.major == 1
1124 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1125 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1126 else
1127 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1129 if (!shader->load_local_constsF)
1131 TRACE("No need to load local float constants for this shader.\n");
1132 return;
1135 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1136 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1138 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1140 checkGLcall("glUniform4fv()");
1143 /* Context activation is done by the caller. */
1144 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1145 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1147 unsigned int i;
1148 struct list* ptr;
1150 for (i = 0; constants_set; constants_set >>= 1, ++i)
1152 if (!(constants_set & 1)) continue;
1154 /* We found this uniform name in the program - go ahead and send the data */
1155 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1158 /* Load immediate constants */
1159 ptr = list_head(&shader->constantsI);
1160 while (ptr)
1162 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1163 unsigned int idx = lconst->idx;
1164 const GLint *values = (const GLint *)lconst->value;
1166 /* We found this uniform name in the program - go ahead and send the data */
1167 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1168 ptr = list_next(&shader->constantsI, ptr);
1170 checkGLcall("glUniform4iv()");
1173 /* Context activation is done by the caller. */
1174 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1175 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1177 unsigned int i;
1178 struct list* ptr;
1180 for (i = 0; constants_set; constants_set >>= 1, ++i)
1182 if (!(constants_set & 1)) continue;
1184 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1187 /* Load immediate constants */
1188 ptr = list_head(&shader->constantsB);
1189 while (ptr)
1191 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1192 unsigned int idx = lconst->idx;
1193 const GLint *values = (const GLint *)lconst->value;
1195 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1196 ptr = list_next(&shader->constantsB, ptr);
1198 checkGLcall("glUniform1iv()");
1201 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1203 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1206 /* Context activation is done by the caller (state handler). */
1207 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1208 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1210 struct
1212 float sx, sy;
1214 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1215 UINT fixup = ps->np2_fixup_info->active;
1216 UINT i;
1218 for (i = 0; fixup; fixup >>= 1, ++i)
1220 const struct wined3d_texture *tex = state->textures[i];
1221 unsigned char idx = ps->np2_fixup_info->idx[i];
1223 if (!tex)
1225 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1226 continue;
1229 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1230 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1233 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1236 /* Taken and adapted from Mesa. */
1237 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1239 float pos, neg, t, det;
1240 struct wined3d_matrix temp;
1242 /* Calculate the determinant of upper left 3x3 submatrix and
1243 * determine if the matrix is singular. */
1244 pos = neg = 0.0f;
1245 t = in->_11 * in->_22 * in->_33;
1246 if (t >= 0.0f)
1247 pos += t;
1248 else
1249 neg += t;
1251 t = in->_21 * in->_32 * in->_13;
1252 if (t >= 0.0f)
1253 pos += t;
1254 else
1255 neg += t;
1256 t = in->_31 * in->_12 * in->_23;
1257 if (t >= 0.0f)
1258 pos += t;
1259 else
1260 neg += t;
1262 t = -in->_31 * in->_22 * in->_13;
1263 if (t >= 0.0f)
1264 pos += t;
1265 else
1266 neg += t;
1267 t = -in->_21 * in->_12 * in->_33;
1268 if (t >= 0.0f)
1269 pos += t;
1270 else
1271 neg += t;
1273 t = -in->_11 * in->_32 * in->_23;
1274 if (t >= 0.0f)
1275 pos += t;
1276 else
1277 neg += t;
1279 det = pos + neg;
1281 if (fabsf(det) < 1e-25f)
1282 return FALSE;
1284 det = 1.0f / det;
1285 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1286 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1287 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1288 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1289 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1290 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1291 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1292 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1293 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1295 *out = temp;
1296 return TRUE;
1299 static void swap_rows(float **a, float **b)
1301 float *tmp = *a;
1303 *a = *b;
1304 *b = tmp;
1307 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
1309 float wtmp[4][8];
1310 float m0, m1, m2, m3, s;
1311 float *r0, *r1, *r2, *r3;
1313 r0 = wtmp[0];
1314 r1 = wtmp[1];
1315 r2 = wtmp[2];
1316 r3 = wtmp[3];
1318 r0[0] = m->_11;
1319 r0[1] = m->_12;
1320 r0[2] = m->_13;
1321 r0[3] = m->_14;
1322 r0[4] = 1.0f;
1323 r0[5] = r0[6] = r0[7] = 0.0f;
1325 r1[0] = m->_21;
1326 r1[1] = m->_22;
1327 r1[2] = m->_23;
1328 r1[3] = m->_24;
1329 r1[5] = 1.0f;
1330 r1[4] = r1[6] = r1[7] = 0.0f;
1332 r2[0] = m->_31;
1333 r2[1] = m->_32;
1334 r2[2] = m->_33;
1335 r2[3] = m->_34;
1336 r2[6] = 1.0f;
1337 r2[4] = r2[5] = r2[7] = 0.0f;
1339 r3[0] = m->_41;
1340 r3[1] = m->_42;
1341 r3[2] = m->_43;
1342 r3[3] = m->_44;
1343 r3[7] = 1.0f;
1344 r3[4] = r3[5] = r3[6] = 0.0f;
1346 /* Choose pivot - or die. */
1347 if (fabsf(r3[0]) > fabsf(r2[0]))
1348 swap_rows(&r3, &r2);
1349 if (fabsf(r2[0]) > fabsf(r1[0]))
1350 swap_rows(&r2, &r1);
1351 if (fabsf(r1[0]) > fabsf(r0[0]))
1352 swap_rows(&r1, &r0);
1353 if (r0[0] == 0.0f)
1354 return FALSE;
1356 /* Eliminate first variable. */
1357 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1358 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1359 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1360 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1361 s = r0[4];
1362 if (s != 0.0f)
1364 r1[4] -= m1 * s;
1365 r2[4] -= m2 * s;
1366 r3[4] -= m3 * s;
1368 s = r0[5];
1369 if (s != 0.0f)
1371 r1[5] -= m1 * s;
1372 r2[5] -= m2 * s;
1373 r3[5] -= m3 * s;
1375 s = r0[6];
1376 if (s != 0.0f)
1378 r1[6] -= m1 * s;
1379 r2[6] -= m2 * s;
1380 r3[6] -= m3 * s;
1382 s = r0[7];
1383 if (s != 0.0f)
1385 r1[7] -= m1 * s;
1386 r2[7] -= m2 * s;
1387 r3[7] -= m3 * s;
1390 /* Choose pivot - or die. */
1391 if (fabsf(r3[1]) > fabsf(r2[1]))
1392 swap_rows(&r3, &r2);
1393 if (fabsf(r2[1]) > fabsf(r1[1]))
1394 swap_rows(&r2, &r1);
1395 if (r1[1] == 0.0f)
1396 return FALSE;
1398 /* Eliminate second variable. */
1399 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1400 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1401 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1402 s = r1[4];
1403 if (s != 0.0f)
1405 r2[4] -= m2 * s;
1406 r3[4] -= m3 * s;
1408 s = r1[5];
1409 if (s != 0.0f)
1411 r2[5] -= m2 * s;
1412 r3[5] -= m3 * s;
1414 s = r1[6];
1415 if (s != 0.0f)
1417 r2[6] -= m2 * s;
1418 r3[6] -= m3 * s;
1420 s = r1[7];
1421 if (s != 0.0f)
1423 r2[7] -= m2 * s;
1424 r3[7] -= m3 * s;
1427 /* Choose pivot - or die. */
1428 if (fabsf(r3[2]) > fabsf(r2[2]))
1429 swap_rows(&r3, &r2);
1430 if (r2[2] == 0.0f)
1431 return FALSE;
1433 /* Eliminate third variable. */
1434 m3 = r3[2] / r2[2];
1435 r3[3] -= m3 * r2[3];
1436 r3[4] -= m3 * r2[4];
1437 r3[5] -= m3 * r2[5];
1438 r3[6] -= m3 * r2[6];
1439 r3[7] -= m3 * r2[7];
1441 /* Last check. */
1442 if (r3[3] == 0.0f)
1443 return FALSE;
1445 /* Back substitute row 3. */
1446 s = 1.0f / r3[3];
1447 r3[4] *= s;
1448 r3[5] *= s;
1449 r3[6] *= s;
1450 r3[7] *= s;
1452 /* Back substitute row 2. */
1453 m2 = r2[3];
1454 s = 1.0f / r2[2];
1455 r2[4] = s * (r2[4] - r3[4] * m2);
1456 r2[5] = s * (r2[5] - r3[5] * m2);
1457 r2[6] = s * (r2[6] - r3[6] * m2);
1458 r2[7] = s * (r2[7] - r3[7] * m2);
1459 m1 = r1[3];
1460 r1[4] -= r3[4] * m1;
1461 r1[5] -= r3[5] * m1;
1462 r1[6] -= r3[6] * m1;
1463 r1[7] -= r3[7] * m1;
1464 m0 = r0[3];
1465 r0[4] -= r3[4] * m0;
1466 r0[5] -= r3[5] * m0;
1467 r0[6] -= r3[6] * m0;
1468 r0[7] -= r3[7] * m0;
1470 /* Back substitute row 1. */
1471 m1 = r1[2];
1472 s = 1.0f / r1[1];
1473 r1[4] = s * (r1[4] - r2[4] * m1);
1474 r1[5] = s * (r1[5] - r2[5] * m1);
1475 r1[6] = s * (r1[6] - r2[6] * m1);
1476 r1[7] = s * (r1[7] - r2[7] * m1);
1477 m0 = r0[2];
1478 r0[4] -= r2[4] * m0;
1479 r0[5] -= r2[5] * m0;
1480 r0[6] -= r2[6] * m0;
1481 r0[7] -= r2[7] * m0;
1483 /* Back substitute row 0. */
1484 m0 = r0[1];
1485 s = 1.0f / r0[0];
1486 r0[4] = s * (r0[4] - r1[4] * m0);
1487 r0[5] = s * (r0[5] - r1[5] * m0);
1488 r0[6] = s * (r0[6] - r1[6] * m0);
1489 r0[7] = s * (r0[7] - r1[7] * m0);
1491 out->_11 = r0[4];
1492 out->_12 = r0[5];
1493 out->_13 = r0[6];
1494 out->_14 = r0[7];
1495 out->_21 = r1[4];
1496 out->_22 = r1[5];
1497 out->_23 = r1[6];
1498 out->_24 = r1[7];
1499 out->_31 = r2[4];
1500 out->_32 = r2[5];
1501 out->_33 = r2[6];
1502 out->_34 = r2[7];
1503 out->_41 = r3[4];
1504 out->_42 = r3[5];
1505 out->_43 = r3[6];
1506 out->_44 = r3[7];
1508 return TRUE;
1511 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1512 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1514 const struct wined3d_gl_info *gl_info = context->gl_info;
1515 float mat[3 * 3];
1516 struct wined3d_matrix mv;
1517 unsigned int i, j;
1519 if (prog->vs.normal_matrix_location == -1)
1520 return;
1522 get_modelview_matrix(context, state, 0, &mv);
1523 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1524 invert_matrix_3d(&mv, &mv);
1525 else
1526 invert_matrix(&mv, &mv);
1527 /* Tests show that singular modelview matrices are used unchanged as normal
1528 * matrices on D3D3 and older. There seems to be no clearly consistent
1529 * behavior on newer D3D versions so always follow older ddraw behavior. */
1530 for (i = 0; i < 3; ++i)
1531 for (j = 0; j < 3; ++j)
1532 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1534 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1535 checkGLcall("glUniformMatrix3fv");
1538 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1539 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1541 const struct wined3d_gl_info *gl_info = context->gl_info;
1542 struct wined3d_matrix mat;
1544 if (tex >= MAX_TEXTURES)
1545 return;
1546 if (prog->vs.texture_matrix_location[tex] == -1)
1547 return;
1549 get_texture_matrix(context, state, tex, &mat);
1550 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1551 checkGLcall("glUniformMatrix4fv");
1554 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1555 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1557 const struct wined3d_gl_info *gl_info = context->gl_info;
1559 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1561 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1562 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1564 else
1566 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1568 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1570 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1571 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1572 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1573 checkGLcall("setting FFP material uniforms");
1576 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1577 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1579 const struct wined3d_gl_info *gl_info = context->gl_info;
1580 struct wined3d_color color;
1582 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1583 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1584 checkGLcall("glUniform3fv");
1587 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1588 const struct wined3d_matrix *src2)
1590 struct wined3d_vec4 temp;
1592 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1593 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1594 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1595 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1597 *dest = temp;
1600 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1601 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1602 struct glsl_shader_prog_link *prog)
1604 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1605 const struct wined3d_gl_info *gl_info = context->gl_info;
1606 struct wined3d_vec4 vec4;
1608 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1609 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1610 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1612 switch (light_info->OriginalParms.type)
1614 case WINED3D_LIGHT_POINT:
1615 multiply_vector_matrix(&vec4, &light_info->position, view);
1616 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1617 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1618 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1619 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1620 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1621 break;
1623 case WINED3D_LIGHT_SPOT:
1624 multiply_vector_matrix(&vec4, &light_info->position, view);
1625 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1627 multiply_vector_matrix(&vec4, &light_info->direction, view);
1628 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1630 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1631 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1632 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1633 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1634 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1635 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1636 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1637 break;
1639 case WINED3D_LIGHT_DIRECTIONAL:
1640 multiply_vector_matrix(&vec4, &light_info->direction, view);
1641 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1642 break;
1644 case WINED3D_LIGHT_PARALLELPOINT:
1645 multiply_vector_matrix(&vec4, &light_info->position, view);
1646 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1647 break;
1649 default:
1650 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1652 checkGLcall("setting FFP lights uniforms");
1655 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1656 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1658 const struct wined3d_gl_info *gl_info = context->gl_info;
1659 float min, max;
1660 float size, att[3];
1662 get_pointsize_minmax(context, state, &min, &max);
1664 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1665 checkGLcall("glUniform1f");
1666 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1667 checkGLcall("glUniform1f");
1669 get_pointsize(context, state, &size, att);
1671 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1672 checkGLcall("glUniform1f");
1673 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1674 checkGLcall("glUniform1f");
1675 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1676 checkGLcall("glUniform1f");
1677 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1678 checkGLcall("glUniform1f");
1681 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1682 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1684 const struct wined3d_gl_info *gl_info = context->gl_info;
1685 struct wined3d_color color;
1686 float start, end, scale;
1687 union
1689 DWORD d;
1690 float f;
1691 } tmpvalue;
1693 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1694 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1695 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1696 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1697 get_fog_start_end(context, state, &start, &end);
1698 scale = 1.0f / (end - start);
1699 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1700 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1701 checkGLcall("fog emulation uniforms");
1704 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1705 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1707 const struct wined3d_gl_info *gl_info = context->gl_info;
1708 struct wined3d_vec4 plane;
1710 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1711 if (!use_vs(state))
1712 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1713 else
1714 plane = state->clip_planes[index];
1716 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1719 /* Context activation is done by the caller (state handler). */
1720 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1721 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1723 struct wined3d_color float_key[2];
1724 const struct wined3d_texture *texture = state->textures[0];
1726 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1727 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1730 /* Context activation is done by the caller (state handler). */
1731 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1732 const struct wined3d_state *state)
1734 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1735 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1736 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1737 const struct wined3d_gl_info *gl_info = context->gl_info;
1738 struct shader_glsl_priv *priv = shader_priv;
1739 float position_fixup[4];
1740 DWORD update_mask;
1742 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1743 UINT constant_version;
1744 int i;
1746 if (!prog) {
1747 /* No GLSL program set - nothing to do. */
1748 return;
1750 constant_version = prog->constant_version;
1751 update_mask = context->constant_update_mask & prog->constant_update_mask;
1753 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1754 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1755 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1757 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1758 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1759 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1761 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1762 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1763 vshader->reg_maps.boolean_constants);
1765 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1767 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1768 shader_glsl_clip_plane_uniform(context, state, i, prog);
1771 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1772 shader_glsl_pointsize_uniform(context, state, prog);
1774 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1776 shader_get_position_fixup(context, state, position_fixup);
1777 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1778 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1779 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1780 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1781 else
1782 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1783 checkGLcall("glUniform4fv");
1786 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1788 struct wined3d_matrix mat;
1790 get_modelview_matrix(context, state, 0, &mat);
1791 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1792 checkGLcall("glUniformMatrix4fv");
1794 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1797 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1799 struct wined3d_matrix mat;
1801 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1803 if (prog->vs.modelview_matrix_location[i] == -1)
1804 break;
1806 get_modelview_matrix(context, state, i, &mat);
1807 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1808 checkGLcall("glUniformMatrix4fv");
1812 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1814 struct wined3d_matrix projection;
1816 get_projection_matrix(context, state, &projection);
1817 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1818 checkGLcall("glUniformMatrix4fv");
1821 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1823 for (i = 0; i < MAX_TEXTURES; ++i)
1824 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1827 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1828 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1830 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1832 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1833 DWORD point_count = 0;
1834 DWORD spot_count = 0;
1835 DWORD directional_count = 0;
1836 DWORD parallel_point_count = 0;
1838 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1840 if (!state->lights[i])
1841 continue;
1843 switch (state->lights[i]->OriginalParms.type)
1845 case WINED3D_LIGHT_POINT:
1846 ++point_count;
1847 break;
1848 case WINED3D_LIGHT_SPOT:
1849 ++spot_count;
1850 break;
1851 case WINED3D_LIGHT_DIRECTIONAL:
1852 ++directional_count;
1853 break;
1854 case WINED3D_LIGHT_PARALLELPOINT:
1855 ++parallel_point_count;
1856 break;
1857 default:
1858 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1859 break;
1862 point_idx = 0;
1863 spot_idx = point_idx + point_count;
1864 directional_idx = spot_idx + spot_count;
1865 parallel_point_idx = directional_idx + directional_count;
1867 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1868 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1870 const struct wined3d_light_info *light_info = state->lights[i];
1871 unsigned int idx;
1873 if (!light_info)
1874 continue;
1876 switch (light_info->OriginalParms.type)
1878 case WINED3D_LIGHT_POINT:
1879 idx = point_idx++;
1880 break;
1881 case WINED3D_LIGHT_SPOT:
1882 idx = spot_idx++;
1883 break;
1884 case WINED3D_LIGHT_DIRECTIONAL:
1885 idx = directional_idx++;
1886 break;
1887 case WINED3D_LIGHT_PARALLELPOINT:
1888 idx = parallel_point_idx++;
1889 break;
1890 default:
1891 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1892 continue;
1894 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1898 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1899 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1900 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1902 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1903 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1904 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1906 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1907 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1908 pshader->reg_maps.boolean_constants);
1910 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1912 for (i = 0; i < MAX_TEXTURES; ++i)
1914 if (prog->ps.bumpenv_mat_location[i] == -1)
1915 continue;
1917 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1918 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1920 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1922 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1923 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1924 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1925 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1929 checkGLcall("bump env uniforms");
1932 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1934 const struct wined3d_vec4 correction_params =
1936 /* Position is relative to the framebuffer, not the viewport. */
1937 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1938 context->render_offscreen ? 1.0f : -1.0f,
1939 0.0f,
1940 0.0f,
1943 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1946 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1947 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1948 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1949 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1951 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1953 struct wined3d_color color;
1955 if (prog->ps.tex_factor_location != -1)
1957 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1958 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1961 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1962 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1963 else
1964 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1966 for (i = 0; i < MAX_TEXTURES; ++i)
1968 if (prog->ps.tss_constant_location[i] == -1)
1969 continue;
1971 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1972 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1975 checkGLcall("fixed function uniforms");
1978 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1979 shader_glsl_load_fog_uniform(context, state, prog);
1981 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1983 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1985 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1986 checkGLcall("alpha test emulation uniform");
1989 if (priv->next_constant_version == UINT_MAX)
1991 TRACE("Max constant version reached, resetting to 0.\n");
1992 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1993 priv->next_constant_version = 1;
1995 else
1997 prog->constant_version = priv->next_constant_version++;
2001 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
2003 struct constant_entry *entries = heap->entries;
2004 unsigned int *positions = heap->positions;
2005 unsigned int heap_idx, parent_idx;
2007 if (!heap->contained[idx])
2009 heap_idx = heap->size++;
2010 heap->contained[idx] = TRUE;
2012 else
2014 heap_idx = positions[idx];
2017 while (heap_idx > 1)
2019 parent_idx = heap_idx >> 1;
2021 if (new_version <= entries[parent_idx].version) break;
2023 entries[heap_idx] = entries[parent_idx];
2024 positions[entries[parent_idx].idx] = heap_idx;
2025 heap_idx = parent_idx;
2028 entries[heap_idx].version = new_version;
2029 entries[heap_idx].idx = idx;
2030 positions[idx] = heap_idx;
2033 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2035 struct shader_glsl_priv *priv = device->shader_priv;
2036 struct constant_heap *heap = &priv->vconst_heap;
2037 UINT i;
2039 for (i = start; i < count + start; ++i)
2041 update_heap_entry(heap, i, priv->next_constant_version);
2045 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2047 struct shader_glsl_priv *priv = device->shader_priv;
2048 struct constant_heap *heap = &priv->pconst_heap;
2049 UINT i;
2051 for (i = start; i < count + start; ++i)
2053 update_heap_entry(heap, i, priv->next_constant_version);
2057 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2059 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2060 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2061 if(shader_major > 3) return ret;
2063 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2064 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2065 return ret;
2068 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2070 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2073 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2075 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2076 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2079 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2081 return shader_glsl_get_version(gl_info) >= 150;
2084 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2086 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2089 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2090 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2092 va_list args;
2093 int ret;
2095 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2096 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2097 for (;;)
2099 va_start(args, format);
2100 ret = shader_vaddline(buffer, format, args);
2101 va_end(args);
2102 if (!ret)
2103 return;
2104 if (!string_buffer_resize(buffer, ret))
2105 return;
2109 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2110 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2112 va_list args;
2113 int ret;
2115 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2116 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2117 for (;;)
2119 va_start(args, format);
2120 ret = shader_vaddline(buffer, format, args);
2121 va_end(args);
2122 if (!ret)
2123 return;
2124 if (!string_buffer_resize(buffer, ret))
2125 return;
2129 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2131 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2134 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2136 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2139 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2141 switch (mode)
2143 case WINED3DSIM_CONSTANT:
2144 return "flat";
2145 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2146 return "noperspective";
2147 default:
2148 FIXME("Unhandled interpolation mode %#x.\n", mode);
2149 case WINED3DSIM_NONE:
2150 case WINED3DSIM_LINEAR:
2151 return "";
2155 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2156 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2158 return wined3d_extract_bits(packed_interpolation_mode,
2159 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2162 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2163 struct wined3d_string_buffer *buffer, unsigned int element_count,
2164 const DWORD *interpolation_mode, BOOL unroll)
2166 enum wined3d_shader_interpolation_mode mode;
2167 unsigned int i;
2169 if (shader_glsl_use_interface_blocks(gl_info))
2171 if (unroll)
2173 shader_addline(buffer, "in shader_in_out {\n");
2174 for (i = 0; i < element_count; ++i)
2176 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2177 shader_addline(buffer, "%s vec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2179 shader_addline(buffer, "} shader_in;\n");
2181 else
2183 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2186 else
2188 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2192 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2193 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2194 const DWORD *interpolation_mode)
2196 enum wined3d_shader_interpolation_mode mode;
2197 unsigned int i;
2199 if (shader_glsl_use_interface_blocks(gl_info))
2201 if (rasterizer_setup)
2203 shader_addline(buffer, "out shader_in_out {\n");
2204 for (i = 0; i < element_count; ++i)
2206 const char *interpolation_qualifiers = "";
2207 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2209 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2210 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2212 shader_addline(buffer, "%s vec4 reg%u;\n", interpolation_qualifiers, i);
2214 shader_addline(buffer, "} shader_out;\n");
2216 else
2218 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2221 else
2223 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2227 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2229 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2232 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2234 switch (primitive_type)
2236 case WINED3D_PT_POINTLIST:
2237 return "points";
2239 case WINED3D_PT_LINELIST:
2240 return "lines";
2242 case WINED3D_PT_LINESTRIP:
2243 return "line_strip";
2245 case WINED3D_PT_TRIANGLELIST:
2246 return "triangles";
2248 case WINED3D_PT_TRIANGLESTRIP:
2249 return "triangle_strip";
2251 case WINED3D_PT_LINELIST_ADJ:
2252 return "lines_adjacency";
2254 case WINED3D_PT_TRIANGLELIST_ADJ:
2255 return "triangles_adjacency";
2257 default:
2258 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2259 return "";
2263 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2265 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2266 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2267 DWORD input_reg_used = shader->u.ps.input_reg_used;
2268 unsigned int i;
2270 if (reg_maps->shader_version.major < 3)
2271 return input_reg_used & (1u << idx);
2273 for (i = 0; i < input_signature->element_count; ++i)
2275 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2277 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2278 continue;
2280 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2281 && input->semantic_idx == idx)
2282 return input_reg_used & (1u << input->register_idx);
2284 return FALSE;
2287 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2288 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2290 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2292 if (version->major >= 4)
2293 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2294 else
2295 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2298 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2299 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2300 unsigned int index)
2302 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2303 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2304 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2305 index, scalar_type, scalar_type, index);
2308 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2309 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2311 unsigned int index = e->register_idx;
2313 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2315 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2316 index);
2317 return;
2319 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2321 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2322 index);
2323 return;
2325 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2326 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2328 if (shader_glsl_use_explicit_attrib_location(gl_info))
2329 shader_addline(buffer, "layout(location = %u) ", index);
2331 switch (e->component_type)
2333 case WINED3D_TYPE_UINT:
2334 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
2335 break;
2336 case WINED3D_TYPE_INT:
2337 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
2338 break;
2340 default:
2341 FIXME("Unhandled type %#x.\n", e->component_type);
2342 /* Fall through. */
2343 case WINED3D_TYPE_UNKNOWN:
2344 case WINED3D_TYPE_FLOAT:
2345 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2346 break;
2350 /** Generate the variable & register declarations for the GLSL output target */
2351 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2352 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2353 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2355 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2356 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2357 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2358 const struct wined3d_gl_info *gl_info = context->gl_info;
2359 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2360 unsigned int uniform_block_base, uniform_block_count;
2361 const struct wined3d_shader_lconst *lconst;
2362 const char *prefix;
2363 unsigned int i;
2364 DWORD map;
2366 prefix = shader_glsl_get_prefix(version->type);
2368 /* Prototype the subroutines */
2369 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2371 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2374 /* Declare the constants (aka uniforms) */
2375 if (shader->limits->constant_float > 0)
2377 unsigned max_constantsF;
2379 /* Unless the shader uses indirect addressing, always declare the
2380 * maximum array size and ignore that we need some uniforms privately.
2381 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2382 * and immediate values, still declare VC[256]. If the shader needs
2383 * more uniforms than we have it won't work in any case. If it uses
2384 * less, the compiler will figure out which uniforms are really used
2385 * and strip them out. This allows a shader to use c255 on a dx9 card,
2386 * as long as it doesn't also use all the other constants.
2388 * If the shader uses indirect addressing the compiler must assume
2389 * that all declared uniforms are used. In this case, declare only the
2390 * amount that we're assured to have.
2392 * Thus we run into problems in these two cases:
2393 * 1) The shader really uses more uniforms than supported.
2394 * 2) The shader uses indirect addressing, less constants than
2395 * supported, but uses a constant index > #supported consts. */
2396 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2398 /* No indirect addressing here. */
2399 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2401 else
2403 if (reg_maps->usesrelconstF)
2405 /* Subtract the other potential uniforms from the max
2406 * available (bools, ints, and 1 row of projection matrix).
2407 * Subtract another uniform for immediate values, which have
2408 * to be loaded via uniform by the driver as well. The shader
2409 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2410 * shader code, so one vec4 should be enough. (Unfortunately
2411 * the Nvidia driver doesn't store 128 and -128 in one float).
2413 * Writing gl_ClipVertex requires one uniform for each
2414 * clipplane as well. */
2415 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2416 if (vs_args->clip_enabled)
2417 max_constantsF -= gl_info->limits.user_clip_distances;
2418 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2419 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2420 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2421 * for now take this into account when calculating the number of available constants
2423 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2424 /* Set by driver quirks in directx.c */
2425 max_constantsF -= gl_info->reserved_glsl_constants;
2427 if (max_constantsF < shader->limits->constant_float)
2429 static unsigned int once;
2431 if (!once++)
2432 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2433 " it may not render correctly.\n");
2434 else
2435 WARN("The hardware does not support enough uniform components to run this shader.\n");
2438 else
2440 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2443 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2444 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2447 /* Always declare the full set of constants, the compiler can remove the
2448 * unused ones because d3d doesn't (yet) support indirect int and bool
2449 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2450 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2451 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2453 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2454 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2456 /* Declare immediate constant buffer */
2457 if (reg_maps->icb)
2458 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2460 /* Declare constant buffers */
2461 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2462 &uniform_block_base, &uniform_block_count);
2463 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2465 if (reg_maps->cb_sizes[i])
2467 shader_addline(buffer, "layout(std140");
2468 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2469 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2470 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2471 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2475 /* Declare texture samplers */
2476 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2478 struct wined3d_shader_sampler_map_entry *entry;
2479 const char *sampler_type_prefix, *sampler_type;
2480 BOOL shadow_sampler, tex_rect;
2482 entry = &reg_maps->sampler_map.entries[i];
2484 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2486 ERR("Invalid resource index %u.\n", entry->resource_idx);
2487 continue;
2490 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2492 case WINED3D_DATA_FLOAT:
2493 case WINED3D_DATA_UNORM:
2494 case WINED3D_DATA_SNORM:
2495 sampler_type_prefix = "";
2496 break;
2498 case WINED3D_DATA_INT:
2499 sampler_type_prefix = "i";
2500 break;
2502 case WINED3D_DATA_UINT:
2503 sampler_type_prefix = "u";
2504 break;
2506 default:
2507 sampler_type_prefix = "";
2508 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2509 break;
2512 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2513 switch (reg_maps->resource_info[entry->resource_idx].type)
2515 case WINED3D_SHADER_RESOURCE_BUFFER:
2516 sampler_type = "samplerBuffer";
2517 break;
2519 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2520 if (shadow_sampler)
2521 sampler_type = "sampler1DShadow";
2522 else
2523 sampler_type = "sampler1D";
2524 break;
2526 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2527 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2528 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2529 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2530 if (shadow_sampler)
2532 if (tex_rect)
2533 sampler_type = "sampler2DRectShadow";
2534 else
2535 sampler_type = "sampler2DShadow";
2537 else
2539 if (tex_rect)
2540 sampler_type = "sampler2DRect";
2541 else
2542 sampler_type = "sampler2D";
2544 break;
2546 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2547 if (shadow_sampler)
2548 FIXME("Unsupported 3D shadow sampler.\n");
2549 sampler_type = "sampler3D";
2550 break;
2552 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2553 if (shadow_sampler)
2554 sampler_type = "samplerCubeShadow";
2555 else
2556 sampler_type = "samplerCube";
2557 break;
2559 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2560 if (shadow_sampler)
2561 sampler_type = "sampler2DArrayShadow";
2562 else
2563 sampler_type = "sampler2DArray";
2564 break;
2566 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2567 if (shadow_sampler)
2568 sampler_type = "samplerCubeArrayShadow";
2569 else
2570 sampler_type = "samplerCubeArray";
2571 break;
2573 default:
2574 sampler_type = "unsupported_sampler";
2575 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2576 break;
2579 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2580 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2581 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2582 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2585 /* Declare images */
2586 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2588 const char *image_type_prefix, *image_type, *read_format;
2590 if (!reg_maps->uav_resource_info[i].type)
2591 continue;
2593 switch (reg_maps->uav_resource_info[i].data_type)
2595 case WINED3D_DATA_FLOAT:
2596 case WINED3D_DATA_UNORM:
2597 case WINED3D_DATA_SNORM:
2598 image_type_prefix = "";
2599 read_format = "r32f";
2600 break;
2602 case WINED3D_DATA_INT:
2603 image_type_prefix = "i";
2604 read_format = "r32i";
2605 break;
2607 case WINED3D_DATA_UINT:
2608 image_type_prefix = "u";
2609 read_format = "r32ui";
2610 break;
2612 default:
2613 image_type_prefix = "";
2614 read_format = "";
2615 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2616 break;
2619 switch (reg_maps->uav_resource_info[i].type)
2621 case WINED3D_SHADER_RESOURCE_BUFFER:
2622 image_type = "imageBuffer";
2623 break;
2625 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2626 image_type = "image2D";
2627 break;
2629 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2630 image_type = "image3D";
2631 break;
2633 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2634 image_type = "image2DArray";
2635 break;
2637 default:
2638 image_type = "unsupported_image";
2639 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2640 break;
2643 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2644 shader_addline(buffer, "layout(binding = %u)\n", i);
2645 if (reg_maps->uav_read_mask & (1u << i))
2646 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2647 read_format, image_type_prefix, image_type, prefix, i);
2648 else
2649 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2650 image_type_prefix, image_type, prefix, i);
2652 if (reg_maps->uav_counter_mask & (1u << i))
2653 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2654 i, prefix, i);
2657 /* Declare address variables */
2658 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2660 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2663 /* Declare output register temporaries */
2664 if (shader->limits->packed_output)
2665 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2667 /* Declare temporary variables */
2668 if (reg_maps->temporary_count)
2670 for (i = 0; i < reg_maps->temporary_count; ++i)
2671 shader_addline(buffer, "vec4 R%u;\n", i);
2673 else if (version->major < 4)
2675 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2677 if (map & 1)
2678 shader_addline(buffer, "vec4 R%u;\n", i);
2682 /* Declare indexable temporary variables */
2683 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2685 if (idx_temp_reg->component_count != 4)
2686 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2687 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2690 /* Declare loop registers aLx */
2691 if (version->major < 4)
2693 for (i = 0; i < reg_maps->loop_depth; ++i)
2695 shader_addline(buffer, "int aL%u;\n", i);
2696 shader_addline(buffer, "int tmpInt%u;\n", i);
2700 /* Temporary variables for matrix operations */
2701 shader_addline(buffer, "vec4 tmp0;\n");
2702 shader_addline(buffer, "vec4 tmp1;\n");
2704 if (!shader->load_local_constsF)
2706 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2708 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2709 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2710 shader_addline(buffer, ";\n");
2715 /*****************************************************************************
2716 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2718 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2719 ****************************************************************************/
2721 /* Prototypes */
2722 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2723 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2725 /** Used for opcode modifiers - They multiply the result by the specified amount */
2726 static const char * const shift_glsl_tab[] = {
2727 "", /* 0 (none) */
2728 "2.0 * ", /* 1 (x2) */
2729 "4.0 * ", /* 2 (x4) */
2730 "8.0 * ", /* 3 (x8) */
2731 "16.0 * ", /* 4 (x16) */
2732 "32.0 * ", /* 5 (x32) */
2733 "", /* 6 (x64) */
2734 "", /* 7 (x128) */
2735 "", /* 8 (d256) */
2736 "", /* 9 (d128) */
2737 "", /* 10 (d64) */
2738 "", /* 11 (d32) */
2739 "0.0625 * ", /* 12 (d16) */
2740 "0.125 * ", /* 13 (d8) */
2741 "0.25 * ", /* 14 (d4) */
2742 "0.5 * " /* 15 (d2) */
2745 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2746 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2747 const char *in_reg, const char *in_regswizzle, char *out_str)
2749 switch (src_modifier)
2751 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2752 case WINED3DSPSM_DW:
2753 case WINED3DSPSM_NONE:
2754 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2755 break;
2756 case WINED3DSPSM_NEG:
2757 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2758 break;
2759 case WINED3DSPSM_NOT:
2760 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2761 break;
2762 case WINED3DSPSM_BIAS:
2763 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2764 break;
2765 case WINED3DSPSM_BIASNEG:
2766 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2767 break;
2768 case WINED3DSPSM_SIGN:
2769 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2770 break;
2771 case WINED3DSPSM_SIGNNEG:
2772 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2773 break;
2774 case WINED3DSPSM_COMP:
2775 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2776 break;
2777 case WINED3DSPSM_X2:
2778 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2779 break;
2780 case WINED3DSPSM_X2NEG:
2781 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2782 break;
2783 case WINED3DSPSM_ABS:
2784 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2785 break;
2786 case WINED3DSPSM_ABSNEG:
2787 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2788 break;
2789 default:
2790 FIXME("Unhandled modifier %u\n", src_modifier);
2791 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2795 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2796 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2798 /* The ARB_shading_language_420pack extension allows swizzle operations on
2799 * scalars. */
2800 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2801 sprintf(register_name, "%s", glsl_variable);
2802 else
2803 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2806 /** Writes the GLSL variable name that corresponds to the register that the
2807 * DX opcode parameter is trying to access */
2808 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2809 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2810 const struct wined3d_shader_instruction *ins)
2812 /* oPos, oFog and oPts in D3D */
2813 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2815 const struct wined3d_shader *shader = ins->ctx->shader;
2816 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2817 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2818 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2819 const char *prefix = shader_glsl_get_prefix(version->type);
2820 struct glsl_src_param rel_param0, rel_param1;
2821 char imm_str[4][17];
2823 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2824 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2825 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2826 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2827 *is_color = FALSE;
2829 switch (reg->type)
2831 case WINED3DSPR_TEMP:
2832 sprintf(register_name, "R%u", reg->idx[0].offset);
2833 break;
2835 case WINED3DSPR_INPUT:
2836 case WINED3DSPR_INCONTROLPOINT:
2837 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2839 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2841 if (reg->idx[0].rel_addr)
2842 FIXME("VS3+ input registers relative addressing.\n");
2843 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2844 *is_color = TRUE;
2845 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2846 break;
2849 if (version->type == WINED3D_SHADER_TYPE_HULL
2850 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2851 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2853 if (reg->idx[0].rel_addr)
2855 if (reg->idx[1].rel_addr)
2856 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2857 rel_param0.param_str, reg->idx[0].offset,
2858 rel_param1.param_str, reg->idx[1].offset);
2859 else
2860 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2861 rel_param0.param_str, reg->idx[0].offset,
2862 reg->idx[1].offset);
2864 else if (reg->idx[1].rel_addr)
2865 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2866 rel_param1.param_str, reg->idx[1].offset);
2867 else
2868 sprintf(register_name, "shader_in[%u].reg[%u]",
2869 reg->idx[0].offset, reg->idx[1].offset);
2870 break;
2873 /* pixel shaders >= 3.0 */
2874 if (version->major >= 3)
2876 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2877 unsigned int in_count = vec4_varyings(version->major, gl_info);
2879 if (reg->idx[0].rel_addr)
2881 /* Removing a + 0 would be an obvious optimization, but
2882 * OS X doesn't see the NOP operation there. */
2883 if (idx)
2885 if (needs_legacy_glsl_syntax(gl_info)
2886 && shader->u.ps.declared_in_count > in_count)
2888 sprintf(register_name,
2889 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2890 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2891 prefix, rel_param0.param_str, idx);
2893 else
2895 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2898 else
2900 if (needs_legacy_glsl_syntax(gl_info)
2901 && shader->u.ps.declared_in_count > in_count)
2903 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2904 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2905 prefix, rel_param0.param_str);
2907 else
2909 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2913 else
2915 if (idx == in_count) sprintf(register_name, "gl_Color");
2916 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2917 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2920 else
2922 if (!reg->idx[0].offset)
2923 strcpy(register_name, "ffp_varying_diffuse");
2924 else
2925 strcpy(register_name, "ffp_varying_specular");
2926 break;
2928 break;
2930 case WINED3DSPR_CONST:
2932 /* Relative addressing */
2933 if (reg->idx[0].rel_addr)
2935 if (wined3d_settings.check_float_constants)
2936 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2937 rel_param0.param_str, reg->idx[0].offset,
2938 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2939 prefix, rel_param0.param_str, reg->idx[0].offset);
2940 else if (reg->idx[0].offset)
2941 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2942 else
2943 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2945 else
2947 if (shader_constant_is_local(shader, reg->idx[0].offset))
2948 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2949 else
2950 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2953 break;
2955 case WINED3DSPR_CONSTINT:
2956 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2957 break;
2959 case WINED3DSPR_CONSTBOOL:
2960 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2961 break;
2963 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2964 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2965 sprintf(register_name, "T%u", reg->idx[0].offset);
2966 else
2967 sprintf(register_name, "A%u", reg->idx[0].offset);
2968 break;
2970 case WINED3DSPR_LOOP:
2971 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2972 break;
2974 case WINED3DSPR_SAMPLER:
2975 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2976 break;
2978 case WINED3DSPR_COLOROUT:
2979 if (reg->idx[0].offset >= gl_info->limits.buffers)
2980 WARN("Write to render target %u, only %d supported.\n",
2981 reg->idx[0].offset, gl_info->limits.buffers);
2983 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2984 break;
2986 case WINED3DSPR_RASTOUT:
2987 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2988 break;
2990 case WINED3DSPR_DEPTHOUT:
2991 case WINED3DSPR_DEPTHOUTGE:
2992 case WINED3DSPR_DEPTHOUTLE:
2993 sprintf(register_name, "gl_FragDepth");
2994 break;
2996 case WINED3DSPR_ATTROUT:
2997 if (!reg->idx[0].offset)
2998 sprintf(register_name, "%s_out[8]", prefix);
2999 else
3000 sprintf(register_name, "%s_out[9]", prefix);
3001 break;
3003 case WINED3DSPR_TEXCRDOUT:
3004 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
3005 if (reg->idx[0].rel_addr)
3006 sprintf(register_name, "%s_out[%s + %u]",
3007 prefix, rel_param0.param_str, reg->idx[0].offset);
3008 else
3009 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3010 break;
3012 case WINED3DSPR_MISCTYPE:
3013 if (!reg->idx[0].offset)
3015 /* vPos */
3016 sprintf(register_name, "vpos");
3018 else if (reg->idx[0].offset == 1)
3020 /* Note that gl_FrontFacing is a bool, while vFace is
3021 * a float for which the sign determines front/back */
3022 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3024 else
3026 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3027 sprintf(register_name, "unrecognized_register");
3029 break;
3031 case WINED3DSPR_IMMCONST:
3032 switch (reg->immconst_type)
3034 case WINED3D_IMMCONST_SCALAR:
3035 switch (data_type)
3037 case WINED3D_DATA_FLOAT:
3038 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3039 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3040 else
3041 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3042 break;
3043 case WINED3D_DATA_INT:
3044 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3045 break;
3046 case WINED3D_DATA_RESOURCE:
3047 case WINED3D_DATA_SAMPLER:
3048 case WINED3D_DATA_UINT:
3049 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3050 break;
3051 default:
3052 sprintf(register_name, "<unhandled data type %#x>", data_type);
3053 break;
3055 break;
3057 case WINED3D_IMMCONST_VEC4:
3058 switch (data_type)
3060 case WINED3D_DATA_FLOAT:
3061 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3063 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3064 reg->u.immconst_data[0], reg->u.immconst_data[1],
3065 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3067 else
3069 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3070 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3071 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3072 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3073 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3074 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3076 break;
3077 case WINED3D_DATA_INT:
3078 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3079 reg->u.immconst_data[0], reg->u.immconst_data[1],
3080 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3081 break;
3082 case WINED3D_DATA_RESOURCE:
3083 case WINED3D_DATA_SAMPLER:
3084 case WINED3D_DATA_UINT:
3085 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3086 reg->u.immconst_data[0], reg->u.immconst_data[1],
3087 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3088 break;
3089 default:
3090 sprintf(register_name, "<unhandled data type %#x>", data_type);
3091 break;
3093 break;
3095 default:
3096 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3097 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3099 break;
3101 case WINED3DSPR_CONSTBUFFER:
3102 if (reg->idx[1].rel_addr)
3103 sprintf(register_name, "%s_cb%u[%s + %u]",
3104 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3105 else
3106 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3107 break;
3109 case WINED3DSPR_IMMCONSTBUFFER:
3110 if (reg->idx[0].rel_addr)
3111 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3112 else
3113 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3114 break;
3116 case WINED3DSPR_PRIMID:
3117 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3118 sprintf(register_name, "gl_PrimitiveIDIn");
3119 else
3120 sprintf(register_name, "gl_PrimitiveID");
3121 break;
3123 case WINED3DSPR_IDXTEMP:
3124 if (reg->idx[1].rel_addr)
3125 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3126 else
3127 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3128 break;
3130 case WINED3DSPR_LOCALTHREADINDEX:
3131 shader_glsl_fixup_scalar_register_variable(register_name,
3132 "int(gl_LocalInvocationIndex)", gl_info);
3133 break;
3135 case WINED3DSPR_GSINSTID:
3136 case WINED3DSPR_OUTPOINTID:
3137 shader_glsl_fixup_scalar_register_variable(register_name,
3138 "gl_InvocationID", gl_info);
3139 break;
3141 case WINED3DSPR_THREADID:
3142 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3143 break;
3145 case WINED3DSPR_THREADGROUPID:
3146 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3147 break;
3149 case WINED3DSPR_LOCALTHREADID:
3150 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3151 break;
3153 case WINED3DSPR_FORKINSTID:
3154 case WINED3DSPR_JOININSTID:
3155 shader_glsl_fixup_scalar_register_variable(register_name,
3156 "phase_instance_id", gl_info);
3157 break;
3159 case WINED3DSPR_TESSCOORD:
3160 sprintf(register_name, "gl_TessCoord");
3161 break;
3163 case WINED3DSPR_OUTCONTROLPOINT:
3164 if (reg->idx[0].rel_addr)
3166 if (reg->idx[1].rel_addr)
3167 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3168 rel_param0.param_str, reg->idx[0].offset,
3169 rel_param1.param_str, reg->idx[1].offset);
3170 else
3171 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3172 rel_param0.param_str, reg->idx[0].offset,
3173 reg->idx[1].offset);
3175 else if (reg->idx[1].rel_addr)
3177 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3178 reg->idx[0].offset, rel_param1.param_str,
3179 reg->idx[1].offset);
3181 else
3183 sprintf(register_name, "shader_out[%u].reg[%u]",
3184 reg->idx[0].offset, reg->idx[1].offset);
3186 break;
3188 case WINED3DSPR_PATCHCONST:
3189 if (version->type == WINED3D_SHADER_TYPE_HULL)
3190 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3191 else
3192 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3193 break;
3195 default:
3196 FIXME("Unhandled register type %#x.\n", reg->type);
3197 sprintf(register_name, "unrecognized_register");
3198 break;
3202 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3204 *str++ = '.';
3205 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3206 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3207 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3208 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3209 *str = '\0';
3212 /* Get the GLSL write mask for the destination register */
3213 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3215 DWORD mask = param->write_mask;
3217 if (shader_is_scalar(&param->reg))
3219 mask = WINED3DSP_WRITEMASK_0;
3220 *write_mask = '\0';
3222 else
3224 shader_glsl_write_mask_to_str(mask, write_mask);
3227 return mask;
3230 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3232 unsigned int size = 0;
3234 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3235 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3236 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3237 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3239 return size;
3242 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3243 unsigned int component_idx)
3245 /* swizzle bits fields: wwzzyyxx */
3246 return (swizzle >> (2 * component_idx)) & 0x3;
3249 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3251 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3252 * but addressed as "rgba". To fix this we need to swap the register's x
3253 * and z components. */
3254 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3255 unsigned int i;
3257 *str++ = '.';
3258 for (i = 0; i < 4; ++i)
3260 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3261 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3263 *str = '\0';
3266 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3267 BOOL fixup, DWORD mask, char *swizzle_str)
3269 if (shader_is_scalar(&param->reg))
3270 *swizzle_str = '\0';
3271 else
3272 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3275 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3276 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3278 if (dst_data_type == src_data_type)
3280 string_buffer_sprintf(dst_param, "%s", src_param);
3281 return;
3284 if (src_data_type == WINED3D_DATA_FLOAT)
3286 switch (dst_data_type)
3288 case WINED3D_DATA_INT:
3289 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3290 return;
3291 case WINED3D_DATA_RESOURCE:
3292 case WINED3D_DATA_SAMPLER:
3293 case WINED3D_DATA_UINT:
3294 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3295 return;
3296 default:
3297 break;
3301 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3303 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3304 return;
3307 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3309 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3310 return;
3313 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3314 string_buffer_sprintf(dst_param, "%s", src_param);
3317 /* From a given parameter token, generate the corresponding GLSL string.
3318 * Also, return the actual register name and swizzle in case the
3319 * caller needs this information as well. */
3320 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3321 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3322 enum wined3d_data_type data_type)
3324 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3325 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3326 enum wined3d_data_type param_data_type;
3327 BOOL is_color = FALSE;
3328 char swizzle_str[6];
3330 glsl_src->reg_name[0] = '\0';
3331 glsl_src->param_str[0] = '\0';
3332 swizzle_str[0] = '\0';
3334 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ins);
3335 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3337 switch (wined3d_src->reg.type)
3339 case WINED3DSPR_IMMCONST:
3340 param_data_type = data_type;
3341 break;
3342 case WINED3DSPR_FORKINSTID:
3343 case WINED3DSPR_GSINSTID:
3344 case WINED3DSPR_JOININSTID:
3345 case WINED3DSPR_LOCALTHREADID:
3346 case WINED3DSPR_LOCALTHREADINDEX:
3347 case WINED3DSPR_OUTPOINTID:
3348 case WINED3DSPR_PRIMID:
3349 case WINED3DSPR_THREADGROUPID:
3350 case WINED3DSPR_THREADID:
3351 param_data_type = WINED3D_DATA_INT;
3352 break;
3353 default:
3354 param_data_type = WINED3D_DATA_FLOAT;
3355 break;
3358 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3359 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3361 string_buffer_release(priv->string_buffers, reg_name);
3364 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3365 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3367 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3370 /* From a given parameter token, generate the corresponding GLSL string.
3371 * Also, return the actual register name and swizzle in case the
3372 * caller needs this information as well. */
3373 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3374 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3376 BOOL is_color = FALSE;
3378 glsl_dst->mask_str[0] = '\0';
3379 glsl_dst->reg_name[0] = '\0';
3381 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3382 glsl_dst->reg_name, &is_color, ins);
3383 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3386 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3387 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3388 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3389 enum wined3d_data_type data_type)
3391 struct glsl_dst_param glsl_dst;
3392 DWORD mask;
3394 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3396 switch (data_type)
3398 case WINED3D_DATA_FLOAT:
3399 shader_addline(buffer, "%s%s = %s(",
3400 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3401 break;
3402 case WINED3D_DATA_INT:
3403 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3404 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3405 break;
3406 case WINED3D_DATA_RESOURCE:
3407 case WINED3D_DATA_SAMPLER:
3408 case WINED3D_DATA_UINT:
3409 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3410 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3411 break;
3412 default:
3413 FIXME("Unhandled data type %#x.\n", data_type);
3414 shader_addline(buffer, "%s%s = %s(",
3415 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3416 break;
3420 return mask;
3423 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3424 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3426 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3429 /** Process GLSL instruction modifiers */
3430 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3432 struct glsl_dst_param dst_param;
3433 DWORD modifiers;
3435 if (!ins->dst_count) return;
3437 modifiers = ins->dst[0].modifiers;
3438 if (!modifiers) return;
3440 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3442 if (modifiers & WINED3DSPDM_SATURATE)
3444 /* _SAT means to clamp the value of the register to between 0 and 1 */
3445 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3446 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3449 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3451 FIXME("_centroid modifier not handled\n");
3454 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3456 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3460 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3462 switch (op)
3464 case WINED3D_SHADER_REL_OP_GT: return ">";
3465 case WINED3D_SHADER_REL_OP_EQ: return "==";
3466 case WINED3D_SHADER_REL_OP_GE: return ">=";
3467 case WINED3D_SHADER_REL_OP_LT: return "<";
3468 case WINED3D_SHADER_REL_OP_NE: return "!=";
3469 case WINED3D_SHADER_REL_OP_LE: return "<=";
3470 default:
3471 FIXME("Unrecognized operator %#x.\n", op);
3472 return "(\?\?)";
3476 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3478 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3481 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3482 unsigned int *coord_size, unsigned int *deriv_size)
3484 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3485 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3487 *coord_size = resource_type_info[resource_type].coord_size;
3488 *deriv_size = *coord_size;
3489 if (is_array)
3490 --(*deriv_size);
3493 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3494 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3496 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3497 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3498 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3499 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3500 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3501 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3502 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3503 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3504 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3505 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3506 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3507 const char *base = "texture", *type_part = "", *suffix = "";
3508 unsigned int coord_size, deriv_size;
3510 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3512 if (resource_type >= ARRAY_SIZE(resource_type_info))
3514 ERR("Unexpected resource type %#x.\n", resource_type);
3515 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3518 /* Note that there's no such thing as a projected cube texture. */
3519 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3520 projected = FALSE;
3522 if (needs_legacy_glsl_syntax(gl_info))
3524 if (shadow)
3525 base = "shadow";
3527 type_part = resource_type_info[resource_type].type_part;
3528 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3529 type_part = "2DRect";
3530 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3531 FIXME("Unhandled resource type %#x.\n", resource_type);
3533 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3535 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3536 suffix = "ARB";
3537 else
3538 FIXME("Unsupported grad function.\n");
3542 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3544 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3545 if (flags & ~texel_fetch_flags)
3546 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3548 base = "texelFetch";
3549 type_part = "";
3552 sample_function->name = string_buffer_get(priv->string_buffers);
3553 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3554 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3556 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3557 if (shadow)
3558 ++coord_size;
3559 sample_function->offset_size = offset ? deriv_size : 0;
3560 sample_function->coord_mask = (1u << coord_size) - 1;
3561 sample_function->deriv_mask = (1u << deriv_size) - 1;
3562 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3565 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3566 struct glsl_sample_function *sample_function)
3568 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3570 string_buffer_release(priv->string_buffers, sample_function->name);
3573 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3574 BOOL sign_fixup, enum fixup_channel_source channel_source)
3576 switch(channel_source)
3578 case CHANNEL_SOURCE_ZERO:
3579 strcat(arguments, "0.0");
3580 break;
3582 case CHANNEL_SOURCE_ONE:
3583 strcat(arguments, "1.0");
3584 break;
3586 case CHANNEL_SOURCE_X:
3587 strcat(arguments, reg_name);
3588 strcat(arguments, ".x");
3589 break;
3591 case CHANNEL_SOURCE_Y:
3592 strcat(arguments, reg_name);
3593 strcat(arguments, ".y");
3594 break;
3596 case CHANNEL_SOURCE_Z:
3597 strcat(arguments, reg_name);
3598 strcat(arguments, ".z");
3599 break;
3601 case CHANNEL_SOURCE_W:
3602 strcat(arguments, reg_name);
3603 strcat(arguments, ".w");
3604 break;
3606 default:
3607 FIXME("Unhandled channel source %#x\n", channel_source);
3608 strcat(arguments, "undefined");
3609 break;
3612 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3615 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3616 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3618 unsigned int mask_size, remaining;
3619 DWORD fixup_mask = 0;
3620 char arguments[256];
3621 char mask_str[6];
3623 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3624 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3625 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3626 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3627 if (!(mask &= fixup_mask))
3628 return;
3630 if (is_complex_fixup(fixup))
3632 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3633 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3634 return;
3637 shader_glsl_write_mask_to_str(mask, mask_str);
3638 mask_size = shader_glsl_get_write_mask_size(mask);
3640 arguments[0] = '\0';
3641 remaining = mask_size;
3642 if (mask & WINED3DSP_WRITEMASK_0)
3644 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3645 if (--remaining) strcat(arguments, ", ");
3647 if (mask & WINED3DSP_WRITEMASK_1)
3649 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3650 if (--remaining) strcat(arguments, ", ");
3652 if (mask & WINED3DSP_WRITEMASK_2)
3654 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3655 if (--remaining) strcat(arguments, ", ");
3657 if (mask & WINED3DSP_WRITEMASK_3)
3659 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3660 if (--remaining) strcat(arguments, ", ");
3663 if (mask_size > 1)
3664 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3665 else
3666 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3669 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3671 char reg_name[256];
3672 BOOL is_color;
3674 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins);
3675 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3678 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3679 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3680 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3681 const char *coord_reg_fmt, ...)
3683 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3684 char dst_swizzle[6];
3685 struct color_fixup_desc fixup;
3686 BOOL np2_fixup = FALSE;
3687 va_list args;
3688 int ret;
3690 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3692 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3693 * We actually rely on it for vertex shaders and SM4+. */
3694 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3696 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3697 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3699 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3700 np2_fixup = TRUE;
3702 else
3704 fixup = COLOR_FIXUP_IDENTITY;
3707 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3709 if (sample_function->output_single_component)
3710 shader_addline(ins->ctx->buffer, "vec4(");
3712 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3713 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3715 for (;;)
3717 va_start(args, coord_reg_fmt);
3718 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3719 va_end(args);
3720 if (!ret)
3721 break;
3722 if (!string_buffer_resize(ins->ctx->buffer, ret))
3723 break;
3726 if (np2_fixup)
3728 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3729 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3731 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3733 case 1:
3734 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3735 idx >> 1, (idx % 2) ? "z" : "x");
3736 break;
3737 case 2:
3738 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3739 idx >> 1, (idx % 2) ? "zw" : "xy");
3740 break;
3741 case 3:
3742 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3743 idx >> 1, (idx % 2) ? "zw" : "xy");
3744 break;
3745 case 4:
3746 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3747 idx >> 1, (idx % 2) ? "zw" : "xy");
3748 break;
3751 if (dx && dy)
3752 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3753 else if (bias)
3754 shader_addline(ins->ctx->buffer, ", %s", bias);
3755 if (sample_function->offset_size)
3757 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3758 shader_addline(ins->ctx->buffer, ", ");
3759 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3761 shader_addline(ins->ctx->buffer, ")");
3763 if (sample_function->output_single_component)
3764 shader_addline(ins->ctx->buffer, ")");
3766 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3768 if (!is_identity_fixup(fixup))
3769 shader_glsl_color_correction(ins, fixup);
3772 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3774 /* Write the final position.
3776 * OpenGL coordinates specify the center of the pixel while D3D coords
3777 * specify the corner. The offsets are stored in z and w in
3778 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3779 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3780 * a MAD. */
3781 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3782 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3784 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3785 * in utils.c
3787 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3788 * shaders are run before the homogeneous divide, so we have to take the w
3789 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3790 * z = z * 2 - w. */
3791 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3794 /*****************************************************************************
3795 * Begin processing individual instruction opcodes
3796 ****************************************************************************/
3798 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3800 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3801 struct glsl_src_param src0_param;
3802 struct glsl_src_param src1_param;
3803 DWORD write_mask;
3804 const char *op;
3806 /* Determine the GLSL operator to use based on the opcode */
3807 switch (ins->handler_idx)
3809 case WINED3DSIH_ADD: op = "+"; break;
3810 case WINED3DSIH_AND: op = "&"; break;
3811 case WINED3DSIH_DIV: op = "/"; break;
3812 case WINED3DSIH_IADD: op = "+"; break;
3813 case WINED3DSIH_ISHL: op = "<<"; break;
3814 case WINED3DSIH_ISHR: op = ">>"; break;
3815 case WINED3DSIH_MUL: op = "*"; break;
3816 case WINED3DSIH_OR: op = "|"; break;
3817 case WINED3DSIH_SUB: op = "-"; break;
3818 case WINED3DSIH_USHR: op = ">>"; break;
3819 case WINED3DSIH_XOR: op = "^"; break;
3820 default:
3821 op = "<unhandled operator>";
3822 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3823 break;
3826 write_mask = shader_glsl_append_dst(buffer, ins);
3827 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3828 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3829 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3832 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3834 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3835 struct glsl_src_param src0_param;
3836 struct glsl_src_param src1_param;
3837 unsigned int mask_size;
3838 DWORD write_mask;
3839 const char *op;
3841 write_mask = shader_glsl_append_dst(buffer, ins);
3842 mask_size = shader_glsl_get_write_mask_size(write_mask);
3843 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3844 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3846 if (mask_size > 1)
3848 switch (ins->handler_idx)
3850 case WINED3DSIH_EQ: op = "equal"; break;
3851 case WINED3DSIH_IEQ: op = "equal"; break;
3852 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3853 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3854 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3855 case WINED3DSIH_LT: op = "lessThan"; break;
3856 case WINED3DSIH_ILT: op = "lessThan"; break;
3857 case WINED3DSIH_ULT: op = "lessThan"; break;
3858 case WINED3DSIH_NE: op = "notEqual"; break;
3859 case WINED3DSIH_INE: op = "notEqual"; break;
3860 default:
3861 op = "<unhandled operator>";
3862 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3863 break;
3866 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3867 mask_size, op, src0_param.param_str, src1_param.param_str);
3869 else
3871 switch (ins->handler_idx)
3873 case WINED3DSIH_EQ: op = "=="; break;
3874 case WINED3DSIH_IEQ: op = "=="; break;
3875 case WINED3DSIH_GE: op = ">="; break;
3876 case WINED3DSIH_IGE: op = ">="; break;
3877 case WINED3DSIH_UGE: op = ">="; break;
3878 case WINED3DSIH_LT: op = "<"; break;
3879 case WINED3DSIH_ILT: op = "<"; break;
3880 case WINED3DSIH_ULT: op = "<"; break;
3881 case WINED3DSIH_NE: op = "!="; break;
3882 case WINED3DSIH_INE: op = "!="; break;
3883 default:
3884 op = "<unhandled operator>";
3885 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3886 break;
3889 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3890 src0_param.param_str, op, src1_param.param_str);
3894 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3896 struct glsl_src_param src_param;
3897 DWORD write_mask;
3898 const char *op;
3900 switch (ins->handler_idx)
3902 case WINED3DSIH_INEG: op = "-"; break;
3903 case WINED3DSIH_NOT: op = "~"; break;
3904 default:
3905 op = "<unhandled operator>";
3906 ERR("Unhandled opcode %s.\n",
3907 debug_d3dshaderinstructionhandler(ins->handler_idx));
3908 break;
3911 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3912 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3913 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3916 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3918 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3919 struct glsl_src_param src0_param;
3920 struct glsl_src_param src1_param;
3921 DWORD write_mask;
3923 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3924 * If not, we can emulate it. */
3925 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3926 FIXME("64-bit integer multiplies not implemented.\n");
3928 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3930 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3931 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3932 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3934 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3935 src0_param.param_str, src1_param.param_str);
3939 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3941 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3942 struct glsl_src_param src0_param, src1_param;
3943 DWORD write_mask;
3945 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3947 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3949 char dst_mask[6];
3951 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3952 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3953 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3954 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3955 dst_mask, src0_param.param_str, src1_param.param_str);
3957 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3958 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3959 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3960 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3962 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3963 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3965 else
3967 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3968 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3969 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3970 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3973 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3975 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3976 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3977 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3978 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3982 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3983 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3985 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3986 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3987 struct glsl_src_param src0_param;
3988 DWORD write_mask;
3990 write_mask = shader_glsl_append_dst(buffer, ins);
3991 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3993 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3994 * shader versions WINED3DSIO_MOVA is used for this. */
3995 if (ins->ctx->reg_maps->shader_version.major == 1
3996 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3997 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3999 /* This is a simple floor() */
4000 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4001 if (mask_size > 1) {
4002 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4003 } else {
4004 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4007 else if (ins->handler_idx == WINED3DSIH_MOVA)
4009 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4011 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4013 if (mask_size > 1)
4014 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4015 else
4016 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4018 else
4020 if (mask_size > 1)
4021 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4022 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4023 else
4024 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4025 src0_param.param_str, src0_param.param_str);
4028 else
4030 shader_addline(buffer, "%s);\n", src0_param.param_str);
4034 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4035 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4037 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4038 struct glsl_src_param src0_param;
4039 struct glsl_src_param src1_param;
4040 DWORD dst_write_mask, src_write_mask;
4041 unsigned int dst_size;
4043 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4044 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4046 /* dp4 works on vec4, dp3 on vec3, etc. */
4047 if (ins->handler_idx == WINED3DSIH_DP4)
4048 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4049 else if (ins->handler_idx == WINED3DSIH_DP3)
4050 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4051 else
4052 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4054 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4055 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4057 if (dst_size > 1) {
4058 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4059 } else {
4060 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4064 /* Note that this instruction has some restrictions. The destination write mask
4065 * can't contain the w component, and the source swizzles have to be .xyzw */
4066 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4068 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4069 struct glsl_src_param src0_param;
4070 struct glsl_src_param src1_param;
4071 char dst_mask[6];
4073 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4074 shader_glsl_append_dst(ins->ctx->buffer, ins);
4075 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4076 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4077 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4080 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4082 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4084 if (!stream)
4085 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4086 else
4087 FIXME("Unhandled primitive stream %u.\n", stream);
4090 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4091 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4092 * GLSL uses the value as-is. */
4093 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4095 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4096 struct glsl_src_param src0_param;
4097 struct glsl_src_param src1_param;
4098 DWORD dst_write_mask;
4099 unsigned int dst_size;
4101 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4102 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4104 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4105 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4107 if (dst_size > 1)
4109 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4110 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4112 else
4114 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4115 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4119 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4120 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4122 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4123 struct glsl_src_param src_param;
4124 const char *instruction;
4125 DWORD write_mask;
4126 unsigned i;
4128 /* Determine the GLSL function to use based on the opcode */
4129 /* TODO: Possibly make this a table for faster lookups */
4130 switch (ins->handler_idx)
4132 case WINED3DSIH_ABS: instruction = "abs"; break;
4133 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4134 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4135 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4136 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4137 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4138 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4139 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4140 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4141 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4142 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4143 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4144 case WINED3DSIH_FRC: instruction = "fract"; break;
4145 case WINED3DSIH_IMAX: instruction = "max"; break;
4146 case WINED3DSIH_IMIN: instruction = "min"; break;
4147 case WINED3DSIH_MAX: instruction = "max"; break;
4148 case WINED3DSIH_MIN: instruction = "min"; break;
4149 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4150 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4151 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4152 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4153 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4154 case WINED3DSIH_UMAX: instruction = "max"; break;
4155 case WINED3DSIH_UMIN: instruction = "min"; break;
4156 default: instruction = "";
4157 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4158 break;
4161 write_mask = shader_glsl_append_dst(buffer, ins);
4163 /* In D3D bits are numbered from the most significant bit. */
4164 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4165 shader_addline(buffer, "31 - ");
4166 shader_addline(buffer, "%s(", instruction);
4168 if (ins->src_count)
4170 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4171 shader_addline(buffer, "%s", src_param.param_str);
4172 for (i = 1; i < ins->src_count; ++i)
4174 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4175 shader_addline(buffer, ", %s", src_param.param_str);
4179 shader_addline(buffer, "));\n");
4182 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4184 struct wined3d_shader_dst_param dst;
4185 struct glsl_src_param src;
4186 DWORD write_mask;
4187 const char *fmt;
4188 unsigned int i;
4190 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4191 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4193 dst = ins->dst[0];
4194 for (i = 0; i < 4; ++i)
4196 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4197 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4198 &dst, dst.reg.data_type)))
4199 continue;
4201 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4202 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4206 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4208 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4209 struct wined3d_shader_dst_param dst;
4210 struct glsl_src_param src[4];
4211 const char *instruction;
4212 BOOL tmp_dst = FALSE;
4213 char mask_char[6];
4214 unsigned int i, j;
4215 DWORD write_mask;
4217 switch (ins->handler_idx)
4219 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4220 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4221 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4222 default:
4223 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4224 return;
4227 for (i = 0; i < ins->src_count; ++i)
4229 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4230 && ins->dst[0].reg.type == ins->src[i].reg.type)
4231 tmp_dst = TRUE;
4234 dst = ins->dst[0];
4235 for (i = 0; i < 4; ++i)
4237 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4238 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4239 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4240 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4241 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4242 continue;
4244 for (j = 0; j < ins->src_count; ++j)
4245 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4246 shader_addline(buffer, "%s(", instruction);
4247 for (j = 0; j < ins->src_count - 2; ++j)
4248 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4249 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4252 if (tmp_dst)
4254 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4255 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4256 shader_addline(buffer, "tmp0%s);\n", mask_char);
4260 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4262 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4264 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4265 struct glsl_src_param src_param;
4266 unsigned int mask_size;
4267 DWORD write_mask;
4268 char dst_mask[6];
4270 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4271 mask_size = shader_glsl_get_write_mask_size(write_mask);
4272 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4274 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4275 src_param.param_str, src_param.param_str);
4276 shader_glsl_append_dst(buffer, ins);
4278 if (mask_size > 1)
4280 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4281 mask_size, src_param.param_str);
4283 else
4285 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4286 src_param.param_str);
4290 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4292 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4293 ins->ctx->reg_maps->shader_version.minor);
4294 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4295 struct glsl_src_param src0_param;
4296 const char *prefix, *suffix;
4297 unsigned int dst_size;
4298 DWORD dst_write_mask;
4300 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4301 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4303 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4304 dst_write_mask = WINED3DSP_WRITEMASK_3;
4306 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4308 switch (ins->handler_idx)
4310 case WINED3DSIH_EXP:
4311 case WINED3DSIH_EXPP:
4312 prefix = "exp2(";
4313 suffix = ")";
4314 break;
4316 case WINED3DSIH_LOG:
4317 case WINED3DSIH_LOGP:
4318 prefix = "log2(abs(";
4319 suffix = "))";
4320 break;
4322 case WINED3DSIH_RCP:
4323 prefix = "1.0 / ";
4324 suffix = "";
4325 break;
4327 case WINED3DSIH_RSQ:
4328 prefix = "inversesqrt(abs(";
4329 suffix = "))";
4330 break;
4332 default:
4333 prefix = "";
4334 suffix = "";
4335 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4336 break;
4339 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4340 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4341 else
4342 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4345 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4346 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4347 * dst.x = 2^(floor(src))
4348 * dst.y = src - floor(src)
4349 * dst.z = 2^src (partial precision is allowed, but optional)
4350 * dst.w = 1.0;
4351 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4352 * dst = 2^src; (partial precision is allowed, but optional)
4354 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4356 if (ins->ctx->reg_maps->shader_version.major < 2)
4358 struct glsl_src_param src_param;
4359 char dst_mask[6];
4361 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4363 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4364 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4365 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4366 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4368 shader_glsl_append_dst(ins->ctx->buffer, ins);
4369 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4370 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4371 return;
4374 shader_glsl_scalar_op(ins);
4377 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4378 const char *vector_constructor, const char *scalar_constructor)
4380 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4381 struct glsl_src_param src_param;
4382 unsigned int mask_size;
4383 DWORD write_mask;
4385 write_mask = shader_glsl_append_dst(buffer, ins);
4386 mask_size = shader_glsl_get_write_mask_size(write_mask);
4387 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4389 if (mask_size > 1)
4390 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4391 else
4392 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4395 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4397 shader_glsl_cast(ins, "ivec", "int");
4400 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4402 shader_glsl_cast(ins, "uvec", "uint");
4405 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4407 shader_glsl_cast(ins, "vec", "float");
4410 /** Process signed comparison opcodes in GLSL. */
4411 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4413 struct glsl_src_param src0_param;
4414 struct glsl_src_param src1_param;
4415 DWORD write_mask;
4416 unsigned int mask_size;
4418 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4419 mask_size = shader_glsl_get_write_mask_size(write_mask);
4420 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4421 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4423 if (mask_size > 1) {
4424 const char *compare;
4426 switch(ins->handler_idx)
4428 case WINED3DSIH_SLT: compare = "lessThan"; break;
4429 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4430 default: compare = "";
4431 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4434 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4435 src0_param.param_str, src1_param.param_str);
4436 } else {
4437 switch(ins->handler_idx)
4439 case WINED3DSIH_SLT:
4440 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4441 * to return 0.0 but step returns 1.0 because step is not < x
4442 * An alternative is a bvec compare padded with an unused second component.
4443 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4444 * issue. Playing with not() is not possible either because not() does not accept
4445 * a scalar.
4447 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4448 src0_param.param_str, src1_param.param_str);
4449 break;
4450 case WINED3DSIH_SGE:
4451 /* Here we can use the step() function and safe a conditional */
4452 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4453 break;
4454 default:
4455 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4461 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4463 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4464 struct wined3d_shader_dst_param dst[2];
4465 struct glsl_src_param src[3];
4466 unsigned int i, j, k;
4467 char mask_char[6];
4468 DWORD write_mask;
4469 BOOL tmp_dst[2];
4471 for (i = 0; i < ins->dst_count; ++i)
4473 tmp_dst[i] = FALSE;
4474 for (j = 0; j < ins->src_count; ++j)
4476 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4477 && ins->dst[i].reg.type == ins->src[j].reg.type)
4478 tmp_dst[i] = TRUE;
4482 dst[0] = ins->dst[0];
4483 dst[1] = ins->dst[1];
4484 for (i = 0; i < 4; ++i)
4486 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4488 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4489 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4490 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4491 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4492 continue;
4494 for (k = 0; k < ARRAY_SIZE(src); ++k)
4495 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4497 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4498 src[0].param_str, src[1].param_str, src[2].param_str);
4502 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4504 if (tmp_dst[i])
4506 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4507 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4508 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4513 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4515 const char *condition_prefix, *condition_suffix;
4516 struct wined3d_shader_dst_param dst;
4517 struct glsl_src_param src0_param;
4518 struct glsl_src_param src1_param;
4519 struct glsl_src_param src2_param;
4520 BOOL temp_destination = FALSE;
4521 DWORD cmp_channel = 0;
4522 unsigned int i, j;
4523 char mask_char[6];
4524 DWORD write_mask;
4526 switch (ins->handler_idx)
4528 case WINED3DSIH_CMP:
4529 condition_prefix = "";
4530 condition_suffix = " >= 0.0";
4531 break;
4533 case WINED3DSIH_CND:
4534 condition_prefix = "";
4535 condition_suffix = " > 0.5";
4536 break;
4538 case WINED3DSIH_MOVC:
4539 condition_prefix = "bool(";
4540 condition_suffix = ")";
4541 break;
4543 default:
4544 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4545 condition_prefix = "<unhandled prefix>";
4546 condition_suffix = "<unhandled suffix>";
4547 break;
4550 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4552 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4553 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4554 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4555 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4557 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4558 condition_prefix, src0_param.param_str, condition_suffix,
4559 src1_param.param_str, src2_param.param_str);
4560 return;
4563 dst = ins->dst[0];
4565 /* Splitting the instruction up in multiple lines imposes a problem:
4566 * The first lines may overwrite source parameters of the following lines.
4567 * Deal with that by using a temporary destination register if needed. */
4568 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4569 && ins->src[0].reg.type == dst.reg.type)
4570 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4571 && ins->src[1].reg.type == dst.reg.type)
4572 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4573 && ins->src[2].reg.type == dst.reg.type))
4574 temp_destination = TRUE;
4576 /* Cycle through all source0 channels. */
4577 for (i = 0; i < 4; ++i)
4579 write_mask = 0;
4580 /* Find the destination channels which use the current source0 channel. */
4581 for (j = 0; j < 4; ++j)
4583 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4585 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4586 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4589 dst.write_mask = ins->dst[0].write_mask & write_mask;
4591 if (temp_destination)
4593 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4594 continue;
4595 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4597 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4598 continue;
4600 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4601 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4602 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4604 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4605 condition_prefix, src0_param.param_str, condition_suffix,
4606 src1_param.param_str, src2_param.param_str);
4609 if (temp_destination)
4611 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4612 shader_glsl_append_dst(ins->ctx->buffer, ins);
4613 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4617 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4618 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4619 * the compare is done per component of src0. */
4620 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4622 struct glsl_src_param src0_param;
4623 struct glsl_src_param src1_param;
4624 struct glsl_src_param src2_param;
4625 DWORD write_mask;
4626 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4627 ins->ctx->reg_maps->shader_version.minor);
4629 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4631 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4632 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4633 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4634 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4636 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4637 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4638 else
4639 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4640 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4641 return;
4644 shader_glsl_conditional_move(ins);
4647 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4648 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4650 struct glsl_src_param src0_param;
4651 struct glsl_src_param src1_param;
4652 struct glsl_src_param src2_param;
4653 DWORD write_mask;
4655 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4656 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4657 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4658 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4659 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4660 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4663 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4664 Vertex shaders to GLSL codes */
4665 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4667 int i;
4668 int nComponents = 0;
4669 struct wined3d_shader_dst_param tmp_dst = {{0}};
4670 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4671 struct wined3d_shader_instruction tmp_ins;
4673 memset(&tmp_ins, 0, sizeof(tmp_ins));
4675 /* Set constants for the temporary argument */
4676 tmp_ins.ctx = ins->ctx;
4677 tmp_ins.dst_count = 1;
4678 tmp_ins.dst = &tmp_dst;
4679 tmp_ins.src_count = 2;
4680 tmp_ins.src = tmp_src;
4682 switch(ins->handler_idx)
4684 case WINED3DSIH_M4x4:
4685 nComponents = 4;
4686 tmp_ins.handler_idx = WINED3DSIH_DP4;
4687 break;
4688 case WINED3DSIH_M4x3:
4689 nComponents = 3;
4690 tmp_ins.handler_idx = WINED3DSIH_DP4;
4691 break;
4692 case WINED3DSIH_M3x4:
4693 nComponents = 4;
4694 tmp_ins.handler_idx = WINED3DSIH_DP3;
4695 break;
4696 case WINED3DSIH_M3x3:
4697 nComponents = 3;
4698 tmp_ins.handler_idx = WINED3DSIH_DP3;
4699 break;
4700 case WINED3DSIH_M3x2:
4701 nComponents = 2;
4702 tmp_ins.handler_idx = WINED3DSIH_DP3;
4703 break;
4704 default:
4705 break;
4708 tmp_dst = ins->dst[0];
4709 tmp_src[0] = ins->src[0];
4710 tmp_src[1] = ins->src[1];
4711 for (i = 0; i < nComponents; ++i)
4713 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4714 shader_glsl_dot(&tmp_ins);
4715 ++tmp_src[1].reg.idx[0].offset;
4720 The LRP instruction performs a component-wise linear interpolation
4721 between the second and third operands using the first operand as the
4722 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4723 This is equivalent to mix(src2, src1, src0);
4725 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4727 struct glsl_src_param src0_param;
4728 struct glsl_src_param src1_param;
4729 struct glsl_src_param src2_param;
4730 DWORD write_mask;
4732 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4734 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4735 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4736 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4738 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4739 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4742 /** Process the WINED3DSIO_LIT instruction in GLSL:
4743 * dst.x = dst.w = 1.0
4744 * dst.y = (src0.x > 0) ? src0.x
4745 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4746 * where src.w is clamped at +- 128
4748 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4750 struct glsl_src_param src0_param;
4751 struct glsl_src_param src1_param;
4752 struct glsl_src_param src3_param;
4753 char dst_mask[6];
4755 shader_glsl_append_dst(ins->ctx->buffer, ins);
4756 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4758 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4759 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4760 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4762 /* The sdk specifies the instruction like this
4763 * dst.x = 1.0;
4764 * if(src.x > 0.0) dst.y = src.x
4765 * else dst.y = 0.0.
4766 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4767 * else dst.z = 0.0;
4768 * dst.w = 1.0;
4769 * (where power = src.w clamped between -128 and 128)
4771 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4772 * dst.x = 1.0 ... No further explanation needed
4773 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4774 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4775 * dst.w = 1.0. ... Nothing fancy.
4777 * So we still have one conditional in there. So do this:
4778 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4780 * 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),
4781 * 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.
4782 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4784 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4785 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4786 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4788 shader_addline(ins->ctx->buffer,
4789 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4790 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4791 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4792 src0_param.param_str, src3_param.param_str, dst_mask);
4795 /** Process the WINED3DSIO_DST instruction in GLSL:
4796 * dst.x = 1.0
4797 * dst.y = src0.x * src0.y
4798 * dst.z = src0.z
4799 * dst.w = src1.w
4801 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4803 struct glsl_src_param src0y_param;
4804 struct glsl_src_param src0z_param;
4805 struct glsl_src_param src1y_param;
4806 struct glsl_src_param src1w_param;
4807 char dst_mask[6];
4809 shader_glsl_append_dst(ins->ctx->buffer, ins);
4810 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4812 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4813 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4814 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4815 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4817 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4818 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4821 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4822 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4823 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4825 * dst.x = cos(src0.?)
4826 * dst.y = sin(src0.?)
4827 * dst.z = dst.z
4828 * dst.w = dst.w
4830 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4832 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4833 struct glsl_src_param src0_param;
4834 DWORD write_mask;
4836 if (ins->ctx->reg_maps->shader_version.major < 4)
4838 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4840 write_mask = shader_glsl_append_dst(buffer, ins);
4841 switch (write_mask)
4843 case WINED3DSP_WRITEMASK_0:
4844 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4845 break;
4847 case WINED3DSP_WRITEMASK_1:
4848 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4849 break;
4851 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4852 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4853 src0_param.param_str, src0_param.param_str);
4854 break;
4856 default:
4857 ERR("Write mask should be .x, .y or .xy\n");
4858 break;
4861 return;
4864 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4867 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4869 char dst_mask[6];
4871 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4872 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4873 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4875 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4876 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4877 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4879 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4880 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4882 else
4884 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4885 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4886 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4889 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4891 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4892 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4893 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4897 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4898 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4899 * generate invalid code
4901 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4903 struct glsl_src_param src0_param;
4904 DWORD write_mask;
4906 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4907 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4909 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4912 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4913 * Start a for() loop where src1.y is the initial value of aL,
4914 * increment aL by src1.z for a total of src1.x iterations.
4915 * Need to use a temporary variable for this operation.
4917 /* FIXME: I don't think nested loops will work correctly this way. */
4918 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4920 struct wined3d_shader_parser_state *state = ins->ctx->state;
4921 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4922 const struct wined3d_shader *shader = ins->ctx->shader;
4923 const struct wined3d_shader_lconst *constant;
4924 struct glsl_src_param src1_param;
4925 const DWORD *control_values = NULL;
4927 if (ins->ctx->reg_maps->shader_version.major < 4)
4929 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4931 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4932 * class hardware doesn't support real varying indexing, but Microsoft
4933 * designed this feature for Shader model 2.x+. If the loop control is
4934 * known at compile time, the GLSL compiler can unroll the loop, and
4935 * replace indirect addressing with direct addressing. */
4936 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4938 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4940 if (constant->idx == ins->src[1].reg.idx[0].offset)
4942 control_values = constant->value;
4943 break;
4948 if (control_values)
4950 struct wined3d_shader_loop_control loop_control;
4951 loop_control.count = control_values[0];
4952 loop_control.start = control_values[1];
4953 loop_control.step = (int)control_values[2];
4955 if (loop_control.step > 0)
4957 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4958 state->current_loop_depth, loop_control.start,
4959 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4960 state->current_loop_depth, loop_control.step);
4962 else if (loop_control.step < 0)
4964 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4965 state->current_loop_depth, loop_control.start,
4966 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4967 state->current_loop_depth, loop_control.step);
4969 else
4971 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4972 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4973 state->current_loop_depth, loop_control.count,
4974 state->current_loop_depth);
4977 else
4979 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4980 state->current_loop_depth, state->current_loop_reg,
4981 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4982 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4985 ++state->current_loop_reg;
4987 else
4989 shader_addline(buffer, "for (;;)\n{\n");
4992 ++state->current_loop_depth;
4995 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4997 struct wined3d_shader_parser_state *state = ins->ctx->state;
4999 shader_addline(ins->ctx->buffer, "}\n");
5001 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5003 --state->current_loop_depth;
5004 --state->current_loop_reg;
5007 if (ins->handler_idx == WINED3DSIH_ENDREP)
5009 --state->current_loop_depth;
5013 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5015 struct wined3d_shader_parser_state *state = ins->ctx->state;
5016 const struct wined3d_shader *shader = ins->ctx->shader;
5017 const struct wined3d_shader_lconst *constant;
5018 struct glsl_src_param src0_param;
5019 const DWORD *control_values = NULL;
5021 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5022 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5024 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5026 if (constant->idx == ins->src[0].reg.idx[0].offset)
5028 control_values = constant->value;
5029 break;
5034 if (control_values)
5036 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5037 state->current_loop_depth, state->current_loop_depth,
5038 control_values[0], state->current_loop_depth);
5040 else
5042 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5043 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5044 state->current_loop_depth, state->current_loop_depth,
5045 src0_param.param_str, state->current_loop_depth);
5048 ++state->current_loop_depth;
5051 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5053 struct glsl_src_param src0_param;
5055 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5056 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5059 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5061 struct glsl_src_param src0_param;
5063 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5064 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5067 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5069 shader_addline(ins->ctx->buffer, "default:\n");
5072 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5074 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
5075 struct glsl_src_param src0_param;
5077 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5078 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
5081 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5083 struct glsl_src_param src0_param;
5084 struct glsl_src_param src1_param;
5086 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5087 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5089 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5090 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5093 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5095 shader_addline(ins->ctx->buffer, "} else {\n");
5098 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5100 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5102 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5103 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5104 shader_glsl_fixup_position(ins->ctx->buffer);
5106 if (!stream)
5107 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5108 else
5109 FIXME("Unhandled primitive stream %u.\n", stream);
5112 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5114 shader_addline(ins->ctx->buffer, "break;\n");
5117 /* FIXME: According to MSDN the compare is done per component. */
5118 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5120 struct glsl_src_param src0_param;
5121 struct glsl_src_param src1_param;
5123 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5124 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5126 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5127 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5130 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5132 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
5133 struct glsl_src_param src_param;
5134 const char *op;
5136 switch (ins->handler_idx)
5138 case WINED3DSIH_BREAKP: op = "break"; break;
5139 case WINED3DSIH_CONTINUEP: op = "continue"; break;
5140 case WINED3DSIH_RETP: op = "return"; break;
5141 default:
5142 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5143 return;
5146 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5147 shader_addline(ins->ctx->buffer, "if (%s(%s)) %s;\n", condition, src_param.param_str, op);
5150 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5152 shader_addline(ins->ctx->buffer, "continue;\n");
5155 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5157 shader_addline(ins->ctx->buffer, "}\n");
5158 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5160 /* Subroutines appear at the end of the shader. */
5161 ins->ctx->state->in_subroutine = TRUE;
5164 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5166 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5169 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5171 struct glsl_src_param src1_param;
5173 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5174 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5175 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5178 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5180 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5182 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5184 shader_glsl_generate_shader_epilogue(ins->ctx);
5185 shader_addline(ins->ctx->buffer, "return;\n");
5189 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5191 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5192 ins->ctx->reg_maps->shader_version.minor);
5193 struct glsl_sample_function sample_function;
5194 DWORD sample_flags = 0;
5195 DWORD resource_idx;
5196 DWORD mask = 0, swizzle;
5197 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5199 /* 1.0-1.4: Use destination register as sampler source.
5200 * 2.0+: Use provided sampler source. */
5201 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5202 resource_idx = ins->dst[0].reg.idx[0].offset;
5203 else
5204 resource_idx = ins->src[1].reg.idx[0].offset;
5206 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5208 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5209 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5210 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5212 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5213 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5215 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5216 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5218 case WINED3D_TTFF_COUNT1:
5219 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5220 break;
5221 case WINED3D_TTFF_COUNT2:
5222 mask = WINED3DSP_WRITEMASK_1;
5223 break;
5224 case WINED3D_TTFF_COUNT3:
5225 mask = WINED3DSP_WRITEMASK_2;
5226 break;
5227 case WINED3D_TTFF_COUNT4:
5228 case WINED3D_TTFF_DISABLE:
5229 mask = WINED3DSP_WRITEMASK_3;
5230 break;
5234 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5236 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5238 if (src_mod == WINED3DSPSM_DZ) {
5239 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5240 mask = WINED3DSP_WRITEMASK_2;
5241 } else if (src_mod == WINED3DSPSM_DW) {
5242 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5243 mask = WINED3DSP_WRITEMASK_3;
5246 else
5248 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5249 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5251 /* ps 2.0 texldp instruction always divides by the fourth component. */
5252 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5253 mask = WINED3DSP_WRITEMASK_3;
5257 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5258 mask |= sample_function.coord_mask;
5259 sample_function.coord_mask = mask;
5261 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5262 else swizzle = ins->src[1].swizzle;
5264 /* 1.0-1.3: Use destination register as coordinate source.
5265 1.4+: Use provided coordinate source register. */
5266 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5268 char coord_mask[6];
5269 shader_glsl_write_mask_to_str(mask, coord_mask);
5270 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5271 "T%u%s", resource_idx, coord_mask);
5273 else
5275 struct glsl_src_param coord_param;
5276 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5277 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5279 struct glsl_src_param bias;
5280 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5281 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5282 NULL, "%s", coord_param.param_str);
5283 } else {
5284 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5285 "%s", coord_param.param_str);
5288 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5291 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5293 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5294 struct glsl_src_param coord_param, dx_param, dy_param;
5295 struct glsl_sample_function sample_function;
5296 DWORD sampler_idx;
5297 DWORD swizzle = ins->src[1].swizzle;
5299 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5301 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5302 shader_glsl_tex(ins);
5303 return;
5306 sampler_idx = ins->src[1].reg.idx[0].offset;
5308 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5309 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5310 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5311 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5313 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5314 NULL, NULL, "%s", coord_param.param_str);
5315 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5318 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5320 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5321 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5322 struct glsl_src_param coord_param, lod_param;
5323 struct glsl_sample_function sample_function;
5324 DWORD swizzle = ins->src[1].swizzle;
5325 DWORD sampler_idx;
5327 sampler_idx = ins->src[1].reg.idx[0].offset;
5329 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5330 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5332 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5334 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5335 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5337 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5338 * However, the NVIDIA drivers allow them in fragment shaders as well,
5339 * even without the appropriate extension. */
5340 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5342 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5343 "%s", coord_param.param_str);
5344 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5347 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5348 unsigned int resource_idx, unsigned int sampler_idx)
5350 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5351 unsigned int i;
5353 for (i = 0; i < sampler_map->count; ++i)
5355 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5356 return entries[i].bind_idx;
5359 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5361 return ~0u;
5364 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5366 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5367 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5368 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5369 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5370 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5371 struct glsl_src_param structure_idx, offset, data, data2;
5372 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5373 enum wined3d_shader_resource_type resource_type;
5374 struct wined3d_string_buffer *address;
5375 enum wined3d_data_type data_type;
5376 unsigned int resource_idx, stride;
5377 const char *op, *resource;
5378 DWORD coord_mask;
5379 BOOL is_tgsm;
5381 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5382 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5383 if (is_tgsm)
5385 if (resource_idx >= reg_maps->tgsm_count)
5387 ERR("Invalid TGSM index %u.\n", resource_idx);
5388 return;
5390 resource = "g";
5391 data_type = WINED3D_DATA_UINT;
5392 coord_mask = 1;
5393 stride = reg_maps->tgsm[resource_idx].stride;
5395 else
5397 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5399 ERR("Invalid UAV index %u.\n", resource_idx);
5400 return;
5402 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5403 if (resource_type >= ARRAY_SIZE(resource_type_info))
5405 ERR("Unexpected resource type %#x.\n", resource_type);
5406 return;
5408 resource = "image";
5409 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5410 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5411 stride = reg_maps->uav_resource_info[resource_idx].stride;
5414 switch (ins->handler_idx)
5416 case WINED3DSIH_ATOMIC_AND:
5417 case WINED3DSIH_IMM_ATOMIC_AND:
5418 if (is_tgsm)
5419 op = "atomicAnd";
5420 else
5421 op = "imageAtomicAnd";
5422 break;
5423 case WINED3DSIH_ATOMIC_CMP_STORE:
5424 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5425 if (is_tgsm)
5426 op = "atomicCompSwap";
5427 else
5428 op = "imageAtomicCompSwap";
5429 break;
5430 case WINED3DSIH_ATOMIC_IADD:
5431 case WINED3DSIH_IMM_ATOMIC_IADD:
5432 if (is_tgsm)
5433 op = "atomicAdd";
5434 else
5435 op = "imageAtomicAdd";
5436 break;
5437 case WINED3DSIH_ATOMIC_IMAX:
5438 case WINED3DSIH_IMM_ATOMIC_IMAX:
5439 if (is_tgsm)
5440 op = "atomicMax";
5441 else
5442 op = "imageAtomicMax";
5443 if (data_type != WINED3D_DATA_INT)
5445 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5446 return;
5448 break;
5449 case WINED3DSIH_ATOMIC_IMIN:
5450 case WINED3DSIH_IMM_ATOMIC_IMIN:
5451 if (is_tgsm)
5452 op = "atomicMin";
5453 else
5454 op = "imageAtomicMin";
5455 if (data_type != WINED3D_DATA_INT)
5457 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5458 return;
5460 break;
5461 case WINED3DSIH_ATOMIC_OR:
5462 case WINED3DSIH_IMM_ATOMIC_OR:
5463 if (is_tgsm)
5464 op = "atomicOr";
5465 else
5466 op = "imageAtomicOr";
5467 break;
5468 case WINED3DSIH_ATOMIC_UMAX:
5469 case WINED3DSIH_IMM_ATOMIC_UMAX:
5470 if (is_tgsm)
5471 op = "atomicMax";
5472 else
5473 op = "imageAtomicMax";
5474 if (data_type != WINED3D_DATA_UINT)
5476 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5477 return;
5479 break;
5480 case WINED3DSIH_ATOMIC_UMIN:
5481 case WINED3DSIH_IMM_ATOMIC_UMIN:
5482 if (is_tgsm)
5483 op = "atomicMin";
5484 else
5485 op = "imageAtomicMin";
5486 if (data_type != WINED3D_DATA_UINT)
5488 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5489 return;
5491 break;
5492 case WINED3DSIH_ATOMIC_XOR:
5493 case WINED3DSIH_IMM_ATOMIC_XOR:
5494 if (is_tgsm)
5495 op = "atomicXor";
5496 else
5497 op = "imageAtomicXor";
5498 break;
5499 case WINED3DSIH_IMM_ATOMIC_EXCH:
5500 if (is_tgsm)
5501 op = "atomicExchange";
5502 else
5503 op = "imageAtomicExchange";
5504 break;
5505 default:
5506 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5507 return;
5510 address = string_buffer_get(priv->string_buffers);
5511 if (stride)
5513 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5514 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5515 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5517 else
5519 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5520 string_buffer_sprintf(address, "%s", offset.param_str);
5521 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5522 shader_addline(address, "/ 4");
5525 if (is_imm_instruction)
5526 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5528 if (is_tgsm)
5529 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5530 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5531 else
5532 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5533 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5535 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5536 shader_addline(buffer, "%s", data.param_str);
5537 if (ins->src_count >= 3)
5539 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5540 shader_addline(buffer, ", %s", data2.param_str);
5543 if (is_imm_instruction)
5544 shader_addline(buffer, ")");
5545 shader_addline(buffer, ");\n");
5547 string_buffer_release(priv->string_buffers, address);
5550 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5552 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5553 const char *op;
5555 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5556 op = "atomicCounterIncrement";
5557 else
5558 op = "atomicCounterDecrement";
5560 shader_glsl_append_dst(ins->ctx->buffer, ins);
5561 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5564 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5566 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5567 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5568 enum wined3d_shader_resource_type resource_type;
5569 struct glsl_src_param image_coord_param;
5570 enum wined3d_data_type data_type;
5571 DWORD coord_mask, write_mask;
5572 unsigned int uav_idx;
5573 char dst_swizzle[6];
5575 uav_idx = ins->src[1].reg.idx[0].offset;
5576 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5578 ERR("Invalid UAV index %u.\n", uav_idx);
5579 return;
5581 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5582 if (resource_type >= ARRAY_SIZE(resource_type_info))
5584 ERR("Unexpected resource type %#x.\n", resource_type);
5585 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5587 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5588 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5590 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5591 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5593 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5594 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5595 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5598 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5600 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5601 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5602 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5603 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5604 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5605 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5606 struct glsl_src_param structure_idx, offset;
5607 struct wined3d_string_buffer *address;
5608 struct wined3d_shader_dst_param dst;
5609 const char *function, *resource;
5611 resource_idx = src->reg.idx[0].offset;
5612 if (src->reg.type == WINED3DSPR_RESOURCE)
5614 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5616 ERR("Invalid resource index %u.\n", resource_idx);
5617 return;
5619 stride = reg_maps->resource_info[resource_idx].stride;
5620 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5621 function = "texelFetch";
5622 resource = "sampler";
5624 else if (src->reg.type == WINED3DSPR_UAV)
5626 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5628 ERR("Invalid UAV index %u.\n", resource_idx);
5629 return;
5631 stride = reg_maps->uav_resource_info[resource_idx].stride;
5632 bind_idx = resource_idx;
5633 function = "imageLoad";
5634 resource = "image";
5636 else
5638 if (resource_idx >= reg_maps->tgsm_count)
5640 ERR("Invalid TGSM index %u.\n", resource_idx);
5641 return;
5643 stride = reg_maps->tgsm[resource_idx].stride;
5644 bind_idx = resource_idx;
5645 function = NULL;
5646 resource = "g";
5649 address = string_buffer_get(priv->string_buffers);
5650 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5652 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5653 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5655 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5656 shader_addline(address, "%s / 4", offset.param_str);
5658 dst = ins->dst[0];
5659 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5661 /* The instruction is split into multiple lines. The first lines may
5662 * overwrite source parameters of the following lines. */
5663 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5664 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5667 for (i = 0; i < 4; ++i)
5669 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5670 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5671 continue;
5673 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5674 if (function)
5675 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5676 function, prefix, resource, bind_idx, address->buffer, swizzle);
5677 else
5678 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5679 prefix, resource, bind_idx, address->buffer, swizzle);
5682 string_buffer_release(priv->string_buffers, address);
5685 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5687 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5688 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5689 struct glsl_src_param image_coord_param, image_data_param;
5690 enum wined3d_shader_resource_type resource_type;
5691 enum wined3d_data_type data_type;
5692 unsigned int uav_idx;
5693 DWORD coord_mask;
5695 uav_idx = ins->dst[0].reg.idx[0].offset;
5696 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5698 ERR("Invalid UAV index %u.\n", uav_idx);
5699 return;
5701 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5702 if (resource_type >= ARRAY_SIZE(resource_type_info))
5704 ERR("Unexpected resource type %#x.\n", resource_type);
5705 return;
5707 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5708 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5710 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5711 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5712 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5713 shader_glsl_get_prefix(version->type), uav_idx,
5714 image_coord_param.param_str, image_data_param.param_str);
5717 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5719 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5720 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5721 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5722 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5723 struct glsl_src_param structure_idx, offset, data;
5724 unsigned int i, resource_idx, stride, src_idx = 0;
5725 struct wined3d_string_buffer *address;
5726 DWORD write_mask;
5727 BOOL is_tgsm;
5729 resource_idx = ins->dst[0].reg.idx[0].offset;
5730 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5731 if (is_tgsm)
5733 if (resource_idx >= reg_maps->tgsm_count)
5735 ERR("Invalid TGSM index %u.\n", resource_idx);
5736 return;
5738 stride = reg_maps->tgsm[resource_idx].stride;
5740 else
5742 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5744 ERR("Invalid UAV index %u.\n", resource_idx);
5745 return;
5747 stride = reg_maps->uav_resource_info[resource_idx].stride;
5750 address = string_buffer_get(priv->string_buffers);
5751 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5753 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5754 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5756 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5757 shader_addline(address, "%s / 4", offset.param_str);
5759 for (i = 0; i < 4; ++i)
5761 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5762 continue;
5764 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5766 if (is_tgsm)
5767 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5768 prefix, resource_idx, address->buffer, i, data.param_str);
5769 else
5770 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5771 prefix, resource_idx, address->buffer, i, data.param_str);
5774 string_buffer_release(priv->string_buffers, address);
5777 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5779 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5780 unsigned int sync_flags = ins->flags;
5782 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5784 shader_addline(buffer, "barrier();\n");
5785 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5788 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5790 shader_addline(buffer, "memoryBarrierShared();\n");
5791 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5794 if (sync_flags)
5795 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5798 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5799 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5801 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5802 unsigned int idx = reg->idx[0].offset;
5804 if (reg->type == WINED3DSPR_RESOURCE)
5806 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5808 ERR("Invalid resource index %u.\n", idx);
5809 return NULL;
5811 return &reg_maps->resource_info[idx];
5814 if (reg->type == WINED3DSPR_UAV)
5816 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5818 ERR("Invalid UAV index %u.\n", idx);
5819 return NULL;
5821 return &reg_maps->uav_resource_info[idx];
5824 FIXME("Unhandled register type %#x.\n", reg->type);
5825 return NULL;
5828 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5830 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5831 const struct wined3d_shader_resource_info *resource_info;
5832 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5833 unsigned int resource_idx;
5834 char dst_swizzle[6];
5835 DWORD write_mask;
5837 write_mask = shader_glsl_append_dst(buffer, ins);
5838 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5840 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5841 return;
5842 resource_idx = ins->src[0].reg.idx[0].offset;
5844 shader_addline(buffer, "ivec2(");
5845 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5847 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5848 resource_idx, WINED3D_SAMPLER_DEFAULT);
5849 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5851 else
5853 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5855 if (resource_info->stride)
5856 shader_addline(buffer, " / %u", resource_info->stride);
5857 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5858 shader_addline(buffer, " * 4");
5859 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5862 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5864 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5865 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5866 enum wined3d_shader_resource_type resource_type;
5867 enum wined3d_shader_register_type reg_type;
5868 unsigned int resource_idx, bind_idx, i;
5869 enum wined3d_data_type dst_data_type;
5870 struct glsl_src_param lod_param;
5871 char dst_swizzle[6];
5872 DWORD write_mask;
5874 dst_data_type = ins->dst[0].reg.data_type;
5875 if (ins->flags == WINED3DSI_RESINFO_UINT)
5876 dst_data_type = WINED3D_DATA_UINT;
5877 else if (ins->flags)
5878 FIXME("Unhandled flags %#x.\n", ins->flags);
5880 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5881 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5883 reg_type = ins->src[1].reg.type;
5884 resource_idx = ins->src[1].reg.idx[0].offset;
5885 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5886 if (reg_type == WINED3DSPR_RESOURCE)
5888 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5889 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5890 resource_idx, WINED3D_SAMPLER_DEFAULT);
5892 else
5894 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5895 bind_idx = resource_idx;
5898 if (resource_type >= ARRAY_SIZE(resource_type_info))
5900 ERR("Unexpected resource type %#x.\n", resource_type);
5901 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5904 if (dst_data_type == WINED3D_DATA_UINT)
5905 shader_addline(ins->ctx->buffer, "uvec4(");
5906 else
5907 shader_addline(ins->ctx->buffer, "vec4(");
5909 if (reg_type == WINED3DSPR_RESOURCE)
5911 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5912 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5914 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5915 shader_addline(ins->ctx->buffer, "0, ");
5917 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5919 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5920 shader_glsl_get_prefix(version->type), bind_idx);
5922 else
5924 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5925 shader_addline(ins->ctx->buffer, "1");
5928 else
5930 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5931 shader_glsl_get_prefix(version->type), bind_idx);
5933 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5934 shader_addline(ins->ctx->buffer, "0, ");
5936 /* For UAVs the returned miplevel count is always 1. */
5937 shader_addline(ins->ctx->buffer, "1");
5940 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5943 /* FIXME: The current implementation does not handle multisample textures correctly. */
5944 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5946 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5947 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5948 struct glsl_src_param coord_param, lod_param;
5949 struct glsl_sample_function sample_function;
5950 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5951 BOOL has_lod_param;
5953 if (wined3d_shader_instruction_has_texel_offset(ins))
5954 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5956 resource_idx = ins->src[1].reg.idx[0].offset;
5957 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5959 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5961 ERR("Invalid resource index %u.\n", resource_idx);
5962 return;
5964 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5966 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5967 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5968 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5969 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5970 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5971 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5972 "%s", coord_param.param_str);
5973 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5976 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5978 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5979 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5980 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5981 struct glsl_sample_function sample_function;
5982 DWORD flags = 0;
5984 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5985 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5986 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5987 flags |= WINED3D_GLSL_SAMPLE_LOD;
5988 if (wined3d_shader_instruction_has_texel_offset(ins))
5989 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5991 resource_idx = ins->src[1].reg.idx[0].offset;
5992 sampler_idx = ins->src[2].reg.idx[0].offset;
5994 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5995 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5997 switch (ins->handler_idx)
5999 case WINED3DSIH_SAMPLE:
6000 break;
6001 case WINED3DSIH_SAMPLE_B:
6002 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6003 lod_param_str = lod_param.param_str;
6004 break;
6005 case WINED3DSIH_SAMPLE_GRAD:
6006 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6007 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6008 dx_param_str = dx_param.param_str;
6009 dy_param_str = dy_param.param_str;
6010 break;
6011 case WINED3DSIH_SAMPLE_LOD:
6012 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6013 lod_param_str = lod_param.param_str;
6014 break;
6015 default:
6016 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6017 break;
6020 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6021 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6022 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6023 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6026 /* GLSL doesn't provide a function to sample from level zero with depth
6027 * comparison for array textures and cube textures. We use textureGrad*()
6028 * to implement sample_c_lz.
6030 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6031 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6032 unsigned int coord_size, const char *coord_param, const char *ref_param)
6034 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6035 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6036 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6037 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6038 char dst_swizzle[6];
6040 WARN("Emitting textureGrad() for sample_c_lz.\n");
6042 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6043 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6044 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6045 sample_function->offset_size ? "Offset" : "",
6046 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6047 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6048 if (sample_function->offset_size)
6050 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6051 shader_addline(buffer, ", ");
6052 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6054 shader_addline(buffer, "))%s);\n", dst_swizzle);
6057 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6059 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6060 const struct wined3d_shader_resource_info *resource_info;
6061 struct glsl_src_param coord_param, compare_param;
6062 struct glsl_sample_function sample_function;
6063 const char *lod_param = NULL;
6064 unsigned int coord_size;
6065 DWORD flags = 0;
6067 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6069 lod_param = "0";
6070 flags |= WINED3D_GLSL_SAMPLE_LOD;
6073 if (wined3d_shader_instruction_has_texel_offset(ins))
6074 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6076 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6077 return;
6078 resource_idx = ins->src[1].reg.idx[0].offset;
6079 sampler_idx = ins->src[2].reg.idx[0].offset;
6081 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6082 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6083 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6084 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6085 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6086 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6087 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6088 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6090 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6091 coord_size, coord_param.param_str, compare_param.param_str);
6093 else
6095 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6096 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6097 coord_size, coord_param.param_str, compare_param.param_str);
6099 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6102 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6104 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6105 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6106 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6107 struct glsl_src_param coord_param, compare_param, offset_param;
6108 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6109 const struct wined3d_shader_resource_info *resource_info;
6110 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6111 unsigned int coord_size, offset_size;
6112 char dst_swizzle[6];
6113 BOOL has_offset;
6115 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6117 FIXME("OpenGL implementation does not support textureGather.\n");
6118 return;
6121 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6122 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6123 || wined3d_shader_instruction_has_texel_offset(ins);
6125 resource_param_idx =
6126 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6127 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6128 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6129 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6130 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6132 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6133 return;
6135 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6137 ERR("Unexpected resource type %#x.\n", resource_info->type);
6138 return;
6140 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6142 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6143 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6145 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6147 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6148 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6149 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6151 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6152 shader_addline(buffer, ", %s", compare_param.param_str);
6154 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6156 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6157 shader_addline(buffer, ", %s", offset_param.param_str);
6159 else if (has_offset)
6161 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6162 shader_addline(buffer, ", ");
6163 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6165 if (component_idx)
6166 shader_addline(buffer, ", %u", component_idx);
6168 shader_addline(buffer, ")%s);\n", dst_swizzle);
6171 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6173 /* FIXME: Make this work for more than just 2D textures */
6174 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6175 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6177 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6179 char dst_mask[6];
6181 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6182 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6183 ins->dst[0].reg.idx[0].offset, dst_mask);
6185 else
6187 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6188 DWORD reg = ins->src[0].reg.idx[0].offset;
6189 char dst_swizzle[6];
6191 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6193 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6195 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6196 struct glsl_src_param div_param;
6197 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6199 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6201 if (mask_size > 1)
6202 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6203 else
6204 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6206 else
6208 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6213 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6214 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6215 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6216 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6218 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6219 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6220 struct glsl_sample_function sample_function;
6221 struct glsl_src_param src0_param;
6222 UINT mask_size;
6224 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6226 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6227 * scalar, and projected sampling would require 4.
6229 * It is a dependent read - not valid with conditional NP2 textures
6231 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6232 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6234 switch(mask_size)
6236 case 1:
6237 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6238 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6239 break;
6241 case 2:
6242 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6243 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6244 break;
6246 case 3:
6247 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6248 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6249 break;
6251 default:
6252 FIXME("Unexpected mask size %u\n", mask_size);
6253 break;
6255 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6258 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6259 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6260 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6262 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6263 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6264 struct glsl_src_param src0_param;
6265 DWORD dst_mask;
6266 unsigned int mask_size;
6268 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6269 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6270 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6272 if (mask_size > 1) {
6273 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6274 } else {
6275 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6279 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6280 * Calculate the depth as dst.x / dst.y */
6281 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6283 struct glsl_dst_param dst_param;
6285 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6287 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6288 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6289 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6290 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6291 * >= 1.0 or < 0.0
6293 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6294 dst_param.reg_name, dst_param.reg_name);
6297 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6298 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6299 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6300 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6302 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6304 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6305 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6306 struct glsl_src_param src0_param;
6308 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6310 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6311 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6314 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6315 * Calculate the 1st of a 2-row matrix multiplication. */
6316 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6318 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6319 DWORD reg = ins->dst[0].reg.idx[0].offset;
6320 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6321 struct glsl_src_param src0_param;
6323 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6324 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6327 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6328 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6329 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6331 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6332 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6333 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6334 DWORD reg = ins->dst[0].reg.idx[0].offset;
6335 struct glsl_src_param src0_param;
6337 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6338 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6339 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6342 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6344 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6345 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6346 struct glsl_sample_function sample_function;
6347 DWORD reg = ins->dst[0].reg.idx[0].offset;
6348 struct glsl_src_param src0_param;
6350 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6351 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6353 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6355 /* Sample the texture using the calculated coordinates */
6356 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6357 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6360 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6361 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6362 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6364 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6365 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6366 struct glsl_sample_function sample_function;
6367 DWORD reg = ins->dst[0].reg.idx[0].offset;
6368 struct glsl_src_param src0_param;
6370 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6371 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6373 /* Dependent read, not valid with conditional NP2 */
6374 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6376 /* Sample the texture using the calculated coordinates */
6377 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6378 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6380 tex_mx->current_row = 0;
6383 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6384 * Perform the 3rd row of a 3x3 matrix multiply */
6385 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6387 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6388 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6389 DWORD reg = ins->dst[0].reg.idx[0].offset;
6390 struct glsl_src_param src0_param;
6391 char dst_mask[6];
6393 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6395 shader_glsl_append_dst(ins->ctx->buffer, ins);
6396 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6397 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6399 tex_mx->current_row = 0;
6402 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6403 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6404 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6406 struct glsl_src_param src0_param;
6407 struct glsl_src_param src1_param;
6408 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6409 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6410 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6411 struct glsl_sample_function sample_function;
6412 DWORD reg = ins->dst[0].reg.idx[0].offset;
6413 char coord_mask[6];
6415 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6416 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6418 /* Perform the last matrix multiply operation */
6419 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6420 /* Reflection calculation */
6421 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6423 /* Dependent read, not valid with conditional NP2 */
6424 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6425 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6427 /* Sample the texture */
6428 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6429 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6430 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6432 tex_mx->current_row = 0;
6435 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6436 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6437 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6439 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6440 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6441 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6442 struct glsl_sample_function sample_function;
6443 DWORD reg = ins->dst[0].reg.idx[0].offset;
6444 struct glsl_src_param src0_param;
6445 char coord_mask[6];
6447 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6449 /* Perform the last matrix multiply operation */
6450 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6452 /* Construct the eye-ray vector from w coordinates */
6453 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6454 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6455 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6457 /* Dependent read, not valid with conditional NP2 */
6458 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6459 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6461 /* Sample the texture using the calculated coordinates */
6462 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6463 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6464 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6466 tex_mx->current_row = 0;
6469 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6470 * Apply a fake bump map transform.
6471 * texbem is pshader <= 1.3 only, this saves a few version checks
6473 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6475 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6476 struct glsl_sample_function sample_function;
6477 struct glsl_src_param coord_param;
6478 DWORD sampler_idx;
6479 DWORD mask;
6480 DWORD flags;
6481 char coord_mask[6];
6483 sampler_idx = ins->dst[0].reg.idx[0].offset;
6484 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6485 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6487 /* Dependent read, not valid with conditional NP2 */
6488 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6489 mask = sample_function.coord_mask;
6491 shader_glsl_write_mask_to_str(mask, coord_mask);
6493 /* With projected textures, texbem only divides the static texture coord,
6494 * not the displacement, so we can't let GL handle this. */
6495 if (flags & WINED3D_PSARGS_PROJECTED)
6497 DWORD div_mask=0;
6498 char coord_div_mask[3];
6499 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6501 case WINED3D_TTFF_COUNT1:
6502 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6503 break;
6504 case WINED3D_TTFF_COUNT2:
6505 div_mask = WINED3DSP_WRITEMASK_1;
6506 break;
6507 case WINED3D_TTFF_COUNT3:
6508 div_mask = WINED3DSP_WRITEMASK_2;
6509 break;
6510 case WINED3D_TTFF_COUNT4:
6511 case WINED3D_TTFF_DISABLE:
6512 div_mask = WINED3DSP_WRITEMASK_3;
6513 break;
6515 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6516 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6519 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6521 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6522 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6523 coord_param.param_str, coord_mask);
6525 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6527 struct glsl_src_param luminance_param;
6528 struct glsl_dst_param dst_param;
6530 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6531 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6533 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6534 dst_param.reg_name, dst_param.mask_str,
6535 luminance_param.param_str, sampler_idx, sampler_idx);
6537 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6540 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6542 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6543 struct glsl_src_param src0_param, src1_param;
6545 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6546 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6548 shader_glsl_append_dst(ins->ctx->buffer, ins);
6549 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6550 src0_param.param_str, sampler_idx, src1_param.param_str);
6553 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6554 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6555 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6557 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6558 struct glsl_sample_function sample_function;
6559 struct glsl_src_param src0_param;
6561 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6563 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6564 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6565 "%s.wx", src0_param.reg_name);
6566 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6569 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6570 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6571 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6573 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6574 struct glsl_sample_function sample_function;
6575 struct glsl_src_param src0_param;
6577 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6579 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6580 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6581 "%s.yz", src0_param.reg_name);
6582 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6585 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6586 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6587 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6589 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6590 struct glsl_sample_function sample_function;
6591 struct glsl_src_param src0_param;
6593 /* Dependent read, not valid with conditional NP2 */
6594 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6595 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6597 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6598 "%s", src0_param.param_str);
6599 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6602 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6603 * If any of the first 3 components are < 0, discard this pixel */
6604 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6606 if (ins->ctx->reg_maps->shader_version.major >= 4)
6608 struct glsl_src_param src_param;
6610 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
6611 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
6613 else
6615 struct glsl_dst_param dst_param;
6617 /* The argument is a destination parameter, and no writemasks are allowed */
6618 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6620 /* 2.0 shaders compare all 4 components in texkill. */
6621 if (ins->ctx->reg_maps->shader_version.major >= 2)
6622 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6623 /* 1.x shaders only compare the first 3 components, probably due to
6624 * the nature of the texkill instruction as a tex* instruction, and
6625 * phase, which kills all .w components. Even if all 4 components are
6626 * defined, only the first 3 are used. */
6627 else
6628 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6632 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6633 * dst = dot2(src0, src1) + src2 */
6634 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6636 struct glsl_src_param src0_param;
6637 struct glsl_src_param src1_param;
6638 struct glsl_src_param src2_param;
6639 DWORD write_mask;
6640 unsigned int mask_size;
6642 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6643 mask_size = shader_glsl_get_write_mask_size(write_mask);
6645 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6646 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6647 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6649 if (mask_size > 1) {
6650 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6651 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6652 } else {
6653 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6654 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6658 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6659 const struct wined3d_shader_signature *input_signature,
6660 const struct wined3d_shader_reg_maps *reg_maps,
6661 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6663 unsigned int i;
6665 for (i = 0; i < input_signature->element_count; ++i)
6667 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6668 const char *semantic_name;
6669 UINT semantic_idx;
6670 char reg_mask[6];
6672 /* Unused */
6673 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6674 continue;
6676 semantic_name = input->semantic_name;
6677 semantic_idx = input->semantic_idx;
6678 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6680 if (args->vp_mode == vertexshader)
6682 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6684 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6685 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6687 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6689 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6691 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6693 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6694 input->register_idx, reg_mask);
6696 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6698 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6699 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6700 input->register_idx, reg_mask);
6701 else
6702 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6704 else
6706 if (input->sysval_semantic)
6707 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6708 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6709 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6710 shader_glsl_shader_input_name(gl_info),
6711 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6714 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6716 if (args->pointsprite)
6717 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6718 shader->u.ps.input_reg_map[input->register_idx]);
6719 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6720 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6721 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6722 needs_legacy_glsl_syntax(gl_info)
6723 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6724 else
6725 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6726 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6728 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6730 if (!semantic_idx)
6731 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6732 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6733 else if (semantic_idx == 1)
6734 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6735 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6736 else
6737 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6738 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6740 else
6742 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6743 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6748 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6750 struct glsl_program_key key;
6752 key.vs_id = entry->vs.id;
6753 key.hs_id = entry->hs.id;
6754 key.ds_id = entry->ds.id;
6755 key.gs_id = entry->gs.id;
6756 key.ps_id = entry->ps.id;
6757 key.cs_id = entry->cs.id;
6759 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6761 ERR("Failed to insert program entry.\n");
6765 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6766 const struct glsl_program_key *key)
6768 struct wine_rb_entry *entry;
6770 entry = wine_rb_get(&priv->program_lookup, key);
6771 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6774 /* Context activation is done by the caller. */
6775 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6776 struct glsl_shader_prog_link *entry)
6778 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6780 GL_EXTCALL(glDeleteProgram(entry->id));
6781 if (entry->vs.id)
6782 list_remove(&entry->vs.shader_entry);
6783 if (entry->hs.id)
6784 list_remove(&entry->hs.shader_entry);
6785 if (entry->ds.id)
6786 list_remove(&entry->ds.shader_entry);
6787 if (entry->gs.id)
6788 list_remove(&entry->gs.shader_entry);
6789 if (entry->ps.id)
6790 list_remove(&entry->ps.shader_entry);
6791 if (entry->cs.id)
6792 list_remove(&entry->cs.shader_entry);
6793 HeapFree(GetProcessHeap(), 0, entry);
6796 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6797 const struct wined3d_gl_info *gl_info, const DWORD *map,
6798 const struct wined3d_shader_signature *input_signature,
6799 const struct wined3d_shader_reg_maps *reg_maps_in,
6800 const struct wined3d_shader_signature *output_signature,
6801 const struct wined3d_shader_reg_maps *reg_maps_out)
6803 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6804 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6805 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6806 unsigned int in_count = vec4_varyings(3, gl_info);
6807 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6808 DWORD in_idx, *set = NULL;
6809 unsigned int i, j;
6810 char reg_mask[6];
6812 set = wined3d_calloc(max_varyings, sizeof(*set));
6814 for (i = 0; i < input_signature->element_count; ++i)
6816 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6818 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6819 continue;
6821 in_idx = map[input->register_idx];
6822 /* Declared, but not read register */
6823 if (in_idx == ~0u)
6824 continue;
6825 if (in_idx >= max_varyings)
6827 FIXME("More input varyings declared than supported, expect issues.\n");
6828 continue;
6831 if (in_idx == in_count)
6832 string_buffer_sprintf(destination, "gl_FrontColor");
6833 else if (in_idx == in_count + 1)
6834 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6835 else
6836 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6838 if (!set[in_idx])
6839 set[in_idx] = ~0u;
6841 for (j = 0; j < output_signature->element_count; ++j)
6843 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6844 DWORD mask;
6846 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6847 || input->semantic_idx != output->semantic_idx
6848 || strcmp(input->semantic_name, output->semantic_name)
6849 || !(mask = input->mask & output->mask))
6850 continue;
6852 if (set[in_idx] == ~0u)
6853 set[in_idx] = 0;
6854 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6855 shader_glsl_write_mask_to_str(mask, reg_mask);
6857 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6858 destination->buffer, reg_mask, output->register_idx, reg_mask);
6862 for (i = 0; i < max_varyings; ++i)
6864 unsigned int size;
6866 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6867 continue;
6869 if (set[i] == ~0u)
6870 set[i] = 0;
6872 size = 0;
6873 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6874 reg_mask[size++] = 'x';
6875 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6876 reg_mask[size++] = 'y';
6877 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6878 reg_mask[size++] = 'z';
6879 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6880 reg_mask[size++] = 'w';
6881 reg_mask[size] = '\0';
6883 if (i == in_count)
6884 string_buffer_sprintf(destination, "gl_FrontColor");
6885 else if (i == in_count + 1)
6886 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6887 else
6888 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6890 if (size == 1)
6891 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6892 else
6893 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6896 HeapFree(GetProcessHeap(), 0, set);
6897 string_buffer_release(&priv->string_buffers, destination);
6900 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6901 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6902 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6903 BOOL rasterizer_setup)
6905 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6906 char reg_mask[6];
6907 unsigned int i;
6909 for (i = 0; i < output_signature->element_count; ++i)
6911 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6913 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6914 continue;
6916 if (output->stream_idx)
6917 continue;
6919 if (output->register_idx >= input_count)
6920 continue;
6922 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6924 shader_addline(buffer,
6925 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
6926 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
6930 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6931 const struct wined3d_gl_info *gl_info, const DWORD *map,
6932 const struct wined3d_shader_signature *input_signature,
6933 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6934 const struct wined3d_shader_signature *output_signature,
6935 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6937 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6938 const char *semantic_name;
6939 UINT semantic_idx;
6940 char reg_mask[6];
6941 unsigned int i;
6943 /* First, sort out position and point size system values. */
6944 for (i = 0; i < output_signature->element_count; ++i)
6946 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6948 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6949 continue;
6951 if (output->stream_idx)
6952 continue;
6954 semantic_name = output->semantic_name;
6955 semantic_idx = output->semantic_idx;
6956 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6958 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6960 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
6961 reg_mask, output->register_idx, reg_mask);
6963 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6965 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
6966 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6968 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6970 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
6971 output->register_idx, reg_mask);
6973 else if (output->sysval_semantic)
6975 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6979 /* Then, setup the pixel shader input. */
6980 if (reg_maps_out->shader_version.major < 4)
6981 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6982 output_signature, reg_maps_out);
6983 else
6984 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
6987 /* Context activation is done by the caller. */
6988 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6989 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6990 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6992 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
6993 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6994 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6995 const char *semantic_name;
6996 UINT semantic_idx;
6997 char reg_mask[6];
6998 unsigned int i;
6999 GLuint ret;
7001 string_buffer_clear(buffer);
7003 shader_glsl_add_version_declaration(buffer, gl_info);
7005 if (per_vertex_point_size)
7007 shader_addline(buffer, "uniform struct\n{\n");
7008 shader_addline(buffer, " float size_min;\n");
7009 shader_addline(buffer, " float size_max;\n");
7010 shader_addline(buffer, "} ffp_point;\n");
7013 if (ps_major < 3)
7015 DWORD colors_written_mask[2] = {0};
7016 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7018 if (!legacy_syntax)
7020 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7021 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7022 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7023 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7026 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7028 for (i = 0; i < vs->output_signature.element_count; ++i)
7030 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7031 DWORD write_mask;
7033 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7034 continue;
7036 semantic_name = output->semantic_name;
7037 semantic_idx = output->semantic_idx;
7038 write_mask = output->mask;
7039 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7041 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7043 if (legacy_syntax)
7044 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7045 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7046 else
7047 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7048 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7050 colors_written_mask[semantic_idx] = write_mask;
7052 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7054 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7055 reg_mask, output->register_idx, reg_mask);
7057 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7059 if (semantic_idx < MAX_TEXTURES)
7061 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7062 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7063 semantic_idx, reg_mask, output->register_idx, reg_mask);
7064 texcoords_written_mask[semantic_idx] = write_mask;
7067 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7069 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7070 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7072 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7074 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7075 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7076 output->register_idx, reg_mask[1]);
7080 for (i = 0; i < 2; ++i)
7082 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7084 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7085 if (!i)
7086 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7087 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7088 reg_mask, reg_mask);
7089 else
7090 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7091 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7092 reg_mask, reg_mask);
7095 for (i = 0; i < MAX_TEXTURES; ++i)
7097 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7098 continue;
7100 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7102 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7103 && !texcoords_written_mask[i])
7104 continue;
7106 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7107 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7108 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7112 else
7114 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7116 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7117 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7118 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7119 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7122 shader_addline(buffer, "}\n");
7124 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7125 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7126 shader_glsl_compile(gl_info, ret, buffer->buffer);
7128 return ret;
7131 static void shader_glsl_generate_stream_output_setup(struct shader_glsl_priv *priv,
7132 const struct wined3d_shader *shader, const struct wined3d_stream_output_desc *so_desc)
7134 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7135 unsigned int i;
7137 shader_addline(buffer, "out shader_in_out\n{\n");
7138 for (i = 0; i < so_desc->element_count; ++i)
7140 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7142 if (e->stream_idx)
7144 FIXME("Unhandled stream %u.\n", e->stream_idx);
7145 continue;
7147 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7148 continue;
7150 if (e->component_idx || e->component_count != 4)
7152 if (e->component_count == 1)
7153 shader_addline(buffer, "float");
7154 else
7155 shader_addline(buffer, "vec%u", e->component_count);
7156 shader_addline(buffer, " reg%u_%u_%u;\n",
7157 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
7159 else
7161 shader_addline(buffer, "vec4 reg%u;\n", e->register_idx);
7164 shader_addline(buffer, "} shader_out;\n");
7166 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7167 shader->limits->packed_output);
7168 for (i = 0; i < so_desc->element_count; ++i)
7170 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7172 if (e->stream_idx)
7174 FIXME("Unhandled stream %u.\n", e->stream_idx);
7175 continue;
7177 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7178 continue;
7180 if (e->component_idx || e->component_count != 4)
7182 DWORD write_mask;
7183 char str_mask[6];
7185 write_mask = ((1u << e->component_count) - 1) << e->component_idx;
7186 shader_glsl_write_mask_to_str(write_mask, str_mask);
7187 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7188 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1,
7189 e->register_idx, str_mask);
7191 else
7193 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n",
7194 e->register_idx, e->register_idx);
7197 shader_addline(buffer, "}\n");
7200 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7201 const struct wined3d_shader *shader, unsigned int input_count,
7202 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7204 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7205 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7207 if (rasterizer_setup)
7208 input_count = min(vec4_varyings(4, gl_info), input_count);
7210 if (input_count)
7211 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7213 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7214 prefix, shader->limits->packed_output);
7216 if (rasterizer_setup)
7217 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7218 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7219 else
7220 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7221 &shader->reg_maps, "shader_out", rasterizer_setup);
7223 shader_addline(buffer, "}\n");
7226 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7227 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7228 const char *reg_mask)
7230 if (!constant->sysval_semantic)
7232 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7233 return;
7236 switch (constant->sysval_semantic)
7238 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7239 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7240 case WINED3D_SV_TESS_FACTOR_LINEDET:
7241 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7242 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7243 break;
7244 case WINED3D_SV_TESS_FACTOR_QUADINT:
7245 case WINED3D_SV_TESS_FACTOR_TRIINT:
7246 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7247 break;
7248 default:
7249 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7250 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7254 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7255 const struct wined3d_shader_signature *signature, BOOL input_setup)
7257 unsigned int i, register_count, user_constant_index, user_constant_count;
7259 register_count = user_constant_count = 0;
7260 for (i = 0; i < signature->element_count; ++i)
7262 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7263 register_count = max(constant->register_idx + 1, register_count);
7264 if (!constant->sysval_semantic)
7265 ++user_constant_count;
7268 if (user_constant_count)
7269 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7270 input_setup ? "in" : "out", user_constant_count);
7271 if (input_setup)
7272 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7274 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7275 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7277 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7278 char reg_mask[6];
7280 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7282 if (input_setup)
7283 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7284 else
7285 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7287 shader_addline(buffer, " = ");
7289 if (input_setup)
7290 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7291 else
7292 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7294 shader_addline(buffer, ";\n");
7296 shader_addline(buffer, "}\n");
7299 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7300 const struct wined3d_gl_info *gl_info)
7302 const char *output = get_fragment_output(gl_info);
7304 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7305 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7306 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7307 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7308 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7309 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7312 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7313 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7315 const char *output = get_fragment_output(gl_info);
7317 switch (mode)
7319 case WINED3D_FFP_PS_FOG_OFF:
7320 return;
7322 case WINED3D_FFP_PS_FOG_LINEAR:
7323 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7324 break;
7326 case WINED3D_FFP_PS_FOG_EXP:
7327 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7328 break;
7330 case WINED3D_FFP_PS_FOG_EXP2:
7331 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7332 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7333 break;
7335 default:
7336 ERR("Invalid fog mode %#x.\n", mode);
7337 return;
7340 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7341 output, output);
7344 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7345 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7347 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7348 * flipping all the operators here, just negate the comparison below. */
7349 static const char * const comparison_operator[] =
7351 "", /* WINED3D_CMP_NEVER */
7352 "<", /* WINED3D_CMP_LESS */
7353 "==", /* WINED3D_CMP_EQUAL */
7354 "<=", /* WINED3D_CMP_LESSEQUAL */
7355 ">", /* WINED3D_CMP_GREATER */
7356 "!=", /* WINED3D_CMP_NOTEQUAL */
7357 ">=", /* WINED3D_CMP_GREATEREQUAL */
7358 "" /* WINED3D_CMP_ALWAYS */
7361 if (alpha_func == WINED3D_CMP_ALWAYS)
7362 return;
7364 if (alpha_func != WINED3D_CMP_NEVER)
7365 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7366 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7367 shader_addline(buffer, " discard;\n");
7370 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7371 const struct wined3d_gl_info *gl_info)
7373 if (gl_info->supported[ARB_GPU_SHADER5])
7374 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7375 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7376 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7377 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7378 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7379 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7380 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7381 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7382 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7383 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7384 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7385 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7386 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7387 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7388 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7389 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7390 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7391 if (gl_info->supported[ARB_TEXTURE_GATHER])
7392 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7393 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7394 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7395 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7396 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7397 if (gl_info->supported[EXT_GPU_SHADER4])
7398 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7399 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7400 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7403 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7404 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7405 const struct ps_compile_args *args)
7407 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7409 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7410 if (reg_maps->shader_version.major < 2)
7411 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7413 if (args->srgb_correction)
7414 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7416 /* SM < 3 does not replace the fog stage. */
7417 if (reg_maps->shader_version.major < 3)
7418 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7420 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7423 /* Context activation is done by the caller. */
7424 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7425 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7426 const struct wined3d_shader *shader,
7427 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7429 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7430 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7431 const char *prefix = shader_glsl_get_prefix(version->type);
7432 const struct wined3d_gl_info *gl_info = context->gl_info;
7433 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7434 unsigned int i, extra_constants_needed = 0;
7435 struct shader_glsl_ctx_priv priv_ctx;
7436 GLuint shader_id;
7437 DWORD map;
7439 memset(&priv_ctx, 0, sizeof(priv_ctx));
7440 priv_ctx.cur_ps_args = args;
7441 priv_ctx.cur_np2fixup_info = np2fixup_info;
7442 priv_ctx.string_buffers = string_buffers;
7444 shader_glsl_add_version_declaration(buffer, gl_info);
7446 shader_glsl_enable_extensions(buffer, gl_info);
7447 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7448 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7449 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7450 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7451 if (shader_glsl_use_explicit_attrib_location(gl_info))
7452 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7453 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7454 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7455 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7456 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7457 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7458 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7459 /* The spec says that it doesn't have to be explicitly enabled, but the
7460 * nvidia drivers write a warning if we don't do so. */
7461 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7462 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7464 /* Base Declarations */
7465 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7467 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7469 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7470 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7471 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7472 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7475 /* Declare uniforms for NP2 texcoord fixup:
7476 * This is NOT done inside the loop that declares the texture samplers
7477 * since the NP2 fixup code is currently only used for the GeforceFX
7478 * series and when forcing the ARB_npot extension off. Modern cards just
7479 * skip the code anyway, so put it inside a separate loop. */
7480 if (args->np2_fixup)
7482 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7483 unsigned int cur = 0;
7485 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7486 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7487 * samplerNP2Fixup stores texture dimensions and is updated through
7488 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7490 for (i = 0; i < shader->limits->sampler; ++i)
7492 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7493 continue;
7495 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7497 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7498 continue;
7501 fixup->idx[i] = cur++;
7504 fixup->num_consts = (cur + 1) >> 1;
7505 fixup->active = args->np2_fixup;
7506 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7509 if (version->major < 3 || args->vp_mode != vertexshader)
7511 shader_addline(buffer, "uniform struct\n{\n");
7512 shader_addline(buffer, " vec4 color;\n");
7513 shader_addline(buffer, " float density;\n");
7514 shader_addline(buffer, " float end;\n");
7515 shader_addline(buffer, " float scale;\n");
7516 shader_addline(buffer, "} ffp_fog;\n");
7518 if (needs_legacy_glsl_syntax(gl_info))
7520 if (glsl_is_color_reg_read(shader, 0))
7521 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7522 if (glsl_is_color_reg_read(shader, 1))
7523 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7524 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7525 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7527 else
7529 if (glsl_is_color_reg_read(shader, 0))
7530 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7531 if (glsl_is_color_reg_read(shader, 1))
7532 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7533 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7534 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7535 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7539 if (version->major >= 3)
7541 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7543 if (args->vp_mode == vertexshader && reg_maps->input_registers)
7544 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7545 shader->u.ps.interpolation_mode, version->major >= 4);
7546 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7549 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7551 if (!(map & 1))
7552 continue;
7554 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7556 if (reg_maps->luminanceparams & (1u << i))
7558 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7559 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7560 extra_constants_needed++;
7563 extra_constants_needed++;
7566 if (args->srgb_correction)
7568 shader_addline(buffer, "const vec4 srgb_const0 = ");
7569 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7570 shader_addline(buffer, ";\n");
7571 shader_addline(buffer, "const vec4 srgb_const1 = ");
7572 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7573 shader_addline(buffer, ";\n");
7575 if (reg_maps->vpos || reg_maps->usesdsy)
7577 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7579 ++extra_constants_needed;
7580 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7582 if (reg_maps->vpos)
7584 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7586 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7587 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7588 args->render_offscreen ? "" : "origin_upper_left, ");
7589 else if (!args->render_offscreen)
7590 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7592 shader_addline(buffer, "vec4 vpos;\n");
7596 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7597 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7599 if (!needs_legacy_glsl_syntax(gl_info))
7601 if (shader_glsl_use_explicit_attrib_location(gl_info))
7602 shader_addline(buffer, "layout(location = 0) ");
7603 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
7606 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7607 FIXME("Insufficient uniforms to run this shader.\n");
7609 if (shader->u.ps.force_early_depth_stencil)
7610 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7612 shader_addline(buffer, "void main()\n{\n");
7614 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7615 * add approximately 0.5. This causes off-by-one problems as spotted by
7616 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7617 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7618 * causes precision troubles when we just subtract 0.5.
7620 * To deal with that, just floor() the position. This will eliminate the
7621 * fraction on all cards.
7623 * TODO: Test how this behaves with multisampling.
7625 * An advantage of floor is that it works even if the driver doesn't add
7626 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7627 * to return in gl_FragCoord, even though coordinates specify the pixel
7628 * centers instead of the pixel corners. This code will behave correctly
7629 * on drivers that returns integer values. */
7630 if (reg_maps->vpos)
7632 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7633 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7634 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7635 shader_addline(buffer,
7636 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7637 else
7638 shader_addline(buffer,
7639 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7642 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
7644 unsigned int i;
7645 WORD map = reg_maps->texcoord;
7647 if (legacy_syntax)
7649 if (glsl_is_color_reg_read(shader, 0))
7650 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7651 if (glsl_is_color_reg_read(shader, 1))
7652 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7655 for (i = 0; map; map >>= 1, ++i)
7657 if (map & 1)
7659 if (args->pointsprite)
7660 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7661 else if (args->texcoords_initialized & (1u << i))
7662 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7663 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7664 else
7665 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7666 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7670 if (legacy_syntax)
7671 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7674 /* Pack 3.0 inputs */
7675 if (reg_maps->shader_version.major >= 3)
7676 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7677 reg_maps->shader_version.major >= 4);
7679 /* Base Shader Body */
7680 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7681 return 0;
7683 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7684 if (reg_maps->shader_version.major < 4)
7685 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
7687 shader_addline(buffer, "}\n");
7689 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7690 TRACE("Compiling shader object %u.\n", shader_id);
7691 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7693 return shader_id;
7696 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7697 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7698 const struct vs_compile_args *args)
7700 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7701 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7702 unsigned int i;
7704 /* Unpack outputs. */
7705 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7707 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7708 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7709 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7710 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7712 if (reg_maps->shader_version.major < 3)
7714 if (args->fog_src == VS_FOG_Z)
7715 shader_addline(buffer, "%s = gl_Position.z;\n",
7716 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7717 else if (!reg_maps->fog)
7718 shader_addline(buffer, "%s = 0.0;\n",
7719 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7722 /* We always store the clipplanes without y inversion. */
7723 if (args->clip_enabled)
7725 if (legacy_syntax)
7726 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7727 else
7728 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7729 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7732 if (args->point_size && !args->per_vertex_point_size)
7733 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7735 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7736 shader_glsl_fixup_position(buffer);
7739 /* Context activation is done by the caller. */
7740 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
7741 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7743 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7744 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7745 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7746 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7747 const struct wined3d_gl_info *gl_info = context->gl_info;
7748 struct shader_glsl_ctx_priv priv_ctx;
7749 GLuint shader_id;
7750 unsigned int i;
7752 memset(&priv_ctx, 0, sizeof(priv_ctx));
7753 priv_ctx.cur_vs_args = args;
7754 priv_ctx.string_buffers = string_buffers;
7756 shader_glsl_add_version_declaration(buffer, gl_info);
7758 shader_glsl_enable_extensions(buffer, gl_info);
7759 if (gl_info->supported[ARB_DRAW_INSTANCED])
7760 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7761 if (shader_glsl_use_explicit_attrib_location(gl_info))
7762 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7764 /* Base Declarations */
7765 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7767 for (i = 0; i < shader->input_signature.element_count; ++i)
7768 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7770 if (args->point_size && !args->per_vertex_point_size)
7772 shader_addline(buffer, "uniform struct\n{\n");
7773 shader_addline(buffer, " float size;\n");
7774 shader_addline(buffer, " float size_min;\n");
7775 shader_addline(buffer, " float size_max;\n");
7776 shader_addline(buffer, "} ffp_point;\n");
7779 if (!needs_legacy_glsl_syntax(gl_info))
7781 if (args->clip_enabled)
7782 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7784 if (version->major < 3)
7786 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7787 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7788 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7789 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7793 if (version->major < 4)
7794 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
7796 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7797 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7799 if (reg_maps->shader_version.major >= 4)
7800 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
7801 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
7803 shader_addline(buffer, "void main()\n{\n");
7805 /* Base Shader Body */
7806 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7807 return 0;
7809 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7810 if (reg_maps->shader_version.major < 4)
7811 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7813 shader_addline(buffer, "}\n");
7815 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7816 TRACE("Compiling shader object %u.\n", shader_id);
7817 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7819 return shader_id;
7822 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
7823 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
7825 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7826 char reg_mask[6];
7827 unsigned int i;
7829 for (i = 0; i < output_signature->element_count; ++i)
7831 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7833 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7834 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
7835 output->register_idx, reg_mask, output->register_idx, reg_mask);
7839 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
7840 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
7841 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
7842 const char *phase_name, unsigned phase_idx)
7844 unsigned int i;
7845 HRESULT hr;
7847 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
7848 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
7849 for (i = 0; i < phase->temporary_count; ++i)
7850 shader_addline(buffer, "vec4 R%u;\n", i);
7851 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
7852 shader_addline(buffer, "}\n");
7853 return hr;
7856 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
7857 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
7859 if (phase->instance_count)
7861 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
7862 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
7863 shader_addline(buffer, "}\n");
7865 else
7867 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
7871 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
7872 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
7874 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7875 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7876 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7877 const struct wined3d_gl_info *gl_info = context->gl_info;
7878 const struct wined3d_hull_shader *hs = &shader->u.hs;
7879 const struct wined3d_shader_phase *phase;
7880 struct shader_glsl_ctx_priv priv_ctx;
7881 GLuint shader_id;
7882 unsigned int i;
7884 memset(&priv_ctx, 0, sizeof(priv_ctx));
7885 priv_ctx.string_buffers = string_buffers;
7887 shader_glsl_add_version_declaration(buffer, gl_info);
7889 shader_glsl_enable_extensions(buffer, gl_info);
7890 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7892 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7894 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
7896 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7897 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
7899 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
7901 if (hs->phases.control_point)
7903 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
7904 shader->limits->packed_output);
7905 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
7906 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
7907 shader_addline(buffer, "}\n");
7910 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
7911 if ((phase = hs->phases.control_point))
7913 for (i = 0; i < phase->temporary_count; ++i)
7914 shader_addline(buffer, "vec4 R%u;\n", i);
7915 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
7916 return 0;
7917 shader_addline(buffer, "setup_hs_output(hs_out);\n");
7919 else
7921 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
7923 shader_addline(buffer, "}\n");
7925 for (i = 0; i < hs->phases.fork_count; ++i)
7927 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7928 &hs->phases.fork[i], "fork", i)))
7929 return 0;
7932 for (i = 0; i < hs->phases.join_count; ++i)
7934 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7935 &hs->phases.join[i], "join", i)))
7936 return 0;
7939 shader_addline(buffer, "void main()\n{\n");
7940 shader_addline(buffer, "hs_control_point_phase();\n");
7941 if (reg_maps->vocp)
7942 shader_addline(buffer, "barrier();\n");
7943 for (i = 0; i < hs->phases.fork_count; ++i)
7944 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
7945 for (i = 0; i < hs->phases.join_count; ++i)
7946 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
7947 shader_addline(buffer, "setup_patch_constant_output();\n");
7948 shader_addline(buffer, "}\n");
7950 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
7951 TRACE("Compiling shader object %u.\n", shader_id);
7952 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7954 return shader_id;
7957 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
7958 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7959 const struct ds_compile_args *args)
7961 shader_addline(buffer, "setup_ds_output(ds_out);\n");
7963 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7964 shader_glsl_fixup_position(buffer);
7967 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
7968 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
7970 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7971 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7972 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7973 const struct wined3d_gl_info *gl_info = context->gl_info;
7974 struct shader_glsl_ctx_priv priv_ctx;
7975 GLuint shader_id;
7977 memset(&priv_ctx, 0, sizeof(priv_ctx));
7978 priv_ctx.cur_ds_args = args;
7979 priv_ctx.string_buffers = string_buffers;
7981 shader_glsl_add_version_declaration(buffer, gl_info);
7983 shader_glsl_enable_extensions(buffer, gl_info);
7984 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7986 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7988 shader_addline(buffer, "layout(");
7989 switch (shader->u.ds.tessellator_domain)
7991 case WINED3D_TESSELLATOR_DOMAIN_LINE:
7992 shader_addline(buffer, "isolines");
7993 break;
7994 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
7995 shader_addline(buffer, "quads");
7996 break;
7997 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
7998 shader_addline(buffer, "triangles");
7999 break;
8001 switch (args->tessellator_output_primitive)
8003 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8004 if (args->render_offscreen)
8005 shader_addline(buffer, ", ccw");
8006 else
8007 shader_addline(buffer, ", cw");
8008 break;
8009 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8010 if (args->render_offscreen)
8011 shader_addline(buffer, ", cw");
8012 else
8013 shader_addline(buffer, ", ccw");
8014 break;
8015 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8016 shader_addline(buffer, ", point_mode");
8017 break;
8018 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8019 break;
8021 switch (args->tessellator_partitioning)
8023 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8024 shader_addline(buffer, ", fractional_odd_spacing");
8025 break;
8026 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8027 shader_addline(buffer, ", fractional_even_spacing");
8028 break;
8029 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8030 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8031 shader_addline(buffer, ", equal_spacing");
8032 break;
8034 shader_addline(buffer, ") in;\n");
8036 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8038 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8039 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8041 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8042 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8043 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8045 shader_addline(buffer, "void main()\n{\n");
8046 shader_addline(buffer, "setup_patch_constant_input();\n");
8048 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8049 return 0;
8051 shader_addline(buffer, "}\n");
8053 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8054 TRACE("Compiling shader object %u.\n", shader_id);
8055 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8057 return shader_id;
8060 /* Context activation is done by the caller. */
8061 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
8062 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8064 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8065 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
8066 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8067 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8068 const struct wined3d_gl_info *gl_info = context->gl_info;
8069 struct shader_glsl_ctx_priv priv_ctx;
8070 GLuint shader_id;
8072 memset(&priv_ctx, 0, sizeof(priv_ctx));
8073 priv_ctx.string_buffers = string_buffers;
8075 shader_glsl_add_version_declaration(buffer, gl_info);
8077 shader_glsl_enable_extensions(buffer, gl_info);
8079 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8081 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
8082 if (shader->u.gs.instance_count > 1)
8083 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8084 shader_addline(buffer, ") in;\n");
8085 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8086 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
8087 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8089 if (!gl_info->supported[ARB_CLIP_CONTROL])
8090 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8092 if (so_desc->rasterizer_stream_idx == WINED3D_NO_RASTERIZER_STREAM)
8094 shader_glsl_generate_stream_output_setup(priv, shader, so_desc);
8096 else
8098 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8099 gl_info, TRUE, args->interpolation_mode);
8101 shader_addline(buffer, "void main()\n{\n");
8102 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8103 return 0;
8104 shader_addline(buffer, "}\n");
8106 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8107 TRACE("Compiling shader object %u.\n", shader_id);
8108 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8110 return shader_id;
8113 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8115 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8116 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8117 struct wined3d_string_buffer *buffer = ctx->buffer;
8118 const struct wined3d_shader *shader = ctx->shader;
8120 switch (shader->reg_maps.shader_version.type)
8122 case WINED3D_SHADER_TYPE_PIXEL:
8123 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args);
8124 break;
8125 case WINED3D_SHADER_TYPE_VERTEX:
8126 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8127 break;
8128 case WINED3D_SHADER_TYPE_DOMAIN:
8129 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8130 break;
8131 case WINED3D_SHADER_TYPE_GEOMETRY:
8132 case WINED3D_SHADER_TYPE_COMPUTE:
8133 break;
8134 default:
8135 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8136 break;
8140 /* Context activation is done by the caller. */
8141 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8142 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8143 const struct wined3d_shader *shader)
8145 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8146 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8147 const struct wined3d_gl_info *gl_info = context->gl_info;
8148 struct shader_glsl_ctx_priv priv_ctx;
8149 GLuint shader_id;
8150 unsigned int i;
8152 memset(&priv_ctx, 0, sizeof(priv_ctx));
8153 priv_ctx.string_buffers = string_buffers;
8155 shader_glsl_add_version_declaration(buffer, gl_info);
8157 shader_glsl_enable_extensions(buffer, gl_info);
8158 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8160 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8162 for (i = 0; i < reg_maps->tgsm_count; ++i)
8164 if (reg_maps->tgsm[i].size)
8165 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8168 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8169 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8171 shader_addline(buffer, "void main()\n{\n");
8172 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8173 shader_addline(buffer, "}\n");
8175 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8176 TRACE("Compiling shader object %u.\n", shader_id);
8177 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8179 return shader_id;
8182 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8183 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8184 struct wined3d_shader *shader,
8185 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8187 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8188 struct glsl_shader_private *shader_data;
8189 struct ps_np2fixup_info *np2fixup;
8190 UINT i;
8191 DWORD new_size;
8192 GLuint ret;
8194 if (!shader->backend_data)
8196 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8197 if (!shader->backend_data)
8199 ERR("Failed to allocate backend data.\n");
8200 return 0;
8203 shader_data = shader->backend_data;
8204 gl_shaders = shader_data->gl_shaders.ps;
8206 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8207 * so a linear search is more performant than a hashmap or a binary search
8208 * (cache coherency etc)
8210 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8212 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8214 if (args->np2_fixup)
8215 *np2fixup_info = &gl_shaders[i].np2fixup;
8216 return gl_shaders[i].id;
8220 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8221 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8222 if (shader_data->num_gl_shaders)
8224 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8225 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
8226 new_size * sizeof(*gl_shaders));
8228 else
8230 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8231 new_size = 1;
8234 if(!new_array) {
8235 ERR("Out of memory\n");
8236 return 0;
8238 shader_data->gl_shaders.ps = new_array;
8239 shader_data->shader_array_size = new_size;
8240 gl_shaders = new_array;
8243 gl_shaders[shader_data->num_gl_shaders].args = *args;
8245 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8246 memset(np2fixup, 0, sizeof(*np2fixup));
8247 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8249 pixelshader_update_resource_types(shader, args->tex_types);
8251 string_buffer_clear(buffer);
8252 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8253 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8255 return ret;
8258 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8259 const DWORD use_map)
8261 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
8262 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
8263 if (stored->point_size != new->point_size)
8264 return FALSE;
8265 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8266 return FALSE;
8267 if (stored->flatshading != new->flatshading)
8268 return FALSE;
8269 if (stored->next_shader_type != new->next_shader_type)
8270 return FALSE;
8271 if (stored->next_shader_input_count != new->next_shader_input_count)
8272 return FALSE;
8273 return stored->fog_src == new->fog_src;
8276 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8277 struct wined3d_shader *shader, const struct vs_compile_args *args)
8279 UINT i;
8280 DWORD new_size;
8281 DWORD use_map = context->stream_info.use_map;
8282 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8283 struct glsl_shader_private *shader_data;
8284 GLuint ret;
8286 if (!shader->backend_data)
8288 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8289 if (!shader->backend_data)
8291 ERR("Failed to allocate backend data.\n");
8292 return 0;
8295 shader_data = shader->backend_data;
8296 gl_shaders = shader_data->gl_shaders.vs;
8298 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8299 * so a linear search is more performant than a hashmap or a binary search
8300 * (cache coherency etc)
8302 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8304 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8305 return gl_shaders[i].id;
8308 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8310 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8311 if (shader_data->num_gl_shaders)
8313 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8314 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
8315 new_size * sizeof(*gl_shaders));
8317 else
8319 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8320 new_size = 1;
8323 if(!new_array) {
8324 ERR("Out of memory\n");
8325 return 0;
8327 shader_data->gl_shaders.vs = new_array;
8328 shader_data->shader_array_size = new_size;
8329 gl_shaders = new_array;
8332 gl_shaders[shader_data->num_gl_shaders].args = *args;
8334 string_buffer_clear(&priv->shader_buffer);
8335 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8336 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8338 return ret;
8341 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8342 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8344 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8345 struct glsl_shader_private *shader_data;
8346 unsigned int new_size;
8347 GLuint ret;
8349 if (!shader->backend_data)
8351 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8353 ERR("Failed to allocate backend data.\n");
8354 return 0;
8357 shader_data = shader->backend_data;
8358 gl_shaders = shader_data->gl_shaders.hs;
8360 if (shader_data->num_gl_shaders > 0)
8362 assert(shader_data->num_gl_shaders == 1);
8363 return gl_shaders[0].id;
8366 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8368 assert(!shader_data->gl_shaders.hs);
8369 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8370 new_size = 1;
8371 if (!new_array)
8373 ERR("Failed to allocate GL shaders array.\n");
8374 return 0;
8376 shader_data->gl_shaders.hs = new_array;
8377 shader_data->shader_array_size = new_size;
8378 gl_shaders = new_array;
8380 string_buffer_clear(&priv->shader_buffer);
8381 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8382 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8384 return ret;
8387 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8388 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8390 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8391 struct glsl_shader_private *shader_data;
8392 unsigned int i, new_size;
8393 GLuint ret;
8395 if (!shader->backend_data)
8397 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8399 ERR("Failed to allocate backend data.\n");
8400 return 0;
8403 shader_data = shader->backend_data;
8404 gl_shaders = shader_data->gl_shaders.ds;
8406 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8408 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8409 return gl_shaders[i].id;
8412 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8414 if (shader_data->num_gl_shaders)
8416 new_size = shader_data->shader_array_size + 1;
8417 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ds,
8418 new_size * sizeof(*new_array));
8420 else
8422 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8423 new_size = 1;
8426 if (!new_array)
8428 ERR("Failed to allocate GL shaders array.\n");
8429 return 0;
8431 shader_data->gl_shaders.ds = new_array;
8432 shader_data->shader_array_size = new_size;
8433 gl_shaders = new_array;
8435 string_buffer_clear(&priv->shader_buffer);
8436 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8437 gl_shaders[shader_data->num_gl_shaders].args = *args;
8438 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8440 return ret;
8443 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8444 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8446 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8447 struct glsl_shader_private *shader_data;
8448 unsigned int i, new_size;
8449 GLuint ret;
8451 if (!shader->backend_data)
8453 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8455 ERR("Failed to allocate backend data.\n");
8456 return 0;
8459 shader_data = shader->backend_data;
8460 gl_shaders = shader_data->gl_shaders.gs;
8462 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8464 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8465 return gl_shaders[i].id;
8468 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8470 if (shader_data->num_gl_shaders)
8472 new_size = shader_data->shader_array_size + 1;
8473 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
8474 new_size * sizeof(*new_array));
8476 else
8478 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8479 new_size = 1;
8482 if (!new_array)
8484 ERR("Failed to allocate GL shaders array.\n");
8485 return 0;
8487 shader_data->gl_shaders.gs = new_array;
8488 shader_data->shader_array_size = new_size;
8489 gl_shaders = new_array;
8491 string_buffer_clear(&priv->shader_buffer);
8492 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8493 gl_shaders[shader_data->num_gl_shaders].args = *args;
8494 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8496 return ret;
8499 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8501 switch (mcs)
8503 case WINED3D_MCS_MATERIAL:
8504 return material;
8505 case WINED3D_MCS_COLOR1:
8506 return "ffp_attrib_diffuse";
8507 case WINED3D_MCS_COLOR2:
8508 return "ffp_attrib_specular";
8509 default:
8510 ERR("Invalid material color source %#x.\n", mcs);
8511 return "<invalid>";
8515 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8516 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8518 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8519 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8520 if (settings->localviewer)
8521 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8522 else
8523 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8524 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8525 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8528 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8529 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8531 const char *diffuse, *specular, *emissive, *ambient;
8532 unsigned int i, idx;
8534 if (!settings->lighting)
8536 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8537 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8538 return;
8541 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8542 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8543 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8544 shader_addline(buffer, "vec3 dir, dst;\n");
8545 shader_addline(buffer, "float att, t;\n");
8547 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8548 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8549 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8550 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8552 idx = 0;
8553 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8555 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8556 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8557 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8558 shader_addline(buffer, "dst.x = 1.0;\n");
8559 if (legacy_lighting)
8561 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8562 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8563 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8565 else
8567 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8569 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8570 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8571 if (!legacy_lighting)
8572 shader_addline(buffer, "att = 1.0 / att;\n");
8573 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8574 if (!settings->normal)
8576 shader_addline(buffer, "}\n");
8577 continue;
8579 shader_addline(buffer, "dir = normalize(dir);\n");
8580 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8581 shader_addline(buffer, "}\n");
8584 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8586 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8587 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8588 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8589 shader_addline(buffer, "dst.x = 1.0;\n");
8590 if (legacy_lighting)
8592 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8593 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8594 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8596 else
8598 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8600 shader_addline(buffer, "dir = normalize(dir);\n");
8601 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8602 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8603 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8604 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8605 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8606 idx, idx, idx, idx);
8607 if (legacy_lighting)
8608 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8609 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8610 idx, idx, idx);
8611 else
8612 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8613 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8614 idx, idx, idx);
8615 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8616 if (!settings->normal)
8618 shader_addline(buffer, "}\n");
8619 continue;
8621 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8622 shader_addline(buffer, "}\n");
8625 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8627 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8628 if (!settings->normal)
8629 continue;
8630 shader_addline(buffer, "att = 1.0;\n");
8631 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8632 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8635 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8637 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8638 if (!settings->normal)
8639 continue;
8640 shader_addline(buffer, "att = 1.0;\n");
8641 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8642 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8645 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8646 ambient, diffuse, emissive);
8647 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8648 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8651 /* Context activation is done by the caller. */
8652 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8653 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8655 static const struct attrib_info
8657 const char type[6];
8658 const char name[24];
8660 attrib_info[] =
8662 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8663 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8664 /* TODO: Indexed vertex blending */
8665 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8666 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8667 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8668 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8669 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8671 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8672 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8673 BOOL output_legacy_fogcoord = legacy_syntax;
8674 BOOL legacy_lighting = priv->legacy_lighting;
8675 GLuint shader_obj;
8676 unsigned int i;
8678 string_buffer_clear(buffer);
8680 shader_glsl_add_version_declaration(buffer, gl_info);
8682 if (shader_glsl_use_explicit_attrib_location(gl_info))
8683 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8685 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8687 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8689 if (shader_glsl_use_explicit_attrib_location(gl_info))
8690 shader_addline(buffer, "layout(location = %u) ", i);
8691 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8693 shader_addline(buffer, "\n");
8695 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8696 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8697 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8698 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
8700 shader_addline(buffer, "uniform struct\n{\n");
8701 shader_addline(buffer, " vec4 emissive;\n");
8702 shader_addline(buffer, " vec4 ambient;\n");
8703 shader_addline(buffer, " vec4 diffuse;\n");
8704 shader_addline(buffer, " vec4 specular;\n");
8705 shader_addline(buffer, " float shininess;\n");
8706 shader_addline(buffer, "} ffp_material;\n");
8708 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8709 shader_addline(buffer, "uniform struct\n{\n");
8710 shader_addline(buffer, " vec4 diffuse;\n");
8711 shader_addline(buffer, " vec4 specular;\n");
8712 shader_addline(buffer, " vec4 ambient;\n");
8713 shader_addline(buffer, " vec4 position;\n");
8714 shader_addline(buffer, " vec3 direction;\n");
8715 shader_addline(buffer, " float range;\n");
8716 shader_addline(buffer, " float falloff;\n");
8717 shader_addline(buffer, " float c_att;\n");
8718 shader_addline(buffer, " float l_att;\n");
8719 shader_addline(buffer, " float q_att;\n");
8720 shader_addline(buffer, " float cos_htheta;\n");
8721 shader_addline(buffer, " float cos_hphi;\n");
8722 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
8724 if (settings->point_size)
8726 shader_addline(buffer, "uniform struct\n{\n");
8727 shader_addline(buffer, " float size;\n");
8728 shader_addline(buffer, " float size_min;\n");
8729 shader_addline(buffer, " float size_max;\n");
8730 shader_addline(buffer, " float c_att;\n");
8731 shader_addline(buffer, " float l_att;\n");
8732 shader_addline(buffer, " float q_att;\n");
8733 shader_addline(buffer, "} ffp_point;\n");
8736 if (legacy_syntax)
8738 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8739 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8740 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8741 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8743 else
8745 if (settings->clipping)
8746 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8748 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8749 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8750 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8751 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8754 shader_addline(buffer, "\nvoid main()\n{\n");
8755 shader_addline(buffer, "float m;\n");
8756 shader_addline(buffer, "vec3 r;\n");
8758 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
8760 if (attrib_info[i].name[0])
8761 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
8762 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
8764 for (i = 0; i < MAX_TEXTURES; ++i)
8766 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
8767 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
8768 && settings->texcoords & (1u << i))
8769 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
8772 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
8774 if (settings->transformed)
8776 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
8777 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8778 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
8780 else
8782 for (i = 0; i < settings->vertexblends; ++i)
8783 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
8785 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
8786 for (i = 0; i < settings->vertexblends + 1; ++i)
8787 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
8789 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8790 if (settings->clipping)
8792 if (legacy_syntax)
8793 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
8794 else
8795 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8796 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
8798 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
8801 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
8802 if (settings->normal)
8804 if (!settings->vertexblends)
8806 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
8808 else
8810 for (i = 0; i < settings->vertexblends + 1; ++i)
8811 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
8814 if (settings->normalize)
8815 shader_addline(buffer, "normal = normalize(normal);\n");
8818 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
8819 if (legacy_syntax)
8821 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
8822 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
8824 else
8826 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
8827 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
8830 for (i = 0; i < MAX_TEXTURES; ++i)
8832 BOOL output_legacy_texcoord = legacy_syntax;
8834 switch (settings->texgen[i] & 0xffff0000)
8836 case WINED3DTSS_TCI_PASSTHRU:
8837 if (settings->texcoords & (1u << i))
8838 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
8839 i, i, i);
8840 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
8841 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
8842 else
8843 output_legacy_texcoord = FALSE;
8844 break;
8846 case WINED3DTSS_TCI_CAMERASPACENORMAL:
8847 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
8848 break;
8850 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
8851 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
8852 break;
8854 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
8855 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8856 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
8857 break;
8859 case WINED3DTSS_TCI_SPHEREMAP:
8860 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
8861 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
8862 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8863 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
8864 break;
8866 default:
8867 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
8868 break;
8870 if (output_legacy_texcoord)
8871 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
8874 switch (settings->fog_mode)
8876 case WINED3D_FFP_VS_FOG_OFF:
8877 output_legacy_fogcoord = FALSE;
8878 break;
8880 case WINED3D_FFP_VS_FOG_FOGCOORD:
8881 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
8882 break;
8884 case WINED3D_FFP_VS_FOG_RANGE:
8885 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
8886 break;
8888 case WINED3D_FFP_VS_FOG_DEPTH:
8889 if (settings->ortho_fog)
8891 if (gl_info->supported[ARB_CLIP_CONTROL])
8892 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
8893 else
8894 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
8895 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
8897 else if (settings->transformed)
8899 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
8901 else
8903 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
8905 break;
8907 default:
8908 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
8909 break;
8911 if (output_legacy_fogcoord)
8912 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
8914 if (settings->point_size)
8916 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
8917 " + ffp_point.l_att * length(ec_pos.xyz)"
8918 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
8919 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
8920 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
8923 shader_addline(buffer, "}\n");
8925 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8926 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
8928 return shader_obj;
8931 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
8932 DWORD argnum, unsigned int stage, DWORD arg)
8934 const char *ret;
8936 if (arg == ARG_UNUSED)
8937 return "<unused arg>";
8939 switch (arg & WINED3DTA_SELECTMASK)
8941 case WINED3DTA_DIFFUSE:
8942 ret = "ffp_varying_diffuse";
8943 break;
8945 case WINED3DTA_CURRENT:
8946 ret = "ret";
8947 break;
8949 case WINED3DTA_TEXTURE:
8950 switch (stage)
8952 case 0: ret = "tex0"; break;
8953 case 1: ret = "tex1"; break;
8954 case 2: ret = "tex2"; break;
8955 case 3: ret = "tex3"; break;
8956 case 4: ret = "tex4"; break;
8957 case 5: ret = "tex5"; break;
8958 case 6: ret = "tex6"; break;
8959 case 7: ret = "tex7"; break;
8960 default:
8961 ret = "<invalid texture>";
8962 break;
8964 break;
8966 case WINED3DTA_TFACTOR:
8967 ret = "tex_factor";
8968 break;
8970 case WINED3DTA_SPECULAR:
8971 ret = "ffp_varying_specular";
8972 break;
8974 case WINED3DTA_TEMP:
8975 ret = "temp_reg";
8976 break;
8978 case WINED3DTA_CONSTANT:
8979 switch (stage)
8981 case 0: ret = "tss_const0"; break;
8982 case 1: ret = "tss_const1"; break;
8983 case 2: ret = "tss_const2"; break;
8984 case 3: ret = "tss_const3"; break;
8985 case 4: ret = "tss_const4"; break;
8986 case 5: ret = "tss_const5"; break;
8987 case 6: ret = "tss_const6"; break;
8988 case 7: ret = "tss_const7"; break;
8989 default:
8990 ret = "<invalid constant>";
8991 break;
8993 break;
8995 default:
8996 return "<unhandled arg>";
8999 if (arg & WINED3DTA_COMPLEMENT)
9001 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9002 if (argnum == 0)
9003 ret = "arg0";
9004 else if (argnum == 1)
9005 ret = "arg1";
9006 else if (argnum == 2)
9007 ret = "arg2";
9010 if (arg & WINED3DTA_ALPHAREPLICATE)
9012 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9013 if (argnum == 0)
9014 ret = "arg0";
9015 else if (argnum == 1)
9016 ret = "arg1";
9017 else if (argnum == 2)
9018 ret = "arg2";
9021 return ret;
9024 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9025 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9027 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9029 if (color && alpha)
9030 dstmask = "";
9031 else if (color)
9032 dstmask = ".xyz";
9033 else
9034 dstmask = ".w";
9036 if (dst == tempreg)
9037 dstreg = "temp_reg";
9038 else
9039 dstreg = "ret";
9041 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9042 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9043 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9045 switch (op)
9047 case WINED3D_TOP_DISABLE:
9048 break;
9050 case WINED3D_TOP_SELECT_ARG1:
9051 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9052 break;
9054 case WINED3D_TOP_SELECT_ARG2:
9055 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9056 break;
9058 case WINED3D_TOP_MODULATE:
9059 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9060 break;
9062 case WINED3D_TOP_MODULATE_4X:
9063 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9064 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9065 break;
9067 case WINED3D_TOP_MODULATE_2X:
9068 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9069 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9070 break;
9072 case WINED3D_TOP_ADD:
9073 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9074 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9075 break;
9077 case WINED3D_TOP_ADD_SIGNED:
9078 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9079 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9080 break;
9082 case WINED3D_TOP_ADD_SIGNED_2X:
9083 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9084 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9085 break;
9087 case WINED3D_TOP_SUBTRACT:
9088 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9089 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9090 break;
9092 case WINED3D_TOP_ADD_SMOOTH:
9093 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9094 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9095 break;
9097 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9098 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9099 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9100 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9101 break;
9103 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9104 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9105 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9106 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9107 break;
9109 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9110 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9111 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9112 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9113 break;
9115 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9116 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9117 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9118 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9119 break;
9121 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9122 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9123 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9124 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9125 break;
9127 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9128 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9129 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9130 break;
9132 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9133 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9134 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9135 break;
9137 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9138 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9139 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9140 break;
9141 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9142 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9143 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9144 break;
9146 case WINED3D_TOP_BUMPENVMAP:
9147 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9148 /* These are handled in the first pass, nothing to do. */
9149 break;
9151 case WINED3D_TOP_DOTPRODUCT3:
9152 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9153 dstreg, dstmask, arg1, arg2, dstmask);
9154 break;
9156 case WINED3D_TOP_MULTIPLY_ADD:
9157 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9158 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9159 break;
9161 case WINED3D_TOP_LERP:
9162 /* MSDN isn't quite right here. */
9163 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9164 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9165 break;
9167 default:
9168 FIXME("Unhandled operation %#x.\n", op);
9169 break;
9173 /* Context activation is done by the caller. */
9174 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9175 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9177 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9178 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9179 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9180 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9181 const struct wined3d_gl_info *gl_info = context->gl_info;
9182 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9183 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9184 UINT lowest_disabled_stage;
9185 GLuint shader_id;
9186 DWORD arg0, arg1, arg2;
9187 unsigned int stage;
9189 string_buffer_clear(buffer);
9191 /* Find out which textures are read */
9192 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9194 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9195 break;
9197 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9198 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9199 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9201 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9202 || (stage == 0 && settings->color_key_enabled))
9203 tex_map |= 1u << stage;
9204 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9205 tfactor_used = TRUE;
9206 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9207 tempreg_used = TRUE;
9208 if (settings->op[stage].dst == tempreg)
9209 tempreg_used = TRUE;
9210 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9211 tss_const_map |= 1u << stage;
9213 switch (settings->op[stage].cop)
9215 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9216 lum_map |= 1u << stage;
9217 /* fall through */
9218 case WINED3D_TOP_BUMPENVMAP:
9219 bump_map |= 1u << stage;
9220 /* fall through */
9221 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9222 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9223 tex_map |= 1u << stage;
9224 break;
9226 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9227 tfactor_used = TRUE;
9228 break;
9230 default:
9231 break;
9234 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9235 continue;
9237 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9238 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9239 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9241 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9242 tex_map |= 1u << stage;
9243 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9244 tfactor_used = TRUE;
9245 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9246 tempreg_used = TRUE;
9247 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9248 tss_const_map |= 1u << stage;
9250 lowest_disabled_stage = stage;
9252 shader_glsl_add_version_declaration(buffer, gl_info);
9254 if (shader_glsl_use_explicit_attrib_location(gl_info))
9255 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9256 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9257 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9258 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9259 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9261 if (!needs_legacy_glsl_syntax(gl_info))
9263 if (shader_glsl_use_explicit_attrib_location(gl_info))
9264 shader_addline(buffer, "layout(location = 0) ");
9265 shader_addline(buffer, "out vec4 ps_out[1];\n");
9268 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9269 shader_addline(buffer, "vec4 ret;\n");
9270 if (tempreg_used || settings->sRGB_write)
9271 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9272 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9274 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9276 const char *sampler_type;
9278 if (tss_const_map & (1u << stage))
9279 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9281 if (!(tex_map & (1u << stage)))
9282 continue;
9284 switch (settings->op[stage].tex_type)
9286 case WINED3D_GL_RES_TYPE_TEX_1D:
9287 sampler_type = "1D";
9288 break;
9289 case WINED3D_GL_RES_TYPE_TEX_2D:
9290 sampler_type = "2D";
9291 break;
9292 case WINED3D_GL_RES_TYPE_TEX_3D:
9293 sampler_type = "3D";
9294 break;
9295 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9296 sampler_type = "Cube";
9297 break;
9298 case WINED3D_GL_RES_TYPE_TEX_RECT:
9299 sampler_type = "2DRect";
9300 break;
9301 default:
9302 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9303 sampler_type = NULL;
9304 break;
9306 if (sampler_type)
9308 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9309 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9310 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9313 shader_addline(buffer, "vec4 tex%u;\n", stage);
9315 if (!(bump_map & (1u << stage)))
9316 continue;
9317 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9319 if (!(lum_map & (1u << stage)))
9320 continue;
9321 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9322 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9324 if (tfactor_used)
9325 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9326 if (settings->color_key_enabled)
9327 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9328 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9330 if (settings->sRGB_write)
9332 shader_addline(buffer, "const vec4 srgb_const0 = ");
9333 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9334 shader_addline(buffer, ";\n");
9335 shader_addline(buffer, "const vec4 srgb_const1 = ");
9336 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9337 shader_addline(buffer, ";\n");
9340 shader_addline(buffer, "uniform struct\n{\n");
9341 shader_addline(buffer, " vec4 color;\n");
9342 shader_addline(buffer, " float density;\n");
9343 shader_addline(buffer, " float end;\n");
9344 shader_addline(buffer, " float scale;\n");
9345 shader_addline(buffer, "} ffp_fog;\n");
9347 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9348 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9350 if (legacy_syntax)
9352 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9353 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9354 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9355 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9356 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9358 else
9360 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9361 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9362 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9363 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9364 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9367 shader_addline(buffer, "void main()\n{\n");
9369 if (legacy_syntax)
9371 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9372 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9375 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9377 if (tex_map & (1u << stage))
9379 if (settings->pointsprite)
9380 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9381 else if (settings->texcoords_initialized & (1u << stage))
9382 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9383 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9384 else
9385 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9389 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9390 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9392 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9393 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9395 /* Generate texture sampling instructions */
9396 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9398 const char *texture_function, *coord_mask;
9399 BOOL proj;
9401 if (!(tex_map & (1u << stage)))
9402 continue;
9404 if (settings->op[stage].projected == proj_none)
9406 proj = FALSE;
9408 else if (settings->op[stage].projected == proj_count4
9409 || settings->op[stage].projected == proj_count3)
9411 proj = TRUE;
9413 else
9415 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9416 proj = TRUE;
9419 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9420 proj = FALSE;
9422 switch (settings->op[stage].tex_type)
9424 case WINED3D_GL_RES_TYPE_TEX_1D:
9425 if (proj)
9427 texture_function = "texture1DProj";
9428 coord_mask = "xw";
9430 else
9432 texture_function = "texture1D";
9433 coord_mask = "x";
9435 break;
9436 case WINED3D_GL_RES_TYPE_TEX_2D:
9437 if (proj)
9439 texture_function = "texture2DProj";
9440 coord_mask = "xyw";
9442 else
9444 texture_function = "texture2D";
9445 coord_mask = "xy";
9447 break;
9448 case WINED3D_GL_RES_TYPE_TEX_3D:
9449 if (proj)
9451 texture_function = "texture3DProj";
9452 coord_mask = "xyzw";
9454 else
9456 texture_function = "texture3D";
9457 coord_mask = "xyz";
9459 break;
9460 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9461 texture_function = "textureCube";
9462 coord_mask = "xyz";
9463 break;
9464 case WINED3D_GL_RES_TYPE_TEX_RECT:
9465 if (proj)
9467 texture_function = "texture2DRectProj";
9468 coord_mask = "xyw";
9470 else
9472 texture_function = "texture2DRect";
9473 coord_mask = "xy";
9475 break;
9476 default:
9477 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9478 texture_function = "";
9479 coord_mask = "xyzw";
9480 break;
9482 if (!legacy_syntax)
9483 texture_function = proj ? "textureProj" : "texture";
9485 if (stage > 0
9486 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9487 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9489 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9491 /* With projective textures, texbem only divides the static
9492 * texture coord, not the displacement, so multiply the
9493 * displacement with the dividing parameter before passing it to
9494 * TXP. */
9495 if (settings->op[stage].projected != proj_none)
9497 if (settings->op[stage].projected == proj_count4)
9499 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9500 stage, stage);
9501 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9503 else
9505 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9506 stage, stage);
9507 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9510 else
9512 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9515 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9516 stage, texture_function, stage, coord_mask);
9518 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9519 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9520 stage, stage - 1, stage - 1, stage - 1);
9522 else if (settings->op[stage].projected == proj_count3)
9524 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9525 stage, texture_function, stage, stage);
9527 else
9529 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9530 stage, texture_function, stage, stage, coord_mask);
9533 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9534 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9535 settings->op[stage].color_fixup);
9538 if (settings->color_key_enabled)
9540 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9541 shader_addline(buffer, " discard;\n");
9544 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9546 /* Generate the main shader */
9547 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9549 BOOL op_equal;
9551 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9552 break;
9554 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9555 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9556 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9557 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9558 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9559 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9560 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9561 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9562 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9563 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9564 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9565 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9566 else
9567 op_equal = settings->op[stage].aop == settings->op[stage].cop
9568 && settings->op[stage].carg0 == settings->op[stage].aarg0
9569 && settings->op[stage].carg1 == settings->op[stage].aarg1
9570 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9572 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9574 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9575 settings->op[stage].cop, settings->op[stage].carg0,
9576 settings->op[stage].carg1, settings->op[stage].carg2);
9578 else if (op_equal)
9580 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
9581 settings->op[stage].cop, settings->op[stage].carg0,
9582 settings->op[stage].carg1, settings->op[stage].carg2);
9584 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9585 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9587 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9588 settings->op[stage].cop, settings->op[stage].carg0,
9589 settings->op[stage].carg1, settings->op[stage].carg2);
9590 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
9591 settings->op[stage].aop, settings->op[stage].aarg0,
9592 settings->op[stage].aarg1, settings->op[stage].aarg2);
9596 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9597 get_fragment_output(gl_info));
9599 if (settings->sRGB_write)
9600 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9602 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9604 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9606 shader_addline(buffer, "}\n");
9608 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9609 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9611 string_buffer_release(&priv->string_buffers, tex_reg_name);
9612 return shader_id;
9615 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9616 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9618 struct glsl_ffp_vertex_shader *shader;
9619 const struct wine_rb_entry *entry;
9621 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9622 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9624 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
9625 return NULL;
9627 shader->desc.settings = *settings;
9628 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9629 list_init(&shader->linked_programs);
9630 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9631 ERR("Failed to insert ffp vertex shader.\n");
9633 return shader;
9636 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9637 const struct ffp_frag_settings *args, const struct wined3d_context *context)
9639 struct glsl_ffp_fragment_shader *glsl_desc;
9640 const struct ffp_frag_desc *desc;
9642 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9643 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9645 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
9646 return NULL;
9648 glsl_desc->entry.settings = *args;
9649 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
9650 list_init(&glsl_desc->linked_programs);
9651 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9653 return glsl_desc;
9657 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9658 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9660 unsigned int i;
9661 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9663 for (i = 0; i < vs_c_count; ++i)
9665 string_buffer_sprintf(name, "vs_c[%u]", i);
9666 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9668 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9670 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9672 string_buffer_sprintf(name, "vs_i[%u]", i);
9673 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9676 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9678 string_buffer_sprintf(name, "vs_b[%u]", i);
9679 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9682 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9684 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9686 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9687 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9689 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9690 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9691 for (i = 0; i < MAX_TEXTURES; ++i)
9693 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9694 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9696 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9697 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9698 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9699 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9700 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9701 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9702 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
9704 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9705 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9706 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9707 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9708 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9709 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9710 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9711 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9712 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9713 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9714 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9715 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9716 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9717 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9718 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9719 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9720 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9721 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9722 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9723 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9724 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9725 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9726 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9727 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9729 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
9730 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
9731 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
9732 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
9733 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
9734 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
9735 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
9737 string_buffer_release(&priv->string_buffers, name);
9740 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
9741 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
9743 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9746 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
9747 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
9749 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9752 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
9753 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
9755 unsigned int i;
9756 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9758 for (i = 0; i < ps_c_count; ++i)
9760 string_buffer_sprintf(name, "ps_c[%u]", i);
9761 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9763 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
9765 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9767 string_buffer_sprintf(name, "ps_i[%u]", i);
9768 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9771 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9773 string_buffer_sprintf(name, "ps_b[%u]", i);
9774 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9777 for (i = 0; i < MAX_TEXTURES; ++i)
9779 string_buffer_sprintf(name, "bumpenv_mat%u", i);
9780 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9781 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
9782 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9783 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
9784 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9785 string_buffer_sprintf(name, "tss_const%u", i);
9786 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9789 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
9790 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
9792 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
9793 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
9794 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
9795 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
9797 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
9799 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
9800 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
9801 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
9803 string_buffer_release(&priv->string_buffers, name);
9806 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
9807 const struct wined3d_context *context, struct wined3d_shader *shader)
9809 struct glsl_context_data *ctx_data = context->shader_backend_data;
9810 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9811 const struct wined3d_gl_info *gl_info = context->gl_info;
9812 struct glsl_cs_compiled_shader *gl_shaders;
9813 struct glsl_shader_private *shader_data;
9814 struct glsl_shader_prog_link *entry;
9815 GLuint shader_id, program_id;
9817 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
9819 ERR("Out of memory.\n");
9820 return E_OUTOFMEMORY;
9823 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
9825 ERR("Failed to allocate backend data.\n");
9826 HeapFree(GetProcessHeap(), 0, entry);
9827 return E_OUTOFMEMORY;
9829 shader_data = shader->backend_data;
9830 gl_shaders = shader_data->gl_shaders.cs;
9832 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
9834 ERR("Failed to allocate GL shader array.\n");
9835 HeapFree(GetProcessHeap(), 0, entry);
9836 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9837 shader->backend_data = NULL;
9838 return E_OUTOFMEMORY;
9840 shader_data->shader_array_size = 1;
9841 gl_shaders = shader_data->gl_shaders.cs;
9843 TRACE("Compiling compute shader %p.\n", shader);
9845 string_buffer_clear(buffer);
9846 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
9847 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
9849 program_id = GL_EXTCALL(glCreateProgram());
9850 TRACE("Created new GLSL shader program %u.\n", program_id);
9852 entry->id = program_id;
9853 entry->vs.id = 0;
9854 entry->hs.id = 0;
9855 entry->ds.id = 0;
9856 entry->gs.id = 0;
9857 entry->ps.id = 0;
9858 entry->cs.id = shader_id;
9859 entry->constant_version = 0;
9860 entry->ps.np2_fixup_info = NULL;
9861 add_glsl_program_entry(priv, entry);
9863 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
9864 GL_EXTCALL(glAttachShader(program_id, shader_id));
9865 checkGLcall("glAttachShader");
9867 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
9869 TRACE("Linking GLSL shader program %u.\n", program_id);
9870 GL_EXTCALL(glLinkProgram(program_id));
9871 shader_glsl_validate_link(gl_info, program_id);
9873 GL_EXTCALL(glUseProgram(program_id));
9874 checkGLcall("glUseProgram");
9875 shader_glsl_load_program_resources(context, priv, program_id, shader);
9876 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
9878 entry->constant_update_mask = 0;
9880 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
9881 checkGLcall("glUseProgram");
9882 return WINED3D_OK;
9885 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
9886 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
9888 struct glsl_shader_private *shader_data;
9890 if (!shader->backend_data)
9892 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
9893 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
9895 ERR("Failed to compile compute shader %p.\n", shader);
9896 return 0;
9899 shader_data = shader->backend_data;
9900 return shader_data->gl_shaders.cs[0].id;
9903 /* Context activation is done by the caller. */
9904 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
9905 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9907 struct glsl_shader_prog_link *entry;
9908 struct wined3d_shader *shader;
9909 struct glsl_program_key key;
9910 GLuint cs_id;
9912 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
9913 return;
9915 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
9917 WARN("Compute shader is NULL.\n");
9918 ctx_data->glsl_program = NULL;
9919 return;
9922 cs_id = find_glsl_compute_shader(context, priv, shader);
9923 memset(&key, 0, sizeof(key));
9924 key.cs_id = cs_id;
9925 if (!(entry = get_glsl_program_entry(priv, &key)))
9926 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
9927 ctx_data->glsl_program = entry;
9930 /* Context activation is done by the caller. */
9931 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
9932 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9934 const struct wined3d_gl_info *gl_info = context->gl_info;
9935 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
9936 const struct ps_np2fixup_info *np2fixup_info = NULL;
9937 struct glsl_shader_prog_link *entry = NULL;
9938 struct wined3d_shader *hshader, *dshader, *gshader;
9939 struct wined3d_shader *vshader = NULL;
9940 struct wined3d_shader *pshader = NULL;
9941 GLuint reorder_shader_id = 0;
9942 struct glsl_program_key key;
9943 GLuint program_id;
9944 unsigned int i;
9945 GLuint vs_id = 0;
9946 GLuint hs_id = 0;
9947 GLuint ds_id = 0;
9948 GLuint gs_id = 0;
9949 GLuint ps_id = 0;
9950 struct list *ps_list, *vs_list;
9951 WORD attribs_map;
9952 struct wined3d_string_buffer *tmp_name;
9954 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
9956 vs_id = ctx_data->glsl_program->vs.id;
9957 vs_list = &ctx_data->glsl_program->vs.shader_entry;
9959 if (use_vs(state))
9960 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
9962 else if (use_vs(state))
9964 struct vs_compile_args vs_compile_args;
9966 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
9968 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
9969 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
9970 vs_list = &vshader->linked_programs;
9972 else if (priv->vertex_pipe == &glsl_vertex_pipe)
9974 struct glsl_ffp_vertex_shader *ffp_shader;
9975 struct wined3d_ffp_vs_settings settings;
9977 wined3d_ffp_get_vs_settings(context, state, &settings);
9978 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
9979 vs_id = ffp_shader->id;
9980 vs_list = &ffp_shader->linked_programs;
9983 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
9984 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
9985 hs_id = ctx_data->glsl_program->hs.id;
9986 else if (hshader)
9987 hs_id = find_glsl_hull_shader(context, priv, hshader);
9989 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
9990 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
9992 ds_id = ctx_data->glsl_program->ds.id;
9994 else if (dshader)
9996 struct ds_compile_args args;
9998 find_ds_compile_args(state, dshader, &args, context);
9999 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
10002 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10003 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10005 gs_id = ctx_data->glsl_program->gs.id;
10007 else if (gshader)
10009 struct gs_compile_args args;
10011 find_gs_compile_args(state, gshader, &args, context);
10012 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
10015 /* A pixel shader is not used when rasterization is disabled. */
10016 if (gshader && gshader->u.gs.so_desc.rasterizer_stream_idx == WINED3D_NO_RASTERIZER_STREAM)
10018 ps_id = 0;
10019 ps_list = NULL;
10021 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10023 ps_id = ctx_data->glsl_program->ps.id;
10024 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10026 if (use_ps(state))
10027 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10029 else if (use_ps(state))
10031 struct ps_compile_args ps_compile_args;
10032 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10033 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
10034 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
10035 pshader, &ps_compile_args, &np2fixup_info);
10036 ps_list = &pshader->linked_programs;
10038 else if (priv->fragment_pipe == &glsl_fragment_pipe
10039 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10041 struct glsl_ffp_fragment_shader *ffp_shader;
10042 struct ffp_frag_settings settings;
10044 gen_ffp_frag_op(context, state, &settings, FALSE);
10045 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
10046 ps_id = ffp_shader->id;
10047 ps_list = &ffp_shader->linked_programs;
10050 key.vs_id = vs_id;
10051 key.hs_id = hs_id;
10052 key.ds_id = ds_id;
10053 key.gs_id = gs_id;
10054 key.ps_id = ps_id;
10055 key.cs_id = 0;
10056 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10058 ctx_data->glsl_program = entry;
10059 return;
10062 /* If we get to this point, then no matching program exists, so we create one */
10063 program_id = GL_EXTCALL(glCreateProgram());
10064 TRACE("Created new GLSL shader program %u.\n", program_id);
10066 /* Create the entry */
10067 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
10068 entry->id = program_id;
10069 entry->vs.id = vs_id;
10070 entry->hs.id = hs_id;
10071 entry->ds.id = ds_id;
10072 entry->gs.id = gs_id;
10073 entry->ps.id = ps_id;
10074 entry->cs.id = 0;
10075 entry->constant_version = 0;
10076 entry->ps.np2_fixup_info = np2fixup_info;
10077 /* Add the hash table entry */
10078 add_glsl_program_entry(priv, entry);
10080 /* Set the current program */
10081 ctx_data->glsl_program = entry;
10083 /* Attach GLSL vshader */
10084 if (vs_id)
10086 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10087 GL_EXTCALL(glAttachShader(program_id, vs_id));
10088 checkGLcall("glAttachShader");
10090 list_add_head(vs_list, &entry->vs.shader_entry);
10093 if (vshader)
10095 attribs_map = vshader->reg_maps.input_registers;
10096 if (vshader->reg_maps.shader_version.major < 4)
10098 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10099 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10100 d3d_info->emulated_flatshading
10101 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10102 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10103 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10104 checkGLcall("glAttachShader");
10105 /* Flag the reorder function for deletion, it will be freed
10106 * automatically when the program is destroyed. */
10107 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10110 else
10112 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10115 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10117 /* Bind vertex attributes to a corresponding index number to match
10118 * the same index numbers as ARB_vertex_programs (makes loading
10119 * vertex attributes simpler). With this method, we can use the
10120 * exact same code to load the attributes later for both ARB and
10121 * GLSL shaders.
10123 * We have to do this here because we need to know the Program ID
10124 * in order to make the bindings work, and it has to be done prior
10125 * to linking the GLSL program. */
10126 tmp_name = string_buffer_get(&priv->string_buffers);
10127 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10129 if (!(attribs_map & 1))
10130 continue;
10132 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10133 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10134 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10136 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10137 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10138 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10139 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10142 checkGLcall("glBindAttribLocation");
10143 string_buffer_release(&priv->string_buffers, tmp_name);
10145 if (!needs_legacy_glsl_syntax(gl_info))
10147 GL_EXTCALL(glBindFragDataLocation(program_id, 0, "ps_out"));
10148 checkGLcall("glBindFragDataLocation");
10152 if (hshader)
10154 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10155 GL_EXTCALL(glAttachShader(program_id, hs_id));
10156 checkGLcall("glAttachShader");
10158 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10161 if (dshader)
10163 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10164 GL_EXTCALL(glAttachShader(program_id, ds_id));
10165 checkGLcall("glAttachShader");
10167 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10170 if (gshader)
10172 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10173 GL_EXTCALL(glAttachShader(program_id, gs_id));
10174 checkGLcall("glAttachShader");
10176 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10178 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10181 /* Attach GLSL pshader */
10182 if (ps_id)
10184 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10185 GL_EXTCALL(glAttachShader(program_id, ps_id));
10186 checkGLcall("glAttachShader");
10188 list_add_head(ps_list, &entry->ps.shader_entry);
10191 /* Link the program */
10192 TRACE("Linking GLSL shader program %u.\n", program_id);
10193 GL_EXTCALL(glLinkProgram(program_id));
10194 shader_glsl_validate_link(gl_info, program_id);
10196 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10197 vshader ? vshader->limits->constant_float : 0);
10198 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10199 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10200 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10201 pshader ? pshader->limits->constant_float : 0);
10202 checkGLcall("Find glsl program uniform locations");
10204 if (needs_legacy_glsl_syntax(gl_info))
10206 if (pshader && pshader->reg_maps.shader_version.major >= 3
10207 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10209 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10210 entry->vs.vertex_color_clamp = GL_FALSE;
10212 else
10214 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10217 else
10219 /* With core profile we never change vertex_color_clamp from
10220 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10221 * glClampColorARB(). */
10222 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10225 /* Set the shader to allow uniform loading on it */
10226 GL_EXTCALL(glUseProgram(program_id));
10227 checkGLcall("glUseProgram");
10229 entry->constant_update_mask = 0;
10230 if (vshader)
10232 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10233 if (vshader->reg_maps.integer_constants)
10234 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10235 if (vshader->reg_maps.boolean_constants)
10236 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10237 if (entry->vs.pos_fixup_location != -1)
10238 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10240 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10242 else
10244 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10245 | WINED3D_SHADER_CONST_FFP_PROJ;
10247 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10249 if (entry->vs.modelview_matrix_location[i] != -1)
10251 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10252 break;
10256 for (i = 0; i < MAX_TEXTURES; ++i)
10258 if (entry->vs.texture_matrix_location[i] != -1)
10260 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10261 break;
10264 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10265 || entry->vs.material_specular_location != -1
10266 || entry->vs.material_emissive_location != -1
10267 || entry->vs.material_shininess_location != -1)
10268 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10269 if (entry->vs.light_ambient_location != -1)
10270 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10272 if (entry->vs.clip_planes_location != -1)
10273 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10274 if (entry->vs.pointsize_min_location != -1)
10275 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10277 if (hshader)
10278 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10280 if (dshader)
10282 if (entry->ds.pos_fixup_location != -1)
10283 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10285 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10288 if (gshader)
10290 if (entry->gs.pos_fixup_location != -1)
10291 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10293 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10296 if (ps_id)
10298 if (pshader)
10300 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10301 if (pshader->reg_maps.integer_constants)
10302 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10303 if (pshader->reg_maps.boolean_constants)
10304 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10305 if (entry->ps.ycorrection_location != -1)
10306 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10308 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10309 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10311 else
10313 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10315 shader_glsl_load_samplers(context, priv, program_id, NULL);
10318 for (i = 0; i < MAX_TEXTURES; ++i)
10320 if (entry->ps.bumpenv_mat_location[i] != -1)
10322 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10323 break;
10327 if (entry->ps.fog_color_location != -1)
10328 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10329 if (entry->ps.alpha_test_ref_location != -1)
10330 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10331 if (entry->ps.np2_fixup_location != -1)
10332 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10333 if (entry->ps.color_key_location != -1)
10334 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10338 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10340 struct wined3d_device *device = shader->device;
10341 struct wined3d_context *context;
10343 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10345 context = context_acquire(device, NULL, 0);
10346 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10347 context_release(context);
10351 /* Context activation is done by the caller. */
10352 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10353 const struct wined3d_state *state)
10355 struct glsl_context_data *ctx_data = context->shader_backend_data;
10356 const struct wined3d_gl_info *gl_info = context->gl_info;
10357 struct shader_glsl_priv *priv = shader_priv;
10358 GLenum current_vertex_color_clamp;
10359 GLuint program_id, prev_id;
10361 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10362 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10364 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10366 set_glsl_shader_program(context, state, priv, ctx_data);
10368 if (ctx_data->glsl_program)
10370 program_id = ctx_data->glsl_program->id;
10371 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
10373 else
10375 program_id = 0;
10376 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10379 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10381 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10382 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10384 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10385 checkGLcall("glClampColorARB");
10387 else
10389 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10393 TRACE("Using GLSL program %u.\n", program_id);
10395 if (prev_id != program_id)
10397 GL_EXTCALL(glUseProgram(program_id));
10398 checkGLcall("glUseProgram");
10400 if (program_id)
10401 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
10404 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10407 /* Context activation is done by the caller. */
10408 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10409 const struct wined3d_state *state)
10411 struct glsl_context_data *ctx_data = context->shader_backend_data;
10412 const struct wined3d_gl_info *gl_info = context->gl_info;
10413 struct shader_glsl_priv *priv = shader_priv;
10414 GLuint program_id, prev_id;
10416 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10417 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10418 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10420 TRACE("Using GLSL program %u.\n", program_id);
10422 if (prev_id != program_id)
10424 GL_EXTCALL(glUseProgram(program_id));
10425 checkGLcall("glUseProgram");
10428 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10429 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10430 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10431 | (1u << WINED3D_SHADER_TYPE_HULL)
10432 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10435 /* "context" is not necessarily the currently active context. */
10436 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10438 struct glsl_context_data *ctx_data = context->shader_backend_data;
10440 ctx_data->glsl_program = NULL;
10441 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10442 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10443 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10444 | (1u << WINED3D_SHADER_TYPE_HULL)
10445 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10446 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10449 /* Context activation is done by the caller. */
10450 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10452 struct glsl_context_data *ctx_data = context->shader_backend_data;
10453 const struct wined3d_gl_info *gl_info = context->gl_info;
10454 struct shader_glsl_priv *priv = shader_priv;
10456 shader_glsl_invalidate_current_program(context);
10457 GL_EXTCALL(glUseProgram(0));
10458 checkGLcall("glUseProgram");
10460 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10461 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10463 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10465 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10466 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10467 checkGLcall("glClampColorARB");
10471 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10472 const struct glsl_shader_prog_link *program)
10474 const struct glsl_context_data *ctx_data;
10475 struct wined3d_context *context;
10476 unsigned int i;
10478 for (i = 0; i < device->context_count; ++i)
10480 context = device->contexts[i];
10481 ctx_data = context->shader_backend_data;
10483 if (ctx_data->glsl_program == program)
10484 shader_glsl_invalidate_current_program(context);
10488 static void shader_glsl_destroy(struct wined3d_shader *shader)
10490 struct glsl_shader_private *shader_data = shader->backend_data;
10491 struct wined3d_device *device = shader->device;
10492 struct shader_glsl_priv *priv = device->shader_priv;
10493 const struct wined3d_gl_info *gl_info;
10494 const struct list *linked_programs;
10495 struct wined3d_context *context;
10497 if (!shader_data || !shader_data->num_gl_shaders)
10499 HeapFree(GetProcessHeap(), 0, shader_data);
10500 shader->backend_data = NULL;
10501 return;
10504 context = context_acquire(device, NULL, 0);
10505 gl_info = context->gl_info;
10507 TRACE("Deleting linked programs.\n");
10508 linked_programs = &shader->linked_programs;
10509 if (linked_programs->next)
10511 struct glsl_shader_prog_link *entry, *entry2;
10512 UINT i;
10514 switch (shader->reg_maps.shader_version.type)
10516 case WINED3D_SHADER_TYPE_PIXEL:
10518 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10520 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10522 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10523 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10524 checkGLcall("glDeleteShader");
10526 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
10528 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10529 struct glsl_shader_prog_link, ps.shader_entry)
10531 shader_glsl_invalidate_contexts_program(device, entry);
10532 delete_glsl_program_entry(priv, gl_info, entry);
10535 break;
10538 case WINED3D_SHADER_TYPE_VERTEX:
10540 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10542 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10544 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10545 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10546 checkGLcall("glDeleteShader");
10548 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
10550 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10551 struct glsl_shader_prog_link, vs.shader_entry)
10553 shader_glsl_invalidate_contexts_program(device, entry);
10554 delete_glsl_program_entry(priv, gl_info, entry);
10557 break;
10560 case WINED3D_SHADER_TYPE_HULL:
10562 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10564 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10566 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10567 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10568 checkGLcall("glDeleteShader");
10570 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.hs);
10572 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10573 struct glsl_shader_prog_link, hs.shader_entry)
10575 shader_glsl_invalidate_contexts_program(device, entry);
10576 delete_glsl_program_entry(priv, gl_info, entry);
10579 break;
10582 case WINED3D_SHADER_TYPE_DOMAIN:
10584 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10586 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10588 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10589 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10590 checkGLcall("glDeleteShader");
10592 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ds);
10594 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10595 struct glsl_shader_prog_link, ds.shader_entry)
10597 shader_glsl_invalidate_contexts_program(device, entry);
10598 delete_glsl_program_entry(priv, gl_info, entry);
10601 break;
10604 case WINED3D_SHADER_TYPE_GEOMETRY:
10606 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10608 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10610 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10611 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10612 checkGLcall("glDeleteShader");
10614 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
10616 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10617 struct glsl_shader_prog_link, gs.shader_entry)
10619 shader_glsl_invalidate_contexts_program(device, entry);
10620 delete_glsl_program_entry(priv, gl_info, entry);
10623 break;
10626 case WINED3D_SHADER_TYPE_COMPUTE:
10628 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10630 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10632 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10633 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10634 checkGLcall("glDeleteShader");
10636 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
10638 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10639 struct glsl_shader_prog_link, cs.shader_entry)
10641 shader_glsl_invalidate_contexts_program(device, entry);
10642 delete_glsl_program_entry(priv, gl_info, entry);
10645 break;
10648 default:
10649 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10650 break;
10654 HeapFree(GetProcessHeap(), 0, shader->backend_data);
10655 shader->backend_data = NULL;
10657 context_release(context);
10660 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10662 const struct glsl_program_key *k = key;
10663 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10664 const struct glsl_shader_prog_link, program_lookup_entry);
10666 if (k->vs_id > prog->vs.id) return 1;
10667 else if (k->vs_id < prog->vs.id) return -1;
10669 if (k->gs_id > prog->gs.id) return 1;
10670 else if (k->gs_id < prog->gs.id) return -1;
10672 if (k->ps_id > prog->ps.id) return 1;
10673 else if (k->ps_id < prog->ps.id) return -1;
10675 if (k->hs_id > prog->hs.id) return 1;
10676 else if (k->hs_id < prog->hs.id) return -1;
10678 if (k->ds_id > prog->ds.id) return 1;
10679 else if (k->ds_id < prog->ds.id) return -1;
10681 if (k->cs_id > prog->cs.id) return 1;
10682 else if (k->cs_id < prog->cs.id) return -1;
10684 return 0;
10687 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10689 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10690 + constant_count * sizeof(*heap->contained)
10691 + constant_count * sizeof(*heap->positions);
10692 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
10694 if (!mem)
10696 ERR("Failed to allocate memory\n");
10697 return FALSE;
10700 heap->entries = mem;
10701 heap->entries[1].version = 0;
10702 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10703 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10704 heap->positions = (unsigned int *)(heap->contained + constant_count);
10705 heap->size = 1;
10707 return TRUE;
10710 static void constant_heap_free(struct constant_heap *heap)
10712 HeapFree(GetProcessHeap(), 0, heap->entries);
10715 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
10716 const struct fragment_pipeline *fragment_pipe)
10718 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
10719 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
10720 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
10721 struct fragment_caps fragment_caps;
10722 void *vertex_priv, *fragment_priv;
10724 string_buffer_list_init(&priv->string_buffers);
10726 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
10728 ERR("Failed to initialize vertex pipe.\n");
10729 HeapFree(GetProcessHeap(), 0, priv);
10730 return E_FAIL;
10733 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
10735 ERR("Failed to initialize fragment pipe.\n");
10736 vertex_pipe->vp_free(device);
10737 HeapFree(GetProcessHeap(), 0, priv);
10738 return E_FAIL;
10741 if (!string_buffer_init(&priv->shader_buffer))
10743 ERR("Failed to initialize shader buffer.\n");
10744 goto fail;
10747 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
10749 ERR("Failed to allocate memory.\n");
10750 goto fail;
10753 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
10755 ERR("Failed to initialize vertex shader constant heap\n");
10756 goto fail;
10759 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
10761 ERR("Failed to initialize pixel shader constant heap\n");
10762 goto fail;
10765 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
10767 priv->next_constant_version = 1;
10768 priv->vertex_pipe = vertex_pipe;
10769 priv->fragment_pipe = fragment_pipe;
10770 fragment_pipe->get_caps(gl_info, &fragment_caps);
10771 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
10772 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
10774 device->vertex_priv = vertex_priv;
10775 device->fragment_priv = fragment_priv;
10776 device->shader_priv = priv;
10778 return WINED3D_OK;
10780 fail:
10781 constant_heap_free(&priv->pconst_heap);
10782 constant_heap_free(&priv->vconst_heap);
10783 HeapFree(GetProcessHeap(), 0, priv->stack);
10784 string_buffer_free(&priv->shader_buffer);
10785 fragment_pipe->free_private(device);
10786 vertex_pipe->vp_free(device);
10787 HeapFree(GetProcessHeap(), 0, priv);
10788 return E_OUTOFMEMORY;
10791 /* Context activation is done by the caller. */
10792 static void shader_glsl_free(struct wined3d_device *device)
10794 struct shader_glsl_priv *priv = device->shader_priv;
10796 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
10797 constant_heap_free(&priv->pconst_heap);
10798 constant_heap_free(&priv->vconst_heap);
10799 HeapFree(GetProcessHeap(), 0, priv->stack);
10800 string_buffer_list_cleanup(&priv->string_buffers);
10801 string_buffer_free(&priv->shader_buffer);
10802 priv->fragment_pipe->free_private(device);
10803 priv->vertex_pipe->vp_free(device);
10805 HeapFree(GetProcessHeap(), 0, device->shader_priv);
10806 device->shader_priv = NULL;
10809 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
10811 struct glsl_context_data *ctx_data;
10812 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
10813 return FALSE;
10814 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10815 context->shader_backend_data = ctx_data;
10816 return TRUE;
10819 static void shader_glsl_free_context_data(struct wined3d_context *context)
10821 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
10824 static void shader_glsl_init_context_state(struct wined3d_context *context)
10826 const struct wined3d_gl_info *gl_info = context->gl_info;
10828 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
10829 checkGLcall("GL_PROGRAM_POINT_SIZE");
10832 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
10834 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
10835 && gl_info->supported[WINED3D_GL_VERSION_3_2]
10836 && gl_info->supported[ARB_SAMPLER_OBJECTS]
10837 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
10838 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
10840 if (shader_model_4
10841 && gl_info->supported[ARB_COMPUTE_SHADER]
10842 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
10843 && gl_info->supported[ARB_DRAW_INDIRECT]
10844 && gl_info->supported[ARB_GPU_SHADER5]
10845 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
10846 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
10847 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
10848 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
10849 && gl_info->supported[ARB_TESSELLATION_SHADER]
10850 && gl_info->supported[ARB_TEXTURE_GATHER]
10851 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
10852 return 5;
10854 if (shader_model_4)
10855 return 4;
10857 /* Support for texldd and texldl instructions in pixel shaders is required
10858 * for SM3. */
10859 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
10860 return 3;
10862 return 2;
10865 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
10867 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
10869 TRACE("Shader model %u.\n", shader_model);
10871 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
10872 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
10873 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
10874 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
10875 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
10876 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
10878 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
10879 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
10881 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
10882 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
10883 caps->varying_count = gl_info->limits.glsl_varyings;
10885 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
10886 * Direct3D minimum requirement.
10888 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
10889 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
10891 * The problem is that the refrast clamps temporary results in the shader to
10892 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
10893 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
10894 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
10895 * offer a way to query this.
10897 if (shader_model >= 4)
10898 caps->ps_1x_max_value = FLT_MAX;
10899 else
10900 caps->ps_1x_max_value = 1024.0f;
10902 /* Ideally we'd only set caps like sRGB writes here if supported by both
10903 * the shader backend and the fragment pipe, but we can get called before
10904 * shader_glsl_alloc(). */
10905 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
10906 | WINED3D_SHADER_CAP_SRGB_WRITE;
10909 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
10911 /* We support everything except YUV conversions. */
10912 return !is_complex_fixup(fixup);
10915 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
10917 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
10918 /* WINED3DSIH_ADD */ shader_glsl_binop,
10919 /* WINED3DSIH_AND */ shader_glsl_binop,
10920 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
10921 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
10922 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
10923 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
10924 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
10925 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
10926 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
10927 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
10928 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
10929 /* WINED3DSIH_BEM */ shader_glsl_bem,
10930 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
10931 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
10932 /* WINED3DSIH_BREAK */ shader_glsl_break,
10933 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
10934 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
10935 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
10936 /* WINED3DSIH_CALL */ shader_glsl_call,
10937 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
10938 /* WINED3DSIH_CASE */ shader_glsl_case,
10939 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
10940 /* WINED3DSIH_CND */ shader_glsl_cnd,
10941 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
10942 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
10943 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
10944 /* WINED3DSIH_CRS */ shader_glsl_cross,
10945 /* WINED3DSIH_CUT */ shader_glsl_cut,
10946 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
10947 /* WINED3DSIH_DCL */ shader_glsl_nop,
10948 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
10949 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
10950 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
10951 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
10952 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
10953 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
10954 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
10955 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
10956 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
10957 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
10958 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
10959 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
10960 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
10961 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
10962 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
10963 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
10964 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
10965 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
10966 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
10967 /* WINED3DSIH_DCL_INTERFACE */ NULL,
10968 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
10969 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
10970 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
10971 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
10972 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
10973 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
10974 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
10975 /* WINED3DSIH_DCL_STREAM */ NULL,
10976 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
10977 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
10978 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
10979 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
10980 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
10981 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
10982 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
10983 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
10984 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
10985 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
10986 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
10987 /* WINED3DSIH_DEF */ shader_glsl_nop,
10988 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
10989 /* WINED3DSIH_DEFB */ shader_glsl_nop,
10990 /* WINED3DSIH_DEFI */ shader_glsl_nop,
10991 /* WINED3DSIH_DIV */ shader_glsl_binop,
10992 /* WINED3DSIH_DP2 */ shader_glsl_dot,
10993 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
10994 /* WINED3DSIH_DP3 */ shader_glsl_dot,
10995 /* WINED3DSIH_DP4 */ shader_glsl_dot,
10996 /* WINED3DSIH_DST */ shader_glsl_dst,
10997 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
10998 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
10999 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11000 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11001 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11002 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11003 /* WINED3DSIH_ELSE */ shader_glsl_else,
11004 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11005 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11006 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11007 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11008 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11009 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11010 /* WINED3DSIH_EQ */ shader_glsl_relop,
11011 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11012 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11013 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11014 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11015 /* WINED3DSIH_FCALL */ NULL,
11016 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11017 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11018 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11019 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11020 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11021 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11022 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11023 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11024 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11025 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11026 /* WINED3DSIH_GE */ shader_glsl_relop,
11027 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11028 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11029 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11030 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11031 /* WINED3DSIH_IADD */ shader_glsl_binop,
11032 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11033 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11034 /* WINED3DSIH_IF */ shader_glsl_if,
11035 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11036 /* WINED3DSIH_IGE */ shader_glsl_relop,
11037 /* WINED3DSIH_ILT */ shader_glsl_relop,
11038 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11039 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11040 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11041 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11042 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11043 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11044 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11045 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11046 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11047 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11048 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11049 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11050 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11051 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11052 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11053 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11054 /* WINED3DSIH_INE */ shader_glsl_relop,
11055 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11056 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11057 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11058 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11059 /* WINED3DSIH_LABEL */ shader_glsl_label,
11060 /* WINED3DSIH_LD */ shader_glsl_ld,
11061 /* WINED3DSIH_LD2DMS */ NULL,
11062 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11063 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11064 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11065 /* WINED3DSIH_LIT */ shader_glsl_lit,
11066 /* WINED3DSIH_LOD */ NULL,
11067 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11068 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11069 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11070 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11071 /* WINED3DSIH_LT */ shader_glsl_relop,
11072 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11073 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11074 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11075 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11076 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11077 /* WINED3DSIH_MAD */ shader_glsl_mad,
11078 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11079 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11080 /* WINED3DSIH_MOV */ shader_glsl_mov,
11081 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11082 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11083 /* WINED3DSIH_MUL */ shader_glsl_binop,
11084 /* WINED3DSIH_NE */ shader_glsl_relop,
11085 /* WINED3DSIH_NOP */ shader_glsl_nop,
11086 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11087 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11088 /* WINED3DSIH_OR */ shader_glsl_binop,
11089 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11090 /* WINED3DSIH_POW */ shader_glsl_pow,
11091 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11092 /* WINED3DSIH_REP */ shader_glsl_rep,
11093 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11094 /* WINED3DSIH_RET */ shader_glsl_ret,
11095 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11096 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11097 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11098 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11099 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11100 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11101 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11102 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11103 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11104 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11105 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11106 /* WINED3DSIH_SAMPLE_INFO */ NULL,
11107 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11108 /* WINED3DSIH_SAMPLE_POS */ NULL,
11109 /* WINED3DSIH_SETP */ NULL,
11110 /* WINED3DSIH_SGE */ shader_glsl_compare,
11111 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11112 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11113 /* WINED3DSIH_SLT */ shader_glsl_compare,
11114 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11115 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11116 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11117 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11118 /* WINED3DSIH_SUB */ shader_glsl_binop,
11119 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11120 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11121 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11122 /* WINED3DSIH_TEX */ shader_glsl_tex,
11123 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11124 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11125 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11126 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11127 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11128 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11129 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11130 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11131 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11132 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11133 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11134 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11135 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11136 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11137 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11138 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11139 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11140 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11141 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11142 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11143 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11144 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11145 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11146 /* WINED3DSIH_UGE */ shader_glsl_relop,
11147 /* WINED3DSIH_ULT */ shader_glsl_relop,
11148 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11149 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11150 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11151 /* WINED3DSIH_USHR */ shader_glsl_binop,
11152 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11153 /* WINED3DSIH_XOR */ shader_glsl_binop,
11156 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11157 SHADER_HANDLER hw_fct;
11159 /* Select handler */
11160 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11162 /* Unhandled opcode */
11163 if (!hw_fct)
11165 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11166 return;
11168 hw_fct(ins);
11170 shader_glsl_add_instruction_modifiers(ins);
11173 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11175 struct shader_glsl_priv *priv = shader_priv;
11177 return priv->ffp_proj_control;
11180 const struct wined3d_shader_backend_ops glsl_shader_backend =
11182 shader_glsl_handle_instruction,
11183 shader_glsl_precompile,
11184 shader_glsl_select,
11185 shader_glsl_select_compute,
11186 shader_glsl_disable,
11187 shader_glsl_update_float_vertex_constants,
11188 shader_glsl_update_float_pixel_constants,
11189 shader_glsl_load_constants,
11190 shader_glsl_destroy,
11191 shader_glsl_alloc,
11192 shader_glsl_free,
11193 shader_glsl_allocate_context_data,
11194 shader_glsl_free_context_data,
11195 shader_glsl_init_context_state,
11196 shader_glsl_get_caps,
11197 shader_glsl_color_fixup_supported,
11198 shader_glsl_has_ffp_proj_control,
11201 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11203 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11205 caps->xyzrhw = TRUE;
11206 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11207 caps->ffp_generic_attributes = TRUE;
11208 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11209 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11210 caps->max_vertex_blend_matrix_index = 0;
11211 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11212 | WINED3DVTXPCAPS_MATERIALSOURCE7
11213 | WINED3DVTXPCAPS_VERTEXFOG
11214 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11215 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11216 | WINED3DVTXPCAPS_LOCALVIEWER
11217 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11218 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11219 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11220 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11223 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11225 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11226 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11227 return 0;
11230 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11232 struct shader_glsl_priv *priv;
11234 if (shader_backend == &glsl_shader_backend)
11236 priv = shader_priv;
11237 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11238 return priv;
11241 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11243 return NULL;
11246 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11248 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11249 struct glsl_ffp_vertex_shader, desc.entry);
11250 struct glsl_shader_prog_link *program, *program2;
11251 struct glsl_ffp_destroy_ctx *ctx = context;
11253 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11254 struct glsl_shader_prog_link, vs.shader_entry)
11256 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11258 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11259 HeapFree(GetProcessHeap(), 0, shader);
11262 /* Context activation is done by the caller. */
11263 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11265 struct shader_glsl_priv *priv = device->vertex_priv;
11266 struct glsl_ffp_destroy_ctx ctx;
11268 ctx.priv = priv;
11269 ctx.gl_info = &device->adapter->gl_info;
11270 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11273 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11274 const struct wined3d_state *state, DWORD state_id) {}
11276 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11277 const struct wined3d_state *state, DWORD state_id)
11279 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11282 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11283 const struct wined3d_state *state, DWORD state_id)
11285 const struct wined3d_gl_info *gl_info = context->gl_info;
11286 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11287 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11288 BOOL transformed = context->stream_info.position_transformed;
11289 BOOL wasrhw = context->last_was_rhw;
11290 unsigned int i;
11292 context->last_was_rhw = transformed;
11294 /* If the vertex declaration contains a transformed position attribute,
11295 * the draw uses the fixed function vertex pipeline regardless of any
11296 * vertex shader set by the application. */
11297 if (transformed != wasrhw
11298 || context->stream_info.swizzle_map != context->last_swizzle_map)
11299 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11301 context->last_swizzle_map = context->stream_info.swizzle_map;
11303 if (!use_vs(state))
11305 if (context->last_was_vshader)
11307 if (legacy_clip_planes)
11308 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11309 clipplane(context, state, STATE_CLIPPLANE(i));
11310 else
11311 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11314 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11316 /* Because of settings->texcoords, we have to regenerate the vertex
11317 * shader on a vdecl change if there aren't enough varyings to just
11318 * always output all the texture coordinates. */
11319 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11320 || normal != context->last_was_normal)
11321 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11323 if (use_ps(state)
11324 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11325 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11326 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11328 else
11330 if (!context->last_was_vshader)
11332 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11333 if (legacy_clip_planes)
11334 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11335 clipplane(context, state, STATE_CLIPPLANE(i));
11336 else
11337 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11341 context->last_was_vshader = use_vs(state);
11342 context->last_was_normal = normal;
11345 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11346 const struct wined3d_state *state, DWORD state_id)
11348 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11349 /* Different vertex shaders potentially require a different vertex attributes setup. */
11350 if (!isStateDirty(context, STATE_VDECL))
11351 context_apply_state(context, state, STATE_VDECL);
11354 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11355 const struct wined3d_state *state, DWORD state_id)
11357 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11358 * winding) are defined in Hull Shaders, while in GLSL those are
11359 * specified in Tessellation Evaluation Shaders. */
11360 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11362 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11363 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11366 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11367 const struct wined3d_state *state, DWORD state_id)
11369 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11371 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11372 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11373 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11374 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11375 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11378 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11379 const struct wined3d_state *state, DWORD state_id)
11381 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11382 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11383 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11384 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11385 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11386 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11387 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11390 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11391 const struct wined3d_state *state, DWORD state_id)
11393 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11396 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11397 const struct wined3d_state *state, DWORD state_id)
11399 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11402 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11404 const struct wined3d_gl_info *gl_info = context->gl_info;
11405 unsigned int k;
11407 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11408 | WINED3D_SHADER_CONST_FFP_LIGHTS
11409 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11411 if (needs_legacy_glsl_syntax(gl_info))
11413 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11415 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11416 clipplane(context, state, STATE_CLIPPLANE(k));
11419 else
11421 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11425 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11426 const struct wined3d_state *state, DWORD state_id)
11428 /* Table fog behavior depends on the projection matrix. */
11429 if (state->render_states[WINED3D_RS_FOGENABLE]
11430 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11431 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11432 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11435 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11436 const struct wined3d_state *state, DWORD state_id)
11438 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11439 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11440 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11441 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11442 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11443 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11446 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11447 const struct wined3d_state *state, DWORD state_id)
11449 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11452 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11453 const struct wined3d_state *state, DWORD state_id)
11455 DWORD sampler = state_id - STATE_SAMPLER(0);
11456 const struct wined3d_texture *texture = state->textures[sampler];
11457 BOOL np2;
11459 if (!texture)
11460 return;
11462 if (sampler >= MAX_TEXTURES)
11463 return;
11465 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11466 || context->lastWasPow2Texture & (1u << sampler))
11468 if (np2)
11469 context->lastWasPow2Texture |= 1u << sampler;
11470 else
11471 context->lastWasPow2Texture &= ~(1u << sampler);
11473 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11477 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11478 const struct wined3d_state *state, DWORD state_id)
11480 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11483 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11484 const struct wined3d_state *state, DWORD state_id)
11486 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11489 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11490 const struct wined3d_state *state, DWORD state_id)
11492 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11495 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11496 const struct wined3d_state *state, DWORD state_id)
11498 if (!use_vs(state))
11499 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11502 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11503 const struct wined3d_state *state, DWORD state_id)
11505 static unsigned int once;
11507 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11508 FIXME("Non-point sprite points not supported in core profile.\n");
11511 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11512 const struct wined3d_state *state, DWORD state_id)
11514 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11517 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11518 const struct wined3d_state *state, DWORD state_id)
11520 const struct wined3d_gl_info *gl_info = context->gl_info;
11521 UINT index = state_id - STATE_CLIPPLANE(0);
11523 if (index >= gl_info->limits.user_clip_distances)
11524 return;
11526 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11529 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11531 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11532 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11533 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11534 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11535 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11536 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11537 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11538 /* Clip planes */
11539 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11540 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11541 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11542 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11543 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11544 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11545 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11546 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11547 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11548 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11549 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11550 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11551 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11552 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11553 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11554 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11555 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11556 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
11557 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11558 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
11559 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11560 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
11561 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11562 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
11563 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11564 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
11565 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11566 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
11567 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11568 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
11569 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11570 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
11571 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11572 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
11573 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11574 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
11575 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11576 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
11577 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11578 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
11579 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11580 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
11581 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11582 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
11583 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11584 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
11585 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11586 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
11587 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11588 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
11589 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11590 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
11591 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11592 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
11593 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11594 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
11595 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11596 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
11597 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11598 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
11599 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11600 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
11601 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11602 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
11603 /* Lights */
11604 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11605 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11606 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11607 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11608 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11609 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11610 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11611 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11612 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11613 /* Viewport */
11614 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11615 /* Transform states */
11616 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11617 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11618 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11619 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11620 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11621 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11622 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11623 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11624 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11625 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11626 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11627 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11628 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11629 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11630 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11631 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11632 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11633 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11634 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11635 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11636 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11637 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11638 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11639 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11640 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11641 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11642 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11643 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11644 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11645 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11646 /* Fog */
11647 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11648 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11649 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11650 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11651 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11652 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11653 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11654 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11655 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11656 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11657 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11658 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11659 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11660 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11661 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11662 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11663 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11664 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11665 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11666 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11667 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11668 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11669 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11670 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11671 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11672 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11673 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11674 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11675 /* NP2 texture matrix fixups. They are not needed if
11676 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11677 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11678 * matrix. */
11679 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11680 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11681 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11682 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11683 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11684 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11685 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11686 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11687 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11688 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11689 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11690 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11691 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11692 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11693 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11694 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11695 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11696 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11697 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11698 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11699 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11700 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11701 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11702 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11703 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11704 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11705 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11706 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11709 /* TODO:
11710 * - Implement vertex tweening. */
11711 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11713 glsl_vertex_pipe_vp_enable,
11714 glsl_vertex_pipe_vp_get_caps,
11715 glsl_vertex_pipe_vp_get_emul_mask,
11716 glsl_vertex_pipe_vp_alloc,
11717 glsl_vertex_pipe_vp_free,
11718 glsl_vertex_pipe_vp_states,
11721 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
11723 /* Nothing to do. */
11726 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
11728 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11729 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11730 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11731 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11732 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11733 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11734 | WINED3DTEXOPCAPS_SELECTARG1
11735 | WINED3DTEXOPCAPS_SELECTARG2
11736 | WINED3DTEXOPCAPS_MODULATE4X
11737 | WINED3DTEXOPCAPS_MODULATE2X
11738 | WINED3DTEXOPCAPS_MODULATE
11739 | WINED3DTEXOPCAPS_ADDSIGNED2X
11740 | WINED3DTEXOPCAPS_ADDSIGNED
11741 | WINED3DTEXOPCAPS_ADD
11742 | WINED3DTEXOPCAPS_SUBTRACT
11743 | WINED3DTEXOPCAPS_ADDSMOOTH
11744 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11745 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11746 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11747 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11748 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
11749 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
11750 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
11751 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
11752 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
11753 | WINED3DTEXOPCAPS_DOTPRODUCT3
11754 | WINED3DTEXOPCAPS_MULTIPLYADD
11755 | WINED3DTEXOPCAPS_LERP
11756 | WINED3DTEXOPCAPS_BUMPENVMAP
11757 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
11758 caps->MaxTextureBlendStages = MAX_TEXTURES;
11759 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
11762 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
11764 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11765 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11766 return 0;
11769 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11771 struct shader_glsl_priv *priv;
11773 if (shader_backend == &glsl_shader_backend)
11775 priv = shader_priv;
11776 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
11777 return priv;
11780 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
11782 return NULL;
11785 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
11787 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11788 struct glsl_ffp_fragment_shader, entry.entry);
11789 struct glsl_shader_prog_link *program, *program2;
11790 struct glsl_ffp_destroy_ctx *ctx = context;
11792 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11793 struct glsl_shader_prog_link, ps.shader_entry)
11795 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11797 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11798 HeapFree(GetProcessHeap(), 0, shader);
11801 /* Context activation is done by the caller. */
11802 static void glsl_fragment_pipe_free(struct wined3d_device *device)
11804 struct shader_glsl_priv *priv = device->fragment_priv;
11805 struct glsl_ffp_destroy_ctx ctx;
11807 ctx.priv = priv;
11808 ctx.gl_info = &device->adapter->gl_info;
11809 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
11812 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
11813 const struct wined3d_state *state, DWORD state_id)
11815 context->last_was_pshader = use_ps(state);
11817 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11820 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
11821 const struct wined3d_state *state, DWORD state_id)
11823 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11826 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
11827 const struct wined3d_state *state, DWORD state_id)
11829 BOOL use_vshader = use_vs(state);
11830 enum fogsource new_source;
11831 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
11832 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
11834 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11836 if (!state->render_states[WINED3D_RS_FOGENABLE])
11837 return;
11839 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
11841 if (use_vshader)
11842 new_source = FOGSOURCE_VS;
11843 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
11844 new_source = FOGSOURCE_COORD;
11845 else
11846 new_source = FOGSOURCE_FFP;
11848 else
11850 new_source = FOGSOURCE_FFP;
11853 if (new_source != context->fog_source || fogstart == fogend)
11855 context->fog_source = new_source;
11856 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11860 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
11861 const struct wined3d_state *state, DWORD state_id)
11863 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11864 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11865 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11867 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
11868 glsl_fragment_pipe_fog(context, state, state_id);
11871 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
11872 const struct wined3d_state *state, DWORD state_id)
11874 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11875 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11876 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11879 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
11880 const struct wined3d_state *state, DWORD state_id)
11882 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11885 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
11886 const struct wined3d_state *state, DWORD state_id)
11888 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
11891 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
11892 const struct wined3d_state *state, DWORD state_id)
11894 const struct wined3d_gl_info *gl_info = context->gl_info;
11895 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
11896 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
11898 if (func)
11900 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
11901 checkGLcall("glAlphaFunc");
11905 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
11906 const struct wined3d_state *state, DWORD state_id)
11908 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11911 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
11912 const struct wined3d_state *state, DWORD state_id)
11914 const struct wined3d_gl_info *gl_info = context->gl_info;
11916 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
11918 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
11919 checkGLcall("glEnable(GL_ALPHA_TEST)");
11921 else
11923 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
11924 checkGLcall("glDisable(GL_ALPHA_TEST)");
11928 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
11929 const struct wined3d_state *state, DWORD state_id)
11931 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
11934 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
11935 const struct wined3d_state *state, DWORD state_id)
11937 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
11940 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
11941 const struct wined3d_state *state, DWORD state_id)
11943 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11946 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
11948 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11949 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
11950 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11951 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11952 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11953 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11954 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11955 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11956 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11957 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11958 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11959 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11960 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11961 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11962 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11963 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11964 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11965 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11966 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11967 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11968 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11969 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11970 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11971 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11972 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11973 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11974 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11975 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11976 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11977 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11978 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11979 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11980 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11981 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11982 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11983 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11984 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11985 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11986 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11987 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11988 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11989 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11990 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11991 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11992 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11993 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11994 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11995 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11996 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11997 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11998 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11999 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12000 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12001 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12002 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12003 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12004 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12005 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12006 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12007 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12008 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12009 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12010 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12011 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12012 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12013 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12014 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12015 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12016 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12017 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12018 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12019 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12020 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12021 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12022 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12023 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12024 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12025 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12026 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12027 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12028 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12029 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12030 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12031 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12032 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12033 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12034 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12035 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12036 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12037 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12038 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12039 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12040 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12041 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12042 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12043 {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 },
12044 {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 },
12045 {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 },
12046 {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 },
12047 {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 },
12048 {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 },
12049 {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 },
12050 {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 },
12051 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12052 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12053 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12054 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12055 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12056 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12057 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12058 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12059 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12060 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12061 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12062 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12063 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12066 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12068 return TRUE;
12071 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12075 const struct fragment_pipeline glsl_fragment_pipe =
12077 glsl_fragment_pipe_enable,
12078 glsl_fragment_pipe_get_caps,
12079 glsl_fragment_pipe_get_emul_mask,
12080 glsl_fragment_pipe_alloc,
12081 glsl_fragment_pipe_free,
12082 glsl_fragment_pipe_alloc_context_data,
12083 glsl_fragment_pipe_free_context_data,
12084 shader_glsl_color_fixup_supported,
12085 glsl_fragment_pipe_state_template,