ntdll: Store memory views in a binary tree instead of a list.
[wine.git] / dlls / wined3d / glsl_shader.c
blob7290fc71a12ba364a1bb56f817ff61c2345dc2d8
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(1, 50))
390 return 150;
391 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
392 return 130;
393 else
394 return 120;
397 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
398 const struct wined3d_gl_info *gl_info)
400 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
403 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
405 char str[4][17];
407 wined3d_ftoa(values[0], str[0]);
408 wined3d_ftoa(values[1], str[1]);
409 wined3d_ftoa(values[2], str[2]);
410 wined3d_ftoa(values[3], str[3]);
411 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
414 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
415 const int *values, unsigned int size)
417 int i;
419 if (!size || size > 4)
421 ERR("Invalid vector size %u.\n", size);
422 return;
425 if (size > 1)
426 shader_addline(buffer, "ivec%u(", size);
428 for (i = 0; i < size; ++i)
429 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
431 if (size > 1)
432 shader_addline(buffer, ")");
435 static const char *get_info_log_line(const char **ptr)
437 const char *p, *q;
439 p = *ptr;
440 if (!(q = strstr(p, "\n")))
442 if (!*p) return NULL;
443 *ptr += strlen(p);
444 return p;
446 *ptr = q + 1;
448 return p;
451 /* Context activation is done by the caller. */
452 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
454 int length = 0;
455 char *log;
457 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
458 return;
460 if (program)
461 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
462 else
463 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
465 /* A size of 1 is just a null-terminated string, so the log should be bigger than
466 * that if there are errors. */
467 if (length > 1)
469 const char *ptr, *line;
471 log = HeapAlloc(GetProcessHeap(), 0, length);
472 /* The info log is supposed to be zero-terminated, but at least some
473 * versions of fglrx don't terminate the string properly. The reported
474 * length does include the terminator, so explicitly set it to zero
475 * here. */
476 log[length - 1] = 0;
477 if (program)
478 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
479 else
480 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
482 ptr = log;
483 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
485 WARN("Info log received from GLSL shader #%u:\n", id);
486 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
488 else
490 FIXME("Info log received from GLSL shader #%u:\n", id);
491 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
493 HeapFree(GetProcessHeap(), 0, log);
497 /* Context activation is done by the caller. */
498 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
500 const char *ptr, *line;
502 TRACE("Compiling shader object %u.\n", shader);
504 if (TRACE_ON(d3d_shader))
506 ptr = src;
507 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
510 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
511 checkGLcall("glShaderSource");
512 GL_EXTCALL(glCompileShader(shader));
513 checkGLcall("glCompileShader");
514 print_glsl_info_log(gl_info, shader, FALSE);
517 /* Context activation is done by the caller. */
518 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
520 GLint i, shader_count, source_size = -1;
521 GLuint *shaders;
522 char *source = NULL;
524 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
525 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
527 ERR("Failed to allocate shader array memory.\n");
528 return;
531 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
532 for (i = 0; i < shader_count; ++i)
534 const char *ptr, *line;
535 GLint tmp;
537 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
539 if (source_size < tmp)
541 HeapFree(GetProcessHeap(), 0, source);
543 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
544 if (!source)
546 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
547 HeapFree(GetProcessHeap(), 0, shaders);
548 return;
550 source_size = tmp;
553 FIXME("Shader %u:\n", shaders[i]);
554 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
555 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
556 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
557 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
558 FIXME("\n");
560 ptr = source;
561 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
562 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
563 FIXME("\n");
566 HeapFree(GetProcessHeap(), 0, source);
567 HeapFree(GetProcessHeap(), 0, shaders);
570 /* Context activation is done by the caller. */
571 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
573 GLint tmp;
575 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
576 return;
578 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
579 if (!tmp)
581 FIXME("Program %u link status invalid.\n", program);
582 shader_glsl_dump_program_source(gl_info, program);
585 print_glsl_info_log(gl_info, program, TRUE);
588 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
590 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
591 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
592 * versions. */
593 return shader_glsl_get_version(gl_info) >= 140;
596 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
598 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
601 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
602 struct shader_glsl_priv *priv, GLuint program_id,
603 const struct wined3d_shader_reg_maps *reg_maps)
605 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
606 struct wined3d_string_buffer *name;
607 unsigned int i, base, count;
608 GLuint block_idx;
610 if (shader_glsl_use_layout_binding_qualifier(gl_info))
611 return;
613 name = string_buffer_get(&priv->string_buffers);
614 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
615 for (i = 0; i < count; ++i)
617 if (!reg_maps->cb_sizes[i])
618 continue;
620 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
621 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
622 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
624 checkGLcall("glUniformBlockBinding");
625 string_buffer_release(&priv->string_buffers, name);
628 /* Context activation is done by the caller. */
629 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
630 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
631 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
633 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
634 unsigned int i, mapped_unit;
635 GLint name_loc;
637 for (i = 0; i < count; ++i)
639 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
640 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
641 if (name_loc == -1)
642 continue;
644 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
645 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
647 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
648 continue;
651 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
652 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
654 checkGLcall("Load sampler bindings");
655 string_buffer_release(&priv->string_buffers, sampler_name);
658 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
659 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
661 const DWORD *tex_unit_map;
662 unsigned int base, count;
664 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
665 if (sampler_idx >= count)
666 return WINED3D_UNMAPPED_STAGE;
667 if (!tex_unit_map)
668 return base + sampler_idx;
669 return tex_unit_map[base + sampler_idx];
672 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
673 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
674 unsigned int sampler_idx)
676 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
677 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
678 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
679 else
680 ERR("Unmapped sampler %u.\n", sampler_idx);
683 /* Context activation is done by the caller. */
684 static void shader_glsl_load_samplers(const struct wined3d_context *context,
685 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
687 const struct wined3d_gl_info *gl_info = context->gl_info;
688 const struct wined3d_shader_version *shader_version;
689 const DWORD *tex_unit_map;
690 unsigned int base, count;
691 const char *prefix;
693 if (shader_glsl_use_layout_binding_qualifier(gl_info))
694 return;
696 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
697 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
698 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
699 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
702 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
703 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
705 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
707 if (icb)
709 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
710 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
711 GLint icb_location;
713 string_buffer_sprintf(icb_name, "%s_icb", prefix);
714 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
715 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
716 checkGLcall("Load immediate constant buffer");
718 string_buffer_release(&priv->string_buffers, icb_name);
722 /* Context activation is done by the caller. */
723 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
724 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
726 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
727 struct wined3d_string_buffer *name;
728 GLint location;
729 unsigned int i;
731 if (shader_glsl_use_layout_binding_qualifier(gl_info))
732 return;
734 name = string_buffer_get(&priv->string_buffers);
735 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
737 if (!reg_maps->uav_resource_info[i].type)
738 continue;
740 string_buffer_sprintf(name, "%s_image%u", prefix, i);
741 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
742 if (location == -1)
743 continue;
745 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
746 GL_EXTCALL(glUniform1i(location, i));
748 checkGLcall("Load image bindings");
749 string_buffer_release(&priv->string_buffers, name);
752 /* Context activation is done by the caller. */
753 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
754 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
756 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
758 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
759 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
760 /* Texture unit mapping is set up to be the same each time the shader
761 * program is used so we can hardcode the sampler uniform values. */
762 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
765 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
766 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
768 if (varyings && *strings)
770 char *ptr = *strings;
772 varyings[*varying_count] = ptr;
774 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
775 ptr += buffer->content_size + 1;
777 *strings = ptr;
780 *strings_length += buffer->content_size + 1;
781 ++(*varying_count);
784 static void append_transform_feedback_skip_components(const char **varyings,
785 unsigned int *varying_count, char **strings, unsigned int *strings_length,
786 struct wined3d_string_buffer *buffer, unsigned int component_count)
788 unsigned int j;
790 for (j = 0; j < component_count / 4; ++j)
792 string_buffer_sprintf(buffer, "gl_SkipComponents4");
793 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
795 if (component_count % 4)
797 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
798 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
802 static void shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
803 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
804 char *strings, unsigned int *strings_length, GLenum buffer_mode)
806 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
808 count = length = 0;
809 highest_output_slot = 0;
810 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
812 stride = 0;
814 for (i = 0; i < so_desc->element_count; ++i)
816 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
818 highest_output_slot = max(highest_output_slot, e->output_slot);
819 if (e->output_slot != buffer_idx)
820 continue;
822 if (e->stream_idx)
824 FIXME("Unhandled stream %u.\n", e->stream_idx);
825 continue;
828 stride += e->component_count;
830 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
832 append_transform_feedback_skip_components(varyings, &count,
833 &strings, &length, buffer, e->component_count);
834 continue;
837 if (e->component_idx || e->component_count != 4)
839 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
840 continue;
843 string_buffer_sprintf(buffer, "shader_in_out.reg[%u]", e->register_idx);
844 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
847 if (buffer_idx < so_desc->buffer_stride_count
848 && stride < so_desc->buffer_strides[buffer_idx] / 4)
850 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
851 append_transform_feedback_skip_components(varyings, &count,
852 &strings, &length, buffer, component_count);
855 if (highest_output_slot <= buffer_idx)
856 break;
858 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
860 string_buffer_sprintf(buffer, "gl_NextBuffer");
861 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
865 if (varying_count)
866 *varying_count = count;
867 if (strings_length)
868 *strings_length = length;
871 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
872 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
874 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
875 const struct wined3d_gl_info *gl_info = context->gl_info;
876 struct wined3d_string_buffer *buffer;
877 unsigned int i, count, length;
878 const char **varyings;
879 char *strings;
880 GLenum mode;
882 if (!so_desc->element_count)
883 return;
885 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
887 mode = GL_INTERLEAVED_ATTRIBS;
889 else
891 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
893 for (i = 0; i < so_desc->element_count; ++i)
895 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
897 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
898 return;
900 ++element_count[so_desc->elements[i].output_slot];
903 if (element_count[0] == so_desc->element_count)
905 mode = GL_INTERLEAVED_ATTRIBS;
907 else
909 mode = GL_SEPARATE_ATTRIBS;
910 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
912 if (element_count[i] != 1)
913 break;
915 for (; i < ARRAY_SIZE(element_count); ++i)
917 if (element_count[i])
919 FIXME("Only single element per buffer is allowed in separate mode.\n");
920 return;
926 buffer = string_buffer_get(&priv->string_buffers);
928 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode);
930 if (!(varyings = wined3d_calloc(count, sizeof(*varyings))))
932 ERR("Out of memory.\n");
933 string_buffer_release(&priv->string_buffers, buffer);
934 return;
936 if (!(strings = wined3d_calloc(length, sizeof(*strings))))
938 ERR("Out of memory.\n");
939 HeapFree(GetProcessHeap(), 0, varyings);
940 string_buffer_release(&priv->string_buffers, buffer);
941 return;
944 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
945 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
946 checkGLcall("glTransformFeedbackVaryings");
948 HeapFree(GetProcessHeap(), 0, varyings);
949 HeapFree(GetProcessHeap(), 0, strings);
950 string_buffer_release(&priv->string_buffers, buffer);
953 /* Context activation is done by the caller. */
954 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
955 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
957 unsigned int start = ~0U, end = 0;
958 int stack_idx = 0;
959 unsigned int heap_idx = 1;
960 unsigned int idx;
962 if (heap->entries[heap_idx].version <= version) return;
964 idx = heap->entries[heap_idx].idx;
965 if (constant_locations[idx] != -1)
966 start = end = idx;
967 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
969 while (stack_idx >= 0)
971 /* Note that we fall through to the next case statement. */
972 switch(stack[stack_idx])
974 case HEAP_NODE_TRAVERSE_LEFT:
976 unsigned int left_idx = heap_idx << 1;
977 if (left_idx < heap->size && heap->entries[left_idx].version > version)
979 heap_idx = left_idx;
980 idx = heap->entries[heap_idx].idx;
981 if (constant_locations[idx] != -1)
983 if (start > idx)
984 start = idx;
985 if (end < idx)
986 end = idx;
989 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
990 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
991 break;
995 case HEAP_NODE_TRAVERSE_RIGHT:
997 unsigned int right_idx = (heap_idx << 1) + 1;
998 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1000 heap_idx = right_idx;
1001 idx = heap->entries[heap_idx].idx;
1002 if (constant_locations[idx] != -1)
1004 if (start > idx)
1005 start = idx;
1006 if (end < idx)
1007 end = idx;
1010 stack[stack_idx++] = HEAP_NODE_POP;
1011 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1012 break;
1016 case HEAP_NODE_POP:
1017 heap_idx >>= 1;
1018 --stack_idx;
1019 break;
1022 if (start <= end)
1023 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1024 checkGLcall("walk_constant_heap()");
1027 /* Context activation is done by the caller. */
1028 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1029 GLint location, const struct wined3d_vec4 *data)
1031 GLfloat clamped_constant[4];
1033 if (location == -1) return;
1035 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1036 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1037 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1038 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1040 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1043 /* Context activation is done by the caller. */
1044 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1045 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1046 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1048 int stack_idx = 0;
1049 unsigned int heap_idx = 1;
1050 unsigned int idx;
1052 if (heap->entries[heap_idx].version <= version) return;
1054 idx = heap->entries[heap_idx].idx;
1055 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1056 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1058 while (stack_idx >= 0)
1060 /* Note that we fall through to the next case statement. */
1061 switch(stack[stack_idx])
1063 case HEAP_NODE_TRAVERSE_LEFT:
1065 unsigned int left_idx = heap_idx << 1;
1066 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1068 heap_idx = left_idx;
1069 idx = heap->entries[heap_idx].idx;
1070 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1072 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1073 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1074 break;
1078 case HEAP_NODE_TRAVERSE_RIGHT:
1080 unsigned int right_idx = (heap_idx << 1) + 1;
1081 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1083 heap_idx = right_idx;
1084 idx = heap->entries[heap_idx].idx;
1085 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1087 stack[stack_idx++] = HEAP_NODE_POP;
1088 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1089 break;
1093 case HEAP_NODE_POP:
1094 heap_idx >>= 1;
1095 --stack_idx;
1096 break;
1099 checkGLcall("walk_constant_heap_clamped()");
1102 /* Context activation is done by the caller. */
1103 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1104 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1105 unsigned char *stack, unsigned int version)
1107 const struct wined3d_shader_lconst *lconst;
1109 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1110 if (shader->reg_maps.shader_version.major == 1
1111 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1112 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1113 else
1114 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1116 if (!shader->load_local_constsF)
1118 TRACE("No need to load local float constants for this shader.\n");
1119 return;
1122 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1123 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1125 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1127 checkGLcall("glUniform4fv()");
1130 /* Context activation is done by the caller. */
1131 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1132 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1134 unsigned int i;
1135 struct list* ptr;
1137 for (i = 0; constants_set; constants_set >>= 1, ++i)
1139 if (!(constants_set & 1)) continue;
1141 /* We found this uniform name in the program - go ahead and send the data */
1142 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1145 /* Load immediate constants */
1146 ptr = list_head(&shader->constantsI);
1147 while (ptr)
1149 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1150 unsigned int idx = lconst->idx;
1151 const GLint *values = (const GLint *)lconst->value;
1153 /* We found this uniform name in the program - go ahead and send the data */
1154 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1155 ptr = list_next(&shader->constantsI, ptr);
1157 checkGLcall("glUniform4iv()");
1160 /* Context activation is done by the caller. */
1161 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1162 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1164 unsigned int i;
1165 struct list* ptr;
1167 for (i = 0; constants_set; constants_set >>= 1, ++i)
1169 if (!(constants_set & 1)) continue;
1171 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1174 /* Load immediate constants */
1175 ptr = list_head(&shader->constantsB);
1176 while (ptr)
1178 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1179 unsigned int idx = lconst->idx;
1180 const GLint *values = (const GLint *)lconst->value;
1182 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1183 ptr = list_next(&shader->constantsB, ptr);
1185 checkGLcall("glUniform1iv()");
1188 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1190 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1193 /* Context activation is done by the caller (state handler). */
1194 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1195 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1197 struct
1199 float sx, sy;
1201 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1202 UINT fixup = ps->np2_fixup_info->active;
1203 UINT i;
1205 for (i = 0; fixup; fixup >>= 1, ++i)
1207 const struct wined3d_texture *tex = state->textures[i];
1208 unsigned char idx = ps->np2_fixup_info->idx[i];
1210 if (!tex)
1212 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1213 continue;
1216 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1217 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1220 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1223 /* Taken and adapted from Mesa. */
1224 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1226 float pos, neg, t, det;
1227 struct wined3d_matrix temp;
1229 /* Calculate the determinant of upper left 3x3 submatrix and
1230 * determine if the matrix is singular. */
1231 pos = neg = 0.0f;
1232 t = in->_11 * in->_22 * in->_33;
1233 if (t >= 0.0f)
1234 pos += t;
1235 else
1236 neg += t;
1238 t = in->_21 * in->_32 * in->_13;
1239 if (t >= 0.0f)
1240 pos += t;
1241 else
1242 neg += t;
1243 t = in->_31 * in->_12 * in->_23;
1244 if (t >= 0.0f)
1245 pos += t;
1246 else
1247 neg += t;
1249 t = -in->_31 * in->_22 * in->_13;
1250 if (t >= 0.0f)
1251 pos += t;
1252 else
1253 neg += t;
1254 t = -in->_21 * in->_12 * in->_33;
1255 if (t >= 0.0f)
1256 pos += t;
1257 else
1258 neg += t;
1260 t = -in->_11 * in->_32 * in->_23;
1261 if (t >= 0.0f)
1262 pos += t;
1263 else
1264 neg += t;
1266 det = pos + neg;
1268 if (fabsf(det) < 1e-25f)
1269 return FALSE;
1271 det = 1.0f / det;
1272 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1273 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1274 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1275 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1276 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1277 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1278 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1279 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1280 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1282 *out = temp;
1283 return TRUE;
1286 static void swap_rows(float **a, float **b)
1288 float *tmp = *a;
1290 *a = *b;
1291 *b = tmp;
1294 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
1296 float wtmp[4][8];
1297 float m0, m1, m2, m3, s;
1298 float *r0, *r1, *r2, *r3;
1300 r0 = wtmp[0];
1301 r1 = wtmp[1];
1302 r2 = wtmp[2];
1303 r3 = wtmp[3];
1305 r0[0] = m->_11;
1306 r0[1] = m->_12;
1307 r0[2] = m->_13;
1308 r0[3] = m->_14;
1309 r0[4] = 1.0f;
1310 r0[5] = r0[6] = r0[7] = 0.0f;
1312 r1[0] = m->_21;
1313 r1[1] = m->_22;
1314 r1[2] = m->_23;
1315 r1[3] = m->_24;
1316 r1[5] = 1.0f;
1317 r1[4] = r1[6] = r1[7] = 0.0f;
1319 r2[0] = m->_31;
1320 r2[1] = m->_32;
1321 r2[2] = m->_33;
1322 r2[3] = m->_34;
1323 r2[6] = 1.0f;
1324 r2[4] = r2[5] = r2[7] = 0.0f;
1326 r3[0] = m->_41;
1327 r3[1] = m->_42;
1328 r3[2] = m->_43;
1329 r3[3] = m->_44;
1330 r3[7] = 1.0f;
1331 r3[4] = r3[5] = r3[6] = 0.0f;
1333 /* Choose pivot - or die. */
1334 if (fabsf(r3[0]) > fabsf(r2[0]))
1335 swap_rows(&r3, &r2);
1336 if (fabsf(r2[0]) > fabsf(r1[0]))
1337 swap_rows(&r2, &r1);
1338 if (fabsf(r1[0]) > fabsf(r0[0]))
1339 swap_rows(&r1, &r0);
1340 if (r0[0] == 0.0f)
1341 return FALSE;
1343 /* Eliminate first variable. */
1344 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1345 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1346 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1347 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1348 s = r0[4];
1349 if (s != 0.0f)
1351 r1[4] -= m1 * s;
1352 r2[4] -= m2 * s;
1353 r3[4] -= m3 * s;
1355 s = r0[5];
1356 if (s != 0.0f)
1358 r1[5] -= m1 * s;
1359 r2[5] -= m2 * s;
1360 r3[5] -= m3 * s;
1362 s = r0[6];
1363 if (s != 0.0f)
1365 r1[6] -= m1 * s;
1366 r2[6] -= m2 * s;
1367 r3[6] -= m3 * s;
1369 s = r0[7];
1370 if (s != 0.0f)
1372 r1[7] -= m1 * s;
1373 r2[7] -= m2 * s;
1374 r3[7] -= m3 * s;
1377 /* Choose pivot - or die. */
1378 if (fabsf(r3[1]) > fabsf(r2[1]))
1379 swap_rows(&r3, &r2);
1380 if (fabsf(r2[1]) > fabsf(r1[1]))
1381 swap_rows(&r2, &r1);
1382 if (r1[1] == 0.0f)
1383 return FALSE;
1385 /* Eliminate second variable. */
1386 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1387 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1388 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1389 s = r1[4];
1390 if (s != 0.0f)
1392 r2[4] -= m2 * s;
1393 r3[4] -= m3 * s;
1395 s = r1[5];
1396 if (s != 0.0f)
1398 r2[5] -= m2 * s;
1399 r3[5] -= m3 * s;
1401 s = r1[6];
1402 if (s != 0.0f)
1404 r2[6] -= m2 * s;
1405 r3[6] -= m3 * s;
1407 s = r1[7];
1408 if (s != 0.0f)
1410 r2[7] -= m2 * s;
1411 r3[7] -= m3 * s;
1414 /* Choose pivot - or die. */
1415 if (fabsf(r3[2]) > fabsf(r2[2]))
1416 swap_rows(&r3, &r2);
1417 if (r2[2] == 0.0f)
1418 return FALSE;
1420 /* Eliminate third variable. */
1421 m3 = r3[2] / r2[2];
1422 r3[3] -= m3 * r2[3];
1423 r3[4] -= m3 * r2[4];
1424 r3[5] -= m3 * r2[5];
1425 r3[6] -= m3 * r2[6];
1426 r3[7] -= m3 * r2[7];
1428 /* Last check. */
1429 if (r3[3] == 0.0f)
1430 return FALSE;
1432 /* Back substitute row 3. */
1433 s = 1.0f / r3[3];
1434 r3[4] *= s;
1435 r3[5] *= s;
1436 r3[6] *= s;
1437 r3[7] *= s;
1439 /* Back substitute row 2. */
1440 m2 = r2[3];
1441 s = 1.0f / r2[2];
1442 r2[4] = s * (r2[4] - r3[4] * m2);
1443 r2[5] = s * (r2[5] - r3[5] * m2);
1444 r2[6] = s * (r2[6] - r3[6] * m2);
1445 r2[7] = s * (r2[7] - r3[7] * m2);
1446 m1 = r1[3];
1447 r1[4] -= r3[4] * m1;
1448 r1[5] -= r3[5] * m1;
1449 r1[6] -= r3[6] * m1;
1450 r1[7] -= r3[7] * m1;
1451 m0 = r0[3];
1452 r0[4] -= r3[4] * m0;
1453 r0[5] -= r3[5] * m0;
1454 r0[6] -= r3[6] * m0;
1455 r0[7] -= r3[7] * m0;
1457 /* Back substitute row 1. */
1458 m1 = r1[2];
1459 s = 1.0f / r1[1];
1460 r1[4] = s * (r1[4] - r2[4] * m1);
1461 r1[5] = s * (r1[5] - r2[5] * m1);
1462 r1[6] = s * (r1[6] - r2[6] * m1);
1463 r1[7] = s * (r1[7] - r2[7] * m1);
1464 m0 = r0[2];
1465 r0[4] -= r2[4] * m0;
1466 r0[5] -= r2[5] * m0;
1467 r0[6] -= r2[6] * m0;
1468 r0[7] -= r2[7] * m0;
1470 /* Back substitute row 0. */
1471 m0 = r0[1];
1472 s = 1.0f / r0[0];
1473 r0[4] = s * (r0[4] - r1[4] * m0);
1474 r0[5] = s * (r0[5] - r1[5] * m0);
1475 r0[6] = s * (r0[6] - r1[6] * m0);
1476 r0[7] = s * (r0[7] - r1[7] * m0);
1478 out->_11 = r0[4];
1479 out->_12 = r0[5];
1480 out->_13 = r0[6];
1481 out->_14 = r0[7];
1482 out->_21 = r1[4];
1483 out->_22 = r1[5];
1484 out->_23 = r1[6];
1485 out->_24 = r1[7];
1486 out->_31 = r2[4];
1487 out->_32 = r2[5];
1488 out->_33 = r2[6];
1489 out->_34 = r2[7];
1490 out->_41 = r3[4];
1491 out->_42 = r3[5];
1492 out->_43 = r3[6];
1493 out->_44 = r3[7];
1495 return TRUE;
1498 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1499 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1501 const struct wined3d_gl_info *gl_info = context->gl_info;
1502 float mat[3 * 3];
1503 struct wined3d_matrix mv;
1504 unsigned int i, j;
1506 if (prog->vs.normal_matrix_location == -1)
1507 return;
1509 get_modelview_matrix(context, state, 0, &mv);
1510 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1511 invert_matrix_3d(&mv, &mv);
1512 else
1513 invert_matrix(&mv, &mv);
1514 /* Tests show that singular modelview matrices are used unchanged as normal
1515 * matrices on D3D3 and older. There seems to be no clearly consistent
1516 * behavior on newer D3D versions so always follow older ddraw behavior. */
1517 for (i = 0; i < 3; ++i)
1518 for (j = 0; j < 3; ++j)
1519 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1521 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1522 checkGLcall("glUniformMatrix3fv");
1525 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1526 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1528 const struct wined3d_gl_info *gl_info = context->gl_info;
1529 struct wined3d_matrix mat;
1531 if (tex >= MAX_TEXTURES)
1532 return;
1533 if (prog->vs.texture_matrix_location[tex] == -1)
1534 return;
1536 get_texture_matrix(context, state, tex, &mat);
1537 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1538 checkGLcall("glUniformMatrix4fv");
1541 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1542 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1544 const struct wined3d_gl_info *gl_info = context->gl_info;
1546 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1548 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1549 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1551 else
1553 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1555 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1557 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1558 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1559 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1560 checkGLcall("setting FFP material uniforms");
1563 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1564 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1566 const struct wined3d_gl_info *gl_info = context->gl_info;
1567 struct wined3d_color color;
1569 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1570 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1571 checkGLcall("glUniform3fv");
1574 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1575 const struct wined3d_matrix *src2)
1577 struct wined3d_vec4 temp;
1579 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1580 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1581 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1582 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1584 *dest = temp;
1587 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1588 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1589 struct glsl_shader_prog_link *prog)
1591 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1592 const struct wined3d_gl_info *gl_info = context->gl_info;
1593 struct wined3d_vec4 vec4;
1595 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1596 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1597 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1599 switch (light_info->OriginalParms.type)
1601 case WINED3D_LIGHT_POINT:
1602 multiply_vector_matrix(&vec4, &light_info->position, view);
1603 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1604 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1605 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1606 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1607 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1608 break;
1610 case WINED3D_LIGHT_SPOT:
1611 multiply_vector_matrix(&vec4, &light_info->position, view);
1612 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1614 multiply_vector_matrix(&vec4, &light_info->direction, view);
1615 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 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].falloff, light_info->OriginalParms.falloff));
1619 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1620 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1621 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1622 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1623 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1624 break;
1626 case WINED3D_LIGHT_DIRECTIONAL:
1627 multiply_vector_matrix(&vec4, &light_info->direction, view);
1628 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1629 break;
1631 case WINED3D_LIGHT_PARALLELPOINT:
1632 multiply_vector_matrix(&vec4, &light_info->position, view);
1633 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1634 break;
1636 default:
1637 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1639 checkGLcall("setting FFP lights uniforms");
1642 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1643 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1645 const struct wined3d_gl_info *gl_info = context->gl_info;
1646 float min, max;
1647 float size, att[3];
1649 get_pointsize_minmax(context, state, &min, &max);
1651 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1652 checkGLcall("glUniform1f");
1653 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1654 checkGLcall("glUniform1f");
1656 get_pointsize(context, state, &size, att);
1658 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1659 checkGLcall("glUniform1f");
1660 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1661 checkGLcall("glUniform1f");
1662 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1663 checkGLcall("glUniform1f");
1664 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1665 checkGLcall("glUniform1f");
1668 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1669 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1671 const struct wined3d_gl_info *gl_info = context->gl_info;
1672 struct wined3d_color color;
1673 float start, end, scale;
1674 union
1676 DWORD d;
1677 float f;
1678 } tmpvalue;
1680 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1681 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1682 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1683 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1684 get_fog_start_end(context, state, &start, &end);
1685 scale = 1.0f / (end - start);
1686 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1687 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1688 checkGLcall("fog emulation uniforms");
1691 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1692 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1694 const struct wined3d_gl_info *gl_info = context->gl_info;
1695 struct wined3d_vec4 plane;
1697 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1698 if (!use_vs(state))
1699 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1700 else
1701 plane = state->clip_planes[index];
1703 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1706 /* Context activation is done by the caller (state handler). */
1707 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1708 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1710 struct wined3d_color float_key[2];
1711 const struct wined3d_texture *texture = state->textures[0];
1713 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1714 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1717 /* Context activation is done by the caller (state handler). */
1718 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1719 const struct wined3d_state *state)
1721 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1722 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1723 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1724 const struct wined3d_gl_info *gl_info = context->gl_info;
1725 struct shader_glsl_priv *priv = shader_priv;
1726 float position_fixup[4];
1727 DWORD update_mask;
1729 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1730 UINT constant_version;
1731 int i;
1733 if (!prog) {
1734 /* No GLSL program set - nothing to do. */
1735 return;
1737 constant_version = prog->constant_version;
1738 update_mask = context->constant_update_mask & prog->constant_update_mask;
1740 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1741 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1742 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1744 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1745 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1746 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1748 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1749 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1750 vshader->reg_maps.boolean_constants);
1752 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1754 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1755 shader_glsl_clip_plane_uniform(context, state, i, prog);
1758 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1759 shader_glsl_pointsize_uniform(context, state, prog);
1761 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1763 shader_get_position_fixup(context, state, position_fixup);
1764 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1765 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1766 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1767 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1768 else
1769 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1770 checkGLcall("glUniform4fv");
1773 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1775 struct wined3d_matrix mat;
1777 get_modelview_matrix(context, state, 0, &mat);
1778 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1779 checkGLcall("glUniformMatrix4fv");
1781 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1784 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1786 struct wined3d_matrix mat;
1788 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1790 if (prog->vs.modelview_matrix_location[i] == -1)
1791 break;
1793 get_modelview_matrix(context, state, i, &mat);
1794 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1795 checkGLcall("glUniformMatrix4fv");
1799 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1801 struct wined3d_matrix projection;
1803 get_projection_matrix(context, state, &projection);
1804 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1805 checkGLcall("glUniformMatrix4fv");
1808 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1810 for (i = 0; i < MAX_TEXTURES; ++i)
1811 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1814 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1815 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1817 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1819 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1820 DWORD point_count = 0;
1821 DWORD spot_count = 0;
1822 DWORD directional_count = 0;
1823 DWORD parallel_point_count = 0;
1825 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1827 if (!state->lights[i])
1828 continue;
1830 switch (state->lights[i]->OriginalParms.type)
1832 case WINED3D_LIGHT_POINT:
1833 ++point_count;
1834 break;
1835 case WINED3D_LIGHT_SPOT:
1836 ++spot_count;
1837 break;
1838 case WINED3D_LIGHT_DIRECTIONAL:
1839 ++directional_count;
1840 break;
1841 case WINED3D_LIGHT_PARALLELPOINT:
1842 ++parallel_point_count;
1843 break;
1844 default:
1845 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1846 break;
1849 point_idx = 0;
1850 spot_idx = point_idx + point_count;
1851 directional_idx = spot_idx + spot_count;
1852 parallel_point_idx = directional_idx + directional_count;
1854 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1855 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1857 const struct wined3d_light_info *light_info = state->lights[i];
1858 unsigned int idx;
1860 if (!light_info)
1861 continue;
1863 switch (light_info->OriginalParms.type)
1865 case WINED3D_LIGHT_POINT:
1866 idx = point_idx++;
1867 break;
1868 case WINED3D_LIGHT_SPOT:
1869 idx = spot_idx++;
1870 break;
1871 case WINED3D_LIGHT_DIRECTIONAL:
1872 idx = directional_idx++;
1873 break;
1874 case WINED3D_LIGHT_PARALLELPOINT:
1875 idx = parallel_point_idx++;
1876 break;
1877 default:
1878 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1879 continue;
1881 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1885 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1886 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1887 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1889 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1890 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1891 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1893 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1894 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1895 pshader->reg_maps.boolean_constants);
1897 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1899 for (i = 0; i < MAX_TEXTURES; ++i)
1901 if (prog->ps.bumpenv_mat_location[i] == -1)
1902 continue;
1904 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1905 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1907 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1909 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1910 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1911 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1912 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1916 checkGLcall("bump env uniforms");
1919 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1921 const struct wined3d_vec4 correction_params =
1923 /* Position is relative to the framebuffer, not the viewport. */
1924 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1925 context->render_offscreen ? 1.0f : -1.0f,
1926 0.0f,
1927 0.0f,
1930 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1933 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1934 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1935 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1936 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1938 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1940 struct wined3d_color color;
1942 if (prog->ps.tex_factor_location != -1)
1944 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1945 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1948 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1949 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1950 else
1951 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1953 for (i = 0; i < MAX_TEXTURES; ++i)
1955 if (prog->ps.tss_constant_location[i] == -1)
1956 continue;
1958 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1959 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1962 checkGLcall("fixed function uniforms");
1965 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1966 shader_glsl_load_fog_uniform(context, state, prog);
1968 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1970 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1972 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1973 checkGLcall("alpha test emulation uniform");
1976 if (priv->next_constant_version == UINT_MAX)
1978 TRACE("Max constant version reached, resetting to 0.\n");
1979 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1980 priv->next_constant_version = 1;
1982 else
1984 prog->constant_version = priv->next_constant_version++;
1988 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1990 struct constant_entry *entries = heap->entries;
1991 unsigned int *positions = heap->positions;
1992 unsigned int heap_idx, parent_idx;
1994 if (!heap->contained[idx])
1996 heap_idx = heap->size++;
1997 heap->contained[idx] = TRUE;
1999 else
2001 heap_idx = positions[idx];
2004 while (heap_idx > 1)
2006 parent_idx = heap_idx >> 1;
2008 if (new_version <= entries[parent_idx].version) break;
2010 entries[heap_idx] = entries[parent_idx];
2011 positions[entries[parent_idx].idx] = heap_idx;
2012 heap_idx = parent_idx;
2015 entries[heap_idx].version = new_version;
2016 entries[heap_idx].idx = idx;
2017 positions[idx] = heap_idx;
2020 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2022 struct shader_glsl_priv *priv = device->shader_priv;
2023 struct constant_heap *heap = &priv->vconst_heap;
2024 UINT i;
2026 for (i = start; i < count + start; ++i)
2028 update_heap_entry(heap, i, priv->next_constant_version);
2032 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2034 struct shader_glsl_priv *priv = device->shader_priv;
2035 struct constant_heap *heap = &priv->pconst_heap;
2036 UINT i;
2038 for (i = start; i < count + start; ++i)
2040 update_heap_entry(heap, i, priv->next_constant_version);
2044 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2046 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2047 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2048 if(shader_major > 3) return ret;
2050 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2051 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2052 return ret;
2055 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2057 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2060 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2062 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2063 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2066 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2068 return shader_glsl_get_version(gl_info) >= 150;
2071 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2073 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2076 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2077 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2079 va_list args;
2080 int ret;
2082 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2083 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2084 for (;;)
2086 va_start(args, format);
2087 ret = shader_vaddline(buffer, format, args);
2088 va_end(args);
2089 if (!ret)
2090 return;
2091 if (!string_buffer_resize(buffer, ret))
2092 return;
2096 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2097 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2099 va_list args;
2100 int ret;
2102 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2103 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2104 for (;;)
2106 va_start(args, format);
2107 ret = shader_vaddline(buffer, format, args);
2108 va_end(args);
2109 if (!ret)
2110 return;
2111 if (!string_buffer_resize(buffer, ret))
2112 return;
2116 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2118 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2121 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2123 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2126 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2127 struct wined3d_string_buffer *buffer, unsigned int element_count)
2129 if (shader_glsl_use_interface_blocks(gl_info))
2130 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2131 else
2132 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2135 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2136 struct wined3d_string_buffer *buffer, unsigned int element_count)
2138 if (shader_glsl_use_interface_blocks(gl_info))
2139 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2140 else
2141 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2144 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2146 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2149 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2151 switch (primitive_type)
2153 case WINED3D_PT_POINTLIST:
2154 return "points";
2156 case WINED3D_PT_LINELIST:
2157 return "lines";
2159 case WINED3D_PT_LINESTRIP:
2160 return "line_strip";
2162 case WINED3D_PT_TRIANGLELIST:
2163 return "triangles";
2165 case WINED3D_PT_TRIANGLESTRIP:
2166 return "triangle_strip";
2168 case WINED3D_PT_LINELIST_ADJ:
2169 return "lines_adjacency";
2171 case WINED3D_PT_TRIANGLELIST_ADJ:
2172 return "triangles_adjacency";
2174 default:
2175 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2176 return "";
2180 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2182 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2183 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2184 DWORD input_reg_used = shader->u.ps.input_reg_used;
2185 unsigned int i;
2187 if (reg_maps->shader_version.major < 3)
2188 return input_reg_used & (1u << idx);
2190 for (i = 0; i < input_signature->element_count; ++i)
2192 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2194 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2195 continue;
2197 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2198 && input->semantic_idx == idx)
2199 return input_reg_used & (1u << input->register_idx);
2201 return FALSE;
2204 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2205 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2207 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2209 if (version->major >= 4)
2210 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2211 else
2212 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2215 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2216 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2217 unsigned int index)
2219 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2220 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2221 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2222 index, scalar_type, scalar_type, index);
2225 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2226 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2228 unsigned int index = e->register_idx;
2230 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2232 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2233 index);
2234 return;
2236 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2238 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2239 index);
2240 return;
2242 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2243 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2245 if (shader_glsl_use_explicit_attrib_location(gl_info))
2246 shader_addline(buffer, "layout(location = %u) ", index);
2248 switch (e->component_type)
2250 case WINED3D_TYPE_UINT:
2251 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
2252 break;
2253 case WINED3D_TYPE_INT:
2254 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
2255 break;
2257 default:
2258 FIXME("Unhandled type %#x.\n", e->component_type);
2259 /* Fall through. */
2260 case WINED3D_TYPE_UNKNOWN:
2261 case WINED3D_TYPE_FLOAT:
2262 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2263 break;
2267 /** Generate the variable & register declarations for the GLSL output target */
2268 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2269 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2270 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2272 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2273 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2274 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2275 const struct wined3d_gl_info *gl_info = context->gl_info;
2276 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2277 unsigned int uniform_block_base, uniform_block_count;
2278 const struct wined3d_shader_lconst *lconst;
2279 const char *prefix;
2280 unsigned int i;
2281 DWORD map;
2283 prefix = shader_glsl_get_prefix(version->type);
2285 /* Prototype the subroutines */
2286 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2288 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2291 /* Declare the constants (aka uniforms) */
2292 if (shader->limits->constant_float > 0)
2294 unsigned max_constantsF;
2296 /* Unless the shader uses indirect addressing, always declare the
2297 * maximum array size and ignore that we need some uniforms privately.
2298 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2299 * and immediate values, still declare VC[256]. If the shader needs
2300 * more uniforms than we have it won't work in any case. If it uses
2301 * less, the compiler will figure out which uniforms are really used
2302 * and strip them out. This allows a shader to use c255 on a dx9 card,
2303 * as long as it doesn't also use all the other constants.
2305 * If the shader uses indirect addressing the compiler must assume
2306 * that all declared uniforms are used. In this case, declare only the
2307 * amount that we're assured to have.
2309 * Thus we run into problems in these two cases:
2310 * 1) The shader really uses more uniforms than supported.
2311 * 2) The shader uses indirect addressing, less constants than
2312 * supported, but uses a constant index > #supported consts. */
2313 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2315 /* No indirect addressing here. */
2316 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2318 else
2320 if (reg_maps->usesrelconstF)
2322 /* Subtract the other potential uniforms from the max
2323 * available (bools, ints, and 1 row of projection matrix).
2324 * Subtract another uniform for immediate values, which have
2325 * to be loaded via uniform by the driver as well. The shader
2326 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2327 * shader code, so one vec4 should be enough. (Unfortunately
2328 * the Nvidia driver doesn't store 128 and -128 in one float).
2330 * Writing gl_ClipVertex requires one uniform for each
2331 * clipplane as well. */
2332 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2333 if (vs_args->clip_enabled)
2334 max_constantsF -= gl_info->limits.user_clip_distances;
2335 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2336 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2337 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2338 * for now take this into account when calculating the number of available constants
2340 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2341 /* Set by driver quirks in directx.c */
2342 max_constantsF -= gl_info->reserved_glsl_constants;
2344 if (max_constantsF < shader->limits->constant_float)
2346 static unsigned int once;
2348 if (!once++)
2349 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2350 " it may not render correctly.\n");
2351 else
2352 WARN("The hardware does not support enough uniform components to run this shader.\n");
2355 else
2357 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2360 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2361 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2364 /* Always declare the full set of constants, the compiler can remove the
2365 * unused ones because d3d doesn't (yet) support indirect int and bool
2366 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2367 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2368 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2370 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2371 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2373 /* Declare immediate constant buffer */
2374 if (reg_maps->icb)
2375 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2377 /* Declare constant buffers */
2378 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2379 &uniform_block_base, &uniform_block_count);
2380 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2382 if (reg_maps->cb_sizes[i])
2384 shader_addline(buffer, "layout(std140");
2385 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2386 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2387 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2388 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2392 /* Declare texture samplers */
2393 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2395 struct wined3d_shader_sampler_map_entry *entry;
2396 const char *sampler_type_prefix, *sampler_type;
2397 BOOL shadow_sampler, tex_rect;
2399 entry = &reg_maps->sampler_map.entries[i];
2401 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2403 ERR("Invalid resource index %u.\n", entry->resource_idx);
2404 continue;
2407 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2409 case WINED3D_DATA_FLOAT:
2410 case WINED3D_DATA_UNORM:
2411 case WINED3D_DATA_SNORM:
2412 sampler_type_prefix = "";
2413 break;
2415 case WINED3D_DATA_INT:
2416 sampler_type_prefix = "i";
2417 break;
2419 case WINED3D_DATA_UINT:
2420 sampler_type_prefix = "u";
2421 break;
2423 default:
2424 sampler_type_prefix = "";
2425 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2426 break;
2429 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2430 switch (reg_maps->resource_info[entry->resource_idx].type)
2432 case WINED3D_SHADER_RESOURCE_BUFFER:
2433 sampler_type = "samplerBuffer";
2434 break;
2436 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2437 if (shadow_sampler)
2438 sampler_type = "sampler1DShadow";
2439 else
2440 sampler_type = "sampler1D";
2441 break;
2443 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2444 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2445 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2446 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2447 if (shadow_sampler)
2449 if (tex_rect)
2450 sampler_type = "sampler2DRectShadow";
2451 else
2452 sampler_type = "sampler2DShadow";
2454 else
2456 if (tex_rect)
2457 sampler_type = "sampler2DRect";
2458 else
2459 sampler_type = "sampler2D";
2461 break;
2463 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2464 if (shadow_sampler)
2465 FIXME("Unsupported 3D shadow sampler.\n");
2466 sampler_type = "sampler3D";
2467 break;
2469 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2470 if (shadow_sampler)
2471 sampler_type = "samplerCubeShadow";
2472 else
2473 sampler_type = "samplerCube";
2474 break;
2476 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2477 if (shadow_sampler)
2478 sampler_type = "sampler2DArrayShadow";
2479 else
2480 sampler_type = "sampler2DArray";
2481 break;
2483 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2484 if (shadow_sampler)
2485 sampler_type = "samplerCubeArrayShadow";
2486 else
2487 sampler_type = "samplerCubeArray";
2488 break;
2490 default:
2491 sampler_type = "unsupported_sampler";
2492 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2493 break;
2496 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2497 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2498 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2499 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2502 /* Declare images */
2503 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2505 const char *image_type_prefix, *image_type, *read_format;
2507 if (!reg_maps->uav_resource_info[i].type)
2508 continue;
2510 switch (reg_maps->uav_resource_info[i].data_type)
2512 case WINED3D_DATA_FLOAT:
2513 case WINED3D_DATA_UNORM:
2514 case WINED3D_DATA_SNORM:
2515 image_type_prefix = "";
2516 read_format = "r32f";
2517 break;
2519 case WINED3D_DATA_INT:
2520 image_type_prefix = "i";
2521 read_format = "r32i";
2522 break;
2524 case WINED3D_DATA_UINT:
2525 image_type_prefix = "u";
2526 read_format = "r32ui";
2527 break;
2529 default:
2530 image_type_prefix = "";
2531 read_format = "";
2532 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2533 break;
2536 switch (reg_maps->uav_resource_info[i].type)
2538 case WINED3D_SHADER_RESOURCE_BUFFER:
2539 image_type = "imageBuffer";
2540 break;
2542 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2543 image_type = "image2D";
2544 break;
2546 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2547 image_type = "image3D";
2548 break;
2550 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2551 image_type = "image2DArray";
2552 break;
2554 default:
2555 image_type = "unsupported_image";
2556 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2557 break;
2560 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2561 shader_addline(buffer, "layout(binding = %u)\n", i);
2562 if (reg_maps->uav_read_mask & (1u << i))
2563 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2564 read_format, image_type_prefix, image_type, prefix, i);
2565 else
2566 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2567 image_type_prefix, image_type, prefix, i);
2569 if (reg_maps->uav_counter_mask & (1u << i))
2570 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2571 i, prefix, i);
2574 /* Declare address variables */
2575 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2577 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2580 /* Declare output register temporaries */
2581 if (shader->limits->packed_output)
2582 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2584 /* Declare temporary variables */
2585 if (reg_maps->temporary_count)
2587 for (i = 0; i < reg_maps->temporary_count; ++i)
2588 shader_addline(buffer, "vec4 R%u;\n", i);
2590 else if (version->major < 4)
2592 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2594 if (map & 1)
2595 shader_addline(buffer, "vec4 R%u;\n", i);
2599 /* Declare indexable temporary variables */
2600 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2602 if (idx_temp_reg->component_count != 4)
2603 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2604 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2607 /* Declare loop registers aLx */
2608 if (version->major < 4)
2610 for (i = 0; i < reg_maps->loop_depth; ++i)
2612 shader_addline(buffer, "int aL%u;\n", i);
2613 shader_addline(buffer, "int tmpInt%u;\n", i);
2617 /* Temporary variables for matrix operations */
2618 shader_addline(buffer, "vec4 tmp0;\n");
2619 shader_addline(buffer, "vec4 tmp1;\n");
2621 if (!shader->load_local_constsF)
2623 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2625 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2626 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2627 shader_addline(buffer, ";\n");
2632 /*****************************************************************************
2633 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2635 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2636 ****************************************************************************/
2638 /* Prototypes */
2639 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2640 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2642 /** Used for opcode modifiers - They multiply the result by the specified amount */
2643 static const char * const shift_glsl_tab[] = {
2644 "", /* 0 (none) */
2645 "2.0 * ", /* 1 (x2) */
2646 "4.0 * ", /* 2 (x4) */
2647 "8.0 * ", /* 3 (x8) */
2648 "16.0 * ", /* 4 (x16) */
2649 "32.0 * ", /* 5 (x32) */
2650 "", /* 6 (x64) */
2651 "", /* 7 (x128) */
2652 "", /* 8 (d256) */
2653 "", /* 9 (d128) */
2654 "", /* 10 (d64) */
2655 "", /* 11 (d32) */
2656 "0.0625 * ", /* 12 (d16) */
2657 "0.125 * ", /* 13 (d8) */
2658 "0.25 * ", /* 14 (d4) */
2659 "0.5 * " /* 15 (d2) */
2662 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2663 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2664 const char *in_reg, const char *in_regswizzle, char *out_str)
2666 switch (src_modifier)
2668 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2669 case WINED3DSPSM_DW:
2670 case WINED3DSPSM_NONE:
2671 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2672 break;
2673 case WINED3DSPSM_NEG:
2674 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2675 break;
2676 case WINED3DSPSM_NOT:
2677 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2678 break;
2679 case WINED3DSPSM_BIAS:
2680 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2681 break;
2682 case WINED3DSPSM_BIASNEG:
2683 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2684 break;
2685 case WINED3DSPSM_SIGN:
2686 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2687 break;
2688 case WINED3DSPSM_SIGNNEG:
2689 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2690 break;
2691 case WINED3DSPSM_COMP:
2692 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2693 break;
2694 case WINED3DSPSM_X2:
2695 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2696 break;
2697 case WINED3DSPSM_X2NEG:
2698 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2699 break;
2700 case WINED3DSPSM_ABS:
2701 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2702 break;
2703 case WINED3DSPSM_ABSNEG:
2704 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2705 break;
2706 default:
2707 FIXME("Unhandled modifier %u\n", src_modifier);
2708 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2712 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2713 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2715 /* The ARB_shading_language_420pack extension allows swizzle operations on
2716 * scalars. */
2717 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2718 sprintf(register_name, "%s", glsl_variable);
2719 else
2720 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2723 /** Writes the GLSL variable name that corresponds to the register that the
2724 * DX opcode parameter is trying to access */
2725 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2726 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2728 /* oPos, oFog and oPts in D3D */
2729 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2731 const struct wined3d_shader *shader = ins->ctx->shader;
2732 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2733 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2734 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2735 const char *prefix = shader_glsl_get_prefix(version->type);
2736 struct glsl_src_param rel_param0, rel_param1;
2737 char imm_str[4][17];
2739 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2740 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2741 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2742 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2743 *is_color = FALSE;
2745 switch (reg->type)
2747 case WINED3DSPR_TEMP:
2748 sprintf(register_name, "R%u", reg->idx[0].offset);
2749 break;
2751 case WINED3DSPR_INPUT:
2752 case WINED3DSPR_INCONTROLPOINT:
2753 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2755 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2757 if (reg->idx[0].rel_addr)
2758 FIXME("VS3+ input registers relative addressing.\n");
2759 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2760 *is_color = TRUE;
2761 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2762 break;
2765 if (version->type == WINED3D_SHADER_TYPE_HULL
2766 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2767 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2769 if (reg->idx[0].rel_addr)
2771 if (reg->idx[1].rel_addr)
2772 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2773 rel_param0.param_str, reg->idx[0].offset,
2774 rel_param1.param_str, reg->idx[1].offset);
2775 else
2776 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2777 rel_param0.param_str, reg->idx[0].offset,
2778 reg->idx[1].offset);
2780 else if (reg->idx[1].rel_addr)
2781 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2782 rel_param1.param_str, reg->idx[1].offset);
2783 else
2784 sprintf(register_name, "shader_in[%u].reg[%u]",
2785 reg->idx[0].offset, reg->idx[1].offset);
2786 break;
2789 /* pixel shaders >= 3.0 */
2790 if (version->major >= 3)
2792 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2793 unsigned int in_count = vec4_varyings(version->major, gl_info);
2795 if (reg->idx[0].rel_addr)
2797 /* Removing a + 0 would be an obvious optimization, but
2798 * OS X doesn't see the NOP operation there. */
2799 if (idx)
2801 if (needs_legacy_glsl_syntax(gl_info)
2802 && shader->u.ps.declared_in_count > in_count)
2804 sprintf(register_name,
2805 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2806 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2807 prefix, rel_param0.param_str, idx);
2809 else
2811 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2814 else
2816 if (needs_legacy_glsl_syntax(gl_info)
2817 && shader->u.ps.declared_in_count > in_count)
2819 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2820 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2821 prefix, rel_param0.param_str);
2823 else
2825 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2829 else
2831 if (idx == in_count) sprintf(register_name, "gl_Color");
2832 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2833 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2836 else
2838 if (!reg->idx[0].offset)
2839 strcpy(register_name, "ffp_varying_diffuse");
2840 else
2841 strcpy(register_name, "ffp_varying_specular");
2842 break;
2844 break;
2846 case WINED3DSPR_CONST:
2848 /* Relative addressing */
2849 if (reg->idx[0].rel_addr)
2851 if (wined3d_settings.check_float_constants)
2852 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2853 rel_param0.param_str, reg->idx[0].offset,
2854 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2855 prefix, rel_param0.param_str, reg->idx[0].offset);
2856 else if (reg->idx[0].offset)
2857 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2858 else
2859 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2861 else
2863 if (shader_constant_is_local(shader, reg->idx[0].offset))
2864 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2865 else
2866 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2869 break;
2871 case WINED3DSPR_CONSTINT:
2872 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2873 break;
2875 case WINED3DSPR_CONSTBOOL:
2876 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2877 break;
2879 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2880 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2881 sprintf(register_name, "T%u", reg->idx[0].offset);
2882 else
2883 sprintf(register_name, "A%u", reg->idx[0].offset);
2884 break;
2886 case WINED3DSPR_LOOP:
2887 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2888 break;
2890 case WINED3DSPR_SAMPLER:
2891 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2892 break;
2894 case WINED3DSPR_COLOROUT:
2895 if (reg->idx[0].offset >= gl_info->limits.buffers)
2896 WARN("Write to render target %u, only %d supported.\n",
2897 reg->idx[0].offset, gl_info->limits.buffers);
2899 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2900 break;
2902 case WINED3DSPR_RASTOUT:
2903 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2904 break;
2906 case WINED3DSPR_DEPTHOUT:
2907 sprintf(register_name, "gl_FragDepth");
2908 break;
2910 case WINED3DSPR_ATTROUT:
2911 if (!reg->idx[0].offset)
2912 sprintf(register_name, "%s_out[8]", prefix);
2913 else
2914 sprintf(register_name, "%s_out[9]", prefix);
2915 break;
2917 case WINED3DSPR_TEXCRDOUT:
2918 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2919 if (reg->idx[0].rel_addr)
2920 sprintf(register_name, "%s_out[%s + %u]",
2921 prefix, rel_param0.param_str, reg->idx[0].offset);
2922 else
2923 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2924 break;
2926 case WINED3DSPR_MISCTYPE:
2927 if (!reg->idx[0].offset)
2929 /* vPos */
2930 sprintf(register_name, "vpos");
2932 else if (reg->idx[0].offset == 1)
2934 /* Note that gl_FrontFacing is a bool, while vFace is
2935 * a float for which the sign determines front/back */
2936 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2938 else
2940 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2941 sprintf(register_name, "unrecognized_register");
2943 break;
2945 case WINED3DSPR_IMMCONST:
2946 switch (reg->immconst_type)
2948 case WINED3D_IMMCONST_SCALAR:
2949 switch (reg->data_type)
2951 case WINED3D_DATA_FLOAT:
2952 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2953 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
2954 else
2955 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
2956 break;
2957 case WINED3D_DATA_INT:
2958 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2959 break;
2960 case WINED3D_DATA_RESOURCE:
2961 case WINED3D_DATA_SAMPLER:
2962 case WINED3D_DATA_UINT:
2963 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2964 break;
2965 default:
2966 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2967 break;
2969 break;
2971 case WINED3D_IMMCONST_VEC4:
2972 switch (reg->data_type)
2974 case WINED3D_DATA_FLOAT:
2975 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2977 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2978 reg->u.immconst_data[0], reg->u.immconst_data[1],
2979 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2981 else
2983 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
2984 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
2985 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
2986 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
2987 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2988 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2990 break;
2991 case WINED3D_DATA_INT:
2992 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2993 reg->u.immconst_data[0], reg->u.immconst_data[1],
2994 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2995 break;
2996 case WINED3D_DATA_RESOURCE:
2997 case WINED3D_DATA_SAMPLER:
2998 case WINED3D_DATA_UINT:
2999 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3000 reg->u.immconst_data[0], reg->u.immconst_data[1],
3001 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3002 break;
3003 default:
3004 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
3005 break;
3007 break;
3009 default:
3010 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3011 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3013 break;
3015 case WINED3DSPR_CONSTBUFFER:
3016 if (reg->idx[1].rel_addr)
3017 sprintf(register_name, "%s_cb%u[%s + %u]",
3018 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3019 else
3020 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3021 break;
3023 case WINED3DSPR_IMMCONSTBUFFER:
3024 if (reg->idx[0].rel_addr)
3025 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3026 else
3027 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3028 break;
3030 case WINED3DSPR_PRIMID:
3031 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3032 sprintf(register_name, "gl_PrimitiveIDIn");
3033 else
3034 sprintf(register_name, "gl_PrimitiveID");
3035 break;
3037 case WINED3DSPR_IDXTEMP:
3038 if (reg->idx[1].rel_addr)
3039 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3040 else
3041 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3042 break;
3044 case WINED3DSPR_LOCALTHREADINDEX:
3045 shader_glsl_fixup_scalar_register_variable(register_name,
3046 "int(gl_LocalInvocationIndex)", gl_info);
3047 break;
3049 case WINED3DSPR_GSINSTID:
3050 case WINED3DSPR_OUTPOINTID:
3051 shader_glsl_fixup_scalar_register_variable(register_name,
3052 "gl_InvocationID", gl_info);
3053 break;
3055 case WINED3DSPR_THREADID:
3056 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3057 break;
3059 case WINED3DSPR_THREADGROUPID:
3060 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3061 break;
3063 case WINED3DSPR_LOCALTHREADID:
3064 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3065 break;
3067 case WINED3DSPR_FORKINSTID:
3068 case WINED3DSPR_JOININSTID:
3069 shader_glsl_fixup_scalar_register_variable(register_name,
3070 "phase_instance_id", gl_info);
3071 break;
3073 case WINED3DSPR_TESSCOORD:
3074 sprintf(register_name, "gl_TessCoord");
3075 break;
3077 case WINED3DSPR_OUTCONTROLPOINT:
3078 if (reg->idx[0].rel_addr)
3080 if (reg->idx[1].rel_addr)
3081 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3082 rel_param0.param_str, reg->idx[0].offset,
3083 rel_param1.param_str, reg->idx[1].offset);
3084 else
3085 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3086 rel_param0.param_str, reg->idx[0].offset,
3087 reg->idx[1].offset);
3089 else if (reg->idx[1].rel_addr)
3091 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3092 reg->idx[0].offset, rel_param1.param_str,
3093 reg->idx[1].offset);
3095 else
3097 sprintf(register_name, "shader_out[%u].reg[%u]",
3098 reg->idx[0].offset, reg->idx[1].offset);
3100 break;
3102 case WINED3DSPR_PATCHCONST:
3103 if (version->type == WINED3D_SHADER_TYPE_HULL)
3104 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3105 else
3106 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3107 break;
3109 default:
3110 FIXME("Unhandled register type %#x.\n", reg->type);
3111 sprintf(register_name, "unrecognized_register");
3112 break;
3116 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3118 *str++ = '.';
3119 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3120 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3121 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3122 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3123 *str = '\0';
3126 /* Get the GLSL write mask for the destination register */
3127 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3129 DWORD mask = param->write_mask;
3131 if (shader_is_scalar(&param->reg))
3133 mask = WINED3DSP_WRITEMASK_0;
3134 *write_mask = '\0';
3136 else
3138 shader_glsl_write_mask_to_str(mask, write_mask);
3141 return mask;
3144 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3146 unsigned int size = 0;
3148 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3149 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3150 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3151 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3153 return size;
3156 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3157 unsigned int component_idx)
3159 /* swizzle bits fields: wwzzyyxx */
3160 return (swizzle >> (2 * component_idx)) & 0x3;
3163 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3165 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3166 * but addressed as "rgba". To fix this we need to swap the register's x
3167 * and z components. */
3168 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3169 unsigned int i;
3171 *str++ = '.';
3172 for (i = 0; i < 4; ++i)
3174 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3175 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3177 *str = '\0';
3180 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3181 BOOL fixup, DWORD mask, char *swizzle_str)
3183 if (shader_is_scalar(&param->reg))
3184 *swizzle_str = '\0';
3185 else
3186 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3189 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3190 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3192 if (dst_data_type == src_data_type)
3194 string_buffer_sprintf(dst_param, "%s", src_param);
3195 return;
3198 if (src_data_type == WINED3D_DATA_FLOAT)
3200 switch (dst_data_type)
3202 case WINED3D_DATA_INT:
3203 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3204 return;
3205 case WINED3D_DATA_RESOURCE:
3206 case WINED3D_DATA_SAMPLER:
3207 case WINED3D_DATA_UINT:
3208 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3209 return;
3210 default:
3211 break;
3215 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3217 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3218 return;
3221 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3223 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3224 return;
3227 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3228 string_buffer_sprintf(dst_param, "%s", src_param);
3231 /* From a given parameter token, generate the corresponding GLSL string.
3232 * Also, return the actual register name and swizzle in case the
3233 * caller needs this information as well. */
3234 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3235 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3236 enum wined3d_data_type data_type)
3238 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3239 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3240 enum wined3d_data_type param_data_type;
3241 BOOL is_color = FALSE;
3242 char swizzle_str[6];
3244 glsl_src->reg_name[0] = '\0';
3245 glsl_src->param_str[0] = '\0';
3246 swizzle_str[0] = '\0';
3248 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
3249 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3251 switch (wined3d_src->reg.type)
3253 case WINED3DSPR_IMMCONST:
3254 param_data_type = data_type;
3255 break;
3256 case WINED3DSPR_FORKINSTID:
3257 case WINED3DSPR_GSINSTID:
3258 case WINED3DSPR_JOININSTID:
3259 case WINED3DSPR_LOCALTHREADID:
3260 case WINED3DSPR_LOCALTHREADINDEX:
3261 case WINED3DSPR_OUTPOINTID:
3262 case WINED3DSPR_PRIMID:
3263 case WINED3DSPR_THREADGROUPID:
3264 case WINED3DSPR_THREADID:
3265 param_data_type = WINED3D_DATA_INT;
3266 break;
3267 default:
3268 param_data_type = WINED3D_DATA_FLOAT;
3269 break;
3272 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3273 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3275 string_buffer_release(priv->string_buffers, reg_name);
3278 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3279 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3281 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3284 /* From a given parameter token, generate the corresponding GLSL string.
3285 * Also, return the actual register name and swizzle in case the
3286 * caller needs this information as well. */
3287 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3288 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3290 BOOL is_color = FALSE;
3292 glsl_dst->mask_str[0] = '\0';
3293 glsl_dst->reg_name[0] = '\0';
3295 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
3296 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3299 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3300 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3301 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3302 enum wined3d_data_type data_type)
3304 struct glsl_dst_param glsl_dst;
3305 DWORD mask;
3307 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3309 switch (data_type)
3311 case WINED3D_DATA_FLOAT:
3312 shader_addline(buffer, "%s%s = %s(",
3313 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3314 break;
3315 case WINED3D_DATA_INT:
3316 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3317 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3318 break;
3319 case WINED3D_DATA_RESOURCE:
3320 case WINED3D_DATA_SAMPLER:
3321 case WINED3D_DATA_UINT:
3322 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3323 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3324 break;
3325 default:
3326 FIXME("Unhandled data type %#x.\n", data_type);
3327 shader_addline(buffer, "%s%s = %s(",
3328 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3329 break;
3333 return mask;
3336 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3337 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3339 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3342 /** Process GLSL instruction modifiers */
3343 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3345 struct glsl_dst_param dst_param;
3346 DWORD modifiers;
3348 if (!ins->dst_count) return;
3350 modifiers = ins->dst[0].modifiers;
3351 if (!modifiers) return;
3353 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3355 if (modifiers & WINED3DSPDM_SATURATE)
3357 /* _SAT means to clamp the value of the register to between 0 and 1 */
3358 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3359 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3362 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3364 FIXME("_centroid modifier not handled\n");
3367 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3369 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3373 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3375 switch (op)
3377 case WINED3D_SHADER_REL_OP_GT: return ">";
3378 case WINED3D_SHADER_REL_OP_EQ: return "==";
3379 case WINED3D_SHADER_REL_OP_GE: return ">=";
3380 case WINED3D_SHADER_REL_OP_LT: return "<";
3381 case WINED3D_SHADER_REL_OP_NE: return "!=";
3382 case WINED3D_SHADER_REL_OP_LE: return "<=";
3383 default:
3384 FIXME("Unrecognized operator %#x.\n", op);
3385 return "(\?\?)";
3389 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3391 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3394 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3395 unsigned int *coord_size, unsigned int *deriv_size)
3397 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3398 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3400 *coord_size = resource_type_info[resource_type].coord_size;
3401 *deriv_size = *coord_size;
3402 if (is_array)
3403 --(*deriv_size);
3406 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3407 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3409 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3410 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3411 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3412 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3413 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3414 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3415 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3416 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3417 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3418 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3419 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3420 const char *base = "texture", *type_part = "", *suffix = "";
3421 unsigned int coord_size, deriv_size;
3423 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3425 if (resource_type >= ARRAY_SIZE(resource_type_info))
3427 ERR("Unexpected resource type %#x.\n", resource_type);
3428 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3431 /* Note that there's no such thing as a projected cube texture. */
3432 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3433 projected = FALSE;
3435 if (needs_legacy_glsl_syntax(gl_info))
3437 if (shadow)
3438 base = "shadow";
3440 type_part = resource_type_info[resource_type].type_part;
3441 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3442 type_part = "2DRect";
3443 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3444 FIXME("Unhandled resource type %#x.\n", resource_type);
3446 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3448 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3449 suffix = "ARB";
3450 else
3451 FIXME("Unsupported grad function.\n");
3455 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3457 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3458 if (flags & ~texel_fetch_flags)
3459 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3461 base = "texelFetch";
3462 type_part = "";
3465 sample_function->name = string_buffer_get(priv->string_buffers);
3466 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3467 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3469 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3470 if (shadow)
3471 ++coord_size;
3472 sample_function->offset_size = offset ? deriv_size : 0;
3473 sample_function->coord_mask = (1u << coord_size) - 1;
3474 sample_function->deriv_mask = (1u << deriv_size) - 1;
3475 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3478 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3479 struct glsl_sample_function *sample_function)
3481 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3483 string_buffer_release(priv->string_buffers, sample_function->name);
3486 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3487 BOOL sign_fixup, enum fixup_channel_source channel_source)
3489 switch(channel_source)
3491 case CHANNEL_SOURCE_ZERO:
3492 strcat(arguments, "0.0");
3493 break;
3495 case CHANNEL_SOURCE_ONE:
3496 strcat(arguments, "1.0");
3497 break;
3499 case CHANNEL_SOURCE_X:
3500 strcat(arguments, reg_name);
3501 strcat(arguments, ".x");
3502 break;
3504 case CHANNEL_SOURCE_Y:
3505 strcat(arguments, reg_name);
3506 strcat(arguments, ".y");
3507 break;
3509 case CHANNEL_SOURCE_Z:
3510 strcat(arguments, reg_name);
3511 strcat(arguments, ".z");
3512 break;
3514 case CHANNEL_SOURCE_W:
3515 strcat(arguments, reg_name);
3516 strcat(arguments, ".w");
3517 break;
3519 default:
3520 FIXME("Unhandled channel source %#x\n", channel_source);
3521 strcat(arguments, "undefined");
3522 break;
3525 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3528 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3529 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3531 unsigned int mask_size, remaining;
3532 DWORD fixup_mask = 0;
3533 char arguments[256];
3534 char mask_str[6];
3536 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3537 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3538 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3539 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3540 if (!(mask &= fixup_mask))
3541 return;
3543 if (is_complex_fixup(fixup))
3545 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3546 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3547 return;
3550 shader_glsl_write_mask_to_str(mask, mask_str);
3551 mask_size = shader_glsl_get_write_mask_size(mask);
3553 arguments[0] = '\0';
3554 remaining = mask_size;
3555 if (mask & WINED3DSP_WRITEMASK_0)
3557 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3558 if (--remaining) strcat(arguments, ", ");
3560 if (mask & WINED3DSP_WRITEMASK_1)
3562 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3563 if (--remaining) strcat(arguments, ", ");
3565 if (mask & WINED3DSP_WRITEMASK_2)
3567 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3568 if (--remaining) strcat(arguments, ", ");
3570 if (mask & WINED3DSP_WRITEMASK_3)
3572 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3573 if (--remaining) strcat(arguments, ", ");
3576 if (mask_size > 1)
3577 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3578 else
3579 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3582 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3584 char reg_name[256];
3585 BOOL is_color;
3587 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3588 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3591 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3592 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3593 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3594 const char *coord_reg_fmt, ...)
3596 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3597 char dst_swizzle[6];
3598 struct color_fixup_desc fixup;
3599 BOOL np2_fixup = FALSE;
3600 va_list args;
3601 int ret;
3603 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3605 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3606 * We actually rely on it for vertex shaders and SM4+. */
3607 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3609 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3610 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3612 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3613 np2_fixup = TRUE;
3615 else
3617 fixup = COLOR_FIXUP_IDENTITY;
3620 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3622 if (sample_function->output_single_component)
3623 shader_addline(ins->ctx->buffer, "vec4(");
3625 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3626 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3628 for (;;)
3630 va_start(args, coord_reg_fmt);
3631 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3632 va_end(args);
3633 if (!ret)
3634 break;
3635 if (!string_buffer_resize(ins->ctx->buffer, ret))
3636 break;
3639 if (np2_fixup)
3641 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3642 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3644 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3646 case 1:
3647 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3648 idx >> 1, (idx % 2) ? "z" : "x");
3649 break;
3650 case 2:
3651 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3652 idx >> 1, (idx % 2) ? "zw" : "xy");
3653 break;
3654 case 3:
3655 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3656 idx >> 1, (idx % 2) ? "zw" : "xy");
3657 break;
3658 case 4:
3659 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3660 idx >> 1, (idx % 2) ? "zw" : "xy");
3661 break;
3664 if (dx && dy)
3665 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3666 else if (bias)
3667 shader_addline(ins->ctx->buffer, ", %s", bias);
3668 if (sample_function->offset_size)
3670 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3671 shader_addline(ins->ctx->buffer, ", ");
3672 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3674 shader_addline(ins->ctx->buffer, ")");
3676 if (sample_function->output_single_component)
3677 shader_addline(ins->ctx->buffer, ")");
3679 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3681 if (!is_identity_fixup(fixup))
3682 shader_glsl_color_correction(ins, fixup);
3685 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3687 /* Write the final position.
3689 * OpenGL coordinates specify the center of the pixel while D3D coords
3690 * specify the corner. The offsets are stored in z and w in
3691 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3692 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3693 * a MAD. */
3694 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3695 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3697 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3698 * in utils.c
3700 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3701 * shaders are run before the homogeneous divide, so we have to take the w
3702 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3703 * z = z * 2 - w. */
3704 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3707 /*****************************************************************************
3708 * Begin processing individual instruction opcodes
3709 ****************************************************************************/
3711 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3713 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3714 struct glsl_src_param src0_param;
3715 struct glsl_src_param src1_param;
3716 DWORD write_mask;
3717 const char *op;
3719 /* Determine the GLSL operator to use based on the opcode */
3720 switch (ins->handler_idx)
3722 case WINED3DSIH_ADD: op = "+"; break;
3723 case WINED3DSIH_AND: op = "&"; break;
3724 case WINED3DSIH_DIV: op = "/"; break;
3725 case WINED3DSIH_IADD: op = "+"; break;
3726 case WINED3DSIH_ISHL: op = "<<"; break;
3727 case WINED3DSIH_ISHR: op = ">>"; break;
3728 case WINED3DSIH_MUL: op = "*"; break;
3729 case WINED3DSIH_OR: op = "|"; break;
3730 case WINED3DSIH_SUB: op = "-"; break;
3731 case WINED3DSIH_USHR: op = ">>"; break;
3732 case WINED3DSIH_XOR: op = "^"; break;
3733 default:
3734 op = "<unhandled operator>";
3735 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3736 break;
3739 write_mask = shader_glsl_append_dst(buffer, ins);
3740 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3741 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3742 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3745 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3747 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3748 struct glsl_src_param src0_param;
3749 struct glsl_src_param src1_param;
3750 unsigned int mask_size;
3751 DWORD write_mask;
3752 const char *op;
3754 write_mask = shader_glsl_append_dst(buffer, ins);
3755 mask_size = shader_glsl_get_write_mask_size(write_mask);
3756 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3757 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3759 if (mask_size > 1)
3761 switch (ins->handler_idx)
3763 case WINED3DSIH_EQ: op = "equal"; break;
3764 case WINED3DSIH_IEQ: op = "equal"; break;
3765 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3766 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3767 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3768 case WINED3DSIH_LT: op = "lessThan"; break;
3769 case WINED3DSIH_ILT: op = "lessThan"; break;
3770 case WINED3DSIH_ULT: op = "lessThan"; break;
3771 case WINED3DSIH_NE: op = "notEqual"; break;
3772 case WINED3DSIH_INE: op = "notEqual"; break;
3773 default:
3774 op = "<unhandled operator>";
3775 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3776 break;
3779 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3780 mask_size, op, src0_param.param_str, src1_param.param_str);
3782 else
3784 switch (ins->handler_idx)
3786 case WINED3DSIH_EQ: op = "=="; break;
3787 case WINED3DSIH_IEQ: op = "=="; break;
3788 case WINED3DSIH_GE: op = ">="; break;
3789 case WINED3DSIH_IGE: op = ">="; break;
3790 case WINED3DSIH_UGE: op = ">="; break;
3791 case WINED3DSIH_LT: op = "<"; break;
3792 case WINED3DSIH_ILT: op = "<"; break;
3793 case WINED3DSIH_ULT: op = "<"; break;
3794 case WINED3DSIH_NE: op = "!="; break;
3795 case WINED3DSIH_INE: op = "!="; break;
3796 default:
3797 op = "<unhandled operator>";
3798 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3799 break;
3802 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3803 src0_param.param_str, op, src1_param.param_str);
3807 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3809 struct glsl_src_param src_param;
3810 DWORD write_mask;
3811 const char *op;
3813 switch (ins->handler_idx)
3815 case WINED3DSIH_INEG: op = "-"; break;
3816 case WINED3DSIH_NOT: op = "~"; break;
3817 default:
3818 op = "<unhandled operator>";
3819 ERR("Unhandled opcode %s.\n",
3820 debug_d3dshaderinstructionhandler(ins->handler_idx));
3821 break;
3824 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3825 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3826 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3829 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3831 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3832 struct glsl_src_param src0_param;
3833 struct glsl_src_param src1_param;
3834 DWORD write_mask;
3836 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3837 * If not, we can emulate it. */
3838 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3839 FIXME("64-bit integer multiplies not implemented.\n");
3841 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3843 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3844 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3845 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3847 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3848 src0_param.param_str, src1_param.param_str);
3852 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3854 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3855 struct glsl_src_param src0_param, src1_param;
3856 DWORD write_mask;
3858 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3860 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3862 char dst_mask[6];
3864 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3865 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3866 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3867 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3868 dst_mask, src0_param.param_str, src1_param.param_str);
3870 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3871 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3872 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3873 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3875 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3876 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3878 else
3880 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3881 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3882 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3883 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3886 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3888 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3889 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3890 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3891 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3895 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3896 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3898 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3899 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3900 struct glsl_src_param src0_param;
3901 DWORD write_mask;
3903 write_mask = shader_glsl_append_dst(buffer, ins);
3904 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3906 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3907 * shader versions WINED3DSIO_MOVA is used for this. */
3908 if (ins->ctx->reg_maps->shader_version.major == 1
3909 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3910 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3912 /* This is a simple floor() */
3913 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3914 if (mask_size > 1) {
3915 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3916 } else {
3917 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3920 else if (ins->handler_idx == WINED3DSIH_MOVA)
3922 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3924 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3926 if (mask_size > 1)
3927 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3928 else
3929 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3931 else
3933 if (mask_size > 1)
3934 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3935 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3936 else
3937 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3938 src0_param.param_str, src0_param.param_str);
3941 else
3943 shader_addline(buffer, "%s);\n", src0_param.param_str);
3947 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3948 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3950 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3951 struct glsl_src_param src0_param;
3952 struct glsl_src_param src1_param;
3953 DWORD dst_write_mask, src_write_mask;
3954 unsigned int dst_size;
3956 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3957 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3959 /* dp4 works on vec4, dp3 on vec3, etc. */
3960 if (ins->handler_idx == WINED3DSIH_DP4)
3961 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3962 else if (ins->handler_idx == WINED3DSIH_DP3)
3963 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3964 else
3965 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3967 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3968 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3970 if (dst_size > 1) {
3971 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3972 } else {
3973 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3977 /* Note that this instruction has some restrictions. The destination write mask
3978 * can't contain the w component, and the source swizzles have to be .xyzw */
3979 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3981 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3982 struct glsl_src_param src0_param;
3983 struct glsl_src_param src1_param;
3984 char dst_mask[6];
3986 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3987 shader_glsl_append_dst(ins->ctx->buffer, ins);
3988 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3989 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3990 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3993 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3995 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3997 if (!stream)
3998 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3999 else
4000 FIXME("Unhandled primitive stream %u.\n", stream);
4003 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4004 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4005 * GLSL uses the value as-is. */
4006 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4008 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4009 struct glsl_src_param src0_param;
4010 struct glsl_src_param src1_param;
4011 DWORD dst_write_mask;
4012 unsigned int dst_size;
4014 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4015 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4017 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4018 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4020 if (dst_size > 1)
4022 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4023 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4025 else
4027 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4028 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4032 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4033 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4035 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4036 struct glsl_src_param src_param;
4037 const char *instruction;
4038 DWORD write_mask;
4039 unsigned i;
4041 /* Determine the GLSL function to use based on the opcode */
4042 /* TODO: Possibly make this a table for faster lookups */
4043 switch (ins->handler_idx)
4045 case WINED3DSIH_ABS: instruction = "abs"; break;
4046 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4047 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4048 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4049 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4050 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4051 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4052 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4053 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4054 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4055 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4056 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4057 case WINED3DSIH_FRC: instruction = "fract"; break;
4058 case WINED3DSIH_IMAX: instruction = "max"; break;
4059 case WINED3DSIH_IMIN: instruction = "min"; break;
4060 case WINED3DSIH_MAX: instruction = "max"; break;
4061 case WINED3DSIH_MIN: instruction = "min"; break;
4062 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4063 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4064 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4065 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4066 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4067 case WINED3DSIH_UMAX: instruction = "max"; break;
4068 case WINED3DSIH_UMIN: instruction = "min"; break;
4069 default: instruction = "";
4070 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4071 break;
4074 write_mask = shader_glsl_append_dst(buffer, ins);
4076 /* In D3D bits are numbered from the most significant bit. */
4077 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4078 shader_addline(buffer, "31 - ");
4079 shader_addline(buffer, "%s(", instruction);
4081 if (ins->src_count)
4083 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4084 shader_addline(buffer, "%s", src_param.param_str);
4085 for (i = 1; i < ins->src_count; ++i)
4087 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4088 shader_addline(buffer, ", %s", src_param.param_str);
4092 shader_addline(buffer, "));\n");
4095 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4097 struct wined3d_shader_dst_param dst;
4098 struct glsl_src_param src;
4099 DWORD write_mask;
4100 const char *fmt;
4101 unsigned int i;
4103 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4104 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4106 dst = ins->dst[0];
4107 for (i = 0; i < 4; ++i)
4109 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4110 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4111 &dst, dst.reg.data_type)))
4112 continue;
4114 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4115 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4119 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4121 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4122 struct wined3d_shader_dst_param dst;
4123 struct glsl_src_param src[4];
4124 const char *instruction;
4125 BOOL tmp_dst = FALSE;
4126 char mask_char[6];
4127 unsigned int i, j;
4128 DWORD write_mask;
4130 switch (ins->handler_idx)
4132 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4133 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4134 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4135 default:
4136 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4137 return;
4140 for (i = 0; i < ins->src_count; ++i)
4142 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4143 && ins->dst[0].reg.type == ins->src[i].reg.type)
4144 tmp_dst = TRUE;
4147 dst = ins->dst[0];
4148 for (i = 0; i < 4; ++i)
4150 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4151 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4152 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4153 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4154 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4155 continue;
4157 for (j = 0; j < ins->src_count; ++j)
4158 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4159 shader_addline(buffer, "%s(", instruction);
4160 for (j = 0; j < ins->src_count - 2; ++j)
4161 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4162 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4165 if (tmp_dst)
4167 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4168 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4169 shader_addline(buffer, "tmp0%s);\n", mask_char);
4173 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4175 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4177 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4178 struct glsl_src_param src_param;
4179 unsigned int mask_size;
4180 DWORD write_mask;
4181 char dst_mask[6];
4183 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4184 mask_size = shader_glsl_get_write_mask_size(write_mask);
4185 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4187 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4188 src_param.param_str, src_param.param_str);
4189 shader_glsl_append_dst(buffer, ins);
4191 if (mask_size > 1)
4193 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4194 mask_size, src_param.param_str);
4196 else
4198 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4199 src_param.param_str);
4203 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4205 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4206 ins->ctx->reg_maps->shader_version.minor);
4207 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4208 struct glsl_src_param src0_param;
4209 const char *prefix, *suffix;
4210 unsigned int dst_size;
4211 DWORD dst_write_mask;
4213 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4214 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4216 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4217 dst_write_mask = WINED3DSP_WRITEMASK_3;
4219 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4221 switch (ins->handler_idx)
4223 case WINED3DSIH_EXP:
4224 case WINED3DSIH_EXPP:
4225 prefix = "exp2(";
4226 suffix = ")";
4227 break;
4229 case WINED3DSIH_LOG:
4230 case WINED3DSIH_LOGP:
4231 prefix = "log2(abs(";
4232 suffix = "))";
4233 break;
4235 case WINED3DSIH_RCP:
4236 prefix = "1.0 / ";
4237 suffix = "";
4238 break;
4240 case WINED3DSIH_RSQ:
4241 prefix = "inversesqrt(abs(";
4242 suffix = "))";
4243 break;
4245 default:
4246 prefix = "";
4247 suffix = "";
4248 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4249 break;
4252 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4253 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4254 else
4255 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4258 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4259 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4260 * dst.x = 2^(floor(src))
4261 * dst.y = src - floor(src)
4262 * dst.z = 2^src (partial precision is allowed, but optional)
4263 * dst.w = 1.0;
4264 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4265 * dst = 2^src; (partial precision is allowed, but optional)
4267 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4269 if (ins->ctx->reg_maps->shader_version.major < 2)
4271 struct glsl_src_param src_param;
4272 char dst_mask[6];
4274 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4276 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4277 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4278 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4279 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4281 shader_glsl_append_dst(ins->ctx->buffer, ins);
4282 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4283 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4284 return;
4287 shader_glsl_scalar_op(ins);
4290 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4291 const char *vector_constructor, const char *scalar_constructor)
4293 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4294 struct glsl_src_param src_param;
4295 unsigned int mask_size;
4296 DWORD write_mask;
4298 write_mask = shader_glsl_append_dst(buffer, ins);
4299 mask_size = shader_glsl_get_write_mask_size(write_mask);
4300 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4302 if (mask_size > 1)
4303 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4304 else
4305 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4308 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4310 shader_glsl_cast(ins, "ivec", "int");
4313 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4315 shader_glsl_cast(ins, "uvec", "uint");
4318 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4320 shader_glsl_cast(ins, "vec", "float");
4323 /** Process signed comparison opcodes in GLSL. */
4324 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4326 struct glsl_src_param src0_param;
4327 struct glsl_src_param src1_param;
4328 DWORD write_mask;
4329 unsigned int mask_size;
4331 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4332 mask_size = shader_glsl_get_write_mask_size(write_mask);
4333 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4334 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4336 if (mask_size > 1) {
4337 const char *compare;
4339 switch(ins->handler_idx)
4341 case WINED3DSIH_SLT: compare = "lessThan"; break;
4342 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4343 default: compare = "";
4344 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4347 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4348 src0_param.param_str, src1_param.param_str);
4349 } else {
4350 switch(ins->handler_idx)
4352 case WINED3DSIH_SLT:
4353 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4354 * to return 0.0 but step returns 1.0 because step is not < x
4355 * An alternative is a bvec compare padded with an unused second component.
4356 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4357 * issue. Playing with not() is not possible either because not() does not accept
4358 * a scalar.
4360 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4361 src0_param.param_str, src1_param.param_str);
4362 break;
4363 case WINED3DSIH_SGE:
4364 /* Here we can use the step() function and safe a conditional */
4365 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4366 break;
4367 default:
4368 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4374 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4376 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4377 struct wined3d_shader_dst_param dst[2];
4378 struct glsl_src_param src[3];
4379 unsigned int i, j, k;
4380 char mask_char[6];
4381 DWORD write_mask;
4382 BOOL tmp_dst[2];
4384 for (i = 0; i < ins->dst_count; ++i)
4386 tmp_dst[i] = FALSE;
4387 for (j = 0; j < ins->src_count; ++j)
4389 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4390 && ins->dst[i].reg.type == ins->src[j].reg.type)
4391 tmp_dst[i] = TRUE;
4395 dst[0] = ins->dst[0];
4396 dst[1] = ins->dst[1];
4397 for (i = 0; i < 4; ++i)
4399 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4401 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4402 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4403 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4404 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4405 continue;
4407 for (k = 0; k < ARRAY_SIZE(src); ++k)
4408 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4410 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4411 src[0].param_str, src[1].param_str, src[2].param_str);
4415 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4417 if (tmp_dst[i])
4419 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4420 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4421 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4426 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4428 const char *condition_prefix, *condition_suffix;
4429 struct wined3d_shader_dst_param dst;
4430 struct glsl_src_param src0_param;
4431 struct glsl_src_param src1_param;
4432 struct glsl_src_param src2_param;
4433 BOOL temp_destination = FALSE;
4434 DWORD cmp_channel = 0;
4435 unsigned int i, j;
4436 char mask_char[6];
4437 DWORD write_mask;
4439 switch (ins->handler_idx)
4441 case WINED3DSIH_CMP:
4442 condition_prefix = "";
4443 condition_suffix = " >= 0.0";
4444 break;
4446 case WINED3DSIH_CND:
4447 condition_prefix = "";
4448 condition_suffix = " > 0.5";
4449 break;
4451 case WINED3DSIH_MOVC:
4452 condition_prefix = "bool(";
4453 condition_suffix = ")";
4454 break;
4456 default:
4457 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4458 condition_prefix = "<unhandled prefix>";
4459 condition_suffix = "<unhandled suffix>";
4460 break;
4463 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4465 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4466 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4467 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4468 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4470 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4471 condition_prefix, src0_param.param_str, condition_suffix,
4472 src1_param.param_str, src2_param.param_str);
4473 return;
4476 dst = ins->dst[0];
4478 /* Splitting the instruction up in multiple lines imposes a problem:
4479 * The first lines may overwrite source parameters of the following lines.
4480 * Deal with that by using a temporary destination register if needed. */
4481 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4482 && ins->src[0].reg.type == dst.reg.type)
4483 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4484 && ins->src[1].reg.type == dst.reg.type)
4485 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4486 && ins->src[2].reg.type == dst.reg.type))
4487 temp_destination = TRUE;
4489 /* Cycle through all source0 channels. */
4490 for (i = 0; i < 4; ++i)
4492 write_mask = 0;
4493 /* Find the destination channels which use the current source0 channel. */
4494 for (j = 0; j < 4; ++j)
4496 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4498 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4499 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4502 dst.write_mask = ins->dst[0].write_mask & write_mask;
4504 if (temp_destination)
4506 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4507 continue;
4508 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4510 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4511 continue;
4513 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4514 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4515 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4517 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4518 condition_prefix, src0_param.param_str, condition_suffix,
4519 src1_param.param_str, src2_param.param_str);
4522 if (temp_destination)
4524 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4525 shader_glsl_append_dst(ins->ctx->buffer, ins);
4526 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4530 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4531 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4532 * the compare is done per component of src0. */
4533 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4535 struct glsl_src_param src0_param;
4536 struct glsl_src_param src1_param;
4537 struct glsl_src_param src2_param;
4538 DWORD write_mask;
4539 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4540 ins->ctx->reg_maps->shader_version.minor);
4542 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4544 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4545 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4546 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4547 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4549 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4550 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4551 else
4552 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4553 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4554 return;
4557 shader_glsl_conditional_move(ins);
4560 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4561 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4563 struct glsl_src_param src0_param;
4564 struct glsl_src_param src1_param;
4565 struct glsl_src_param src2_param;
4566 DWORD write_mask;
4568 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4569 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4570 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4571 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4572 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4573 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4576 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4577 Vertex shaders to GLSL codes */
4578 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4580 int i;
4581 int nComponents = 0;
4582 struct wined3d_shader_dst_param tmp_dst = {{0}};
4583 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4584 struct wined3d_shader_instruction tmp_ins;
4586 memset(&tmp_ins, 0, sizeof(tmp_ins));
4588 /* Set constants for the temporary argument */
4589 tmp_ins.ctx = ins->ctx;
4590 tmp_ins.dst_count = 1;
4591 tmp_ins.dst = &tmp_dst;
4592 tmp_ins.src_count = 2;
4593 tmp_ins.src = tmp_src;
4595 switch(ins->handler_idx)
4597 case WINED3DSIH_M4x4:
4598 nComponents = 4;
4599 tmp_ins.handler_idx = WINED3DSIH_DP4;
4600 break;
4601 case WINED3DSIH_M4x3:
4602 nComponents = 3;
4603 tmp_ins.handler_idx = WINED3DSIH_DP4;
4604 break;
4605 case WINED3DSIH_M3x4:
4606 nComponents = 4;
4607 tmp_ins.handler_idx = WINED3DSIH_DP3;
4608 break;
4609 case WINED3DSIH_M3x3:
4610 nComponents = 3;
4611 tmp_ins.handler_idx = WINED3DSIH_DP3;
4612 break;
4613 case WINED3DSIH_M3x2:
4614 nComponents = 2;
4615 tmp_ins.handler_idx = WINED3DSIH_DP3;
4616 break;
4617 default:
4618 break;
4621 tmp_dst = ins->dst[0];
4622 tmp_src[0] = ins->src[0];
4623 tmp_src[1] = ins->src[1];
4624 for (i = 0; i < nComponents; ++i)
4626 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4627 shader_glsl_dot(&tmp_ins);
4628 ++tmp_src[1].reg.idx[0].offset;
4633 The LRP instruction performs a component-wise linear interpolation
4634 between the second and third operands using the first operand as the
4635 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4636 This is equivalent to mix(src2, src1, src0);
4638 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4640 struct glsl_src_param src0_param;
4641 struct glsl_src_param src1_param;
4642 struct glsl_src_param src2_param;
4643 DWORD write_mask;
4645 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4647 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4648 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4649 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4651 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4652 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4655 /** Process the WINED3DSIO_LIT instruction in GLSL:
4656 * dst.x = dst.w = 1.0
4657 * dst.y = (src0.x > 0) ? src0.x
4658 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4659 * where src.w is clamped at +- 128
4661 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4663 struct glsl_src_param src0_param;
4664 struct glsl_src_param src1_param;
4665 struct glsl_src_param src3_param;
4666 char dst_mask[6];
4668 shader_glsl_append_dst(ins->ctx->buffer, ins);
4669 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4671 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4672 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4673 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4675 /* The sdk specifies the instruction like this
4676 * dst.x = 1.0;
4677 * if(src.x > 0.0) dst.y = src.x
4678 * else dst.y = 0.0.
4679 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4680 * else dst.z = 0.0;
4681 * dst.w = 1.0;
4682 * (where power = src.w clamped between -128 and 128)
4684 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4685 * dst.x = 1.0 ... No further explanation needed
4686 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4687 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4688 * dst.w = 1.0. ... Nothing fancy.
4690 * So we still have one conditional in there. So do this:
4691 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4693 * 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),
4694 * 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.
4695 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4697 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4698 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4699 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4701 shader_addline(ins->ctx->buffer,
4702 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4703 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4704 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4705 src0_param.param_str, src3_param.param_str, dst_mask);
4708 /** Process the WINED3DSIO_DST instruction in GLSL:
4709 * dst.x = 1.0
4710 * dst.y = src0.x * src0.y
4711 * dst.z = src0.z
4712 * dst.w = src1.w
4714 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4716 struct glsl_src_param src0y_param;
4717 struct glsl_src_param src0z_param;
4718 struct glsl_src_param src1y_param;
4719 struct glsl_src_param src1w_param;
4720 char dst_mask[6];
4722 shader_glsl_append_dst(ins->ctx->buffer, ins);
4723 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4725 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4726 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4727 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4728 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4730 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4731 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4734 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4735 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4736 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4738 * dst.x = cos(src0.?)
4739 * dst.y = sin(src0.?)
4740 * dst.z = dst.z
4741 * dst.w = dst.w
4743 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4745 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4746 struct glsl_src_param src0_param;
4747 DWORD write_mask;
4749 if (ins->ctx->reg_maps->shader_version.major < 4)
4751 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4753 write_mask = shader_glsl_append_dst(buffer, ins);
4754 switch (write_mask)
4756 case WINED3DSP_WRITEMASK_0:
4757 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4758 break;
4760 case WINED3DSP_WRITEMASK_1:
4761 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4762 break;
4764 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4765 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4766 src0_param.param_str, src0_param.param_str);
4767 break;
4769 default:
4770 ERR("Write mask should be .x, .y or .xy\n");
4771 break;
4774 return;
4777 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4780 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4782 char dst_mask[6];
4784 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4785 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4786 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4788 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4789 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4790 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4792 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4793 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4795 else
4797 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4798 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4799 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4802 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4804 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4805 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4806 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4810 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4811 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4812 * generate invalid code
4814 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4816 struct glsl_src_param src0_param;
4817 DWORD write_mask;
4819 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4820 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4822 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4825 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4826 * Start a for() loop where src1.y is the initial value of aL,
4827 * increment aL by src1.z for a total of src1.x iterations.
4828 * Need to use a temporary variable for this operation.
4830 /* FIXME: I don't think nested loops will work correctly this way. */
4831 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4833 struct wined3d_shader_parser_state *state = ins->ctx->state;
4834 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4835 const struct wined3d_shader *shader = ins->ctx->shader;
4836 const struct wined3d_shader_lconst *constant;
4837 struct glsl_src_param src1_param;
4838 const DWORD *control_values = NULL;
4840 if (ins->ctx->reg_maps->shader_version.major < 4)
4842 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4844 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4845 * class hardware doesn't support real varying indexing, but Microsoft
4846 * designed this feature for Shader model 2.x+. If the loop control is
4847 * known at compile time, the GLSL compiler can unroll the loop, and
4848 * replace indirect addressing with direct addressing. */
4849 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4851 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4853 if (constant->idx == ins->src[1].reg.idx[0].offset)
4855 control_values = constant->value;
4856 break;
4861 if (control_values)
4863 struct wined3d_shader_loop_control loop_control;
4864 loop_control.count = control_values[0];
4865 loop_control.start = control_values[1];
4866 loop_control.step = (int)control_values[2];
4868 if (loop_control.step > 0)
4870 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4871 state->current_loop_depth, loop_control.start,
4872 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4873 state->current_loop_depth, loop_control.step);
4875 else if (loop_control.step < 0)
4877 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4878 state->current_loop_depth, loop_control.start,
4879 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4880 state->current_loop_depth, loop_control.step);
4882 else
4884 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4885 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4886 state->current_loop_depth, loop_control.count,
4887 state->current_loop_depth);
4890 else
4892 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4893 state->current_loop_depth, state->current_loop_reg,
4894 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4895 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4898 ++state->current_loop_reg;
4900 else
4902 shader_addline(buffer, "for (;;)\n{\n");
4905 ++state->current_loop_depth;
4908 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4910 struct wined3d_shader_parser_state *state = ins->ctx->state;
4912 shader_addline(ins->ctx->buffer, "}\n");
4914 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4916 --state->current_loop_depth;
4917 --state->current_loop_reg;
4920 if (ins->handler_idx == WINED3DSIH_ENDREP)
4922 --state->current_loop_depth;
4926 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4928 struct wined3d_shader_parser_state *state = ins->ctx->state;
4929 const struct wined3d_shader *shader = ins->ctx->shader;
4930 const struct wined3d_shader_lconst *constant;
4931 struct glsl_src_param src0_param;
4932 const DWORD *control_values = NULL;
4934 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4935 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4937 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4939 if (constant->idx == ins->src[0].reg.idx[0].offset)
4941 control_values = constant->value;
4942 break;
4947 if (control_values)
4949 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4950 state->current_loop_depth, state->current_loop_depth,
4951 control_values[0], state->current_loop_depth);
4953 else
4955 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4956 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4957 state->current_loop_depth, state->current_loop_depth,
4958 src0_param.param_str, state->current_loop_depth);
4961 ++state->current_loop_depth;
4964 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4966 struct glsl_src_param src0_param;
4968 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4969 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4972 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4974 struct glsl_src_param src0_param;
4976 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4977 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4980 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4982 shader_addline(ins->ctx->buffer, "default:\n");
4985 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4987 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4988 struct glsl_src_param src0_param;
4990 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4991 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4994 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4996 struct glsl_src_param src0_param;
4997 struct glsl_src_param src1_param;
4999 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5000 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5002 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5003 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5006 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5008 shader_addline(ins->ctx->buffer, "} else {\n");
5011 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5013 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5015 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5016 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5017 shader_glsl_fixup_position(ins->ctx->buffer);
5019 if (!stream)
5020 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5021 else
5022 FIXME("Unhandled primitive stream %u.\n", stream);
5025 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5027 shader_addline(ins->ctx->buffer, "break;\n");
5030 /* FIXME: According to MSDN the compare is done per component. */
5031 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5033 struct glsl_src_param src0_param;
5034 struct glsl_src_param src1_param;
5036 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5037 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5039 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5040 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5043 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5045 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
5046 struct glsl_src_param src_param;
5047 const char *op;
5049 switch (ins->handler_idx)
5051 case WINED3DSIH_BREAKP: op = "break"; break;
5052 case WINED3DSIH_CONTINUEP: op = "continue"; break;
5053 case WINED3DSIH_RETP: op = "return"; break;
5054 default:
5055 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5056 return;
5059 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5060 shader_addline(ins->ctx->buffer, "if (%s(%s)) %s;\n", condition, src_param.param_str, op);
5063 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5065 shader_addline(ins->ctx->buffer, "continue;\n");
5068 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5070 shader_addline(ins->ctx->buffer, "}\n");
5071 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5073 /* Subroutines appear at the end of the shader. */
5074 ins->ctx->state->in_subroutine = TRUE;
5077 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5079 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5082 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5084 struct glsl_src_param src1_param;
5086 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5087 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5088 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5091 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5093 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5095 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5097 shader_glsl_generate_shader_epilogue(ins->ctx);
5098 shader_addline(ins->ctx->buffer, "return;\n");
5102 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5104 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5105 ins->ctx->reg_maps->shader_version.minor);
5106 struct glsl_sample_function sample_function;
5107 DWORD sample_flags = 0;
5108 DWORD resource_idx;
5109 DWORD mask = 0, swizzle;
5110 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5112 /* 1.0-1.4: Use destination register as sampler source.
5113 * 2.0+: Use provided sampler source. */
5114 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5115 resource_idx = ins->dst[0].reg.idx[0].offset;
5116 else
5117 resource_idx = ins->src[1].reg.idx[0].offset;
5119 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5121 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5122 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5123 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5125 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5126 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5128 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5129 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5131 case WINED3D_TTFF_COUNT1:
5132 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5133 break;
5134 case WINED3D_TTFF_COUNT2:
5135 mask = WINED3DSP_WRITEMASK_1;
5136 break;
5137 case WINED3D_TTFF_COUNT3:
5138 mask = WINED3DSP_WRITEMASK_2;
5139 break;
5140 case WINED3D_TTFF_COUNT4:
5141 case WINED3D_TTFF_DISABLE:
5142 mask = WINED3DSP_WRITEMASK_3;
5143 break;
5147 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5149 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5151 if (src_mod == WINED3DSPSM_DZ) {
5152 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5153 mask = WINED3DSP_WRITEMASK_2;
5154 } else if (src_mod == WINED3DSPSM_DW) {
5155 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5156 mask = WINED3DSP_WRITEMASK_3;
5159 else
5161 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5162 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5164 /* ps 2.0 texldp instruction always divides by the fourth component. */
5165 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5166 mask = WINED3DSP_WRITEMASK_3;
5170 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5171 mask |= sample_function.coord_mask;
5172 sample_function.coord_mask = mask;
5174 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5175 else swizzle = ins->src[1].swizzle;
5177 /* 1.0-1.3: Use destination register as coordinate source.
5178 1.4+: Use provided coordinate source register. */
5179 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5181 char coord_mask[6];
5182 shader_glsl_write_mask_to_str(mask, coord_mask);
5183 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5184 "T%u%s", resource_idx, coord_mask);
5186 else
5188 struct glsl_src_param coord_param;
5189 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5190 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5192 struct glsl_src_param bias;
5193 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5194 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5195 NULL, "%s", coord_param.param_str);
5196 } else {
5197 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5198 "%s", coord_param.param_str);
5201 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5204 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5206 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5207 struct glsl_src_param coord_param, dx_param, dy_param;
5208 struct glsl_sample_function sample_function;
5209 DWORD sampler_idx;
5210 DWORD swizzle = ins->src[1].swizzle;
5212 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5214 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5215 shader_glsl_tex(ins);
5216 return;
5219 sampler_idx = ins->src[1].reg.idx[0].offset;
5221 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5222 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5223 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5224 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5226 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5227 NULL, NULL, "%s", coord_param.param_str);
5228 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5231 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5233 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5234 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5235 struct glsl_src_param coord_param, lod_param;
5236 struct glsl_sample_function sample_function;
5237 DWORD swizzle = ins->src[1].swizzle;
5238 DWORD sampler_idx;
5240 sampler_idx = ins->src[1].reg.idx[0].offset;
5242 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5243 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5245 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5247 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5248 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5250 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5251 * However, the NVIDIA drivers allow them in fragment shaders as well,
5252 * even without the appropriate extension. */
5253 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5255 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5256 "%s", coord_param.param_str);
5257 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5260 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5261 unsigned int resource_idx, unsigned int sampler_idx)
5263 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5264 unsigned int i;
5266 for (i = 0; i < sampler_map->count; ++i)
5268 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5269 return entries[i].bind_idx;
5272 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5274 return ~0u;
5277 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5279 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5280 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5281 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5282 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5283 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5284 struct glsl_src_param structure_idx, offset, data, data2;
5285 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5286 enum wined3d_shader_resource_type resource_type;
5287 struct wined3d_string_buffer *address;
5288 enum wined3d_data_type data_type;
5289 unsigned int resource_idx, stride;
5290 const char *op, *resource;
5291 DWORD coord_mask;
5292 BOOL is_tgsm;
5294 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5295 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5296 if (is_tgsm)
5298 if (resource_idx >= reg_maps->tgsm_count)
5300 ERR("Invalid TGSM index %u.\n", resource_idx);
5301 return;
5303 resource = "g";
5304 data_type = WINED3D_DATA_UINT;
5305 coord_mask = 1;
5306 stride = reg_maps->tgsm[resource_idx].stride;
5308 else
5310 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5312 ERR("Invalid UAV index %u.\n", resource_idx);
5313 return;
5315 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5316 if (resource_type >= ARRAY_SIZE(resource_type_info))
5318 ERR("Unexpected resource type %#x.\n", resource_type);
5319 return;
5321 resource = "image";
5322 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5323 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5324 stride = reg_maps->uav_resource_info[resource_idx].stride;
5327 switch (ins->handler_idx)
5329 case WINED3DSIH_ATOMIC_AND:
5330 case WINED3DSIH_IMM_ATOMIC_AND:
5331 if (is_tgsm)
5332 op = "atomicAnd";
5333 else
5334 op = "imageAtomicAnd";
5335 break;
5336 case WINED3DSIH_ATOMIC_CMP_STORE:
5337 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5338 if (is_tgsm)
5339 op = "atomicCompSwap";
5340 else
5341 op = "imageAtomicCompSwap";
5342 break;
5343 case WINED3DSIH_ATOMIC_IADD:
5344 case WINED3DSIH_IMM_ATOMIC_IADD:
5345 if (is_tgsm)
5346 op = "atomicAdd";
5347 else
5348 op = "imageAtomicAdd";
5349 break;
5350 case WINED3DSIH_ATOMIC_IMAX:
5351 case WINED3DSIH_IMM_ATOMIC_IMAX:
5352 if (is_tgsm)
5353 op = "atomicMax";
5354 else
5355 op = "imageAtomicMax";
5356 if (data_type != WINED3D_DATA_INT)
5358 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5359 return;
5361 break;
5362 case WINED3DSIH_ATOMIC_IMIN:
5363 case WINED3DSIH_IMM_ATOMIC_IMIN:
5364 if (is_tgsm)
5365 op = "atomicMin";
5366 else
5367 op = "imageAtomicMin";
5368 if (data_type != WINED3D_DATA_INT)
5370 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5371 return;
5373 break;
5374 case WINED3DSIH_ATOMIC_OR:
5375 case WINED3DSIH_IMM_ATOMIC_OR:
5376 if (is_tgsm)
5377 op = "atomicOr";
5378 else
5379 op = "imageAtomicOr";
5380 break;
5381 case WINED3DSIH_ATOMIC_UMAX:
5382 case WINED3DSIH_IMM_ATOMIC_UMAX:
5383 if (is_tgsm)
5384 op = "atomicMax";
5385 else
5386 op = "imageAtomicMax";
5387 if (data_type != WINED3D_DATA_UINT)
5389 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5390 return;
5392 break;
5393 case WINED3DSIH_ATOMIC_UMIN:
5394 case WINED3DSIH_IMM_ATOMIC_UMIN:
5395 if (is_tgsm)
5396 op = "atomicMin";
5397 else
5398 op = "imageAtomicMin";
5399 if (data_type != WINED3D_DATA_UINT)
5401 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5402 return;
5404 break;
5405 case WINED3DSIH_ATOMIC_XOR:
5406 case WINED3DSIH_IMM_ATOMIC_XOR:
5407 if (is_tgsm)
5408 op = "atomicXor";
5409 else
5410 op = "imageAtomicXor";
5411 break;
5412 case WINED3DSIH_IMM_ATOMIC_EXCH:
5413 if (is_tgsm)
5414 op = "atomicExchange";
5415 else
5416 op = "imageAtomicExchange";
5417 break;
5418 default:
5419 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5420 return;
5423 address = string_buffer_get(priv->string_buffers);
5424 if (stride)
5426 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5427 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5428 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5430 else
5432 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5433 string_buffer_sprintf(address, "%s", offset.param_str);
5434 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5435 shader_addline(address, "/ 4");
5438 if (is_imm_instruction)
5439 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5441 if (is_tgsm)
5442 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5443 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5444 else
5445 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5446 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5448 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5449 shader_addline(buffer, "%s", data.param_str);
5450 if (ins->src_count >= 3)
5452 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5453 shader_addline(buffer, ", %s", data2.param_str);
5456 if (is_imm_instruction)
5457 shader_addline(buffer, ")");
5458 shader_addline(buffer, ");\n");
5460 string_buffer_release(priv->string_buffers, address);
5463 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5465 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5466 const char *op;
5468 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5469 op = "atomicCounterIncrement";
5470 else
5471 op = "atomicCounterDecrement";
5473 shader_glsl_append_dst(ins->ctx->buffer, ins);
5474 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5477 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5479 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5480 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5481 enum wined3d_shader_resource_type resource_type;
5482 struct glsl_src_param image_coord_param;
5483 enum wined3d_data_type data_type;
5484 DWORD coord_mask, write_mask;
5485 unsigned int uav_idx;
5486 char dst_swizzle[6];
5488 uav_idx = ins->src[1].reg.idx[0].offset;
5489 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5491 ERR("Invalid UAV index %u.\n", uav_idx);
5492 return;
5494 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5495 if (resource_type >= ARRAY_SIZE(resource_type_info))
5497 ERR("Unexpected resource type %#x.\n", resource_type);
5498 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5500 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5501 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5503 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5504 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5506 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5507 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5508 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5511 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5513 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5514 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5515 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5516 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5517 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5518 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5519 struct glsl_src_param structure_idx, offset;
5520 struct wined3d_string_buffer *address;
5521 struct wined3d_shader_dst_param dst;
5522 const char *function, *resource;
5524 resource_idx = src->reg.idx[0].offset;
5525 if (src->reg.type == WINED3DSPR_RESOURCE)
5527 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5529 ERR("Invalid resource index %u.\n", resource_idx);
5530 return;
5532 stride = reg_maps->resource_info[resource_idx].stride;
5533 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5534 function = "texelFetch";
5535 resource = "sampler";
5537 else if (src->reg.type == WINED3DSPR_UAV)
5539 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5541 ERR("Invalid UAV index %u.\n", resource_idx);
5542 return;
5544 stride = reg_maps->uav_resource_info[resource_idx].stride;
5545 bind_idx = resource_idx;
5546 function = "imageLoad";
5547 resource = "image";
5549 else
5551 if (resource_idx >= reg_maps->tgsm_count)
5553 ERR("Invalid TGSM index %u.\n", resource_idx);
5554 return;
5556 stride = reg_maps->tgsm[resource_idx].stride;
5557 bind_idx = resource_idx;
5558 function = NULL;
5559 resource = "g";
5562 address = string_buffer_get(priv->string_buffers);
5563 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5565 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5566 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5568 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5569 shader_addline(address, "%s / 4", offset.param_str);
5571 dst = ins->dst[0];
5572 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5574 /* The instruction is split into multiple lines. The first lines may
5575 * overwrite source parameters of the following lines. */
5576 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5577 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5580 for (i = 0; i < 4; ++i)
5582 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5583 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5584 continue;
5586 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5587 if (function)
5588 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5589 function, prefix, resource, bind_idx, address->buffer, swizzle);
5590 else
5591 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5592 prefix, resource, bind_idx, address->buffer, swizzle);
5595 string_buffer_release(priv->string_buffers, address);
5598 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5600 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5601 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5602 struct glsl_src_param image_coord_param, image_data_param;
5603 enum wined3d_shader_resource_type resource_type;
5604 enum wined3d_data_type data_type;
5605 unsigned int uav_idx;
5606 DWORD coord_mask;
5608 uav_idx = ins->dst[0].reg.idx[0].offset;
5609 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5611 ERR("Invalid UAV index %u.\n", uav_idx);
5612 return;
5614 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5615 if (resource_type >= ARRAY_SIZE(resource_type_info))
5617 ERR("Unexpected resource type %#x.\n", resource_type);
5618 return;
5620 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5621 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5623 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5624 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5625 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5626 shader_glsl_get_prefix(version->type), uav_idx,
5627 image_coord_param.param_str, image_data_param.param_str);
5630 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5632 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5633 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5634 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5635 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5636 struct glsl_src_param structure_idx, offset, data;
5637 unsigned int i, resource_idx, stride, src_idx = 0;
5638 struct wined3d_string_buffer *address;
5639 DWORD write_mask;
5640 BOOL is_tgsm;
5642 resource_idx = ins->dst[0].reg.idx[0].offset;
5643 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5644 if (is_tgsm)
5646 if (resource_idx >= reg_maps->tgsm_count)
5648 ERR("Invalid TGSM index %u.\n", resource_idx);
5649 return;
5651 stride = reg_maps->tgsm[resource_idx].stride;
5653 else
5655 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5657 ERR("Invalid UAV index %u.\n", resource_idx);
5658 return;
5660 stride = reg_maps->uav_resource_info[resource_idx].stride;
5663 address = string_buffer_get(priv->string_buffers);
5664 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5666 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5667 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5669 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5670 shader_addline(address, "%s / 4", offset.param_str);
5672 for (i = 0; i < 4; ++i)
5674 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5675 continue;
5677 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5679 if (is_tgsm)
5680 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5681 prefix, resource_idx, address->buffer, i, data.param_str);
5682 else
5683 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5684 prefix, resource_idx, address->buffer, i, data.param_str);
5687 string_buffer_release(priv->string_buffers, address);
5690 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5692 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5693 unsigned int sync_flags = ins->flags;
5695 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5697 shader_addline(buffer, "barrier();\n");
5698 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5701 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5703 shader_addline(buffer, "memoryBarrierShared();\n");
5704 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5707 if (sync_flags)
5708 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5711 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5712 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5714 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5715 unsigned int idx = reg->idx[0].offset;
5717 if (reg->type == WINED3DSPR_RESOURCE)
5719 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5721 ERR("Invalid resource index %u.\n", idx);
5722 return NULL;
5724 return &reg_maps->resource_info[idx];
5727 if (reg->type == WINED3DSPR_UAV)
5729 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5731 ERR("Invalid UAV index %u.\n", idx);
5732 return NULL;
5734 return &reg_maps->uav_resource_info[idx];
5737 FIXME("Unhandled register type %#x.\n", reg->type);
5738 return NULL;
5741 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5743 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5744 const struct wined3d_shader_resource_info *resource_info;
5745 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5746 unsigned int resource_idx;
5747 char dst_swizzle[6];
5748 DWORD write_mask;
5750 write_mask = shader_glsl_append_dst(buffer, ins);
5751 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5753 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5754 return;
5755 resource_idx = ins->src[0].reg.idx[0].offset;
5757 shader_addline(buffer, "ivec2(");
5758 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5760 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5761 resource_idx, WINED3D_SAMPLER_DEFAULT);
5762 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5764 else
5766 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5768 if (resource_info->stride)
5769 shader_addline(buffer, " / %u", resource_info->stride);
5770 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5771 shader_addline(buffer, " * 4");
5772 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5775 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5777 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5778 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5779 enum wined3d_shader_resource_type resource_type;
5780 enum wined3d_shader_register_type reg_type;
5781 unsigned int resource_idx, bind_idx, i;
5782 enum wined3d_data_type dst_data_type;
5783 struct glsl_src_param lod_param;
5784 char dst_swizzle[6];
5785 DWORD write_mask;
5787 dst_data_type = ins->dst[0].reg.data_type;
5788 if (ins->flags == WINED3DSI_RESINFO_UINT)
5789 dst_data_type = WINED3D_DATA_UINT;
5790 else if (ins->flags)
5791 FIXME("Unhandled flags %#x.\n", ins->flags);
5793 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5794 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5796 reg_type = ins->src[1].reg.type;
5797 resource_idx = ins->src[1].reg.idx[0].offset;
5798 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5799 if (reg_type == WINED3DSPR_RESOURCE)
5801 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5802 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5803 resource_idx, WINED3D_SAMPLER_DEFAULT);
5805 else
5807 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5808 bind_idx = resource_idx;
5811 if (resource_type >= ARRAY_SIZE(resource_type_info))
5813 ERR("Unexpected resource type %#x.\n", resource_type);
5814 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5817 if (dst_data_type == WINED3D_DATA_UINT)
5818 shader_addline(ins->ctx->buffer, "uvec4(");
5819 else
5820 shader_addline(ins->ctx->buffer, "vec4(");
5822 if (reg_type == WINED3DSPR_RESOURCE)
5824 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5825 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5827 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5828 shader_addline(ins->ctx->buffer, "0, ");
5830 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5832 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5833 shader_glsl_get_prefix(version->type), bind_idx);
5835 else
5837 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5838 shader_addline(ins->ctx->buffer, "1");
5841 else
5843 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5844 shader_glsl_get_prefix(version->type), bind_idx);
5846 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5847 shader_addline(ins->ctx->buffer, "0, ");
5849 /* For UAVs the returned miplevel count is always 1. */
5850 shader_addline(ins->ctx->buffer, "1");
5853 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5856 /* FIXME: The current implementation does not handle multisample textures correctly. */
5857 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5859 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5860 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5861 struct glsl_src_param coord_param, lod_param;
5862 struct glsl_sample_function sample_function;
5863 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5864 BOOL has_lod_param;
5866 if (wined3d_shader_instruction_has_texel_offset(ins))
5867 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5869 resource_idx = ins->src[1].reg.idx[0].offset;
5870 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5872 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5874 ERR("Invalid resource index %u.\n", resource_idx);
5875 return;
5877 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5879 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5880 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5881 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5882 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5883 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5884 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5885 "%s", coord_param.param_str);
5886 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5889 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5891 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5892 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5893 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5894 struct glsl_sample_function sample_function;
5895 DWORD flags = 0;
5897 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5898 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5899 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5900 flags |= WINED3D_GLSL_SAMPLE_LOD;
5901 if (wined3d_shader_instruction_has_texel_offset(ins))
5902 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5904 resource_idx = ins->src[1].reg.idx[0].offset;
5905 sampler_idx = ins->src[2].reg.idx[0].offset;
5907 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5908 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5910 switch (ins->handler_idx)
5912 case WINED3DSIH_SAMPLE:
5913 break;
5914 case WINED3DSIH_SAMPLE_B:
5915 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5916 lod_param_str = lod_param.param_str;
5917 break;
5918 case WINED3DSIH_SAMPLE_GRAD:
5919 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5920 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5921 dx_param_str = dx_param.param_str;
5922 dy_param_str = dy_param.param_str;
5923 break;
5924 case WINED3DSIH_SAMPLE_LOD:
5925 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5926 lod_param_str = lod_param.param_str;
5927 break;
5928 default:
5929 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5930 break;
5933 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5934 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5935 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5936 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5939 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5941 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5942 struct glsl_src_param coord_param, compare_param;
5943 struct glsl_sample_function sample_function;
5944 const char *lod_param = NULL;
5945 unsigned int coord_size;
5946 DWORD flags = 0;
5948 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5950 lod_param = "0";
5951 flags |= WINED3D_GLSL_SAMPLE_LOD;
5954 if (wined3d_shader_instruction_has_texel_offset(ins))
5955 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5957 resource_idx = ins->src[1].reg.idx[0].offset;
5958 sampler_idx = ins->src[2].reg.idx[0].offset;
5960 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5961 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5962 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5963 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5964 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5965 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5966 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5967 coord_size, coord_param.param_str, compare_param.param_str);
5968 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5971 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
5973 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
5974 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5975 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5976 struct glsl_src_param coord_param, compare_param, offset_param;
5977 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5978 const struct wined3d_shader_resource_info *resource_info;
5979 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5980 unsigned int coord_size, offset_size;
5981 char dst_swizzle[6];
5982 BOOL has_offset;
5984 if (!gl_info->supported[ARB_TEXTURE_GATHER])
5986 FIXME("OpenGL implementation does not support textureGather.\n");
5987 return;
5990 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
5991 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
5992 || wined3d_shader_instruction_has_texel_offset(ins);
5994 resource_param_idx =
5995 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
5996 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
5997 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
5998 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
5999 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6001 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6002 return;
6004 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6006 ERR("Unexpected resource type %#x.\n", resource_info->type);
6007 return;
6009 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6011 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6012 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6014 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6016 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6017 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6018 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6020 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6021 shader_addline(buffer, ", %s", compare_param.param_str);
6023 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6025 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6026 shader_addline(buffer, ", %s", offset_param.param_str);
6028 else if (has_offset)
6030 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6031 shader_addline(buffer, ", ");
6032 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6034 if (component_idx)
6035 shader_addline(buffer, ", %u", component_idx);
6037 shader_addline(buffer, ")%s);\n", dst_swizzle);
6040 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6042 /* FIXME: Make this work for more than just 2D textures */
6043 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6044 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6046 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6048 char dst_mask[6];
6050 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6051 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6052 ins->dst[0].reg.idx[0].offset, dst_mask);
6054 else
6056 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6057 DWORD reg = ins->src[0].reg.idx[0].offset;
6058 char dst_swizzle[6];
6060 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6062 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6064 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6065 struct glsl_src_param div_param;
6066 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6068 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6070 if (mask_size > 1)
6071 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6072 else
6073 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6075 else
6077 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6082 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6083 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6084 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6085 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6087 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6088 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6089 struct glsl_sample_function sample_function;
6090 struct glsl_src_param src0_param;
6091 UINT mask_size;
6093 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6095 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6096 * scalar, and projected sampling would require 4.
6098 * It is a dependent read - not valid with conditional NP2 textures
6100 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6101 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6103 switch(mask_size)
6105 case 1:
6106 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6107 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6108 break;
6110 case 2:
6111 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6112 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6113 break;
6115 case 3:
6116 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6117 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6118 break;
6120 default:
6121 FIXME("Unexpected mask size %u\n", mask_size);
6122 break;
6124 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6127 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6128 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6129 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6131 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6132 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6133 struct glsl_src_param src0_param;
6134 DWORD dst_mask;
6135 unsigned int mask_size;
6137 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6138 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6139 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6141 if (mask_size > 1) {
6142 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6143 } else {
6144 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6148 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6149 * Calculate the depth as dst.x / dst.y */
6150 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6152 struct glsl_dst_param dst_param;
6154 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6156 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6157 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6158 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6159 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6160 * >= 1.0 or < 0.0
6162 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6163 dst_param.reg_name, dst_param.reg_name);
6166 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6167 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6168 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6169 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6171 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6173 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6174 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6175 struct glsl_src_param src0_param;
6177 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6179 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6180 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6183 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6184 * Calculate the 1st of a 2-row matrix multiplication. */
6185 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6187 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6188 DWORD reg = ins->dst[0].reg.idx[0].offset;
6189 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6190 struct glsl_src_param src0_param;
6192 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6193 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6196 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6197 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6198 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6200 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6201 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6202 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6203 DWORD reg = ins->dst[0].reg.idx[0].offset;
6204 struct glsl_src_param src0_param;
6206 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6207 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6208 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6211 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6213 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6214 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6215 struct glsl_sample_function sample_function;
6216 DWORD reg = ins->dst[0].reg.idx[0].offset;
6217 struct glsl_src_param src0_param;
6219 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6220 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6222 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6224 /* Sample the texture using the calculated coordinates */
6225 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6226 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6229 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6230 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6231 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6233 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6234 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6235 struct glsl_sample_function sample_function;
6236 DWORD reg = ins->dst[0].reg.idx[0].offset;
6237 struct glsl_src_param src0_param;
6239 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6240 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6242 /* Dependent read, not valid with conditional NP2 */
6243 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6245 /* Sample the texture using the calculated coordinates */
6246 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6247 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6249 tex_mx->current_row = 0;
6252 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6253 * Perform the 3rd row of a 3x3 matrix multiply */
6254 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6256 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6257 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6258 DWORD reg = ins->dst[0].reg.idx[0].offset;
6259 struct glsl_src_param src0_param;
6260 char dst_mask[6];
6262 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6264 shader_glsl_append_dst(ins->ctx->buffer, ins);
6265 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6266 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6268 tex_mx->current_row = 0;
6271 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6272 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6273 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6275 struct glsl_src_param src0_param;
6276 struct glsl_src_param src1_param;
6277 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6278 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6279 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6280 struct glsl_sample_function sample_function;
6281 DWORD reg = ins->dst[0].reg.idx[0].offset;
6282 char coord_mask[6];
6284 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6285 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6287 /* Perform the last matrix multiply operation */
6288 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6289 /* Reflection calculation */
6290 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6292 /* Dependent read, not valid with conditional NP2 */
6293 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6294 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6296 /* Sample the texture */
6297 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6298 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6299 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6301 tex_mx->current_row = 0;
6304 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6305 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6306 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6308 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6309 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6310 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6311 struct glsl_sample_function sample_function;
6312 DWORD reg = ins->dst[0].reg.idx[0].offset;
6313 struct glsl_src_param src0_param;
6314 char coord_mask[6];
6316 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6318 /* Perform the last matrix multiply operation */
6319 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6321 /* Construct the eye-ray vector from w coordinates */
6322 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6323 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6324 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6326 /* Dependent read, not valid with conditional NP2 */
6327 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6328 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6330 /* Sample the texture using the calculated coordinates */
6331 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6332 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6333 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6335 tex_mx->current_row = 0;
6338 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6339 * Apply a fake bump map transform.
6340 * texbem is pshader <= 1.3 only, this saves a few version checks
6342 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6344 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6345 struct glsl_sample_function sample_function;
6346 struct glsl_src_param coord_param;
6347 DWORD sampler_idx;
6348 DWORD mask;
6349 DWORD flags;
6350 char coord_mask[6];
6352 sampler_idx = ins->dst[0].reg.idx[0].offset;
6353 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6354 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6356 /* Dependent read, not valid with conditional NP2 */
6357 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6358 mask = sample_function.coord_mask;
6360 shader_glsl_write_mask_to_str(mask, coord_mask);
6362 /* With projected textures, texbem only divides the static texture coord,
6363 * not the displacement, so we can't let GL handle this. */
6364 if (flags & WINED3D_PSARGS_PROJECTED)
6366 DWORD div_mask=0;
6367 char coord_div_mask[3];
6368 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6370 case WINED3D_TTFF_COUNT1:
6371 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6372 break;
6373 case WINED3D_TTFF_COUNT2:
6374 div_mask = WINED3DSP_WRITEMASK_1;
6375 break;
6376 case WINED3D_TTFF_COUNT3:
6377 div_mask = WINED3DSP_WRITEMASK_2;
6378 break;
6379 case WINED3D_TTFF_COUNT4:
6380 case WINED3D_TTFF_DISABLE:
6381 div_mask = WINED3DSP_WRITEMASK_3;
6382 break;
6384 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6385 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6388 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6390 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6391 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6392 coord_param.param_str, coord_mask);
6394 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6396 struct glsl_src_param luminance_param;
6397 struct glsl_dst_param dst_param;
6399 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6400 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6402 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6403 dst_param.reg_name, dst_param.mask_str,
6404 luminance_param.param_str, sampler_idx, sampler_idx);
6406 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6409 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6411 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6412 struct glsl_src_param src0_param, src1_param;
6414 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6415 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6417 shader_glsl_append_dst(ins->ctx->buffer, ins);
6418 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6419 src0_param.param_str, sampler_idx, src1_param.param_str);
6422 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6423 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6424 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6426 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6427 struct glsl_sample_function sample_function;
6428 struct glsl_src_param src0_param;
6430 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6432 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6433 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6434 "%s.wx", src0_param.reg_name);
6435 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6438 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6439 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6440 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6442 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6443 struct glsl_sample_function sample_function;
6444 struct glsl_src_param src0_param;
6446 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6448 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6449 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6450 "%s.yz", src0_param.reg_name);
6451 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6454 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6455 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6456 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6458 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6459 struct glsl_sample_function sample_function;
6460 struct glsl_src_param src0_param;
6462 /* Dependent read, not valid with conditional NP2 */
6463 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6464 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6466 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6467 "%s", src0_param.param_str);
6468 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6471 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6472 * If any of the first 3 components are < 0, discard this pixel */
6473 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6475 if (ins->ctx->reg_maps->shader_version.major >= 4)
6477 struct glsl_src_param src_param;
6479 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
6480 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
6482 else
6484 struct glsl_dst_param dst_param;
6486 /* The argument is a destination parameter, and no writemasks are allowed */
6487 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6489 /* 2.0 shaders compare all 4 components in texkill. */
6490 if (ins->ctx->reg_maps->shader_version.major >= 2)
6491 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6492 /* 1.x shaders only compare the first 3 components, probably due to
6493 * the nature of the texkill instruction as a tex* instruction, and
6494 * phase, which kills all .w components. Even if all 4 components are
6495 * defined, only the first 3 are used. */
6496 else
6497 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6501 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6502 * dst = dot2(src0, src1) + src2 */
6503 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6505 struct glsl_src_param src0_param;
6506 struct glsl_src_param src1_param;
6507 struct glsl_src_param src2_param;
6508 DWORD write_mask;
6509 unsigned int mask_size;
6511 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6512 mask_size = shader_glsl_get_write_mask_size(write_mask);
6514 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6515 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6516 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6518 if (mask_size > 1) {
6519 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6520 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6521 } else {
6522 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6523 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6527 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6528 const struct wined3d_shader_signature *input_signature,
6529 const struct wined3d_shader_reg_maps *reg_maps,
6530 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
6532 unsigned int i;
6534 for (i = 0; i < input_signature->element_count; ++i)
6536 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6537 const char *semantic_name;
6538 UINT semantic_idx;
6539 char reg_mask[6];
6541 /* Unused */
6542 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6543 continue;
6545 semantic_name = input->semantic_name;
6546 semantic_idx = input->semantic_idx;
6547 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6549 if (args->vp_mode == vertexshader)
6551 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6553 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6554 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6556 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6558 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6560 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6562 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6563 input->register_idx, reg_mask);
6565 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6567 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6568 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6569 input->register_idx, reg_mask);
6570 else
6571 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6573 else
6575 if (input->sysval_semantic)
6576 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6577 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6578 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6579 shader_glsl_shader_input_name(gl_info),
6580 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6583 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6585 if (args->pointsprite)
6586 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6587 shader->u.ps.input_reg_map[input->register_idx]);
6588 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6589 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6590 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6591 needs_legacy_glsl_syntax(gl_info)
6592 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6593 else
6594 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6595 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6597 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6599 if (!semantic_idx)
6600 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6601 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6602 else if (semantic_idx == 1)
6603 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6604 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6605 else
6606 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6607 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6609 else
6611 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6612 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6617 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6619 struct glsl_program_key key;
6621 key.vs_id = entry->vs.id;
6622 key.hs_id = entry->hs.id;
6623 key.ds_id = entry->ds.id;
6624 key.gs_id = entry->gs.id;
6625 key.ps_id = entry->ps.id;
6626 key.cs_id = entry->cs.id;
6628 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6630 ERR("Failed to insert program entry.\n");
6634 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6635 const struct glsl_program_key *key)
6637 struct wine_rb_entry *entry;
6639 entry = wine_rb_get(&priv->program_lookup, key);
6640 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6643 /* Context activation is done by the caller. */
6644 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6645 struct glsl_shader_prog_link *entry)
6647 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6649 GL_EXTCALL(glDeleteProgram(entry->id));
6650 if (entry->vs.id)
6651 list_remove(&entry->vs.shader_entry);
6652 if (entry->hs.id)
6653 list_remove(&entry->hs.shader_entry);
6654 if (entry->ds.id)
6655 list_remove(&entry->ds.shader_entry);
6656 if (entry->gs.id)
6657 list_remove(&entry->gs.shader_entry);
6658 if (entry->ps.id)
6659 list_remove(&entry->ps.shader_entry);
6660 if (entry->cs.id)
6661 list_remove(&entry->cs.shader_entry);
6662 HeapFree(GetProcessHeap(), 0, entry);
6665 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6666 const struct wined3d_gl_info *gl_info, const DWORD *map,
6667 const struct wined3d_shader_signature *input_signature,
6668 const struct wined3d_shader_reg_maps *reg_maps_in,
6669 const struct wined3d_shader_signature *output_signature,
6670 const struct wined3d_shader_reg_maps *reg_maps_out)
6672 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6673 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6674 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6675 unsigned int in_count = vec4_varyings(3, gl_info);
6676 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6677 DWORD in_idx, *set = NULL;
6678 unsigned int i, j;
6679 char reg_mask[6];
6681 set = wined3d_calloc(max_varyings, sizeof(*set));
6683 for (i = 0; i < input_signature->element_count; ++i)
6685 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6687 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6688 continue;
6690 in_idx = map[input->register_idx];
6691 /* Declared, but not read register */
6692 if (in_idx == ~0u)
6693 continue;
6694 if (in_idx >= max_varyings)
6696 FIXME("More input varyings declared than supported, expect issues.\n");
6697 continue;
6700 if (in_idx == in_count)
6701 string_buffer_sprintf(destination, "gl_FrontColor");
6702 else if (in_idx == in_count + 1)
6703 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6704 else
6705 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6707 if (!set[in_idx])
6708 set[in_idx] = ~0u;
6710 for (j = 0; j < output_signature->element_count; ++j)
6712 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6713 DWORD mask;
6715 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6716 || input->semantic_idx != output->semantic_idx
6717 || strcmp(input->semantic_name, output->semantic_name)
6718 || !(mask = input->mask & output->mask))
6719 continue;
6721 if (set[in_idx] == ~0u)
6722 set[in_idx] = 0;
6723 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6724 shader_glsl_write_mask_to_str(mask, reg_mask);
6726 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6727 destination->buffer, reg_mask, output->register_idx, reg_mask);
6731 for (i = 0; i < max_varyings; ++i)
6733 unsigned int size;
6735 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6736 continue;
6738 if (set[i] == ~0u)
6739 set[i] = 0;
6741 size = 0;
6742 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6743 reg_mask[size++] = 'x';
6744 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6745 reg_mask[size++] = 'y';
6746 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6747 reg_mask[size++] = 'z';
6748 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6749 reg_mask[size++] = 'w';
6750 reg_mask[size] = '\0';
6752 if (i == in_count)
6753 string_buffer_sprintf(destination, "gl_FrontColor");
6754 else if (i == in_count + 1)
6755 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6756 else
6757 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6759 if (size == 1)
6760 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6761 else
6762 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6765 HeapFree(GetProcessHeap(), 0, set);
6766 string_buffer_release(&priv->string_buffers, destination);
6769 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6770 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6771 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name)
6773 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6774 char reg_mask[6];
6775 unsigned int i;
6777 for (i = 0; i < output_signature->element_count; ++i)
6779 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6781 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6782 continue;
6784 if (output->stream_idx)
6785 continue;
6787 if (output->register_idx >= input_count)
6788 continue;
6790 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6792 shader_addline(buffer, "%s.reg[%u]%s = outputs[%u]%s;\n",
6793 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
6797 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6798 const struct wined3d_gl_info *gl_info, const DWORD *map,
6799 const struct wined3d_shader_signature *input_signature,
6800 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6801 const struct wined3d_shader_signature *output_signature,
6802 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6804 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6805 const char *semantic_name;
6806 UINT semantic_idx;
6807 char reg_mask[6];
6808 unsigned int i;
6810 /* First, sort out position and point size system values. */
6811 for (i = 0; i < output_signature->element_count; ++i)
6813 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6815 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6816 continue;
6818 if (output->stream_idx)
6819 continue;
6821 semantic_name = output->semantic_name;
6822 semantic_idx = output->semantic_idx;
6823 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6825 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6827 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
6828 reg_mask, output->register_idx, reg_mask);
6830 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6832 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
6833 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6835 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6837 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
6838 output->register_idx, reg_mask);
6840 else if (output->sysval_semantic)
6842 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6846 /* Then, setup the pixel shader input. */
6847 if (reg_maps_out->shader_version.major < 4)
6848 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6849 output_signature, reg_maps_out);
6850 else
6851 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out");
6854 /* Context activation is done by the caller. */
6855 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6856 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6857 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6859 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
6860 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6861 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6862 const char *semantic_name;
6863 UINT semantic_idx;
6864 char reg_mask[6];
6865 unsigned int i;
6866 GLuint ret;
6868 string_buffer_clear(buffer);
6870 shader_glsl_add_version_declaration(buffer, gl_info);
6872 if (per_vertex_point_size)
6874 shader_addline(buffer, "uniform struct\n{\n");
6875 shader_addline(buffer, " float size_min;\n");
6876 shader_addline(buffer, " float size_max;\n");
6877 shader_addline(buffer, "} ffp_point;\n");
6880 if (ps_major < 3)
6882 DWORD colors_written_mask[2] = {0};
6883 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
6885 if (!legacy_syntax)
6887 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
6888 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
6889 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6890 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6893 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
6895 for (i = 0; i < vs->output_signature.element_count; ++i)
6897 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
6898 DWORD write_mask;
6900 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
6901 continue;
6903 semantic_name = output->semantic_name;
6904 semantic_idx = output->semantic_idx;
6905 write_mask = output->mask;
6906 shader_glsl_write_mask_to_str(write_mask, reg_mask);
6908 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
6910 if (legacy_syntax)
6911 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
6912 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
6913 else
6914 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
6915 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
6917 colors_written_mask[semantic_idx] = write_mask;
6919 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
6921 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
6922 reg_mask, output->register_idx, reg_mask);
6924 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6926 if (semantic_idx < MAX_TEXTURES)
6928 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
6929 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
6930 semantic_idx, reg_mask, output->register_idx, reg_mask);
6931 texcoords_written_mask[semantic_idx] = write_mask;
6934 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6936 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
6937 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6939 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6941 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
6942 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6943 output->register_idx, reg_mask[1]);
6947 for (i = 0; i < 2; ++i)
6949 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6951 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6952 if (!i)
6953 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6954 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
6955 reg_mask, reg_mask);
6956 else
6957 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6958 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6959 reg_mask, reg_mask);
6962 for (i = 0; i < MAX_TEXTURES; ++i)
6964 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6965 continue;
6967 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6969 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6970 && !texcoords_written_mask[i])
6971 continue;
6973 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6974 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6975 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6979 else
6981 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6983 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count);
6984 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
6985 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6986 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6989 shader_addline(buffer, "}\n");
6991 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6992 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6993 shader_glsl_compile(gl_info, ret, buffer->buffer);
6995 return ret;
6998 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
6999 const struct wined3d_shader *shader, unsigned int input_count,
7000 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup)
7002 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7003 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7005 if (rasterizer_setup)
7006 input_count = min(vec4_varyings(4, gl_info), input_count);
7008 if (input_count)
7009 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count);
7011 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7012 prefix, shader->limits->packed_output);
7014 if (rasterizer_setup)
7015 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7016 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7017 else
7018 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7019 &shader->reg_maps, "shader_out");
7021 shader_addline(buffer, "}\n");
7024 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7025 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7026 const char *reg_mask)
7028 if (!constant->sysval_semantic)
7030 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7031 return;
7034 switch (constant->sysval_semantic)
7036 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7037 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7038 case WINED3D_SV_TESS_FACTOR_LINEDET:
7039 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7040 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7041 break;
7042 case WINED3D_SV_TESS_FACTOR_QUADINT:
7043 case WINED3D_SV_TESS_FACTOR_TRIINT:
7044 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7045 break;
7046 default:
7047 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7048 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7052 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7053 const struct wined3d_shader_signature *signature, BOOL input_setup)
7055 unsigned int i, register_count, user_constant_index, user_constant_count;
7057 register_count = user_constant_count = 0;
7058 for (i = 0; i < signature->element_count; ++i)
7060 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7061 register_count = max(constant->register_idx + 1, register_count);
7062 if (!constant->sysval_semantic)
7063 ++user_constant_count;
7066 if (user_constant_count)
7067 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7068 input_setup ? "in" : "out", user_constant_count);
7069 if (input_setup)
7070 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7072 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7073 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7075 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7076 char reg_mask[6];
7078 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7080 if (input_setup)
7081 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7082 else
7083 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7085 shader_addline(buffer, " = ");
7087 if (input_setup)
7088 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7089 else
7090 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7092 shader_addline(buffer, ";\n");
7094 shader_addline(buffer, "}\n");
7097 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7098 const struct wined3d_gl_info *gl_info)
7100 const char *output = get_fragment_output(gl_info);
7102 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7103 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7104 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7105 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7106 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7107 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7110 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7111 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7113 const char *output = get_fragment_output(gl_info);
7115 switch (mode)
7117 case WINED3D_FFP_PS_FOG_OFF:
7118 return;
7120 case WINED3D_FFP_PS_FOG_LINEAR:
7121 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7122 break;
7124 case WINED3D_FFP_PS_FOG_EXP:
7125 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7126 break;
7128 case WINED3D_FFP_PS_FOG_EXP2:
7129 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7130 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7131 break;
7133 default:
7134 ERR("Invalid fog mode %#x.\n", mode);
7135 return;
7138 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7139 output, output);
7142 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7143 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7145 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7146 * flipping all the operators here, just negate the comparison below. */
7147 static const char * const comparison_operator[] =
7149 "", /* WINED3D_CMP_NEVER */
7150 "<", /* WINED3D_CMP_LESS */
7151 "==", /* WINED3D_CMP_EQUAL */
7152 "<=", /* WINED3D_CMP_LESSEQUAL */
7153 ">", /* WINED3D_CMP_GREATER */
7154 "!=", /* WINED3D_CMP_NOTEQUAL */
7155 ">=", /* WINED3D_CMP_GREATEREQUAL */
7156 "" /* WINED3D_CMP_ALWAYS */
7159 if (alpha_func == WINED3D_CMP_ALWAYS)
7160 return;
7162 if (alpha_func != WINED3D_CMP_NEVER)
7163 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7164 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7165 shader_addline(buffer, " discard;\n");
7168 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7169 const struct wined3d_gl_info *gl_info)
7171 if (gl_info->supported[ARB_GPU_SHADER5])
7172 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7173 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7174 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7175 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7176 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7177 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7178 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7179 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7180 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7181 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7182 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7183 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7184 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7185 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7186 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7187 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7188 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7189 if (gl_info->supported[ARB_TEXTURE_GATHER])
7190 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7191 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7192 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7193 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7194 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7195 if (gl_info->supported[EXT_GPU_SHADER4])
7196 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7197 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7198 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7201 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7202 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7203 const struct ps_compile_args *args)
7205 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7207 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7208 if (reg_maps->shader_version.major < 2)
7209 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7211 if (args->srgb_correction)
7212 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7214 /* SM < 3 does not replace the fog stage. */
7215 if (reg_maps->shader_version.major < 3)
7216 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7218 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7221 /* Context activation is done by the caller. */
7222 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7223 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7224 const struct wined3d_shader *shader,
7225 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7227 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7228 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7229 const char *prefix = shader_glsl_get_prefix(version->type);
7230 const struct wined3d_gl_info *gl_info = context->gl_info;
7231 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7232 unsigned int i, extra_constants_needed = 0;
7233 struct shader_glsl_ctx_priv priv_ctx;
7234 GLuint shader_id;
7235 DWORD map;
7237 memset(&priv_ctx, 0, sizeof(priv_ctx));
7238 priv_ctx.cur_ps_args = args;
7239 priv_ctx.cur_np2fixup_info = np2fixup_info;
7240 priv_ctx.string_buffers = string_buffers;
7242 shader_glsl_add_version_declaration(buffer, gl_info);
7244 shader_glsl_enable_extensions(buffer, gl_info);
7245 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7246 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7247 if (shader_glsl_use_explicit_attrib_location(gl_info))
7248 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7249 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7250 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7251 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7252 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7253 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7254 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7255 /* The spec says that it doesn't have to be explicitly enabled, but the
7256 * nvidia drivers write a warning if we don't do so. */
7257 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7258 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7260 /* Base Declarations */
7261 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7263 /* Declare uniforms for NP2 texcoord fixup:
7264 * This is NOT done inside the loop that declares the texture samplers
7265 * since the NP2 fixup code is currently only used for the GeforceFX
7266 * series and when forcing the ARB_npot extension off. Modern cards just
7267 * skip the code anyway, so put it inside a separate loop. */
7268 if (args->np2_fixup)
7270 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7271 unsigned int cur = 0;
7273 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7274 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7275 * samplerNP2Fixup stores texture dimensions and is updated through
7276 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7278 for (i = 0; i < shader->limits->sampler; ++i)
7280 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7281 continue;
7283 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7285 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7286 continue;
7289 fixup->idx[i] = cur++;
7292 fixup->num_consts = (cur + 1) >> 1;
7293 fixup->active = args->np2_fixup;
7294 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7297 if (version->major < 3 || args->vp_mode != vertexshader)
7299 shader_addline(buffer, "uniform struct\n{\n");
7300 shader_addline(buffer, " vec4 color;\n");
7301 shader_addline(buffer, " float density;\n");
7302 shader_addline(buffer, " float end;\n");
7303 shader_addline(buffer, " float scale;\n");
7304 shader_addline(buffer, "} ffp_fog;\n");
7306 if (needs_legacy_glsl_syntax(gl_info))
7308 if (glsl_is_color_reg_read(shader, 0))
7309 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7310 if (glsl_is_color_reg_read(shader, 1))
7311 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7312 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7313 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7315 else
7317 if (glsl_is_color_reg_read(shader, 0))
7318 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7319 if (glsl_is_color_reg_read(shader, 1))
7320 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7321 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7322 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7323 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7327 if (version->major >= 3)
7329 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7331 if (args->vp_mode == vertexshader && reg_maps->input_registers)
7332 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count);
7333 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7336 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7338 if (!(map & 1))
7339 continue;
7341 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7343 if (reg_maps->luminanceparams & (1u << i))
7345 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7346 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7347 extra_constants_needed++;
7350 extra_constants_needed++;
7353 if (args->srgb_correction)
7355 shader_addline(buffer, "const vec4 srgb_const0 = ");
7356 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7357 shader_addline(buffer, ";\n");
7358 shader_addline(buffer, "const vec4 srgb_const1 = ");
7359 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7360 shader_addline(buffer, ";\n");
7362 if (reg_maps->vpos || reg_maps->usesdsy)
7364 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7366 ++extra_constants_needed;
7367 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7369 if (reg_maps->vpos)
7371 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7373 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7374 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7375 args->render_offscreen ? "" : "origin_upper_left, ");
7376 else if (!args->render_offscreen)
7377 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7379 shader_addline(buffer, "vec4 vpos;\n");
7383 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7384 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7386 if (!needs_legacy_glsl_syntax(gl_info))
7388 if (shader_glsl_use_explicit_attrib_location(gl_info))
7389 shader_addline(buffer, "layout(location = 0) ");
7390 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
7393 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7394 FIXME("Insufficient uniforms to run this shader.\n");
7396 if (shader->u.ps.force_early_depth_stencil)
7397 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7399 shader_addline(buffer, "void main()\n{\n");
7401 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7402 * add approximately 0.5. This causes off-by-one problems as spotted by
7403 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7404 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7405 * causes precision troubles when we just subtract 0.5.
7407 * To deal with that, just floor() the position. This will eliminate the
7408 * fraction on all cards.
7410 * TODO: Test how this behaves with multisampling.
7412 * An advantage of floor is that it works even if the driver doesn't add
7413 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7414 * to return in gl_FragCoord, even though coordinates specify the pixel
7415 * centers instead of the pixel corners. This code will behave correctly
7416 * on drivers that returns integer values. */
7417 if (reg_maps->vpos)
7419 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7420 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7421 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7422 shader_addline(buffer,
7423 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7424 else
7425 shader_addline(buffer,
7426 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7429 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
7431 unsigned int i;
7432 WORD map = reg_maps->texcoord;
7434 if (legacy_syntax)
7436 if (glsl_is_color_reg_read(shader, 0))
7437 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7438 if (glsl_is_color_reg_read(shader, 1))
7439 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7442 for (i = 0; map; map >>= 1, ++i)
7444 if (map & 1)
7446 if (args->pointsprite)
7447 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7448 else if (args->texcoords_initialized & (1u << i))
7449 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7450 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7451 else
7452 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7453 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7457 if (legacy_syntax)
7458 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7461 /* Pack 3.0 inputs */
7462 if (reg_maps->shader_version.major >= 3)
7463 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
7465 /* Base Shader Body */
7466 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7467 return 0;
7469 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7470 if (reg_maps->shader_version.major < 4)
7471 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
7473 shader_addline(buffer, "}\n");
7475 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7476 TRACE("Compiling shader object %u.\n", shader_id);
7477 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7479 return shader_id;
7482 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7483 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7484 const struct vs_compile_args *args)
7486 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7487 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7488 unsigned int i;
7490 /* Unpack outputs. */
7491 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7493 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7494 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7495 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7496 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7498 if (reg_maps->shader_version.major < 3)
7500 if (args->fog_src == VS_FOG_Z)
7501 shader_addline(buffer, "%s = gl_Position.z;\n",
7502 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7503 else if (!reg_maps->fog)
7504 shader_addline(buffer, "%s = 0.0;\n",
7505 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7508 /* We always store the clipplanes without y inversion. */
7509 if (args->clip_enabled)
7511 if (legacy_syntax)
7512 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7513 else
7514 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7515 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7518 if (args->point_size && !args->per_vertex_point_size)
7519 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7521 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7522 shader_glsl_fixup_position(buffer);
7525 /* Context activation is done by the caller. */
7526 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
7527 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7529 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7530 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7531 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7532 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7533 const struct wined3d_gl_info *gl_info = context->gl_info;
7534 struct shader_glsl_ctx_priv priv_ctx;
7535 GLuint shader_id;
7536 unsigned int i;
7538 memset(&priv_ctx, 0, sizeof(priv_ctx));
7539 priv_ctx.cur_vs_args = args;
7540 priv_ctx.string_buffers = string_buffers;
7542 shader_glsl_add_version_declaration(buffer, gl_info);
7544 shader_glsl_enable_extensions(buffer, gl_info);
7545 if (gl_info->supported[ARB_DRAW_INSTANCED])
7546 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7547 if (shader_glsl_use_explicit_attrib_location(gl_info))
7548 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7550 /* Base Declarations */
7551 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7553 for (i = 0; i < shader->input_signature.element_count; ++i)
7554 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7556 if (args->point_size && !args->per_vertex_point_size)
7558 shader_addline(buffer, "uniform struct\n{\n");
7559 shader_addline(buffer, " float size;\n");
7560 shader_addline(buffer, " float size_min;\n");
7561 shader_addline(buffer, " float size_max;\n");
7562 shader_addline(buffer, "} ffp_point;\n");
7565 if (!needs_legacy_glsl_syntax(gl_info))
7567 if (args->clip_enabled)
7568 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7570 if (version->major < 3)
7572 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7573 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7574 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7575 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7579 if (version->major < 4)
7580 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
7582 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7583 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7585 if (reg_maps->shader_version.major >= 4)
7586 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
7587 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL);
7589 shader_addline(buffer, "void main()\n{\n");
7591 /* Base Shader Body */
7592 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7593 return 0;
7595 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7596 if (reg_maps->shader_version.major < 4)
7597 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7599 shader_addline(buffer, "}\n");
7601 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7602 TRACE("Compiling shader object %u.\n", shader_id);
7603 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7605 return shader_id;
7608 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
7609 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
7611 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7612 char reg_mask[6];
7613 unsigned int i;
7615 for (i = 0; i < output_signature->element_count; ++i)
7617 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7619 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7620 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
7621 output->register_idx, reg_mask, output->register_idx, reg_mask);
7625 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
7626 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
7627 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
7628 const char *phase_name, unsigned phase_idx)
7630 unsigned int i;
7631 HRESULT hr;
7633 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
7634 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
7635 for (i = 0; i < phase->temporary_count; ++i)
7636 shader_addline(buffer, "vec4 R%u;\n", i);
7637 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
7638 shader_addline(buffer, "}\n");
7639 return hr;
7642 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
7643 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
7645 if (phase->instance_count)
7647 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
7648 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
7649 shader_addline(buffer, "}\n");
7651 else
7653 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
7657 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
7658 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
7660 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7661 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7662 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7663 const struct wined3d_gl_info *gl_info = context->gl_info;
7664 const struct wined3d_hull_shader *hs = &shader->u.hs;
7665 const struct wined3d_shader_phase *phase;
7666 struct shader_glsl_ctx_priv priv_ctx;
7667 GLuint shader_id;
7668 unsigned int i;
7670 memset(&priv_ctx, 0, sizeof(priv_ctx));
7671 priv_ctx.string_buffers = string_buffers;
7673 shader_glsl_add_version_declaration(buffer, gl_info);
7675 shader_glsl_enable_extensions(buffer, gl_info);
7676 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7678 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7680 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
7682 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7683 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
7685 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
7687 if (hs->phases.control_point)
7689 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
7690 shader->limits->packed_output);
7691 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
7692 &shader->reg_maps, "shader_out[gl_InvocationID]");
7693 shader_addline(buffer, "}\n");
7696 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
7697 if ((phase = hs->phases.control_point))
7699 for (i = 0; i < phase->temporary_count; ++i)
7700 shader_addline(buffer, "vec4 R%u;\n", i);
7701 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
7702 return 0;
7703 shader_addline(buffer, "setup_hs_output(hs_out);\n");
7705 else
7707 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
7709 shader_addline(buffer, "}\n");
7711 for (i = 0; i < hs->phases.fork_count; ++i)
7713 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7714 &hs->phases.fork[i], "fork", i)))
7715 return 0;
7718 for (i = 0; i < hs->phases.join_count; ++i)
7720 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7721 &hs->phases.join[i], "join", i)))
7722 return 0;
7725 shader_addline(buffer, "void main()\n{\n");
7726 shader_addline(buffer, "hs_control_point_phase();\n");
7727 if (reg_maps->vocp)
7728 shader_addline(buffer, "barrier();\n");
7729 for (i = 0; i < hs->phases.fork_count; ++i)
7730 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
7731 for (i = 0; i < hs->phases.join_count; ++i)
7732 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
7733 shader_addline(buffer, "setup_patch_constant_output();\n");
7734 shader_addline(buffer, "}\n");
7736 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
7737 TRACE("Compiling shader object %u.\n", shader_id);
7738 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7740 return shader_id;
7743 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
7744 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7745 const struct ds_compile_args *args)
7747 shader_addline(buffer, "setup_ds_output(ds_out);\n");
7749 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7750 shader_glsl_fixup_position(buffer);
7753 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
7754 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
7756 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7757 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7758 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7759 const struct wined3d_gl_info *gl_info = context->gl_info;
7760 struct shader_glsl_ctx_priv priv_ctx;
7761 GLuint shader_id;
7763 memset(&priv_ctx, 0, sizeof(priv_ctx));
7764 priv_ctx.cur_ds_args = args;
7765 priv_ctx.string_buffers = string_buffers;
7767 shader_glsl_add_version_declaration(buffer, gl_info);
7769 shader_glsl_enable_extensions(buffer, gl_info);
7770 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7772 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7774 shader_addline(buffer, "layout(");
7775 switch (shader->u.ds.tessellator_domain)
7777 case WINED3D_TESSELLATOR_DOMAIN_LINE:
7778 shader_addline(buffer, "isolines");
7779 break;
7780 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
7781 shader_addline(buffer, "quads");
7782 break;
7783 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
7784 shader_addline(buffer, "triangles");
7785 break;
7787 switch (args->tessellator_output_primitive)
7789 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
7790 if (args->render_offscreen)
7791 shader_addline(buffer, ", ccw");
7792 else
7793 shader_addline(buffer, ", cw");
7794 break;
7795 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
7796 if (args->render_offscreen)
7797 shader_addline(buffer, ", cw");
7798 else
7799 shader_addline(buffer, ", ccw");
7800 break;
7801 case WINED3D_TESSELLATOR_OUTPUT_POINT:
7802 shader_addline(buffer, ", point_mode");
7803 break;
7804 case WINED3D_TESSELLATOR_OUTPUT_LINE:
7805 break;
7807 switch (args->tessellator_partitioning)
7809 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
7810 shader_addline(buffer, ", fractional_odd_spacing");
7811 break;
7812 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
7813 shader_addline(buffer, ", fractional_even_spacing");
7814 break;
7815 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
7816 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
7817 shader_addline(buffer, ", equal_spacing");
7818 break;
7820 shader_addline(buffer, ") in;\n");
7822 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7824 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7825 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7827 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
7828 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL);
7829 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
7831 shader_addline(buffer, "void main()\n{\n");
7832 shader_addline(buffer, "setup_patch_constant_input();\n");
7834 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7835 return 0;
7837 shader_addline(buffer, "}\n");
7839 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
7840 TRACE("Compiling shader object %u.\n", shader_id);
7841 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7843 return shader_id;
7846 /* Context activation is done by the caller. */
7847 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
7848 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
7850 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7851 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7852 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7853 const struct wined3d_gl_info *gl_info = context->gl_info;
7854 struct shader_glsl_ctx_priv priv_ctx;
7855 GLuint shader_id;
7857 memset(&priv_ctx, 0, sizeof(priv_ctx));
7858 priv_ctx.string_buffers = string_buffers;
7860 shader_glsl_add_version_declaration(buffer, gl_info);
7862 shader_glsl_enable_extensions(buffer, gl_info);
7864 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7866 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
7867 if (shader->u.gs.instance_count > 1)
7868 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
7869 shader_addline(buffer, ") in;\n");
7870 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
7871 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
7872 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7874 if (!gl_info->supported[ARB_CLIP_CONTROL])
7875 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7877 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info, TRUE);
7878 shader_addline(buffer, "void main()\n{\n");
7879 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7880 return 0;
7881 shader_addline(buffer, "}\n");
7883 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
7884 TRACE("Compiling shader object %u.\n", shader_id);
7885 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7887 return shader_id;
7890 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
7892 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
7893 const struct wined3d_gl_info *gl_info = ctx->gl_info;
7894 struct wined3d_string_buffer *buffer = ctx->buffer;
7895 const struct wined3d_shader *shader = ctx->shader;
7897 switch (shader->reg_maps.shader_version.type)
7899 case WINED3D_SHADER_TYPE_PIXEL:
7900 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args);
7901 break;
7902 case WINED3D_SHADER_TYPE_VERTEX:
7903 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
7904 break;
7905 case WINED3D_SHADER_TYPE_DOMAIN:
7906 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
7907 break;
7908 case WINED3D_SHADER_TYPE_GEOMETRY:
7909 case WINED3D_SHADER_TYPE_COMPUTE:
7910 break;
7911 default:
7912 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7913 break;
7917 /* Context activation is done by the caller. */
7918 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
7919 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7920 const struct wined3d_shader *shader)
7922 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
7923 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7924 const struct wined3d_gl_info *gl_info = context->gl_info;
7925 struct shader_glsl_ctx_priv priv_ctx;
7926 GLuint shader_id;
7927 unsigned int i;
7929 memset(&priv_ctx, 0, sizeof(priv_ctx));
7930 priv_ctx.string_buffers = string_buffers;
7932 shader_glsl_add_version_declaration(buffer, gl_info);
7934 shader_glsl_enable_extensions(buffer, gl_info);
7935 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
7937 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7939 for (i = 0; i < reg_maps->tgsm_count; ++i)
7941 if (reg_maps->tgsm[i].size)
7942 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
7945 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
7946 thread_group_size->x, thread_group_size->y, thread_group_size->z);
7948 shader_addline(buffer, "void main()\n{\n");
7949 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
7950 shader_addline(buffer, "}\n");
7952 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
7953 TRACE("Compiling shader object %u.\n", shader_id);
7954 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7956 return shader_id;
7959 static GLuint find_glsl_pshader(const struct wined3d_context *context,
7960 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7961 struct wined3d_shader *shader,
7962 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
7964 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
7965 struct glsl_shader_private *shader_data;
7966 struct ps_np2fixup_info *np2fixup;
7967 UINT i;
7968 DWORD new_size;
7969 GLuint ret;
7971 if (!shader->backend_data)
7973 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
7974 if (!shader->backend_data)
7976 ERR("Failed to allocate backend data.\n");
7977 return 0;
7980 shader_data = shader->backend_data;
7981 gl_shaders = shader_data->gl_shaders.ps;
7983 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7984 * so a linear search is more performant than a hashmap or a binary search
7985 * (cache coherency etc)
7987 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7989 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
7991 if (args->np2_fixup)
7992 *np2fixup_info = &gl_shaders[i].np2fixup;
7993 return gl_shaders[i].id;
7997 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7998 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
7999 if (shader_data->num_gl_shaders)
8001 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8002 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
8003 new_size * sizeof(*gl_shaders));
8005 else
8007 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8008 new_size = 1;
8011 if(!new_array) {
8012 ERR("Out of memory\n");
8013 return 0;
8015 shader_data->gl_shaders.ps = new_array;
8016 shader_data->shader_array_size = new_size;
8017 gl_shaders = new_array;
8020 gl_shaders[shader_data->num_gl_shaders].args = *args;
8022 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8023 memset(np2fixup, 0, sizeof(*np2fixup));
8024 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8026 pixelshader_update_resource_types(shader, args->tex_types);
8028 string_buffer_clear(buffer);
8029 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8030 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8032 return ret;
8035 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8036 const DWORD use_map)
8038 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
8039 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
8040 if (stored->point_size != new->point_size)
8041 return FALSE;
8042 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8043 return FALSE;
8044 if (stored->flatshading != new->flatshading)
8045 return FALSE;
8046 if (stored->next_shader_type != new->next_shader_type)
8047 return FALSE;
8048 if (stored->next_shader_input_count != new->next_shader_input_count)
8049 return FALSE;
8050 return stored->fog_src == new->fog_src;
8053 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8054 struct wined3d_shader *shader, const struct vs_compile_args *args)
8056 UINT i;
8057 DWORD new_size;
8058 DWORD use_map = context->stream_info.use_map;
8059 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8060 struct glsl_shader_private *shader_data;
8061 GLuint ret;
8063 if (!shader->backend_data)
8065 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8066 if (!shader->backend_data)
8068 ERR("Failed to allocate backend data.\n");
8069 return 0;
8072 shader_data = shader->backend_data;
8073 gl_shaders = shader_data->gl_shaders.vs;
8075 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8076 * so a linear search is more performant than a hashmap or a binary search
8077 * (cache coherency etc)
8079 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8081 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8082 return gl_shaders[i].id;
8085 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8087 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8088 if (shader_data->num_gl_shaders)
8090 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8091 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
8092 new_size * sizeof(*gl_shaders));
8094 else
8096 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8097 new_size = 1;
8100 if(!new_array) {
8101 ERR("Out of memory\n");
8102 return 0;
8104 shader_data->gl_shaders.vs = new_array;
8105 shader_data->shader_array_size = new_size;
8106 gl_shaders = new_array;
8109 gl_shaders[shader_data->num_gl_shaders].args = *args;
8111 string_buffer_clear(&priv->shader_buffer);
8112 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8113 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8115 return ret;
8118 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8119 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8121 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8122 struct glsl_shader_private *shader_data;
8123 unsigned int new_size;
8124 GLuint ret;
8126 if (!shader->backend_data)
8128 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8130 ERR("Failed to allocate backend data.\n");
8131 return 0;
8134 shader_data = shader->backend_data;
8135 gl_shaders = shader_data->gl_shaders.hs;
8137 if (shader_data->num_gl_shaders > 0)
8139 assert(shader_data->num_gl_shaders == 1);
8140 return gl_shaders[0].id;
8143 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8145 assert(!shader_data->gl_shaders.hs);
8146 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8147 new_size = 1;
8148 if (!new_array)
8150 ERR("Failed to allocate GL shaders array.\n");
8151 return 0;
8153 shader_data->gl_shaders.hs = new_array;
8154 shader_data->shader_array_size = new_size;
8155 gl_shaders = new_array;
8157 string_buffer_clear(&priv->shader_buffer);
8158 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8159 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8161 return ret;
8164 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8165 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8167 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8168 struct glsl_shader_private *shader_data;
8169 unsigned int i, new_size;
8170 GLuint ret;
8172 if (!shader->backend_data)
8174 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8176 ERR("Failed to allocate backend data.\n");
8177 return 0;
8180 shader_data = shader->backend_data;
8181 gl_shaders = shader_data->gl_shaders.ds;
8183 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8185 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8186 return gl_shaders[i].id;
8189 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8191 if (shader_data->num_gl_shaders)
8193 new_size = shader_data->shader_array_size + 1;
8194 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ds,
8195 new_size * sizeof(*new_array));
8197 else
8199 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8200 new_size = 1;
8203 if (!new_array)
8205 ERR("Failed to allocate GL shaders array.\n");
8206 return 0;
8208 shader_data->gl_shaders.ds = new_array;
8209 shader_data->shader_array_size = new_size;
8210 gl_shaders = new_array;
8212 string_buffer_clear(&priv->shader_buffer);
8213 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8214 gl_shaders[shader_data->num_gl_shaders].args = *args;
8215 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8217 return ret;
8220 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8221 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8223 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8224 struct glsl_shader_private *shader_data;
8225 unsigned int i, new_size;
8226 GLuint ret;
8228 if (!shader->backend_data)
8230 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8232 ERR("Failed to allocate backend data.\n");
8233 return 0;
8236 shader_data = shader->backend_data;
8237 gl_shaders = shader_data->gl_shaders.gs;
8239 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8241 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8242 return gl_shaders[i].id;
8245 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8247 if (shader_data->num_gl_shaders)
8249 new_size = shader_data->shader_array_size + 1;
8250 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
8251 new_size * sizeof(*new_array));
8253 else
8255 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8256 new_size = 1;
8259 if (!new_array)
8261 ERR("Failed to allocate GL shaders array.\n");
8262 return 0;
8264 shader_data->gl_shaders.gs = new_array;
8265 shader_data->shader_array_size = new_size;
8266 gl_shaders = new_array;
8268 string_buffer_clear(&priv->shader_buffer);
8269 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8270 gl_shaders[shader_data->num_gl_shaders].args = *args;
8271 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8273 return ret;
8276 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8278 switch (mcs)
8280 case WINED3D_MCS_MATERIAL:
8281 return material;
8282 case WINED3D_MCS_COLOR1:
8283 return "ffp_attrib_diffuse";
8284 case WINED3D_MCS_COLOR2:
8285 return "ffp_attrib_specular";
8286 default:
8287 ERR("Invalid material color source %#x.\n", mcs);
8288 return "<invalid>";
8292 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8293 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8295 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8296 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8297 if (settings->localviewer)
8298 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8299 else
8300 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8301 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8302 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8305 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8306 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8308 const char *diffuse, *specular, *emissive, *ambient;
8309 unsigned int i, idx;
8311 if (!settings->lighting)
8313 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8314 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8315 return;
8318 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8319 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8320 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8321 shader_addline(buffer, "vec3 dir, dst;\n");
8322 shader_addline(buffer, "float att, t;\n");
8324 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8325 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8326 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8327 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8329 idx = 0;
8330 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8332 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8333 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8334 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8335 shader_addline(buffer, "dst.x = 1.0;\n");
8336 if (legacy_lighting)
8338 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8339 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8340 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8342 else
8344 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8346 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8347 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8348 if (!legacy_lighting)
8349 shader_addline(buffer, "att = 1.0 / att;\n");
8350 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8351 if (!settings->normal)
8353 shader_addline(buffer, "}\n");
8354 continue;
8356 shader_addline(buffer, "dir = normalize(dir);\n");
8357 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8358 shader_addline(buffer, "}\n");
8361 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8363 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8364 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8365 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8366 shader_addline(buffer, "dst.x = 1.0;\n");
8367 if (legacy_lighting)
8369 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8370 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8371 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8373 else
8375 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8377 shader_addline(buffer, "dir = normalize(dir);\n");
8378 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8379 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8380 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8381 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8382 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8383 idx, idx, idx, idx);
8384 if (legacy_lighting)
8385 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8386 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8387 idx, idx, idx);
8388 else
8389 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8390 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8391 idx, idx, idx);
8392 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8393 if (!settings->normal)
8395 shader_addline(buffer, "}\n");
8396 continue;
8398 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8399 shader_addline(buffer, "}\n");
8402 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8404 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8405 if (!settings->normal)
8406 continue;
8407 shader_addline(buffer, "att = 1.0;\n");
8408 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8409 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8412 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8414 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8415 if (!settings->normal)
8416 continue;
8417 shader_addline(buffer, "att = 1.0;\n");
8418 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8419 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8422 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8423 ambient, diffuse, emissive);
8424 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8425 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8428 /* Context activation is done by the caller. */
8429 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8430 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8432 static const struct attrib_info
8434 const char type[6];
8435 const char name[24];
8437 attrib_info[] =
8439 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8440 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8441 /* TODO: Indexed vertex blending */
8442 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8443 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8444 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8445 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8446 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8448 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8449 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8450 BOOL output_legacy_fogcoord = legacy_syntax;
8451 BOOL legacy_lighting = priv->legacy_lighting;
8452 GLuint shader_obj;
8453 unsigned int i;
8455 string_buffer_clear(buffer);
8457 shader_glsl_add_version_declaration(buffer, gl_info);
8459 if (shader_glsl_use_explicit_attrib_location(gl_info))
8460 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8462 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8464 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8466 if (shader_glsl_use_explicit_attrib_location(gl_info))
8467 shader_addline(buffer, "layout(location = %u) ", i);
8468 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8470 shader_addline(buffer, "\n");
8472 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8473 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8474 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8475 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
8477 shader_addline(buffer, "uniform struct\n{\n");
8478 shader_addline(buffer, " vec4 emissive;\n");
8479 shader_addline(buffer, " vec4 ambient;\n");
8480 shader_addline(buffer, " vec4 diffuse;\n");
8481 shader_addline(buffer, " vec4 specular;\n");
8482 shader_addline(buffer, " float shininess;\n");
8483 shader_addline(buffer, "} ffp_material;\n");
8485 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8486 shader_addline(buffer, "uniform struct\n{\n");
8487 shader_addline(buffer, " vec4 diffuse;\n");
8488 shader_addline(buffer, " vec4 specular;\n");
8489 shader_addline(buffer, " vec4 ambient;\n");
8490 shader_addline(buffer, " vec4 position;\n");
8491 shader_addline(buffer, " vec3 direction;\n");
8492 shader_addline(buffer, " float range;\n");
8493 shader_addline(buffer, " float falloff;\n");
8494 shader_addline(buffer, " float c_att;\n");
8495 shader_addline(buffer, " float l_att;\n");
8496 shader_addline(buffer, " float q_att;\n");
8497 shader_addline(buffer, " float cos_htheta;\n");
8498 shader_addline(buffer, " float cos_hphi;\n");
8499 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
8501 if (settings->point_size)
8503 shader_addline(buffer, "uniform struct\n{\n");
8504 shader_addline(buffer, " float size;\n");
8505 shader_addline(buffer, " float size_min;\n");
8506 shader_addline(buffer, " float size_max;\n");
8507 shader_addline(buffer, " float c_att;\n");
8508 shader_addline(buffer, " float l_att;\n");
8509 shader_addline(buffer, " float q_att;\n");
8510 shader_addline(buffer, "} ffp_point;\n");
8513 if (legacy_syntax)
8515 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8516 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8517 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8518 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8520 else
8522 if (settings->clipping)
8523 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8525 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8526 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8527 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8528 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8531 shader_addline(buffer, "\nvoid main()\n{\n");
8532 shader_addline(buffer, "float m;\n");
8533 shader_addline(buffer, "vec3 r;\n");
8535 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
8537 if (attrib_info[i].name[0])
8538 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
8539 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
8541 for (i = 0; i < MAX_TEXTURES; ++i)
8543 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
8544 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
8545 && settings->texcoords & (1u << i))
8546 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
8549 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
8551 if (settings->transformed)
8553 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
8554 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8555 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
8557 else
8559 for (i = 0; i < settings->vertexblends; ++i)
8560 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
8562 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
8563 for (i = 0; i < settings->vertexblends + 1; ++i)
8564 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
8566 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8567 if (settings->clipping)
8569 if (legacy_syntax)
8570 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
8571 else
8572 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8573 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
8575 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
8578 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
8579 if (settings->normal)
8581 if (!settings->vertexblends)
8583 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
8585 else
8587 for (i = 0; i < settings->vertexblends + 1; ++i)
8588 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
8591 if (settings->normalize)
8592 shader_addline(buffer, "normal = normalize(normal);\n");
8595 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
8596 if (legacy_syntax)
8598 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
8599 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
8601 else
8603 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
8604 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
8607 for (i = 0; i < MAX_TEXTURES; ++i)
8609 BOOL output_legacy_texcoord = legacy_syntax;
8611 switch (settings->texgen[i] & 0xffff0000)
8613 case WINED3DTSS_TCI_PASSTHRU:
8614 if (settings->texcoords & (1u << i))
8615 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
8616 i, i, i);
8617 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
8618 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
8619 else
8620 output_legacy_texcoord = FALSE;
8621 break;
8623 case WINED3DTSS_TCI_CAMERASPACENORMAL:
8624 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
8625 break;
8627 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
8628 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
8629 break;
8631 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
8632 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8633 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
8634 break;
8636 case WINED3DTSS_TCI_SPHEREMAP:
8637 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
8638 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
8639 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8640 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
8641 break;
8643 default:
8644 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
8645 break;
8647 if (output_legacy_texcoord)
8648 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
8651 switch (settings->fog_mode)
8653 case WINED3D_FFP_VS_FOG_OFF:
8654 output_legacy_fogcoord = FALSE;
8655 break;
8657 case WINED3D_FFP_VS_FOG_FOGCOORD:
8658 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
8659 break;
8661 case WINED3D_FFP_VS_FOG_RANGE:
8662 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
8663 break;
8665 case WINED3D_FFP_VS_FOG_DEPTH:
8666 if (settings->ortho_fog)
8668 if (gl_info->supported[ARB_CLIP_CONTROL])
8669 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
8670 else
8671 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
8672 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
8674 else if (settings->transformed)
8676 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
8678 else
8680 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
8682 break;
8684 default:
8685 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
8686 break;
8688 if (output_legacy_fogcoord)
8689 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
8691 if (settings->point_size)
8693 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
8694 " + ffp_point.l_att * length(ec_pos.xyz)"
8695 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
8696 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
8697 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
8700 shader_addline(buffer, "}\n");
8702 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8703 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
8705 return shader_obj;
8708 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
8709 DWORD argnum, unsigned int stage, DWORD arg)
8711 const char *ret;
8713 if (arg == ARG_UNUSED)
8714 return "<unused arg>";
8716 switch (arg & WINED3DTA_SELECTMASK)
8718 case WINED3DTA_DIFFUSE:
8719 ret = "ffp_varying_diffuse";
8720 break;
8722 case WINED3DTA_CURRENT:
8723 ret = "ret";
8724 break;
8726 case WINED3DTA_TEXTURE:
8727 switch (stage)
8729 case 0: ret = "tex0"; break;
8730 case 1: ret = "tex1"; break;
8731 case 2: ret = "tex2"; break;
8732 case 3: ret = "tex3"; break;
8733 case 4: ret = "tex4"; break;
8734 case 5: ret = "tex5"; break;
8735 case 6: ret = "tex6"; break;
8736 case 7: ret = "tex7"; break;
8737 default:
8738 ret = "<invalid texture>";
8739 break;
8741 break;
8743 case WINED3DTA_TFACTOR:
8744 ret = "tex_factor";
8745 break;
8747 case WINED3DTA_SPECULAR:
8748 ret = "ffp_varying_specular";
8749 break;
8751 case WINED3DTA_TEMP:
8752 ret = "temp_reg";
8753 break;
8755 case WINED3DTA_CONSTANT:
8756 switch (stage)
8758 case 0: ret = "tss_const0"; break;
8759 case 1: ret = "tss_const1"; break;
8760 case 2: ret = "tss_const2"; break;
8761 case 3: ret = "tss_const3"; break;
8762 case 4: ret = "tss_const4"; break;
8763 case 5: ret = "tss_const5"; break;
8764 case 6: ret = "tss_const6"; break;
8765 case 7: ret = "tss_const7"; break;
8766 default:
8767 ret = "<invalid constant>";
8768 break;
8770 break;
8772 default:
8773 return "<unhandled arg>";
8776 if (arg & WINED3DTA_COMPLEMENT)
8778 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
8779 if (argnum == 0)
8780 ret = "arg0";
8781 else if (argnum == 1)
8782 ret = "arg1";
8783 else if (argnum == 2)
8784 ret = "arg2";
8787 if (arg & WINED3DTA_ALPHAREPLICATE)
8789 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
8790 if (argnum == 0)
8791 ret = "arg0";
8792 else if (argnum == 1)
8793 ret = "arg1";
8794 else if (argnum == 2)
8795 ret = "arg2";
8798 return ret;
8801 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
8802 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
8804 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
8806 if (color && alpha)
8807 dstmask = "";
8808 else if (color)
8809 dstmask = ".xyz";
8810 else
8811 dstmask = ".w";
8813 if (dst == tempreg)
8814 dstreg = "temp_reg";
8815 else
8816 dstreg = "ret";
8818 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
8819 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
8820 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
8822 switch (op)
8824 case WINED3D_TOP_DISABLE:
8825 break;
8827 case WINED3D_TOP_SELECT_ARG1:
8828 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
8829 break;
8831 case WINED3D_TOP_SELECT_ARG2:
8832 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
8833 break;
8835 case WINED3D_TOP_MODULATE:
8836 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8837 break;
8839 case WINED3D_TOP_MODULATE_4X:
8840 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
8841 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8842 break;
8844 case WINED3D_TOP_MODULATE_2X:
8845 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
8846 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8847 break;
8849 case WINED3D_TOP_ADD:
8850 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
8851 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8852 break;
8854 case WINED3D_TOP_ADD_SIGNED:
8855 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
8856 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8857 break;
8859 case WINED3D_TOP_ADD_SIGNED_2X:
8860 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
8861 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8862 break;
8864 case WINED3D_TOP_SUBTRACT:
8865 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
8866 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
8867 break;
8869 case WINED3D_TOP_ADD_SMOOTH:
8870 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
8871 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
8872 break;
8874 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
8875 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
8876 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8877 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8878 break;
8880 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
8881 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
8882 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8883 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8884 break;
8886 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
8887 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
8888 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8889 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8890 break;
8892 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
8893 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
8894 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8895 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
8896 break;
8898 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
8899 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
8900 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8901 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8902 break;
8904 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
8905 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
8906 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
8907 break;
8909 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
8910 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
8911 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
8912 break;
8914 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
8915 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8916 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
8917 break;
8918 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
8919 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
8920 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
8921 break;
8923 case WINED3D_TOP_BUMPENVMAP:
8924 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
8925 /* These are handled in the first pass, nothing to do. */
8926 break;
8928 case WINED3D_TOP_DOTPRODUCT3:
8929 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
8930 dstreg, dstmask, arg1, arg2, dstmask);
8931 break;
8933 case WINED3D_TOP_MULTIPLY_ADD:
8934 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
8935 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
8936 break;
8938 case WINED3D_TOP_LERP:
8939 /* MSDN isn't quite right here. */
8940 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
8941 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
8942 break;
8944 default:
8945 FIXME("Unhandled operation %#x.\n", op);
8946 break;
8950 /* Context activation is done by the caller. */
8951 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
8952 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
8954 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
8955 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
8956 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8957 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
8958 const struct wined3d_gl_info *gl_info = context->gl_info;
8959 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8960 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
8961 UINT lowest_disabled_stage;
8962 GLuint shader_id;
8963 DWORD arg0, arg1, arg2;
8964 unsigned int stage;
8966 string_buffer_clear(buffer);
8968 /* Find out which textures are read */
8969 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8971 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8972 break;
8974 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
8975 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
8976 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
8978 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
8979 || (stage == 0 && settings->color_key_enabled))
8980 tex_map |= 1u << stage;
8981 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
8982 tfactor_used = TRUE;
8983 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
8984 tempreg_used = TRUE;
8985 if (settings->op[stage].dst == tempreg)
8986 tempreg_used = TRUE;
8987 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
8988 tss_const_map |= 1u << stage;
8990 switch (settings->op[stage].cop)
8992 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
8993 lum_map |= 1u << stage;
8994 /* fall through */
8995 case WINED3D_TOP_BUMPENVMAP:
8996 bump_map |= 1u << stage;
8997 /* fall through */
8998 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
8999 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9000 tex_map |= 1u << stage;
9001 break;
9003 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9004 tfactor_used = TRUE;
9005 break;
9007 default:
9008 break;
9011 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9012 continue;
9014 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9015 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9016 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9018 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9019 tex_map |= 1u << stage;
9020 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9021 tfactor_used = TRUE;
9022 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9023 tempreg_used = TRUE;
9024 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9025 tss_const_map |= 1u << stage;
9027 lowest_disabled_stage = stage;
9029 shader_glsl_add_version_declaration(buffer, gl_info);
9031 if (shader_glsl_use_explicit_attrib_location(gl_info))
9032 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9033 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9034 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9035 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9036 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9038 if (!needs_legacy_glsl_syntax(gl_info))
9040 if (shader_glsl_use_explicit_attrib_location(gl_info))
9041 shader_addline(buffer, "layout(location = 0) ");
9042 shader_addline(buffer, "out vec4 ps_out[1];\n");
9045 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9046 shader_addline(buffer, "vec4 ret;\n");
9047 if (tempreg_used || settings->sRGB_write)
9048 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9049 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9051 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9053 const char *sampler_type;
9055 if (tss_const_map & (1u << stage))
9056 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9058 if (!(tex_map & (1u << stage)))
9059 continue;
9061 switch (settings->op[stage].tex_type)
9063 case WINED3D_GL_RES_TYPE_TEX_1D:
9064 sampler_type = "1D";
9065 break;
9066 case WINED3D_GL_RES_TYPE_TEX_2D:
9067 sampler_type = "2D";
9068 break;
9069 case WINED3D_GL_RES_TYPE_TEX_3D:
9070 sampler_type = "3D";
9071 break;
9072 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9073 sampler_type = "Cube";
9074 break;
9075 case WINED3D_GL_RES_TYPE_TEX_RECT:
9076 sampler_type = "2DRect";
9077 break;
9078 default:
9079 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9080 sampler_type = NULL;
9081 break;
9083 if (sampler_type)
9085 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9086 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9087 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9090 shader_addline(buffer, "vec4 tex%u;\n", stage);
9092 if (!(bump_map & (1u << stage)))
9093 continue;
9094 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9096 if (!(lum_map & (1u << stage)))
9097 continue;
9098 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9099 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9101 if (tfactor_used)
9102 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9103 if (settings->color_key_enabled)
9104 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9105 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9107 if (settings->sRGB_write)
9109 shader_addline(buffer, "const vec4 srgb_const0 = ");
9110 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9111 shader_addline(buffer, ";\n");
9112 shader_addline(buffer, "const vec4 srgb_const1 = ");
9113 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9114 shader_addline(buffer, ";\n");
9117 shader_addline(buffer, "uniform struct\n{\n");
9118 shader_addline(buffer, " vec4 color;\n");
9119 shader_addline(buffer, " float density;\n");
9120 shader_addline(buffer, " float end;\n");
9121 shader_addline(buffer, " float scale;\n");
9122 shader_addline(buffer, "} ffp_fog;\n");
9124 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9125 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9127 if (legacy_syntax)
9129 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9130 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9131 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9132 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9133 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9135 else
9137 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9138 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9139 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9140 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9141 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9144 shader_addline(buffer, "void main()\n{\n");
9146 if (legacy_syntax)
9148 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9149 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9152 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9154 if (tex_map & (1u << stage))
9156 if (settings->pointsprite)
9157 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9158 else if (settings->texcoords_initialized & (1u << stage))
9159 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9160 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9161 else
9162 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9166 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9167 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9169 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9170 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9172 /* Generate texture sampling instructions */
9173 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9175 const char *texture_function, *coord_mask;
9176 BOOL proj;
9178 if (!(tex_map & (1u << stage)))
9179 continue;
9181 if (settings->op[stage].projected == proj_none)
9183 proj = FALSE;
9185 else if (settings->op[stage].projected == proj_count4
9186 || settings->op[stage].projected == proj_count3)
9188 proj = TRUE;
9190 else
9192 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9193 proj = TRUE;
9196 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9197 proj = FALSE;
9199 switch (settings->op[stage].tex_type)
9201 case WINED3D_GL_RES_TYPE_TEX_1D:
9202 if (proj)
9204 texture_function = "texture1DProj";
9205 coord_mask = "xw";
9207 else
9209 texture_function = "texture1D";
9210 coord_mask = "x";
9212 break;
9213 case WINED3D_GL_RES_TYPE_TEX_2D:
9214 if (proj)
9216 texture_function = "texture2DProj";
9217 coord_mask = "xyw";
9219 else
9221 texture_function = "texture2D";
9222 coord_mask = "xy";
9224 break;
9225 case WINED3D_GL_RES_TYPE_TEX_3D:
9226 if (proj)
9228 texture_function = "texture3DProj";
9229 coord_mask = "xyzw";
9231 else
9233 texture_function = "texture3D";
9234 coord_mask = "xyz";
9236 break;
9237 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9238 texture_function = "textureCube";
9239 coord_mask = "xyz";
9240 break;
9241 case WINED3D_GL_RES_TYPE_TEX_RECT:
9242 if (proj)
9244 texture_function = "texture2DRectProj";
9245 coord_mask = "xyw";
9247 else
9249 texture_function = "texture2DRect";
9250 coord_mask = "xy";
9252 break;
9253 default:
9254 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9255 texture_function = "";
9256 coord_mask = "xyzw";
9257 break;
9259 if (!legacy_syntax)
9260 texture_function = proj ? "textureProj" : "texture";
9262 if (stage > 0
9263 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9264 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9266 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9268 /* With projective textures, texbem only divides the static
9269 * texture coord, not the displacement, so multiply the
9270 * displacement with the dividing parameter before passing it to
9271 * TXP. */
9272 if (settings->op[stage].projected != proj_none)
9274 if (settings->op[stage].projected == proj_count4)
9276 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9277 stage, stage);
9278 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9280 else
9282 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9283 stage, stage);
9284 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9287 else
9289 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9292 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9293 stage, texture_function, stage, coord_mask);
9295 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9296 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9297 stage, stage - 1, stage - 1, stage - 1);
9299 else if (settings->op[stage].projected == proj_count3)
9301 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9302 stage, texture_function, stage, stage);
9304 else
9306 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9307 stage, texture_function, stage, stage, coord_mask);
9310 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9311 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9312 settings->op[stage].color_fixup);
9315 if (settings->color_key_enabled)
9317 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9318 shader_addline(buffer, " discard;\n");
9321 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9323 /* Generate the main shader */
9324 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9326 BOOL op_equal;
9328 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9329 break;
9331 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9332 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9333 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9334 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9335 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9336 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9337 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9338 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9339 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9340 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9341 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9342 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9343 else
9344 op_equal = settings->op[stage].aop == settings->op[stage].cop
9345 && settings->op[stage].carg0 == settings->op[stage].aarg0
9346 && settings->op[stage].carg1 == settings->op[stage].aarg1
9347 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9349 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9351 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9352 settings->op[stage].cop, settings->op[stage].carg0,
9353 settings->op[stage].carg1, settings->op[stage].carg2);
9355 else if (op_equal)
9357 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
9358 settings->op[stage].cop, settings->op[stage].carg0,
9359 settings->op[stage].carg1, settings->op[stage].carg2);
9361 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9362 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9364 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9365 settings->op[stage].cop, settings->op[stage].carg0,
9366 settings->op[stage].carg1, settings->op[stage].carg2);
9367 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
9368 settings->op[stage].aop, settings->op[stage].aarg0,
9369 settings->op[stage].aarg1, settings->op[stage].aarg2);
9373 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9374 get_fragment_output(gl_info));
9376 if (settings->sRGB_write)
9377 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9379 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9381 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9383 shader_addline(buffer, "}\n");
9385 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9386 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9388 string_buffer_release(&priv->string_buffers, tex_reg_name);
9389 return shader_id;
9392 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9393 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9395 struct glsl_ffp_vertex_shader *shader;
9396 const struct wine_rb_entry *entry;
9398 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9399 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9401 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
9402 return NULL;
9404 shader->desc.settings = *settings;
9405 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9406 list_init(&shader->linked_programs);
9407 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9408 ERR("Failed to insert ffp vertex shader.\n");
9410 return shader;
9413 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9414 const struct ffp_frag_settings *args, const struct wined3d_context *context)
9416 struct glsl_ffp_fragment_shader *glsl_desc;
9417 const struct ffp_frag_desc *desc;
9419 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9420 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9422 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
9423 return NULL;
9425 glsl_desc->entry.settings = *args;
9426 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
9427 list_init(&glsl_desc->linked_programs);
9428 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9430 return glsl_desc;
9434 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9435 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9437 unsigned int i;
9438 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9440 for (i = 0; i < vs_c_count; ++i)
9442 string_buffer_sprintf(name, "vs_c[%u]", i);
9443 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9445 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9447 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9449 string_buffer_sprintf(name, "vs_i[%u]", i);
9450 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9453 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9455 string_buffer_sprintf(name, "vs_b[%u]", i);
9456 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9459 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9461 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9463 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9464 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9466 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9467 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9468 for (i = 0; i < MAX_TEXTURES; ++i)
9470 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9471 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9473 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9474 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9475 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9476 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9477 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9478 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9479 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
9481 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9482 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9483 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9484 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9485 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9486 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9487 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9488 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9489 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9490 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9491 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9492 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9493 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9494 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9495 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9496 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9497 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9498 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9499 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9500 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9501 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9502 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9503 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9504 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9506 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
9507 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
9508 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
9509 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
9510 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
9511 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
9512 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
9514 string_buffer_release(&priv->string_buffers, name);
9517 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
9518 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
9520 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9523 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
9524 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
9526 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9529 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
9530 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
9532 unsigned int i;
9533 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9535 for (i = 0; i < ps_c_count; ++i)
9537 string_buffer_sprintf(name, "ps_c[%u]", i);
9538 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9540 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
9542 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9544 string_buffer_sprintf(name, "ps_i[%u]", i);
9545 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9548 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9550 string_buffer_sprintf(name, "ps_b[%u]", i);
9551 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9554 for (i = 0; i < MAX_TEXTURES; ++i)
9556 string_buffer_sprintf(name, "bumpenv_mat%u", i);
9557 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9558 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
9559 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9560 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
9561 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9562 string_buffer_sprintf(name, "tss_const%u", i);
9563 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9566 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
9567 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
9569 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
9570 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
9571 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
9572 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
9574 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
9576 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
9577 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
9578 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
9580 string_buffer_release(&priv->string_buffers, name);
9583 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
9584 const struct wined3d_context *context, struct wined3d_shader *shader)
9586 struct glsl_context_data *ctx_data = context->shader_backend_data;
9587 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9588 const struct wined3d_gl_info *gl_info = context->gl_info;
9589 struct glsl_cs_compiled_shader *gl_shaders;
9590 struct glsl_shader_private *shader_data;
9591 struct glsl_shader_prog_link *entry;
9592 GLuint shader_id, program_id;
9594 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
9596 ERR("Out of memory.\n");
9597 return E_OUTOFMEMORY;
9600 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
9602 ERR("Failed to allocate backend data.\n");
9603 HeapFree(GetProcessHeap(), 0, entry);
9604 return E_OUTOFMEMORY;
9606 shader_data = shader->backend_data;
9607 gl_shaders = shader_data->gl_shaders.cs;
9609 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
9611 ERR("Failed to allocate GL shader array.\n");
9612 HeapFree(GetProcessHeap(), 0, entry);
9613 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9614 shader->backend_data = NULL;
9615 return E_OUTOFMEMORY;
9617 shader_data->shader_array_size = 1;
9618 gl_shaders = shader_data->gl_shaders.cs;
9620 TRACE("Compiling compute shader %p.\n", shader);
9622 string_buffer_clear(buffer);
9623 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
9624 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
9626 program_id = GL_EXTCALL(glCreateProgram());
9627 TRACE("Created new GLSL shader program %u.\n", program_id);
9629 entry->id = program_id;
9630 entry->vs.id = 0;
9631 entry->hs.id = 0;
9632 entry->ds.id = 0;
9633 entry->gs.id = 0;
9634 entry->ps.id = 0;
9635 entry->cs.id = shader_id;
9636 entry->constant_version = 0;
9637 entry->ps.np2_fixup_info = NULL;
9638 add_glsl_program_entry(priv, entry);
9640 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
9641 GL_EXTCALL(glAttachShader(program_id, shader_id));
9642 checkGLcall("glAttachShader");
9644 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
9646 TRACE("Linking GLSL shader program %u.\n", program_id);
9647 GL_EXTCALL(glLinkProgram(program_id));
9648 shader_glsl_validate_link(gl_info, program_id);
9650 GL_EXTCALL(glUseProgram(program_id));
9651 checkGLcall("glUseProgram");
9652 shader_glsl_load_program_resources(context, priv, program_id, shader);
9653 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
9655 entry->constant_update_mask = 0;
9657 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
9658 checkGLcall("glUseProgram");
9659 return WINED3D_OK;
9662 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
9663 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
9665 struct glsl_shader_private *shader_data;
9667 if (!shader->backend_data)
9669 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
9670 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
9672 ERR("Failed to compile compute shader %p.\n", shader);
9673 return 0;
9676 shader_data = shader->backend_data;
9677 return shader_data->gl_shaders.cs[0].id;
9680 /* Context activation is done by the caller. */
9681 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
9682 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9684 struct glsl_shader_prog_link *entry;
9685 struct wined3d_shader *shader;
9686 struct glsl_program_key key;
9687 GLuint cs_id;
9689 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
9690 return;
9692 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
9694 WARN("Compute shader is NULL.\n");
9695 ctx_data->glsl_program = NULL;
9696 return;
9699 cs_id = find_glsl_compute_shader(context, priv, shader);
9700 memset(&key, 0, sizeof(key));
9701 key.cs_id = cs_id;
9702 if (!(entry = get_glsl_program_entry(priv, &key)))
9703 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
9704 ctx_data->glsl_program = entry;
9707 /* Context activation is done by the caller. */
9708 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
9709 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9711 const struct wined3d_gl_info *gl_info = context->gl_info;
9712 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
9713 const struct ps_np2fixup_info *np2fixup_info = NULL;
9714 struct glsl_shader_prog_link *entry = NULL;
9715 struct wined3d_shader *hshader, *dshader, *gshader;
9716 struct wined3d_shader *vshader = NULL;
9717 struct wined3d_shader *pshader = NULL;
9718 GLuint reorder_shader_id = 0;
9719 struct glsl_program_key key;
9720 GLuint program_id;
9721 unsigned int i;
9722 GLuint vs_id = 0;
9723 GLuint hs_id = 0;
9724 GLuint ds_id = 0;
9725 GLuint gs_id = 0;
9726 GLuint ps_id = 0;
9727 struct list *ps_list, *vs_list;
9728 WORD attribs_map;
9729 struct wined3d_string_buffer *tmp_name;
9731 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
9733 vs_id = ctx_data->glsl_program->vs.id;
9734 vs_list = &ctx_data->glsl_program->vs.shader_entry;
9736 if (use_vs(state))
9737 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
9739 else if (use_vs(state))
9741 struct vs_compile_args vs_compile_args;
9743 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
9745 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
9746 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
9747 vs_list = &vshader->linked_programs;
9749 else if (priv->vertex_pipe == &glsl_vertex_pipe)
9751 struct glsl_ffp_vertex_shader *ffp_shader;
9752 struct wined3d_ffp_vs_settings settings;
9754 wined3d_ffp_get_vs_settings(context, state, &settings);
9755 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
9756 vs_id = ffp_shader->id;
9757 vs_list = &ffp_shader->linked_programs;
9760 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
9761 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
9762 hs_id = ctx_data->glsl_program->hs.id;
9763 else if (hshader)
9764 hs_id = find_glsl_hull_shader(context, priv, hshader);
9766 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
9767 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
9769 ds_id = ctx_data->glsl_program->ds.id;
9771 else if (dshader)
9773 struct ds_compile_args args;
9775 find_ds_compile_args(state, dshader, &args, context);
9776 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
9779 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
9780 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
9782 gs_id = ctx_data->glsl_program->gs.id;
9784 else if (gshader)
9786 struct gs_compile_args args;
9788 find_gs_compile_args(state, gshader, &args);
9789 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
9792 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
9794 ps_id = ctx_data->glsl_program->ps.id;
9795 ps_list = &ctx_data->glsl_program->ps.shader_entry;
9797 if (use_ps(state))
9798 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
9800 else if (use_ps(state))
9802 struct ps_compile_args ps_compile_args;
9803 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
9804 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
9805 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
9806 pshader, &ps_compile_args, &np2fixup_info);
9807 ps_list = &pshader->linked_programs;
9809 else if (priv->fragment_pipe == &glsl_fragment_pipe
9810 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
9812 struct glsl_ffp_fragment_shader *ffp_shader;
9813 struct ffp_frag_settings settings;
9815 gen_ffp_frag_op(context, state, &settings, FALSE);
9816 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
9817 ps_id = ffp_shader->id;
9818 ps_list = &ffp_shader->linked_programs;
9821 key.vs_id = vs_id;
9822 key.hs_id = hs_id;
9823 key.ds_id = ds_id;
9824 key.gs_id = gs_id;
9825 key.ps_id = ps_id;
9826 key.cs_id = 0;
9827 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
9829 ctx_data->glsl_program = entry;
9830 return;
9833 /* If we get to this point, then no matching program exists, so we create one */
9834 program_id = GL_EXTCALL(glCreateProgram());
9835 TRACE("Created new GLSL shader program %u.\n", program_id);
9837 /* Create the entry */
9838 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
9839 entry->id = program_id;
9840 entry->vs.id = vs_id;
9841 entry->hs.id = hs_id;
9842 entry->ds.id = ds_id;
9843 entry->gs.id = gs_id;
9844 entry->ps.id = ps_id;
9845 entry->cs.id = 0;
9846 entry->constant_version = 0;
9847 entry->ps.np2_fixup_info = np2fixup_info;
9848 /* Add the hash table entry */
9849 add_glsl_program_entry(priv, entry);
9851 /* Set the current program */
9852 ctx_data->glsl_program = entry;
9854 /* Attach GLSL vshader */
9855 if (vs_id)
9857 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
9858 GL_EXTCALL(glAttachShader(program_id, vs_id));
9859 checkGLcall("glAttachShader");
9861 list_add_head(vs_list, &entry->vs.shader_entry);
9864 if (vshader)
9866 attribs_map = vshader->reg_maps.input_registers;
9867 if (vshader->reg_maps.shader_version.major < 4)
9869 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
9870 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
9871 d3d_info->emulated_flatshading
9872 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
9873 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
9874 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
9875 checkGLcall("glAttachShader");
9876 /* Flag the reorder function for deletion, it will be freed
9877 * automatically when the program is destroyed. */
9878 GL_EXTCALL(glDeleteShader(reorder_shader_id));
9881 else
9883 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
9886 if (!shader_glsl_use_explicit_attrib_location(gl_info))
9888 /* Bind vertex attributes to a corresponding index number to match
9889 * the same index numbers as ARB_vertex_programs (makes loading
9890 * vertex attributes simpler). With this method, we can use the
9891 * exact same code to load the attributes later for both ARB and
9892 * GLSL shaders.
9894 * We have to do this here because we need to know the Program ID
9895 * in order to make the bindings work, and it has to be done prior
9896 * to linking the GLSL program. */
9897 tmp_name = string_buffer_get(&priv->string_buffers);
9898 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
9900 if (!(attribs_map & 1))
9901 continue;
9903 string_buffer_sprintf(tmp_name, "vs_in%u", i);
9904 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
9905 if (vshader && vshader->reg_maps.shader_version.major >= 4)
9907 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
9908 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
9909 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
9910 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
9913 checkGLcall("glBindAttribLocation");
9914 string_buffer_release(&priv->string_buffers, tmp_name);
9916 if (!needs_legacy_glsl_syntax(gl_info))
9918 GL_EXTCALL(glBindFragDataLocation(program_id, 0, "ps_out"));
9919 checkGLcall("glBindFragDataLocation");
9923 if (hshader)
9925 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
9926 GL_EXTCALL(glAttachShader(program_id, hs_id));
9927 checkGLcall("glAttachShader");
9929 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
9932 if (dshader)
9934 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
9935 GL_EXTCALL(glAttachShader(program_id, ds_id));
9936 checkGLcall("glAttachShader");
9938 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
9941 if (gshader)
9943 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
9944 GL_EXTCALL(glAttachShader(program_id, gs_id));
9945 checkGLcall("glAttachShader");
9947 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
9949 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
9952 /* Attach GLSL pshader */
9953 if (ps_id)
9955 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
9956 GL_EXTCALL(glAttachShader(program_id, ps_id));
9957 checkGLcall("glAttachShader");
9959 list_add_head(ps_list, &entry->ps.shader_entry);
9962 /* Link the program */
9963 TRACE("Linking GLSL shader program %u.\n", program_id);
9964 GL_EXTCALL(glLinkProgram(program_id));
9965 shader_glsl_validate_link(gl_info, program_id);
9967 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
9968 vshader ? vshader->limits->constant_float : 0);
9969 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
9970 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
9971 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
9972 pshader ? pshader->limits->constant_float : 0);
9973 checkGLcall("Find glsl program uniform locations");
9975 if (needs_legacy_glsl_syntax(gl_info))
9977 if (pshader && pshader->reg_maps.shader_version.major >= 3
9978 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
9980 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
9981 entry->vs.vertex_color_clamp = GL_FALSE;
9983 else
9985 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
9988 else
9990 /* With core profile we never change vertex_color_clamp from
9991 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
9992 * glClampColorARB(). */
9993 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
9996 /* Set the shader to allow uniform loading on it */
9997 GL_EXTCALL(glUseProgram(program_id));
9998 checkGLcall("glUseProgram");
10000 entry->constant_update_mask = 0;
10001 if (vshader)
10003 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10004 if (vshader->reg_maps.integer_constants)
10005 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10006 if (vshader->reg_maps.boolean_constants)
10007 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10008 if (entry->vs.pos_fixup_location != -1)
10009 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10011 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10013 else
10015 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10016 | WINED3D_SHADER_CONST_FFP_PROJ;
10018 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10020 if (entry->vs.modelview_matrix_location[i] != -1)
10022 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10023 break;
10027 for (i = 0; i < MAX_TEXTURES; ++i)
10029 if (entry->vs.texture_matrix_location[i] != -1)
10031 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10032 break;
10035 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10036 || entry->vs.material_specular_location != -1
10037 || entry->vs.material_emissive_location != -1
10038 || entry->vs.material_shininess_location != -1)
10039 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10040 if (entry->vs.light_ambient_location != -1)
10041 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10043 if (entry->vs.clip_planes_location != -1)
10044 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10045 if (entry->vs.pointsize_min_location != -1)
10046 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10048 if (hshader)
10049 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10051 if (dshader)
10053 if (entry->ds.pos_fixup_location != -1)
10054 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10056 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10059 if (gshader)
10061 if (entry->gs.pos_fixup_location != -1)
10062 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10064 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10067 if (ps_id)
10069 if (pshader)
10071 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10072 if (pshader->reg_maps.integer_constants)
10073 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10074 if (pshader->reg_maps.boolean_constants)
10075 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10076 if (entry->ps.ycorrection_location != -1)
10077 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10079 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10080 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10082 else
10084 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10086 shader_glsl_load_samplers(context, priv, program_id, NULL);
10089 for (i = 0; i < MAX_TEXTURES; ++i)
10091 if (entry->ps.bumpenv_mat_location[i] != -1)
10093 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10094 break;
10098 if (entry->ps.fog_color_location != -1)
10099 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10100 if (entry->ps.alpha_test_ref_location != -1)
10101 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10102 if (entry->ps.np2_fixup_location != -1)
10103 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10104 if (entry->ps.color_key_location != -1)
10105 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10109 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10111 struct wined3d_device *device = shader->device;
10112 struct wined3d_context *context;
10114 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10116 context = context_acquire(device, NULL, 0);
10117 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10118 context_release(context);
10122 /* Context activation is done by the caller. */
10123 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10124 const struct wined3d_state *state)
10126 struct glsl_context_data *ctx_data = context->shader_backend_data;
10127 const struct wined3d_gl_info *gl_info = context->gl_info;
10128 struct shader_glsl_priv *priv = shader_priv;
10129 GLenum current_vertex_color_clamp;
10130 GLuint program_id, prev_id;
10132 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10133 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10135 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10137 set_glsl_shader_program(context, state, priv, ctx_data);
10139 if (ctx_data->glsl_program)
10141 program_id = ctx_data->glsl_program->id;
10142 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
10144 else
10146 program_id = 0;
10147 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10150 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10152 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10153 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10155 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10156 checkGLcall("glClampColorARB");
10158 else
10160 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10164 TRACE("Using GLSL program %u.\n", program_id);
10166 if (prev_id != program_id)
10168 GL_EXTCALL(glUseProgram(program_id));
10169 checkGLcall("glUseProgram");
10171 if (program_id)
10172 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
10175 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10178 /* Context activation is done by the caller. */
10179 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10180 const struct wined3d_state *state)
10182 struct glsl_context_data *ctx_data = context->shader_backend_data;
10183 const struct wined3d_gl_info *gl_info = context->gl_info;
10184 struct shader_glsl_priv *priv = shader_priv;
10185 GLuint program_id, prev_id;
10187 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10188 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10189 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10191 TRACE("Using GLSL program %u.\n", program_id);
10193 if (prev_id != program_id)
10195 GL_EXTCALL(glUseProgram(program_id));
10196 checkGLcall("glUseProgram");
10199 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10200 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10201 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10202 | (1u << WINED3D_SHADER_TYPE_HULL)
10203 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10206 /* "context" is not necessarily the currently active context. */
10207 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10209 struct glsl_context_data *ctx_data = context->shader_backend_data;
10211 ctx_data->glsl_program = NULL;
10212 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10213 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10214 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10215 | (1u << WINED3D_SHADER_TYPE_HULL)
10216 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10217 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10220 /* Context activation is done by the caller. */
10221 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10223 struct glsl_context_data *ctx_data = context->shader_backend_data;
10224 const struct wined3d_gl_info *gl_info = context->gl_info;
10225 struct shader_glsl_priv *priv = shader_priv;
10227 shader_glsl_invalidate_current_program(context);
10228 GL_EXTCALL(glUseProgram(0));
10229 checkGLcall("glUseProgram");
10231 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10232 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10234 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10236 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10237 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10238 checkGLcall("glClampColorARB");
10242 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10243 const struct glsl_shader_prog_link *program)
10245 const struct glsl_context_data *ctx_data;
10246 struct wined3d_context *context;
10247 unsigned int i;
10249 for (i = 0; i < device->context_count; ++i)
10251 context = device->contexts[i];
10252 ctx_data = context->shader_backend_data;
10254 if (ctx_data->glsl_program == program)
10255 shader_glsl_invalidate_current_program(context);
10259 static void shader_glsl_destroy(struct wined3d_shader *shader)
10261 struct glsl_shader_private *shader_data = shader->backend_data;
10262 struct wined3d_device *device = shader->device;
10263 struct shader_glsl_priv *priv = device->shader_priv;
10264 const struct wined3d_gl_info *gl_info;
10265 const struct list *linked_programs;
10266 struct wined3d_context *context;
10268 if (!shader_data || !shader_data->num_gl_shaders)
10270 HeapFree(GetProcessHeap(), 0, shader_data);
10271 shader->backend_data = NULL;
10272 return;
10275 context = context_acquire(device, NULL, 0);
10276 gl_info = context->gl_info;
10278 TRACE("Deleting linked programs.\n");
10279 linked_programs = &shader->linked_programs;
10280 if (linked_programs->next)
10282 struct glsl_shader_prog_link *entry, *entry2;
10283 UINT i;
10285 switch (shader->reg_maps.shader_version.type)
10287 case WINED3D_SHADER_TYPE_PIXEL:
10289 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10291 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10293 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10294 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10295 checkGLcall("glDeleteShader");
10297 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
10299 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10300 struct glsl_shader_prog_link, ps.shader_entry)
10302 shader_glsl_invalidate_contexts_program(device, entry);
10303 delete_glsl_program_entry(priv, gl_info, entry);
10306 break;
10309 case WINED3D_SHADER_TYPE_VERTEX:
10311 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10313 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10315 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10316 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10317 checkGLcall("glDeleteShader");
10319 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
10321 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10322 struct glsl_shader_prog_link, vs.shader_entry)
10324 shader_glsl_invalidate_contexts_program(device, entry);
10325 delete_glsl_program_entry(priv, gl_info, entry);
10328 break;
10331 case WINED3D_SHADER_TYPE_HULL:
10333 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10335 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10337 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10338 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10339 checkGLcall("glDeleteShader");
10341 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.hs);
10343 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10344 struct glsl_shader_prog_link, hs.shader_entry)
10346 shader_glsl_invalidate_contexts_program(device, entry);
10347 delete_glsl_program_entry(priv, gl_info, entry);
10350 break;
10353 case WINED3D_SHADER_TYPE_DOMAIN:
10355 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10357 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10359 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10360 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10361 checkGLcall("glDeleteShader");
10363 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ds);
10365 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10366 struct glsl_shader_prog_link, ds.shader_entry)
10368 shader_glsl_invalidate_contexts_program(device, entry);
10369 delete_glsl_program_entry(priv, gl_info, entry);
10372 break;
10375 case WINED3D_SHADER_TYPE_GEOMETRY:
10377 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10379 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10381 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10382 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10383 checkGLcall("glDeleteShader");
10385 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
10387 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10388 struct glsl_shader_prog_link, gs.shader_entry)
10390 shader_glsl_invalidate_contexts_program(device, entry);
10391 delete_glsl_program_entry(priv, gl_info, entry);
10394 break;
10397 case WINED3D_SHADER_TYPE_COMPUTE:
10399 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10401 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10403 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10404 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10405 checkGLcall("glDeleteShader");
10407 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
10409 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10410 struct glsl_shader_prog_link, cs.shader_entry)
10412 shader_glsl_invalidate_contexts_program(device, entry);
10413 delete_glsl_program_entry(priv, gl_info, entry);
10416 break;
10419 default:
10420 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10421 break;
10425 HeapFree(GetProcessHeap(), 0, shader->backend_data);
10426 shader->backend_data = NULL;
10428 context_release(context);
10431 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10433 const struct glsl_program_key *k = key;
10434 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10435 const struct glsl_shader_prog_link, program_lookup_entry);
10437 if (k->vs_id > prog->vs.id) return 1;
10438 else if (k->vs_id < prog->vs.id) return -1;
10440 if (k->gs_id > prog->gs.id) return 1;
10441 else if (k->gs_id < prog->gs.id) return -1;
10443 if (k->ps_id > prog->ps.id) return 1;
10444 else if (k->ps_id < prog->ps.id) return -1;
10446 if (k->hs_id > prog->hs.id) return 1;
10447 else if (k->hs_id < prog->hs.id) return -1;
10449 if (k->ds_id > prog->ds.id) return 1;
10450 else if (k->ds_id < prog->ds.id) return -1;
10452 if (k->cs_id > prog->cs.id) return 1;
10453 else if (k->cs_id < prog->cs.id) return -1;
10455 return 0;
10458 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10460 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10461 + constant_count * sizeof(*heap->contained)
10462 + constant_count * sizeof(*heap->positions);
10463 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
10465 if (!mem)
10467 ERR("Failed to allocate memory\n");
10468 return FALSE;
10471 heap->entries = mem;
10472 heap->entries[1].version = 0;
10473 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10474 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10475 heap->positions = (unsigned int *)(heap->contained + constant_count);
10476 heap->size = 1;
10478 return TRUE;
10481 static void constant_heap_free(struct constant_heap *heap)
10483 HeapFree(GetProcessHeap(), 0, heap->entries);
10486 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
10487 const struct fragment_pipeline *fragment_pipe)
10489 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
10490 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
10491 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
10492 struct fragment_caps fragment_caps;
10493 void *vertex_priv, *fragment_priv;
10495 string_buffer_list_init(&priv->string_buffers);
10497 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
10499 ERR("Failed to initialize vertex pipe.\n");
10500 HeapFree(GetProcessHeap(), 0, priv);
10501 return E_FAIL;
10504 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
10506 ERR("Failed to initialize fragment pipe.\n");
10507 vertex_pipe->vp_free(device);
10508 HeapFree(GetProcessHeap(), 0, priv);
10509 return E_FAIL;
10512 if (!string_buffer_init(&priv->shader_buffer))
10514 ERR("Failed to initialize shader buffer.\n");
10515 goto fail;
10518 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
10520 ERR("Failed to allocate memory.\n");
10521 goto fail;
10524 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
10526 ERR("Failed to initialize vertex shader constant heap\n");
10527 goto fail;
10530 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
10532 ERR("Failed to initialize pixel shader constant heap\n");
10533 goto fail;
10536 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
10538 priv->next_constant_version = 1;
10539 priv->vertex_pipe = vertex_pipe;
10540 priv->fragment_pipe = fragment_pipe;
10541 fragment_pipe->get_caps(gl_info, &fragment_caps);
10542 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
10543 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
10545 device->vertex_priv = vertex_priv;
10546 device->fragment_priv = fragment_priv;
10547 device->shader_priv = priv;
10549 return WINED3D_OK;
10551 fail:
10552 constant_heap_free(&priv->pconst_heap);
10553 constant_heap_free(&priv->vconst_heap);
10554 HeapFree(GetProcessHeap(), 0, priv->stack);
10555 string_buffer_free(&priv->shader_buffer);
10556 fragment_pipe->free_private(device);
10557 vertex_pipe->vp_free(device);
10558 HeapFree(GetProcessHeap(), 0, priv);
10559 return E_OUTOFMEMORY;
10562 /* Context activation is done by the caller. */
10563 static void shader_glsl_free(struct wined3d_device *device)
10565 struct shader_glsl_priv *priv = device->shader_priv;
10567 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
10568 constant_heap_free(&priv->pconst_heap);
10569 constant_heap_free(&priv->vconst_heap);
10570 HeapFree(GetProcessHeap(), 0, priv->stack);
10571 string_buffer_list_cleanup(&priv->string_buffers);
10572 string_buffer_free(&priv->shader_buffer);
10573 priv->fragment_pipe->free_private(device);
10574 priv->vertex_pipe->vp_free(device);
10576 HeapFree(GetProcessHeap(), 0, device->shader_priv);
10577 device->shader_priv = NULL;
10580 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
10582 struct glsl_context_data *ctx_data;
10583 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
10584 return FALSE;
10585 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10586 context->shader_backend_data = ctx_data;
10587 return TRUE;
10590 static void shader_glsl_free_context_data(struct wined3d_context *context)
10592 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
10595 static void shader_glsl_init_context_state(struct wined3d_context *context)
10597 const struct wined3d_gl_info *gl_info = context->gl_info;
10599 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
10600 checkGLcall("GL_PROGRAM_POINT_SIZE");
10603 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
10605 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
10606 && gl_info->supported[WINED3D_GL_VERSION_3_2]
10607 && gl_info->supported[ARB_SAMPLER_OBJECTS]
10608 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
10609 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
10611 if (shader_model_4
10612 && gl_info->supported[ARB_COMPUTE_SHADER]
10613 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
10614 && gl_info->supported[ARB_GPU_SHADER5]
10615 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
10616 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
10617 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
10618 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
10619 && gl_info->supported[ARB_TESSELLATION_SHADER]
10620 && gl_info->supported[ARB_TEXTURE_GATHER]
10621 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
10622 return 5;
10624 if (shader_model_4)
10625 return 4;
10627 /* Support for texldd and texldl instructions in pixel shaders is required
10628 * for SM3. */
10629 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
10630 return 3;
10632 return 2;
10635 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
10637 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
10639 TRACE("Shader model %u.\n", shader_model);
10641 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
10642 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
10643 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
10644 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
10645 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
10646 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
10648 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
10649 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
10651 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
10652 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
10653 caps->varying_count = gl_info->limits.glsl_varyings;
10655 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
10656 * Direct3D minimum requirement.
10658 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
10659 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
10661 * The problem is that the refrast clamps temporary results in the shader to
10662 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
10663 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
10664 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
10665 * offer a way to query this.
10667 if (shader_model >= 4)
10668 caps->ps_1x_max_value = FLT_MAX;
10669 else
10670 caps->ps_1x_max_value = 1024.0f;
10672 /* Ideally we'd only set caps like sRGB writes here if supported by both
10673 * the shader backend and the fragment pipe, but we can get called before
10674 * shader_glsl_alloc(). */
10675 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
10676 | WINED3D_SHADER_CAP_SRGB_WRITE;
10679 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
10681 /* We support everything except YUV conversions. */
10682 return !is_complex_fixup(fixup);
10685 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
10687 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
10688 /* WINED3DSIH_ADD */ shader_glsl_binop,
10689 /* WINED3DSIH_AND */ shader_glsl_binop,
10690 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
10691 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
10692 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
10693 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
10694 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
10695 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
10696 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
10697 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
10698 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
10699 /* WINED3DSIH_BEM */ shader_glsl_bem,
10700 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
10701 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
10702 /* WINED3DSIH_BREAK */ shader_glsl_break,
10703 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
10704 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
10705 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
10706 /* WINED3DSIH_CALL */ shader_glsl_call,
10707 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
10708 /* WINED3DSIH_CASE */ shader_glsl_case,
10709 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
10710 /* WINED3DSIH_CND */ shader_glsl_cnd,
10711 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
10712 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
10713 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
10714 /* WINED3DSIH_CRS */ shader_glsl_cross,
10715 /* WINED3DSIH_CUT */ shader_glsl_cut,
10716 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
10717 /* WINED3DSIH_DCL */ shader_glsl_nop,
10718 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
10719 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
10720 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
10721 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
10722 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
10723 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
10724 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
10725 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
10726 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
10727 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
10728 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
10729 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
10730 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
10731 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
10732 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
10733 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
10734 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
10735 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
10736 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
10737 /* WINED3DSIH_DCL_INTERFACE */ NULL,
10738 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
10739 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
10740 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
10741 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
10742 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
10743 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
10744 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
10745 /* WINED3DSIH_DCL_STREAM */ NULL,
10746 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
10747 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
10748 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
10749 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
10750 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
10751 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
10752 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
10753 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
10754 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
10755 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
10756 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
10757 /* WINED3DSIH_DEF */ shader_glsl_nop,
10758 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
10759 /* WINED3DSIH_DEFB */ shader_glsl_nop,
10760 /* WINED3DSIH_DEFI */ shader_glsl_nop,
10761 /* WINED3DSIH_DIV */ shader_glsl_binop,
10762 /* WINED3DSIH_DP2 */ shader_glsl_dot,
10763 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
10764 /* WINED3DSIH_DP3 */ shader_glsl_dot,
10765 /* WINED3DSIH_DP4 */ shader_glsl_dot,
10766 /* WINED3DSIH_DST */ shader_glsl_dst,
10767 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
10768 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
10769 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
10770 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
10771 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
10772 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
10773 /* WINED3DSIH_ELSE */ shader_glsl_else,
10774 /* WINED3DSIH_EMIT */ shader_glsl_emit,
10775 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
10776 /* WINED3DSIH_ENDIF */ shader_glsl_end,
10777 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
10778 /* WINED3DSIH_ENDREP */ shader_glsl_end,
10779 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
10780 /* WINED3DSIH_EQ */ shader_glsl_relop,
10781 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
10782 /* WINED3DSIH_EXPP */ shader_glsl_expp,
10783 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
10784 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
10785 /* WINED3DSIH_FCALL */ NULL,
10786 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
10787 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
10788 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
10789 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
10790 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
10791 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
10792 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
10793 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
10794 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
10795 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
10796 /* WINED3DSIH_GE */ shader_glsl_relop,
10797 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
10798 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
10799 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
10800 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
10801 /* WINED3DSIH_IADD */ shader_glsl_binop,
10802 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
10803 /* WINED3DSIH_IEQ */ shader_glsl_relop,
10804 /* WINED3DSIH_IF */ shader_glsl_if,
10805 /* WINED3DSIH_IFC */ shader_glsl_ifc,
10806 /* WINED3DSIH_IGE */ shader_glsl_relop,
10807 /* WINED3DSIH_ILT */ shader_glsl_relop,
10808 /* WINED3DSIH_IMAD */ shader_glsl_mad,
10809 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
10810 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
10811 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
10812 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
10813 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
10814 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
10815 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
10816 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
10817 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
10818 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
10819 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
10820 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
10821 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
10822 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
10823 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
10824 /* WINED3DSIH_INE */ shader_glsl_relop,
10825 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
10826 /* WINED3DSIH_ISHL */ shader_glsl_binop,
10827 /* WINED3DSIH_ISHR */ shader_glsl_binop,
10828 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
10829 /* WINED3DSIH_LABEL */ shader_glsl_label,
10830 /* WINED3DSIH_LD */ shader_glsl_ld,
10831 /* WINED3DSIH_LD2DMS */ NULL,
10832 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
10833 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
10834 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
10835 /* WINED3DSIH_LIT */ shader_glsl_lit,
10836 /* WINED3DSIH_LOD */ NULL,
10837 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
10838 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
10839 /* WINED3DSIH_LOOP */ shader_glsl_loop,
10840 /* WINED3DSIH_LRP */ shader_glsl_lrp,
10841 /* WINED3DSIH_LT */ shader_glsl_relop,
10842 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
10843 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
10844 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
10845 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
10846 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
10847 /* WINED3DSIH_MAD */ shader_glsl_mad,
10848 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
10849 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
10850 /* WINED3DSIH_MOV */ shader_glsl_mov,
10851 /* WINED3DSIH_MOVA */ shader_glsl_mov,
10852 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
10853 /* WINED3DSIH_MUL */ shader_glsl_binop,
10854 /* WINED3DSIH_NE */ shader_glsl_relop,
10855 /* WINED3DSIH_NOP */ shader_glsl_nop,
10856 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
10857 /* WINED3DSIH_NRM */ shader_glsl_nrm,
10858 /* WINED3DSIH_OR */ shader_glsl_binop,
10859 /* WINED3DSIH_PHASE */ shader_glsl_nop,
10860 /* WINED3DSIH_POW */ shader_glsl_pow,
10861 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
10862 /* WINED3DSIH_REP */ shader_glsl_rep,
10863 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
10864 /* WINED3DSIH_RET */ shader_glsl_ret,
10865 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
10866 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
10867 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
10868 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
10869 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
10870 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
10871 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
10872 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
10873 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
10874 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
10875 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
10876 /* WINED3DSIH_SAMPLE_INFO */ NULL,
10877 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
10878 /* WINED3DSIH_SAMPLE_POS */ NULL,
10879 /* WINED3DSIH_SETP */ NULL,
10880 /* WINED3DSIH_SGE */ shader_glsl_compare,
10881 /* WINED3DSIH_SGN */ shader_glsl_sgn,
10882 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
10883 /* WINED3DSIH_SLT */ shader_glsl_compare,
10884 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
10885 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
10886 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
10887 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
10888 /* WINED3DSIH_SUB */ shader_glsl_binop,
10889 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
10890 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
10891 /* WINED3DSIH_SYNC */ shader_glsl_sync,
10892 /* WINED3DSIH_TEX */ shader_glsl_tex,
10893 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
10894 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
10895 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
10896 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
10897 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
10898 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
10899 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
10900 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
10901 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
10902 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
10903 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
10904 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
10905 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
10906 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
10907 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
10908 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
10909 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
10910 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
10911 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
10912 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
10913 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
10914 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
10915 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
10916 /* WINED3DSIH_UGE */ shader_glsl_relop,
10917 /* WINED3DSIH_ULT */ shader_glsl_relop,
10918 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
10919 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
10920 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
10921 /* WINED3DSIH_USHR */ shader_glsl_binop,
10922 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
10923 /* WINED3DSIH_XOR */ shader_glsl_binop,
10926 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
10927 SHADER_HANDLER hw_fct;
10929 /* Select handler */
10930 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
10932 /* Unhandled opcode */
10933 if (!hw_fct)
10935 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
10936 return;
10938 hw_fct(ins);
10940 shader_glsl_add_instruction_modifiers(ins);
10943 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
10945 struct shader_glsl_priv *priv = shader_priv;
10947 return priv->ffp_proj_control;
10950 const struct wined3d_shader_backend_ops glsl_shader_backend =
10952 shader_glsl_handle_instruction,
10953 shader_glsl_precompile,
10954 shader_glsl_select,
10955 shader_glsl_select_compute,
10956 shader_glsl_disable,
10957 shader_glsl_update_float_vertex_constants,
10958 shader_glsl_update_float_pixel_constants,
10959 shader_glsl_load_constants,
10960 shader_glsl_destroy,
10961 shader_glsl_alloc,
10962 shader_glsl_free,
10963 shader_glsl_allocate_context_data,
10964 shader_glsl_free_context_data,
10965 shader_glsl_init_context_state,
10966 shader_glsl_get_caps,
10967 shader_glsl_color_fixup_supported,
10968 shader_glsl_has_ffp_proj_control,
10971 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
10973 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
10975 caps->xyzrhw = TRUE;
10976 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
10977 caps->ffp_generic_attributes = TRUE;
10978 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
10979 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
10980 caps->max_vertex_blend_matrix_index = 0;
10981 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
10982 | WINED3DVTXPCAPS_MATERIALSOURCE7
10983 | WINED3DVTXPCAPS_VERTEXFOG
10984 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
10985 | WINED3DVTXPCAPS_POSITIONALLIGHTS
10986 | WINED3DVTXPCAPS_LOCALVIEWER
10987 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
10988 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
10989 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
10990 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
10993 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
10995 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10996 return GL_EXT_EMUL_ARB_MULTITEXTURE;
10997 return 0;
11000 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11002 struct shader_glsl_priv *priv;
11004 if (shader_backend == &glsl_shader_backend)
11006 priv = shader_priv;
11007 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11008 return priv;
11011 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11013 return NULL;
11016 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11018 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11019 struct glsl_ffp_vertex_shader, desc.entry);
11020 struct glsl_shader_prog_link *program, *program2;
11021 struct glsl_ffp_destroy_ctx *ctx = context;
11023 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11024 struct glsl_shader_prog_link, vs.shader_entry)
11026 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11028 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11029 HeapFree(GetProcessHeap(), 0, shader);
11032 /* Context activation is done by the caller. */
11033 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11035 struct shader_glsl_priv *priv = device->vertex_priv;
11036 struct glsl_ffp_destroy_ctx ctx;
11038 ctx.priv = priv;
11039 ctx.gl_info = &device->adapter->gl_info;
11040 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11043 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11044 const struct wined3d_state *state, DWORD state_id) {}
11046 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11047 const struct wined3d_state *state, DWORD state_id)
11049 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11052 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11053 const struct wined3d_state *state, DWORD state_id)
11055 const struct wined3d_gl_info *gl_info = context->gl_info;
11056 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11057 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11058 BOOL transformed = context->stream_info.position_transformed;
11059 BOOL wasrhw = context->last_was_rhw;
11060 unsigned int i;
11062 context->last_was_rhw = transformed;
11064 /* If the vertex declaration contains a transformed position attribute,
11065 * the draw uses the fixed function vertex pipeline regardless of any
11066 * vertex shader set by the application. */
11067 if (transformed != wasrhw
11068 || context->stream_info.swizzle_map != context->last_swizzle_map)
11069 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11071 context->last_swizzle_map = context->stream_info.swizzle_map;
11073 if (!use_vs(state))
11075 if (context->last_was_vshader)
11077 if (legacy_clip_planes)
11078 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11079 clipplane(context, state, STATE_CLIPPLANE(i));
11080 else
11081 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11084 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11086 /* Because of settings->texcoords, we have to regenerate the vertex
11087 * shader on a vdecl change if there aren't enough varyings to just
11088 * always output all the texture coordinates. */
11089 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11090 || normal != context->last_was_normal)
11091 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11093 if (use_ps(state)
11094 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11095 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11096 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11098 else
11100 if (!context->last_was_vshader)
11102 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11103 if (legacy_clip_planes)
11104 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11105 clipplane(context, state, STATE_CLIPPLANE(i));
11106 else
11107 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11111 context->last_was_vshader = use_vs(state);
11112 context->last_was_normal = normal;
11115 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11116 const struct wined3d_state *state, DWORD state_id)
11118 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11119 /* Different vertex shaders potentially require a different vertex attributes setup. */
11120 if (!isStateDirty(context, STATE_VDECL))
11121 context_apply_state(context, state, STATE_VDECL);
11124 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11125 const struct wined3d_state *state, DWORD state_id)
11127 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11128 * winding) are defined in Hull Shaders, while in GLSL those are
11129 * specified in Tessellation Evaluation Shaders. */
11130 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11132 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11133 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11136 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11137 const struct wined3d_state *state, DWORD state_id)
11139 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11140 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11141 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11142 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11143 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11146 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11147 const struct wined3d_state *state, DWORD state_id)
11149 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11150 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11151 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11152 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11153 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11154 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11155 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11158 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11159 const struct wined3d_state *state, DWORD state_id)
11161 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11164 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11165 const struct wined3d_state *state, DWORD state_id)
11167 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11170 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11172 const struct wined3d_gl_info *gl_info = context->gl_info;
11173 unsigned int k;
11175 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11176 | WINED3D_SHADER_CONST_FFP_LIGHTS
11177 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11179 if (needs_legacy_glsl_syntax(gl_info))
11181 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11183 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11184 clipplane(context, state, STATE_CLIPPLANE(k));
11187 else
11189 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11193 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11194 const struct wined3d_state *state, DWORD state_id)
11196 /* Table fog behavior depends on the projection matrix. */
11197 if (state->render_states[WINED3D_RS_FOGENABLE]
11198 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11199 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11200 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11203 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11204 const struct wined3d_state *state, DWORD state_id)
11206 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11207 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11208 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11209 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11210 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11211 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11214 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11215 const struct wined3d_state *state, DWORD state_id)
11217 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11220 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11221 const struct wined3d_state *state, DWORD state_id)
11223 DWORD sampler = state_id - STATE_SAMPLER(0);
11224 const struct wined3d_texture *texture = state->textures[sampler];
11225 BOOL np2;
11227 if (!texture)
11228 return;
11230 if (sampler >= MAX_TEXTURES)
11231 return;
11233 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11234 || context->lastWasPow2Texture & (1u << sampler))
11236 if (np2)
11237 context->lastWasPow2Texture |= 1u << sampler;
11238 else
11239 context->lastWasPow2Texture &= ~(1u << sampler);
11241 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11245 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11246 const struct wined3d_state *state, DWORD state_id)
11248 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11251 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11252 const struct wined3d_state *state, DWORD state_id)
11254 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11257 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11258 const struct wined3d_state *state, DWORD state_id)
11260 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11263 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11264 const struct wined3d_state *state, DWORD state_id)
11266 if (!use_vs(state))
11267 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11270 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11271 const struct wined3d_state *state, DWORD state_id)
11273 static unsigned int once;
11275 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11276 FIXME("Non-point sprite points not supported in core profile.\n");
11279 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11280 const struct wined3d_state *state, DWORD state_id)
11282 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11285 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11286 const struct wined3d_state *state, DWORD state_id)
11288 const struct wined3d_gl_info *gl_info = context->gl_info;
11289 UINT index = state_id - STATE_CLIPPLANE(0);
11291 if (index >= gl_info->limits.user_clip_distances)
11292 return;
11294 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11297 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11299 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11300 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11301 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11302 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11303 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11304 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11305 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11306 /* Clip planes */
11307 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11308 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11309 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11310 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11311 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11312 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11313 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11314 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11315 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11316 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11317 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11318 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11319 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11320 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11321 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11322 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11323 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11324 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
11325 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11326 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
11327 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11328 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
11329 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11330 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
11331 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11332 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
11333 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11334 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
11335 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11336 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
11337 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11338 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
11339 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11340 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
11341 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11342 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
11343 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11344 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
11345 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11346 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
11347 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11348 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
11349 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11350 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
11351 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11352 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
11353 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11354 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
11355 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11356 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
11357 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11358 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
11359 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11360 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
11361 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11362 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
11363 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11364 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
11365 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11366 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
11367 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11368 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
11369 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11370 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
11371 /* Lights */
11372 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11373 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11374 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11375 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11376 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11377 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11378 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11379 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11380 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11381 /* Viewport */
11382 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11383 /* Transform states */
11384 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11385 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11386 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11387 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11388 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11389 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11390 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11391 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11392 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11393 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11394 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11395 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11396 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11397 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11398 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11399 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11400 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11401 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11402 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11403 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11404 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11405 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11406 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11407 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11408 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11409 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11410 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11411 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11412 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11413 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11414 /* Fog */
11415 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11416 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11417 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11418 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11419 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11420 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11421 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11422 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11423 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11424 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11425 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11426 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11427 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11428 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11429 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11430 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11431 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11432 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11433 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11434 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11435 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11436 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11437 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11438 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11439 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11440 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11441 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11442 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11443 /* NP2 texture matrix fixups. They are not needed if
11444 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11445 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11446 * matrix. */
11447 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11448 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11449 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11450 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11451 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11452 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11453 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11454 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11455 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11456 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11457 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11458 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11459 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11460 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11461 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11462 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11463 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11464 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11465 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11466 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11467 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11468 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11469 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11470 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11471 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11472 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11473 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11474 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11477 /* TODO:
11478 * - Implement vertex tweening. */
11479 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11481 glsl_vertex_pipe_vp_enable,
11482 glsl_vertex_pipe_vp_get_caps,
11483 glsl_vertex_pipe_vp_get_emul_mask,
11484 glsl_vertex_pipe_vp_alloc,
11485 glsl_vertex_pipe_vp_free,
11486 glsl_vertex_pipe_vp_states,
11489 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
11491 /* Nothing to do. */
11494 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
11496 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11497 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11498 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11499 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11500 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11501 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11502 | WINED3DTEXOPCAPS_SELECTARG1
11503 | WINED3DTEXOPCAPS_SELECTARG2
11504 | WINED3DTEXOPCAPS_MODULATE4X
11505 | WINED3DTEXOPCAPS_MODULATE2X
11506 | WINED3DTEXOPCAPS_MODULATE
11507 | WINED3DTEXOPCAPS_ADDSIGNED2X
11508 | WINED3DTEXOPCAPS_ADDSIGNED
11509 | WINED3DTEXOPCAPS_ADD
11510 | WINED3DTEXOPCAPS_SUBTRACT
11511 | WINED3DTEXOPCAPS_ADDSMOOTH
11512 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11513 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11514 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11515 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11516 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
11517 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
11518 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
11519 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
11520 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
11521 | WINED3DTEXOPCAPS_DOTPRODUCT3
11522 | WINED3DTEXOPCAPS_MULTIPLYADD
11523 | WINED3DTEXOPCAPS_LERP
11524 | WINED3DTEXOPCAPS_BUMPENVMAP
11525 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
11526 caps->MaxTextureBlendStages = MAX_TEXTURES;
11527 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
11530 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
11532 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11533 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11534 return 0;
11537 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11539 struct shader_glsl_priv *priv;
11541 if (shader_backend == &glsl_shader_backend)
11543 priv = shader_priv;
11544 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
11545 return priv;
11548 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
11550 return NULL;
11553 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
11555 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11556 struct glsl_ffp_fragment_shader, entry.entry);
11557 struct glsl_shader_prog_link *program, *program2;
11558 struct glsl_ffp_destroy_ctx *ctx = context;
11560 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11561 struct glsl_shader_prog_link, ps.shader_entry)
11563 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11565 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11566 HeapFree(GetProcessHeap(), 0, shader);
11569 /* Context activation is done by the caller. */
11570 static void glsl_fragment_pipe_free(struct wined3d_device *device)
11572 struct shader_glsl_priv *priv = device->fragment_priv;
11573 struct glsl_ffp_destroy_ctx ctx;
11575 ctx.priv = priv;
11576 ctx.gl_info = &device->adapter->gl_info;
11577 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
11580 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
11581 const struct wined3d_state *state, DWORD state_id)
11583 context->last_was_pshader = use_ps(state);
11585 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11588 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
11589 const struct wined3d_state *state, DWORD state_id)
11591 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11594 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
11595 const struct wined3d_state *state, DWORD state_id)
11597 BOOL use_vshader = use_vs(state);
11598 enum fogsource new_source;
11599 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
11600 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
11602 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11604 if (!state->render_states[WINED3D_RS_FOGENABLE])
11605 return;
11607 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
11609 if (use_vshader)
11610 new_source = FOGSOURCE_VS;
11611 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
11612 new_source = FOGSOURCE_COORD;
11613 else
11614 new_source = FOGSOURCE_FFP;
11616 else
11618 new_source = FOGSOURCE_FFP;
11621 if (new_source != context->fog_source || fogstart == fogend)
11623 context->fog_source = new_source;
11624 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11628 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
11629 const struct wined3d_state *state, DWORD state_id)
11631 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11632 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11633 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11635 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
11636 glsl_fragment_pipe_fog(context, state, state_id);
11639 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
11640 const struct wined3d_state *state, DWORD state_id)
11642 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11643 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11644 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11647 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
11648 const struct wined3d_state *state, DWORD state_id)
11650 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11653 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
11654 const struct wined3d_state *state, DWORD state_id)
11656 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
11659 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
11660 const struct wined3d_state *state, DWORD state_id)
11662 const struct wined3d_gl_info *gl_info = context->gl_info;
11663 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
11664 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
11666 if (func)
11668 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
11669 checkGLcall("glAlphaFunc");
11673 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
11674 const struct wined3d_state *state, DWORD state_id)
11676 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11679 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
11680 const struct wined3d_state *state, DWORD state_id)
11682 const struct wined3d_gl_info *gl_info = context->gl_info;
11684 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
11686 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
11687 checkGLcall("glEnable(GL_ALPHA_TEST)");
11689 else
11691 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
11692 checkGLcall("glDisable(GL_ALPHA_TEST)");
11696 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
11697 const struct wined3d_state *state, DWORD state_id)
11699 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
11702 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
11703 const struct wined3d_state *state, DWORD state_id)
11705 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
11708 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
11709 const struct wined3d_state *state, DWORD state_id)
11711 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11714 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
11716 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11717 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
11718 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11719 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11720 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11721 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11722 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11723 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11724 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11725 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11726 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11727 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11728 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11729 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11730 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11731 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11732 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11733 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11734 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11735 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11736 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11737 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11738 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11739 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11740 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11741 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11742 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11743 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11744 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11745 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11746 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11747 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11748 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11749 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11750 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11751 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11752 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11753 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11754 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11755 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11756 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11757 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11758 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11759 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11760 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11761 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11762 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11763 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11764 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11765 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11766 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11767 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11768 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11769 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11770 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11771 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11772 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11773 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11774 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11775 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11776 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11777 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11778 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11779 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11780 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11781 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11782 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11783 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11784 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11785 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11786 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11787 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11788 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11789 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11790 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11791 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
11792 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
11793 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
11794 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
11795 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
11796 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
11797 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
11798 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11799 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
11800 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
11801 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11802 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11803 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
11804 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
11805 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
11806 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11807 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
11808 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
11809 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
11810 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
11811 {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 },
11812 {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 },
11813 {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 },
11814 {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 },
11815 {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 },
11816 {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 },
11817 {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 },
11818 {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 },
11819 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11820 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11821 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11822 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11823 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11824 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11825 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11826 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11827 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11828 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
11829 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
11830 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
11831 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
11834 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
11836 return TRUE;
11839 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
11843 const struct fragment_pipeline glsl_fragment_pipe =
11845 glsl_fragment_pipe_enable,
11846 glsl_fragment_pipe_get_caps,
11847 glsl_fragment_pipe_get_emul_mask,
11848 glsl_fragment_pipe_alloc,
11849 glsl_fragment_pipe_free,
11850 glsl_fragment_pipe_alloc_context_data,
11851 glsl_fragment_pipe_free_context_data,
11852 shader_glsl_color_fixup_supported,
11853 glsl_fragment_pipe_state_template,