wined3d: Allow using more than MAX_COMBINED_SAMPLERS texture image units.
[wine.git] / dlls / wined3d / glsl_shader.c
blob572d2ef9773abb08a11f38452ffb751eb003d146
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 {
119 struct wined3d_string_buffer shader_buffer;
120 struct wined3d_string_buffer_list string_buffers;
121 struct wine_rb_tree program_lookup;
122 struct constant_heap vconst_heap;
123 struct constant_heap pconst_heap;
124 unsigned char *stack;
125 UINT next_constant_version;
127 const struct wined3d_vertex_pipe_ops *vertex_pipe;
128 const struct fragment_pipeline *fragment_pipe;
129 struct wine_rb_tree ffp_vertex_shaders;
130 struct wine_rb_tree ffp_fragment_shaders;
131 BOOL ffp_proj_control;
132 BOOL legacy_lighting;
135 struct glsl_vs_program
137 struct list shader_entry;
138 GLuint id;
139 GLenum vertex_color_clamp;
140 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
141 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
142 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
143 GLint pos_fixup_location;
145 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
146 GLint projection_matrix_location;
147 GLint normal_matrix_location;
148 GLint texture_matrix_location[MAX_TEXTURES];
149 GLint material_ambient_location;
150 GLint material_diffuse_location;
151 GLint material_specular_location;
152 GLint material_emissive_location;
153 GLint material_shininess_location;
154 GLint light_ambient_location;
155 struct
157 GLint diffuse;
158 GLint specular;
159 GLint ambient;
160 GLint position;
161 GLint direction;
162 GLint range;
163 GLint falloff;
164 GLint c_att;
165 GLint l_att;
166 GLint q_att;
167 GLint cos_htheta;
168 GLint cos_hphi;
169 } light_location[MAX_ACTIVE_LIGHTS];
170 GLint pointsize_location;
171 GLint pointsize_min_location;
172 GLint pointsize_max_location;
173 GLint pointsize_c_att_location;
174 GLint pointsize_l_att_location;
175 GLint pointsize_q_att_location;
176 GLint clip_planes_location;
179 struct glsl_gs_program
181 struct list shader_entry;
182 GLuint id;
184 GLint pos_fixup_location;
187 struct glsl_ps_program
189 struct list shader_entry;
190 GLuint id;
191 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
192 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
193 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
194 GLint bumpenv_mat_location[MAX_TEXTURES];
195 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
196 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
197 GLint tss_constant_location[MAX_TEXTURES];
198 GLint tex_factor_location;
199 GLint specular_enable_location;
200 GLint fog_color_location;
201 GLint fog_density_location;
202 GLint fog_end_location;
203 GLint fog_scale_location;
204 GLint alpha_test_ref_location;
205 GLint ycorrection_location;
206 GLint np2_fixup_location;
207 GLint color_key_location;
208 const struct ps_np2fixup_info *np2_fixup_info;
211 struct glsl_cs_program
213 struct list shader_entry;
214 GLuint id;
217 /* Struct to maintain data about a linked GLSL program */
218 struct glsl_shader_prog_link
220 struct wine_rb_entry program_lookup_entry;
221 struct glsl_vs_program vs;
222 struct glsl_gs_program gs;
223 struct glsl_ps_program ps;
224 struct glsl_cs_program cs;
225 GLuint id;
226 DWORD constant_update_mask;
227 UINT constant_version;
230 struct glsl_program_key
232 GLuint vs_id;
233 GLuint gs_id;
234 GLuint ps_id;
235 GLuint cs_id;
238 struct shader_glsl_ctx_priv {
239 const struct vs_compile_args *cur_vs_args;
240 const struct ps_compile_args *cur_ps_args;
241 struct ps_np2fixup_info *cur_np2fixup_info;
242 struct wined3d_string_buffer_list *string_buffers;
245 struct glsl_context_data
247 struct glsl_shader_prog_link *glsl_program;
248 GLenum vertex_color_clamp;
251 struct glsl_ps_compiled_shader
253 struct ps_compile_args args;
254 struct ps_np2fixup_info np2fixup;
255 GLuint id;
258 struct glsl_vs_compiled_shader
260 struct vs_compile_args args;
261 GLuint id;
264 struct glsl_gs_compiled_shader
266 struct gs_compile_args args;
267 GLuint id;
270 struct glsl_cs_compiled_shader
272 GLuint id;
275 struct glsl_shader_private
277 union
279 struct glsl_vs_compiled_shader *vs;
280 struct glsl_gs_compiled_shader *gs;
281 struct glsl_ps_compiled_shader *ps;
282 struct glsl_cs_compiled_shader *cs;
283 } gl_shaders;
284 UINT num_gl_shaders, shader_array_size;
287 struct glsl_ffp_vertex_shader
289 struct wined3d_ffp_vs_desc desc;
290 GLuint id;
291 struct list linked_programs;
294 struct glsl_ffp_fragment_shader
296 struct ffp_frag_desc entry;
297 GLuint id;
298 struct list linked_programs;
301 struct glsl_ffp_destroy_ctx
303 struct shader_glsl_priv *priv;
304 const struct wined3d_gl_info *gl_info;
307 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
309 static const char *debug_gl_shader_type(GLenum type)
311 switch (type)
313 #define WINED3D_TO_STR(u) case u: return #u
314 WINED3D_TO_STR(GL_VERTEX_SHADER);
315 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
316 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
317 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
318 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
319 WINED3D_TO_STR(GL_COMPUTE_SHADER);
320 #undef WINED3D_TO_STR
321 default:
322 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
326 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
328 switch (type)
330 case WINED3D_SHADER_TYPE_VERTEX:
331 return "vs";
333 case WINED3D_SHADER_TYPE_HULL:
334 return "hs";
336 case WINED3D_SHADER_TYPE_DOMAIN:
337 return "ds";
339 case WINED3D_SHADER_TYPE_GEOMETRY:
340 return "gs";
342 case WINED3D_SHADER_TYPE_PIXEL:
343 return "ps";
345 case WINED3D_SHADER_TYPE_COMPUTE:
346 return "cs";
348 default:
349 FIXME("Unhandled shader type %#x.\n", type);
350 return "unknown";
354 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
355 const struct wined3d_shader_version *version)
357 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
358 || (version && version->type == WINED3D_SHADER_TYPE_COMPUTE))
359 return 150;
360 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
361 return 130;
362 else
363 return 120;
366 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
367 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_version *version)
369 unsigned int glsl_version = shader_glsl_get_version(gl_info, version);
370 if (glsl_version >= 150 && gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
371 shader_addline(buffer, "#version %u compatibility\n", glsl_version);
372 else
373 shader_addline(buffer, "#version %u\n", glsl_version);
376 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
378 char str[4][17];
380 wined3d_ftoa(values[0], str[0]);
381 wined3d_ftoa(values[1], str[1]);
382 wined3d_ftoa(values[2], str[2]);
383 wined3d_ftoa(values[3], str[3]);
384 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
387 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
388 const int *values, unsigned int size)
390 int i;
392 if (!size || size > 4)
394 ERR("Invalid vector size %u.\n", size);
395 return;
398 if (size > 1)
399 shader_addline(buffer, "ivec%u(", size);
401 for (i = 0; i < size; ++i)
402 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
404 if (size > 1)
405 shader_addline(buffer, ")");
408 static const char *get_info_log_line(const char **ptr)
410 const char *p, *q;
412 p = *ptr;
413 if (!(q = strstr(p, "\n")))
415 if (!*p) return NULL;
416 *ptr += strlen(p);
417 return p;
419 *ptr = q + 1;
421 return p;
424 /* Context activation is done by the caller. */
425 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
427 int length = 0;
428 char *log;
430 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
431 return;
433 if (program)
434 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
435 else
436 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
438 /* A size of 1 is just a null-terminated string, so the log should be bigger than
439 * that if there are errors. */
440 if (length > 1)
442 const char *ptr, *line;
444 log = HeapAlloc(GetProcessHeap(), 0, length);
445 /* The info log is supposed to be zero-terminated, but at least some
446 * versions of fglrx don't terminate the string properly. The reported
447 * length does include the terminator, so explicitly set it to zero
448 * here. */
449 log[length - 1] = 0;
450 if (program)
451 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
452 else
453 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
455 ptr = log;
456 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
458 WARN("Info log received from GLSL shader #%u:\n", id);
459 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
461 else
463 FIXME("Info log received from GLSL shader #%u:\n", id);
464 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
466 HeapFree(GetProcessHeap(), 0, log);
470 /* Context activation is done by the caller. */
471 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
473 const char *ptr, *line;
475 TRACE("Compiling shader object %u.\n", shader);
477 if (TRACE_ON(d3d_shader))
479 ptr = src;
480 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
483 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
484 checkGLcall("glShaderSource");
485 GL_EXTCALL(glCompileShader(shader));
486 checkGLcall("glCompileShader");
487 print_glsl_info_log(gl_info, shader, FALSE);
490 /* Context activation is done by the caller. */
491 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
493 GLint i, shader_count, source_size = -1;
494 GLuint *shaders;
495 char *source = NULL;
497 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
498 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
500 ERR("Failed to allocate shader array memory.\n");
501 return;
504 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
505 for (i = 0; i < shader_count; ++i)
507 const char *ptr, *line;
508 GLint tmp;
510 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
512 if (source_size < tmp)
514 HeapFree(GetProcessHeap(), 0, source);
516 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
517 if (!source)
519 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
520 HeapFree(GetProcessHeap(), 0, shaders);
521 return;
523 source_size = tmp;
526 FIXME("Shader %u:\n", shaders[i]);
527 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
528 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
529 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
530 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
531 FIXME("\n");
533 ptr = source;
534 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
535 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
536 FIXME("\n");
539 HeapFree(GetProcessHeap(), 0, source);
540 HeapFree(GetProcessHeap(), 0, shaders);
543 /* Context activation is done by the caller. */
544 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
546 GLint tmp;
548 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
549 return;
551 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
552 if (!tmp)
554 FIXME("Program %u link status invalid.\n", program);
555 shader_glsl_dump_program_source(gl_info, program);
558 print_glsl_info_log(gl_info, program, TRUE);
561 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
563 return !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_SHADING_LANGUAGE_420PACK];
566 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
567 struct shader_glsl_priv *priv, GLuint program_id,
568 const struct wined3d_shader_reg_maps *reg_maps)
570 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
571 struct wined3d_string_buffer *name;
572 unsigned int i, base, count;
573 GLuint block_idx;
575 if (shader_glsl_use_layout_binding_qualifier(gl_info))
576 return;
578 name = string_buffer_get(&priv->string_buffers);
579 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
580 for (i = 0; i < count; ++i)
582 if (!reg_maps->cb_sizes[i])
583 continue;
585 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
586 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
587 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
589 checkGLcall("glUniformBlockBinding");
590 string_buffer_release(&priv->string_buffers, name);
593 /* Context activation is done by the caller. */
594 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
595 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
596 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
598 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
599 unsigned int i, mapped_unit;
600 GLint name_loc;
602 for (i = 0; i < count; ++i)
604 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
605 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
606 if (name_loc == -1)
607 continue;
609 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
610 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
612 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
613 continue;
616 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
617 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
619 checkGLcall("Load sampler bindings");
620 string_buffer_release(&priv->string_buffers, sampler_name);
623 /* Context activation is done by the caller. */
624 static void shader_glsl_load_samplers(const struct wined3d_context *context,
625 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
627 const struct wined3d_shader_version *version = &reg_maps->shader_version;
628 const char *prefix = shader_glsl_get_prefix(version->type);
629 const struct wined3d_gl_info *gl_info = context->gl_info;
630 const DWORD *tex_unit_map;
631 unsigned int base, count;
633 if (reg_maps->shader_version.major >= 4)
635 tex_unit_map = NULL;
636 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, version->type, &base, &count);
638 else
640 tex_unit_map = context->tex_unit_map;
641 switch (reg_maps->shader_version.type)
643 case WINED3D_SHADER_TYPE_PIXEL:
644 base = 0;
645 count = MAX_FRAGMENT_SAMPLERS;
646 break;
647 case WINED3D_SHADER_TYPE_VERTEX:
648 base = MAX_FRAGMENT_SAMPLERS;
649 count = MAX_VERTEX_SAMPLERS;
650 break;
651 default:
652 ERR("Unhandled shader type %#x.\n", reg_maps->shader_version.type);
653 return;
657 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
660 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
661 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
663 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
665 if (icb)
667 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
668 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
669 GLint icb_location;
671 string_buffer_sprintf(icb_name, "%s_icb", prefix);
672 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
673 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
674 checkGLcall("Load immediate constant buffer");
676 string_buffer_release(&priv->string_buffers, icb_name);
680 /* Context activation is done by the caller. */
681 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
682 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
684 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
685 struct wined3d_string_buffer *name;
686 GLint location;
687 unsigned int i;
689 if (shader_glsl_use_layout_binding_qualifier(gl_info))
690 return;
692 name = string_buffer_get(&priv->string_buffers);
693 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
695 if (!reg_maps->uav_resource_info[i].type)
696 continue;
698 string_buffer_sprintf(name, "%s_image%u", prefix, i);
699 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
700 if (location == -1)
701 continue;
703 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
704 GL_EXTCALL(glUniform1i(location, i));
706 checkGLcall("Load image bindings");
707 string_buffer_release(&priv->string_buffers, name);
710 /* Context activation is done by the caller. */
711 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
712 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
714 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
716 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
717 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
718 /* Texture unit mapping is set up to be the same each time the shader
719 * program is used so we can hardcode the sampler uniform values. */
720 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
723 /* Context activation is done by the caller. */
724 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
725 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
727 unsigned int start = ~0U, end = 0;
728 int stack_idx = 0;
729 unsigned int heap_idx = 1;
730 unsigned int idx;
732 if (heap->entries[heap_idx].version <= version) return;
734 idx = heap->entries[heap_idx].idx;
735 if (constant_locations[idx] != -1)
736 start = end = idx;
737 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
739 while (stack_idx >= 0)
741 /* Note that we fall through to the next case statement. */
742 switch(stack[stack_idx])
744 case HEAP_NODE_TRAVERSE_LEFT:
746 unsigned int left_idx = heap_idx << 1;
747 if (left_idx < heap->size && heap->entries[left_idx].version > version)
749 heap_idx = left_idx;
750 idx = heap->entries[heap_idx].idx;
751 if (constant_locations[idx] != -1)
753 if (start > idx)
754 start = idx;
755 if (end < idx)
756 end = idx;
759 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
760 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
761 break;
765 case HEAP_NODE_TRAVERSE_RIGHT:
767 unsigned int right_idx = (heap_idx << 1) + 1;
768 if (right_idx < heap->size && heap->entries[right_idx].version > version)
770 heap_idx = right_idx;
771 idx = heap->entries[heap_idx].idx;
772 if (constant_locations[idx] != -1)
774 if (start > idx)
775 start = idx;
776 if (end < idx)
777 end = idx;
780 stack[stack_idx++] = HEAP_NODE_POP;
781 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
782 break;
786 case HEAP_NODE_POP:
787 heap_idx >>= 1;
788 --stack_idx;
789 break;
792 if (start <= end)
793 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
794 checkGLcall("walk_constant_heap()");
797 /* Context activation is done by the caller. */
798 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
799 GLint location, const struct wined3d_vec4 *data)
801 GLfloat clamped_constant[4];
803 if (location == -1) return;
805 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
806 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
807 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
808 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
810 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
813 /* Context activation is done by the caller. */
814 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
815 const struct wined3d_vec4 *constants, const GLint *constant_locations,
816 const struct constant_heap *heap, unsigned char *stack, DWORD version)
818 int stack_idx = 0;
819 unsigned int heap_idx = 1;
820 unsigned int idx;
822 if (heap->entries[heap_idx].version <= version) return;
824 idx = heap->entries[heap_idx].idx;
825 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
826 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
828 while (stack_idx >= 0)
830 /* Note that we fall through to the next case statement. */
831 switch(stack[stack_idx])
833 case HEAP_NODE_TRAVERSE_LEFT:
835 unsigned int left_idx = heap_idx << 1;
836 if (left_idx < heap->size && heap->entries[left_idx].version > version)
838 heap_idx = left_idx;
839 idx = heap->entries[heap_idx].idx;
840 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
842 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
843 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
844 break;
848 case HEAP_NODE_TRAVERSE_RIGHT:
850 unsigned int right_idx = (heap_idx << 1) + 1;
851 if (right_idx < heap->size && heap->entries[right_idx].version > version)
853 heap_idx = right_idx;
854 idx = heap->entries[heap_idx].idx;
855 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
857 stack[stack_idx++] = HEAP_NODE_POP;
858 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
859 break;
863 case HEAP_NODE_POP:
864 heap_idx >>= 1;
865 --stack_idx;
866 break;
869 checkGLcall("walk_constant_heap_clamped()");
872 /* Context activation is done by the caller. */
873 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
874 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
875 unsigned char *stack, unsigned int version)
877 const struct wined3d_shader_lconst *lconst;
879 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
880 if (shader->reg_maps.shader_version.major == 1
881 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
882 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
883 else
884 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
886 if (!shader->load_local_constsF)
888 TRACE("No need to load local float constants for this shader.\n");
889 return;
892 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
893 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
895 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
897 checkGLcall("glUniform4fv()");
900 /* Context activation is done by the caller. */
901 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
902 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
904 unsigned int i;
905 struct list* ptr;
907 for (i = 0; constants_set; constants_set >>= 1, ++i)
909 if (!(constants_set & 1)) continue;
911 /* We found this uniform name in the program - go ahead and send the data */
912 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
915 /* Load immediate constants */
916 ptr = list_head(&shader->constantsI);
917 while (ptr)
919 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
920 unsigned int idx = lconst->idx;
921 const GLint *values = (const GLint *)lconst->value;
923 /* We found this uniform name in the program - go ahead and send the data */
924 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
925 ptr = list_next(&shader->constantsI, ptr);
927 checkGLcall("glUniform4iv()");
930 /* Context activation is done by the caller. */
931 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
932 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
934 unsigned int i;
935 struct list* ptr;
937 for (i = 0; constants_set; constants_set >>= 1, ++i)
939 if (!(constants_set & 1)) continue;
941 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
944 /* Load immediate constants */
945 ptr = list_head(&shader->constantsB);
946 while (ptr)
948 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
949 unsigned int idx = lconst->idx;
950 const GLint *values = (const GLint *)lconst->value;
952 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
953 ptr = list_next(&shader->constantsB, ptr);
955 checkGLcall("glUniform1iv()");
958 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
960 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
963 /* Context activation is done by the caller (state handler). */
964 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
965 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
967 struct
969 float sx, sy;
971 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
972 UINT fixup = ps->np2_fixup_info->active;
973 UINT i;
975 for (i = 0; fixup; fixup >>= 1, ++i)
977 const struct wined3d_texture *tex = state->textures[i];
978 unsigned char idx = ps->np2_fixup_info->idx[i];
980 if (!tex)
982 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
983 continue;
986 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
987 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
990 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
993 /* Taken and adapted from Mesa. */
994 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
996 float pos, neg, t, det;
997 struct wined3d_matrix temp;
999 /* Calculate the determinant of upper left 3x3 submatrix and
1000 * determine if the matrix is singular. */
1001 pos = neg = 0.0f;
1002 t = in->_11 * in->_22 * in->_33;
1003 if (t >= 0.0f)
1004 pos += t;
1005 else
1006 neg += t;
1008 t = in->_21 * in->_32 * in->_13;
1009 if (t >= 0.0f)
1010 pos += t;
1011 else
1012 neg += t;
1013 t = in->_31 * in->_12 * in->_23;
1014 if (t >= 0.0f)
1015 pos += t;
1016 else
1017 neg += t;
1019 t = -in->_31 * in->_22 * in->_13;
1020 if (t >= 0.0f)
1021 pos += t;
1022 else
1023 neg += t;
1024 t = -in->_21 * in->_12 * in->_33;
1025 if (t >= 0.0f)
1026 pos += t;
1027 else
1028 neg += t;
1030 t = -in->_11 * in->_32 * in->_23;
1031 if (t >= 0.0f)
1032 pos += t;
1033 else
1034 neg += t;
1036 det = pos + neg;
1038 if (fabsf(det) < 1e-25f)
1039 return FALSE;
1041 det = 1.0f / det;
1042 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1043 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1044 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1045 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1046 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1047 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1048 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1049 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1050 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1052 *out = temp;
1053 return TRUE;
1056 static void swap_rows(float **a, float **b)
1058 float *tmp = *a;
1060 *a = *b;
1061 *b = tmp;
1064 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
1066 float wtmp[4][8];
1067 float m0, m1, m2, m3, s;
1068 float *r0, *r1, *r2, *r3;
1070 r0 = wtmp[0];
1071 r1 = wtmp[1];
1072 r2 = wtmp[2];
1073 r3 = wtmp[3];
1075 r0[0] = m->_11;
1076 r0[1] = m->_12;
1077 r0[2] = m->_13;
1078 r0[3] = m->_14;
1079 r0[4] = 1.0f;
1080 r0[5] = r0[6] = r0[7] = 0.0f;
1082 r1[0] = m->_21;
1083 r1[1] = m->_22;
1084 r1[2] = m->_23;
1085 r1[3] = m->_24;
1086 r1[5] = 1.0f;
1087 r1[4] = r1[6] = r1[7] = 0.0f;
1089 r2[0] = m->_31;
1090 r2[1] = m->_32;
1091 r2[2] = m->_33;
1092 r2[3] = m->_34;
1093 r2[6] = 1.0f;
1094 r2[4] = r2[5] = r2[7] = 0.0f;
1096 r3[0] = m->_41;
1097 r3[1] = m->_42;
1098 r3[2] = m->_43;
1099 r3[3] = m->_44;
1100 r3[7] = 1.0f;
1101 r3[4] = r3[5] = r3[6] = 0.0f;
1103 /* Choose pivot - or die. */
1104 if (fabsf(r3[0]) > fabsf(r2[0]))
1105 swap_rows(&r3, &r2);
1106 if (fabsf(r2[0]) > fabsf(r1[0]))
1107 swap_rows(&r2, &r1);
1108 if (fabsf(r1[0]) > fabsf(r0[0]))
1109 swap_rows(&r1, &r0);
1110 if (r0[0] == 0.0f)
1111 return FALSE;
1113 /* Eliminate first variable. */
1114 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1115 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1116 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1117 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1118 s = r0[4];
1119 if (s != 0.0f)
1121 r1[4] -= m1 * s;
1122 r2[4] -= m2 * s;
1123 r3[4] -= m3 * s;
1125 s = r0[5];
1126 if (s != 0.0f)
1128 r1[5] -= m1 * s;
1129 r2[5] -= m2 * s;
1130 r3[5] -= m3 * s;
1132 s = r0[6];
1133 if (s != 0.0f)
1135 r1[6] -= m1 * s;
1136 r2[6] -= m2 * s;
1137 r3[6] -= m3 * s;
1139 s = r0[7];
1140 if (s != 0.0f)
1142 r1[7] -= m1 * s;
1143 r2[7] -= m2 * s;
1144 r3[7] -= m3 * s;
1147 /* Choose pivot - or die. */
1148 if (fabsf(r3[1]) > fabsf(r2[1]))
1149 swap_rows(&r3, &r2);
1150 if (fabsf(r2[1]) > fabsf(r1[1]))
1151 swap_rows(&r2, &r1);
1152 if (r1[1] == 0.0f)
1153 return FALSE;
1155 /* Eliminate second variable. */
1156 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1157 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1158 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1159 s = r1[4];
1160 if (s != 0.0f)
1162 r2[4] -= m2 * s;
1163 r3[4] -= m3 * s;
1165 s = r1[5];
1166 if (s != 0.0f)
1168 r2[5] -= m2 * s;
1169 r3[5] -= m3 * s;
1171 s = r1[6];
1172 if (s != 0.0f)
1174 r2[6] -= m2 * s;
1175 r3[6] -= m3 * s;
1177 s = r1[7];
1178 if (s != 0.0f)
1180 r2[7] -= m2 * s;
1181 r3[7] -= m3 * s;
1184 /* Choose pivot - or die. */
1185 if (fabsf(r3[2]) > fabsf(r2[2]))
1186 swap_rows(&r3, &r2);
1187 if (r2[2] == 0.0f)
1188 return FALSE;
1190 /* Eliminate third variable. */
1191 m3 = r3[2] / r2[2];
1192 r3[3] -= m3 * r2[3];
1193 r3[4] -= m3 * r2[4];
1194 r3[5] -= m3 * r2[5];
1195 r3[6] -= m3 * r2[6];
1196 r3[7] -= m3 * r2[7];
1198 /* Last check. */
1199 if (r3[3] == 0.0f)
1200 return FALSE;
1202 /* Back substitute row 3. */
1203 s = 1.0f / r3[3];
1204 r3[4] *= s;
1205 r3[5] *= s;
1206 r3[6] *= s;
1207 r3[7] *= s;
1209 /* Back substitute row 2. */
1210 m2 = r2[3];
1211 s = 1.0f / r2[2];
1212 r2[4] = s * (r2[4] - r3[4] * m2);
1213 r2[5] = s * (r2[5] - r3[5] * m2);
1214 r2[6] = s * (r2[6] - r3[6] * m2);
1215 r2[7] = s * (r2[7] - r3[7] * m2);
1216 m1 = r1[3];
1217 r1[4] -= r3[4] * m1;
1218 r1[5] -= r3[5] * m1;
1219 r1[6] -= r3[6] * m1;
1220 r1[7] -= r3[7] * m1;
1221 m0 = r0[3];
1222 r0[4] -= r3[4] * m0;
1223 r0[5] -= r3[5] * m0;
1224 r0[6] -= r3[6] * m0;
1225 r0[7] -= r3[7] * m0;
1227 /* Back substitute row 1. */
1228 m1 = r1[2];
1229 s = 1.0f / r1[1];
1230 r1[4] = s * (r1[4] - r2[4] * m1);
1231 r1[5] = s * (r1[5] - r2[5] * m1);
1232 r1[6] = s * (r1[6] - r2[6] * m1);
1233 r1[7] = s * (r1[7] - r2[7] * m1);
1234 m0 = r0[2];
1235 r0[4] -= r2[4] * m0;
1236 r0[5] -= r2[5] * m0;
1237 r0[6] -= r2[6] * m0;
1238 r0[7] -= r2[7] * m0;
1240 /* Back substitute row 0. */
1241 m0 = r0[1];
1242 s = 1.0f / r0[0];
1243 r0[4] = s * (r0[4] - r1[4] * m0);
1244 r0[5] = s * (r0[5] - r1[5] * m0);
1245 r0[6] = s * (r0[6] - r1[6] * m0);
1246 r0[7] = s * (r0[7] - r1[7] * m0);
1248 out->_11 = r0[4];
1249 out->_12 = r0[5];
1250 out->_13 = r0[6];
1251 out->_14 = r0[7];
1252 out->_21 = r1[4];
1253 out->_22 = r1[5];
1254 out->_23 = r1[6];
1255 out->_24 = r1[7];
1256 out->_31 = r2[4];
1257 out->_32 = r2[5];
1258 out->_33 = r2[6];
1259 out->_34 = r2[7];
1260 out->_41 = r3[4];
1261 out->_42 = r3[5];
1262 out->_43 = r3[6];
1263 out->_44 = r3[7];
1265 return TRUE;
1268 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1269 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1271 const struct wined3d_gl_info *gl_info = context->gl_info;
1272 float mat[3 * 3];
1273 struct wined3d_matrix mv;
1274 unsigned int i, j;
1276 if (prog->vs.normal_matrix_location == -1)
1277 return;
1279 get_modelview_matrix(context, state, 0, &mv);
1280 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1281 invert_matrix_3d(&mv, &mv);
1282 else
1283 invert_matrix(&mv, &mv);
1284 /* Tests show that singular modelview matrices are used unchanged as normal
1285 * matrices on D3D3 and older. There seems to be no clearly consistent
1286 * behavior on newer D3D versions so always follow older ddraw behavior. */
1287 for (i = 0; i < 3; ++i)
1288 for (j = 0; j < 3; ++j)
1289 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1291 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1292 checkGLcall("glUniformMatrix3fv");
1295 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1296 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1298 const struct wined3d_gl_info *gl_info = context->gl_info;
1299 struct wined3d_matrix mat;
1301 if (tex >= MAX_TEXTURES)
1302 return;
1303 if (prog->vs.texture_matrix_location[tex] == -1)
1304 return;
1306 get_texture_matrix(context, state, tex, &mat);
1307 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1308 checkGLcall("glUniformMatrix4fv");
1311 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1312 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1314 const struct wined3d_gl_info *gl_info = context->gl_info;
1316 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1318 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1319 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1321 else
1323 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1325 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1327 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1328 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1329 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1330 checkGLcall("setting FFP material uniforms");
1333 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1334 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1336 const struct wined3d_gl_info *gl_info = context->gl_info;
1337 struct wined3d_color color;
1339 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1340 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1341 checkGLcall("glUniform3fv");
1344 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1345 const struct wined3d_matrix *src2)
1347 struct wined3d_vec4 temp;
1349 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1350 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1351 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1352 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1354 *dest = temp;
1357 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1358 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1359 struct glsl_shader_prog_link *prog)
1361 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1362 const struct wined3d_gl_info *gl_info = context->gl_info;
1363 struct wined3d_vec4 vec4;
1365 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1366 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1367 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1369 switch (light_info->OriginalParms.type)
1371 case WINED3D_LIGHT_POINT:
1372 multiply_vector_matrix(&vec4, &light_info->position, view);
1373 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1374 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1375 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1376 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1377 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1378 break;
1380 case WINED3D_LIGHT_SPOT:
1381 multiply_vector_matrix(&vec4, &light_info->position, view);
1382 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1384 multiply_vector_matrix(&vec4, &light_info->direction, view);
1385 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1387 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1388 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1389 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1390 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1391 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1392 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1393 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1394 break;
1396 case WINED3D_LIGHT_DIRECTIONAL:
1397 multiply_vector_matrix(&vec4, &light_info->direction, view);
1398 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1399 break;
1401 case WINED3D_LIGHT_PARALLELPOINT:
1402 multiply_vector_matrix(&vec4, &light_info->position, view);
1403 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1404 break;
1406 default:
1407 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1409 checkGLcall("setting FFP lights uniforms");
1412 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1413 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1415 const struct wined3d_gl_info *gl_info = context->gl_info;
1416 float min, max;
1417 float size, att[3];
1419 get_pointsize_minmax(context, state, &min, &max);
1421 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1422 checkGLcall("glUniform1f");
1423 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1424 checkGLcall("glUniform1f");
1426 get_pointsize(context, state, &size, att);
1428 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1429 checkGLcall("glUniform1f");
1430 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1431 checkGLcall("glUniform1f");
1432 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1433 checkGLcall("glUniform1f");
1434 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1435 checkGLcall("glUniform1f");
1438 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1439 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1441 const struct wined3d_gl_info *gl_info = context->gl_info;
1442 struct wined3d_color color;
1443 float start, end, scale;
1444 union
1446 DWORD d;
1447 float f;
1448 } tmpvalue;
1450 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1451 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1452 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1453 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1454 get_fog_start_end(context, state, &start, &end);
1455 scale = 1.0f / (end - start);
1456 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1457 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1458 checkGLcall("fog emulation uniforms");
1461 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1462 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1464 const struct wined3d_gl_info *gl_info = context->gl_info;
1465 struct wined3d_vec4 plane;
1467 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1468 if (!use_vs(state))
1469 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1470 else
1471 plane = state->clip_planes[index];
1473 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1476 /* Context activation is done by the caller (state handler). */
1477 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1478 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1480 struct wined3d_color float_key[2];
1481 const struct wined3d_texture *texture = state->textures[0];
1483 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1484 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1487 /* Context activation is done by the caller (state handler). */
1488 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1489 const struct wined3d_state *state)
1491 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1492 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1493 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1494 const struct wined3d_gl_info *gl_info = context->gl_info;
1495 struct shader_glsl_priv *priv = shader_priv;
1496 float position_fixup[4];
1497 DWORD update_mask;
1499 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1500 UINT constant_version;
1501 int i;
1503 if (!prog) {
1504 /* No GLSL program set - nothing to do. */
1505 return;
1507 constant_version = prog->constant_version;
1508 update_mask = context->constant_update_mask & prog->constant_update_mask;
1510 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1511 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1512 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1514 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1515 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1516 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1518 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1519 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1520 vshader->reg_maps.boolean_constants);
1522 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1524 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1525 shader_glsl_clip_plane_uniform(context, state, i, prog);
1528 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1529 shader_glsl_pointsize_uniform(context, state, prog);
1531 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1533 shader_get_position_fixup(context, state, position_fixup);
1534 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1535 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1536 else
1537 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1538 checkGLcall("glUniform4fv");
1541 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1543 struct wined3d_matrix mat;
1545 get_modelview_matrix(context, state, 0, &mat);
1546 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1547 checkGLcall("glUniformMatrix4fv");
1549 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1552 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1554 struct wined3d_matrix mat;
1556 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1558 if (prog->vs.modelview_matrix_location[i] == -1)
1559 break;
1561 get_modelview_matrix(context, state, i, &mat);
1562 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1563 checkGLcall("glUniformMatrix4fv");
1567 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1569 struct wined3d_matrix projection;
1571 get_projection_matrix(context, state, &projection);
1572 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1573 checkGLcall("glUniformMatrix4fv");
1576 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1578 for (i = 0; i < MAX_TEXTURES; ++i)
1579 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1582 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1583 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1585 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1587 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1588 DWORD point_count = 0;
1589 DWORD spot_count = 0;
1590 DWORD directional_count = 0;
1591 DWORD parallel_point_count = 0;
1593 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1595 if (!state->lights[i])
1596 continue;
1598 switch (state->lights[i]->OriginalParms.type)
1600 case WINED3D_LIGHT_POINT:
1601 ++point_count;
1602 break;
1603 case WINED3D_LIGHT_SPOT:
1604 ++spot_count;
1605 break;
1606 case WINED3D_LIGHT_DIRECTIONAL:
1607 ++directional_count;
1608 break;
1609 case WINED3D_LIGHT_PARALLELPOINT:
1610 ++parallel_point_count;
1611 break;
1612 default:
1613 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1614 break;
1617 point_idx = 0;
1618 spot_idx = point_idx + point_count;
1619 directional_idx = spot_idx + spot_count;
1620 parallel_point_idx = directional_idx + directional_count;
1622 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1623 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1625 const struct wined3d_light_info *light_info = state->lights[i];
1626 unsigned int idx;
1628 if (!light_info)
1629 continue;
1631 switch (light_info->OriginalParms.type)
1633 case WINED3D_LIGHT_POINT:
1634 idx = point_idx++;
1635 break;
1636 case WINED3D_LIGHT_SPOT:
1637 idx = spot_idx++;
1638 break;
1639 case WINED3D_LIGHT_DIRECTIONAL:
1640 idx = directional_idx++;
1641 break;
1642 case WINED3D_LIGHT_PARALLELPOINT:
1643 idx = parallel_point_idx++;
1644 break;
1645 default:
1646 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1647 continue;
1649 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1653 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1654 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1655 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1657 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1658 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1659 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1661 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1662 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1663 pshader->reg_maps.boolean_constants);
1665 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1667 for (i = 0; i < MAX_TEXTURES; ++i)
1669 if (prog->ps.bumpenv_mat_location[i] == -1)
1670 continue;
1672 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1673 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1675 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1677 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1678 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1679 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1680 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1684 checkGLcall("bump env uniforms");
1687 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1689 const struct wined3d_vec4 correction_params =
1691 /* Position is relative to the framebuffer, not the viewport. */
1692 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1693 context->render_offscreen ? 1.0f : -1.0f,
1694 0.0f,
1695 0.0f,
1698 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1701 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1702 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1703 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1704 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1706 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1708 struct wined3d_color color;
1710 if (prog->ps.tex_factor_location != -1)
1712 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1713 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1716 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1717 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1718 else
1719 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1721 for (i = 0; i < MAX_TEXTURES; ++i)
1723 if (prog->ps.tss_constant_location[i] == -1)
1724 continue;
1726 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1727 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1730 checkGLcall("fixed function uniforms");
1733 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1734 shader_glsl_load_fog_uniform(context, state, prog);
1736 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1738 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1740 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1741 checkGLcall("alpha test emulation uniform");
1744 if (priv->next_constant_version == UINT_MAX)
1746 TRACE("Max constant version reached, resetting to 0.\n");
1747 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1748 priv->next_constant_version = 1;
1750 else
1752 prog->constant_version = priv->next_constant_version++;
1756 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1758 struct constant_entry *entries = heap->entries;
1759 unsigned int *positions = heap->positions;
1760 unsigned int heap_idx, parent_idx;
1762 if (!heap->contained[idx])
1764 heap_idx = heap->size++;
1765 heap->contained[idx] = TRUE;
1767 else
1769 heap_idx = positions[idx];
1772 while (heap_idx > 1)
1774 parent_idx = heap_idx >> 1;
1776 if (new_version <= entries[parent_idx].version) break;
1778 entries[heap_idx] = entries[parent_idx];
1779 positions[entries[parent_idx].idx] = heap_idx;
1780 heap_idx = parent_idx;
1783 entries[heap_idx].version = new_version;
1784 entries[heap_idx].idx = idx;
1785 positions[idx] = heap_idx;
1788 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1790 struct shader_glsl_priv *priv = device->shader_priv;
1791 struct constant_heap *heap = &priv->vconst_heap;
1792 UINT i;
1794 for (i = start; i < count + start; ++i)
1796 update_heap_entry(heap, i, priv->next_constant_version);
1800 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1802 struct shader_glsl_priv *priv = device->shader_priv;
1803 struct constant_heap *heap = &priv->pconst_heap;
1804 UINT i;
1806 for (i = start; i < count + start; ++i)
1808 update_heap_entry(heap, i, priv->next_constant_version);
1812 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1814 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1815 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1816 if(shader_major > 3) return ret;
1818 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1819 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1820 return ret;
1823 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1825 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1828 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1830 return !needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION];
1833 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1835 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1838 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1839 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1841 va_list args;
1842 int ret;
1844 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1845 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1846 for (;;)
1848 va_start(args, format);
1849 ret = shader_vaddline(buffer, format, args);
1850 va_end(args);
1851 if (!ret)
1852 return;
1853 if (!string_buffer_resize(buffer, ret))
1854 return;
1858 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1859 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1861 va_list args;
1862 int ret;
1864 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1865 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1866 for (;;)
1868 va_start(args, format);
1869 ret = shader_vaddline(buffer, format, args);
1870 va_end(args);
1871 if (!ret)
1872 return;
1873 if (!string_buffer_resize(buffer, ret))
1874 return;
1878 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1880 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1883 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1885 switch (primitive_type)
1887 case WINED3D_PT_POINTLIST:
1888 return "points";
1890 case WINED3D_PT_LINELIST:
1891 return "lines";
1893 case WINED3D_PT_LINESTRIP:
1894 return "line_strip";
1896 case WINED3D_PT_TRIANGLELIST:
1897 return "triangles";
1899 case WINED3D_PT_TRIANGLESTRIP:
1900 return "triangle_strip";
1902 case WINED3D_PT_LINELIST_ADJ:
1903 return "lines_adjacency";
1905 case WINED3D_PT_TRIANGLELIST_ADJ:
1906 return "triangles_adjacency";
1908 default:
1909 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1910 return "";
1914 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1916 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1917 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1918 DWORD input_reg_used = shader->u.ps.input_reg_used;
1919 unsigned int i;
1921 if (reg_maps->shader_version.major < 3)
1922 return input_reg_used & (1u << idx);
1924 for (i = 0; i < input_signature->element_count; ++i)
1926 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1928 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1929 continue;
1931 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1932 && input->semantic_idx == idx)
1933 return input_reg_used & (1u << input->register_idx);
1935 return FALSE;
1938 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1939 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1941 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1943 if (version->major >= 4)
1944 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1945 else
1946 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1949 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1950 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1951 unsigned int index)
1953 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1954 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1955 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1956 index, scalar_type, scalar_type, index);
1959 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1960 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1962 unsigned int index = e->register_idx;
1964 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1966 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1967 index);
1968 return;
1970 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1972 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1973 index);
1974 return;
1976 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1977 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1979 if (shader_glsl_use_explicit_attrib_location(gl_info))
1980 shader_addline(buffer, "layout(location = %u) ", index);
1982 switch (e->component_type)
1984 case WINED3D_TYPE_UINT:
1985 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1986 break;
1987 case WINED3D_TYPE_INT:
1988 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1989 break;
1991 default:
1992 FIXME("Unhandled type %#x.\n", e->component_type);
1993 /* Fall through. */
1994 case WINED3D_TYPE_UNKNOWN:
1995 case WINED3D_TYPE_FLOAT:
1996 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1997 break;
2001 /** Generate the variable & register declarations for the GLSL output target */
2002 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2003 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2004 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2006 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2007 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2008 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2009 const struct wined3d_gl_info *gl_info = context->gl_info;
2010 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2011 unsigned int uniform_block_base, uniform_block_count;
2012 unsigned int i, extra_constants_needed = 0;
2013 const struct wined3d_shader_lconst *lconst;
2014 const char *prefix;
2015 DWORD map;
2017 prefix = shader_glsl_get_prefix(version->type);
2019 /* Prototype the subroutines */
2020 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2022 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2025 /* Declare the constants (aka uniforms) */
2026 if (shader->limits->constant_float > 0)
2028 unsigned max_constantsF;
2030 /* Unless the shader uses indirect addressing, always declare the
2031 * maximum array size and ignore that we need some uniforms privately.
2032 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2033 * and immediate values, still declare VC[256]. If the shader needs
2034 * more uniforms than we have it won't work in any case. If it uses
2035 * less, the compiler will figure out which uniforms are really used
2036 * and strip them out. This allows a shader to use c255 on a dx9 card,
2037 * as long as it doesn't also use all the other constants.
2039 * If the shader uses indirect addressing the compiler must assume
2040 * that all declared uniforms are used. In this case, declare only the
2041 * amount that we're assured to have.
2043 * Thus we run into problems in these two cases:
2044 * 1) The shader really uses more uniforms than supported.
2045 * 2) The shader uses indirect addressing, less constants than
2046 * supported, but uses a constant index > #supported consts. */
2047 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2049 /* No indirect addressing here. */
2050 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2052 else
2054 if (reg_maps->usesrelconstF)
2056 /* Subtract the other potential uniforms from the max
2057 * available (bools, ints, and 1 row of projection matrix).
2058 * Subtract another uniform for immediate values, which have
2059 * to be loaded via uniform by the driver as well. The shader
2060 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2061 * shader code, so one vec4 should be enough. (Unfortunately
2062 * the Nvidia driver doesn't store 128 and -128 in one float).
2064 * Writing gl_ClipVertex requires one uniform for each
2065 * clipplane as well. */
2066 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2067 if (vs_args->clip_enabled)
2068 max_constantsF -= gl_info->limits.user_clip_distances;
2069 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2070 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2071 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2072 * for now take this into account when calculating the number of available constants
2074 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2075 /* Set by driver quirks in directx.c */
2076 max_constantsF -= gl_info->reserved_glsl_constants;
2078 if (max_constantsF < shader->limits->constant_float)
2080 static unsigned int once;
2082 if (!once++)
2083 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2084 " it may not render correctly.\n");
2085 else
2086 WARN("The hardware does not support enough uniform components to run this shader.\n");
2089 else
2091 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2094 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2095 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2098 /* Always declare the full set of constants, the compiler can remove the
2099 * unused ones because d3d doesn't (yet) support indirect int and bool
2100 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2101 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2102 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2104 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2105 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2107 /* Declare immediate constant buffer */
2108 if (reg_maps->icb)
2109 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2111 /* Declare constant buffers */
2112 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2113 &uniform_block_base, &uniform_block_count);
2114 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2116 if (reg_maps->cb_sizes[i])
2118 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2119 shader_addline(buffer, "layout(binding = %u)\n", uniform_block_base + i);
2120 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2121 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2125 /* Declare texture samplers */
2126 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2128 struct wined3d_shader_sampler_map_entry *entry;
2129 const char *sampler_type_prefix, *sampler_type;
2130 BOOL shadow_sampler, tex_rect;
2132 entry = &reg_maps->sampler_map.entries[i];
2134 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2136 ERR("Invalid resource index %u.\n", entry->resource_idx);
2137 continue;
2140 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2142 case WINED3D_DATA_FLOAT:
2143 case WINED3D_DATA_UNORM:
2144 case WINED3D_DATA_SNORM:
2145 sampler_type_prefix = "";
2146 break;
2148 case WINED3D_DATA_INT:
2149 sampler_type_prefix = "i";
2150 break;
2152 case WINED3D_DATA_UINT:
2153 sampler_type_prefix = "u";
2154 break;
2156 default:
2157 sampler_type_prefix = "";
2158 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2159 break;
2162 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2163 switch (reg_maps->resource_info[entry->resource_idx].type)
2165 case WINED3D_SHADER_RESOURCE_BUFFER:
2166 sampler_type = "samplerBuffer";
2167 break;
2169 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2170 if (shadow_sampler)
2171 sampler_type = "sampler1DShadow";
2172 else
2173 sampler_type = "sampler1D";
2174 break;
2176 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2177 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2178 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2179 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2180 if (shadow_sampler)
2182 if (tex_rect)
2183 sampler_type = "sampler2DRectShadow";
2184 else
2185 sampler_type = "sampler2DShadow";
2187 else
2189 if (tex_rect)
2190 sampler_type = "sampler2DRect";
2191 else
2192 sampler_type = "sampler2D";
2194 break;
2196 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2197 if (shadow_sampler)
2198 FIXME("Unsupported 3D shadow sampler.\n");
2199 sampler_type = "sampler3D";
2200 break;
2202 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2203 if (shadow_sampler)
2204 FIXME("Unsupported Cube shadow sampler.\n");
2205 sampler_type = "samplerCube";
2206 break;
2208 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2209 if (shadow_sampler)
2210 sampler_type = "sampler2DArrayShadow";
2211 else
2212 sampler_type = "sampler2DArray";
2213 break;
2215 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2216 if (shadow_sampler)
2217 FIXME("Unsupported Cube array shadow sampler.\n");
2218 sampler_type = "samplerCubeArray";
2219 break;
2221 default:
2222 sampler_type = "unsupported_sampler";
2223 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2224 break;
2226 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2227 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2230 /* Declare images */
2231 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2233 const char *image_type_prefix, *image_type, *read_format;
2235 if (!reg_maps->uav_resource_info[i].type)
2236 continue;
2238 switch (reg_maps->uav_resource_info[i].data_type)
2240 case WINED3D_DATA_FLOAT:
2241 case WINED3D_DATA_UNORM:
2242 case WINED3D_DATA_SNORM:
2243 image_type_prefix = "";
2244 read_format = "r32f";
2245 break;
2247 case WINED3D_DATA_INT:
2248 image_type_prefix = "i";
2249 read_format = "r32i";
2250 break;
2252 case WINED3D_DATA_UINT:
2253 image_type_prefix = "u";
2254 read_format = "r32ui";
2255 break;
2257 default:
2258 image_type_prefix = "";
2259 read_format = "";
2260 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2261 break;
2264 switch (reg_maps->uav_resource_info[i].type)
2266 case WINED3D_SHADER_RESOURCE_BUFFER:
2267 image_type = "imageBuffer";
2268 break;
2270 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2271 image_type = "image2D";
2272 break;
2274 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2275 image_type = "image3D";
2276 break;
2278 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2279 image_type = "image2DArray";
2280 break;
2282 default:
2283 image_type = "unsupported_image";
2284 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2285 break;
2288 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2289 shader_addline(buffer, "layout(binding = %u)\n", i);
2290 if (reg_maps->uav_read_mask & (1u << i))
2291 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2292 read_format, image_type_prefix, image_type, prefix, i);
2293 else
2294 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2295 image_type_prefix, image_type, prefix, i);
2297 if (reg_maps->uav_counter_mask & (1u << i))
2298 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2299 i, prefix, i);
2302 /* Declare uniforms for NP2 texcoord fixup:
2303 * This is NOT done inside the loop that declares the texture samplers
2304 * since the NP2 fixup code is currently only used for the GeforceFX
2305 * series and when forcing the ARB_npot extension off. Modern cards just
2306 * skip the code anyway, so put it inside a separate loop. */
2307 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2309 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2310 UINT cur = 0;
2312 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2313 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2314 * samplerNP2Fixup stores texture dimensions and is updated through
2315 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2317 for (i = 0; i < shader->limits->sampler; ++i)
2319 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2320 continue;
2322 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2324 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2325 continue;
2328 fixup->idx[i] = cur++;
2331 fixup->num_consts = (cur + 1) >> 1;
2332 fixup->active = ps_args->np2_fixup;
2333 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2336 /* Declare address variables */
2337 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2339 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2342 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2344 for (i = 0; i < shader->input_signature.element_count; ++i)
2345 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2347 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2349 shader_addline(buffer, "uniform struct\n{\n");
2350 shader_addline(buffer, " float size;\n");
2351 shader_addline(buffer, " float size_min;\n");
2352 shader_addline(buffer, " float size_max;\n");
2353 shader_addline(buffer, "} ffp_point;\n");
2356 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2358 if (vs_args->clip_enabled)
2359 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
2361 if (version->major < 3)
2363 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2364 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2365 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2366 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2370 if (version->major < 4)
2371 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2373 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2375 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2377 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2379 else
2381 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2382 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2383 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2384 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2387 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2389 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2391 shader_addline(buffer, "uniform struct\n{\n");
2392 shader_addline(buffer, " vec4 color;\n");
2393 shader_addline(buffer, " float density;\n");
2394 shader_addline(buffer, " float end;\n");
2395 shader_addline(buffer, " float scale;\n");
2396 shader_addline(buffer, "} ffp_fog;\n");
2398 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2400 if (glsl_is_color_reg_read(shader, 0))
2401 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2402 if (glsl_is_color_reg_read(shader, 1))
2403 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2404 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2405 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2407 else
2409 if (glsl_is_color_reg_read(shader, 0))
2410 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2411 if (glsl_is_color_reg_read(shader, 1))
2412 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2413 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2414 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2415 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2419 if (version->major >= 3)
2421 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2423 if (ps_args->vp_mode == vertexshader)
2424 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2425 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2428 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2430 if (!(map & 1))
2431 continue;
2433 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2435 if (reg_maps->luminanceparams & (1u << i))
2437 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2438 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2439 extra_constants_needed++;
2442 extra_constants_needed++;
2445 if (ps_args->srgb_correction)
2447 shader_addline(buffer, "const vec4 srgb_const0 = ");
2448 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2449 shader_addline(buffer, ";\n");
2450 shader_addline(buffer, "const vec4 srgb_const1 = ");
2451 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2452 shader_addline(buffer, ";\n");
2454 if (reg_maps->vpos || reg_maps->usesdsy)
2456 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2458 ++extra_constants_needed;
2459 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2461 if (reg_maps->vpos)
2463 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2465 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
2466 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2467 ps_args->render_offscreen ? "" : "origin_upper_left, ");
2468 else if (!ps_args->render_offscreen)
2469 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2471 shader_addline(buffer, "vec4 vpos;\n");
2475 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2476 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2478 if (!needs_legacy_glsl_syntax(gl_info))
2479 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2481 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2482 FIXME("Insufficient uniforms to run this shader.\n");
2485 /* Declare output register temporaries */
2486 if (shader->limits->packed_output)
2487 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2489 /* Declare temporary variables */
2490 if (reg_maps->temporary_count)
2492 for (i = 0; i < reg_maps->temporary_count; ++i)
2493 shader_addline(buffer, "vec4 R%u;\n", i);
2495 else
2497 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2499 if (map & 1)
2500 shader_addline(buffer, "vec4 R%u;\n", i);
2504 /* Declare indexable temporary variables */
2505 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2507 if (idx_temp_reg->component_count != 4)
2508 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2509 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2512 /* Declare loop registers aLx */
2513 if (version->major < 4)
2515 for (i = 0; i < reg_maps->loop_depth; ++i)
2517 shader_addline(buffer, "int aL%u;\n", i);
2518 shader_addline(buffer, "int tmpInt%u;\n", i);
2522 /* Temporary variables for matrix operations */
2523 shader_addline(buffer, "vec4 tmp0;\n");
2524 shader_addline(buffer, "vec4 tmp1;\n");
2526 if (!shader->load_local_constsF)
2528 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2530 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2531 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2532 shader_addline(buffer, ";\n");
2537 /*****************************************************************************
2538 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2540 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2541 ****************************************************************************/
2543 /* Prototypes */
2544 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2545 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2547 /** Used for opcode modifiers - They multiply the result by the specified amount */
2548 static const char * const shift_glsl_tab[] = {
2549 "", /* 0 (none) */
2550 "2.0 * ", /* 1 (x2) */
2551 "4.0 * ", /* 2 (x4) */
2552 "8.0 * ", /* 3 (x8) */
2553 "16.0 * ", /* 4 (x16) */
2554 "32.0 * ", /* 5 (x32) */
2555 "", /* 6 (x64) */
2556 "", /* 7 (x128) */
2557 "", /* 8 (d256) */
2558 "", /* 9 (d128) */
2559 "", /* 10 (d64) */
2560 "", /* 11 (d32) */
2561 "0.0625 * ", /* 12 (d16) */
2562 "0.125 * ", /* 13 (d8) */
2563 "0.25 * ", /* 14 (d4) */
2564 "0.5 * " /* 15 (d2) */
2567 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2568 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2569 const char *in_reg, const char *in_regswizzle, char *out_str)
2571 switch (src_modifier)
2573 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2574 case WINED3DSPSM_DW:
2575 case WINED3DSPSM_NONE:
2576 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2577 break;
2578 case WINED3DSPSM_NEG:
2579 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2580 break;
2581 case WINED3DSPSM_NOT:
2582 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2583 break;
2584 case WINED3DSPSM_BIAS:
2585 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2586 break;
2587 case WINED3DSPSM_BIASNEG:
2588 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2589 break;
2590 case WINED3DSPSM_SIGN:
2591 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2592 break;
2593 case WINED3DSPSM_SIGNNEG:
2594 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2595 break;
2596 case WINED3DSPSM_COMP:
2597 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2598 break;
2599 case WINED3DSPSM_X2:
2600 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2601 break;
2602 case WINED3DSPSM_X2NEG:
2603 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2604 break;
2605 case WINED3DSPSM_ABS:
2606 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2607 break;
2608 case WINED3DSPSM_ABSNEG:
2609 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2610 break;
2611 default:
2612 FIXME("Unhandled modifier %u\n", src_modifier);
2613 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2617 /** Writes the GLSL variable name that corresponds to the register that the
2618 * DX opcode parameter is trying to access */
2619 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2620 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2622 /* oPos, oFog and oPts in D3D */
2623 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2625 const struct wined3d_shader *shader = ins->ctx->shader;
2626 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2627 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2628 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2629 const char *prefix = shader_glsl_get_prefix(version->type);
2630 struct glsl_src_param rel_param0, rel_param1;
2631 char imm_str[4][17];
2633 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2634 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2635 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2636 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2637 *is_color = FALSE;
2639 switch (reg->type)
2641 case WINED3DSPR_TEMP:
2642 sprintf(register_name, "R%u", reg->idx[0].offset);
2643 break;
2645 case WINED3DSPR_INPUT:
2646 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2648 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2650 if (reg->idx[0].rel_addr)
2651 FIXME("VS3+ input registers relative addressing.\n");
2652 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2653 *is_color = TRUE;
2654 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2655 break;
2658 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2660 if (reg->idx[0].rel_addr)
2662 if (reg->idx[1].rel_addr)
2663 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2664 rel_param0.param_str, reg->idx[0].offset,
2665 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2666 rel_param1.param_str, reg->idx[1].offset);
2667 else
2668 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2669 rel_param0.param_str, reg->idx[0].offset,
2670 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2671 reg->idx[1].offset);
2673 else if (reg->idx[1].rel_addr)
2674 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2675 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2676 rel_param1.param_str, reg->idx[1].offset);
2677 else
2678 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2679 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2680 reg->idx[1].offset);
2681 break;
2684 /* pixel shaders >= 3.0 */
2685 if (version->major >= 3)
2687 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2688 unsigned int in_count = vec4_varyings(version->major, gl_info);
2690 if (reg->idx[0].rel_addr)
2692 /* Removing a + 0 would be an obvious optimization, but
2693 * OS X doesn't see the NOP operation there. */
2694 if (idx)
2696 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2697 && shader->u.ps.declared_in_count > in_count)
2699 sprintf(register_name,
2700 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2701 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2702 prefix, rel_param0.param_str, idx);
2704 else
2706 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2709 else
2711 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2712 && shader->u.ps.declared_in_count > in_count)
2714 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2715 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2716 prefix, rel_param0.param_str);
2718 else
2720 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2724 else
2726 if (idx == in_count) sprintf(register_name, "gl_Color");
2727 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2728 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2731 else
2733 if (!reg->idx[0].offset)
2734 strcpy(register_name, "ffp_varying_diffuse");
2735 else
2736 strcpy(register_name, "ffp_varying_specular");
2737 break;
2739 break;
2741 case WINED3DSPR_CONST:
2743 /* Relative addressing */
2744 if (reg->idx[0].rel_addr)
2746 if (wined3d_settings.check_float_constants)
2747 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2748 rel_param0.param_str, reg->idx[0].offset,
2749 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2750 prefix, rel_param0.param_str, reg->idx[0].offset);
2751 else if (reg->idx[0].offset)
2752 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2753 else
2754 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2756 else
2758 if (shader_constant_is_local(shader, reg->idx[0].offset))
2759 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2760 else
2761 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2764 break;
2766 case WINED3DSPR_CONSTINT:
2767 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2768 break;
2770 case WINED3DSPR_CONSTBOOL:
2771 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2772 break;
2774 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2775 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2776 sprintf(register_name, "T%u", reg->idx[0].offset);
2777 else
2778 sprintf(register_name, "A%u", reg->idx[0].offset);
2779 break;
2781 case WINED3DSPR_LOOP:
2782 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2783 break;
2785 case WINED3DSPR_SAMPLER:
2786 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2787 break;
2789 case WINED3DSPR_COLOROUT:
2790 if (reg->idx[0].offset >= gl_info->limits.buffers)
2791 WARN("Write to render target %u, only %d supported.\n",
2792 reg->idx[0].offset, gl_info->limits.buffers);
2794 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2795 break;
2797 case WINED3DSPR_RASTOUT:
2798 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2799 break;
2801 case WINED3DSPR_DEPTHOUT:
2802 sprintf(register_name, "gl_FragDepth");
2803 break;
2805 case WINED3DSPR_ATTROUT:
2806 if (!reg->idx[0].offset)
2807 sprintf(register_name, "%s_out[8]", prefix);
2808 else
2809 sprintf(register_name, "%s_out[9]", prefix);
2810 break;
2812 case WINED3DSPR_TEXCRDOUT:
2813 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2814 if (reg->idx[0].rel_addr)
2815 FIXME("VS3 output registers relative addressing.\n");
2816 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2817 break;
2819 case WINED3DSPR_MISCTYPE:
2820 if (!reg->idx[0].offset)
2822 /* vPos */
2823 sprintf(register_name, "vpos");
2825 else if (reg->idx[0].offset == 1)
2827 /* Note that gl_FrontFacing is a bool, while vFace is
2828 * a float for which the sign determines front/back */
2829 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2831 else
2833 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2834 sprintf(register_name, "unrecognized_register");
2836 break;
2838 case WINED3DSPR_IMMCONST:
2839 switch (reg->immconst_type)
2841 case WINED3D_IMMCONST_SCALAR:
2842 switch (reg->data_type)
2844 case WINED3D_DATA_FLOAT:
2845 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2846 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
2847 else
2848 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
2849 break;
2850 case WINED3D_DATA_INT:
2851 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2852 break;
2853 case WINED3D_DATA_RESOURCE:
2854 case WINED3D_DATA_SAMPLER:
2855 case WINED3D_DATA_UINT:
2856 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2857 break;
2858 default:
2859 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2860 break;
2862 break;
2864 case WINED3D_IMMCONST_VEC4:
2865 switch (reg->data_type)
2867 case WINED3D_DATA_FLOAT:
2868 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2870 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2871 reg->u.immconst_data[0], reg->u.immconst_data[1],
2872 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2874 else
2876 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
2877 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
2878 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
2879 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
2880 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2881 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2883 break;
2884 case WINED3D_DATA_INT:
2885 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2886 reg->u.immconst_data[0], reg->u.immconst_data[1],
2887 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2888 break;
2889 case WINED3D_DATA_RESOURCE:
2890 case WINED3D_DATA_SAMPLER:
2891 case WINED3D_DATA_UINT:
2892 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2893 reg->u.immconst_data[0], reg->u.immconst_data[1],
2894 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2895 break;
2896 default:
2897 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2898 break;
2900 break;
2902 default:
2903 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2904 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2906 break;
2908 case WINED3DSPR_CONSTBUFFER:
2909 if (reg->idx[1].rel_addr)
2910 sprintf(register_name, "%s_cb%u[%s + %u]",
2911 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2912 else
2913 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2914 break;
2916 case WINED3DSPR_IMMCONSTBUFFER:
2917 if (reg->idx[0].rel_addr)
2918 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2919 else
2920 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2921 break;
2923 case WINED3DSPR_PRIMID:
2924 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2925 break;
2927 case WINED3DSPR_IDXTEMP:
2928 if (reg->idx[1].rel_addr)
2929 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2930 else
2931 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2932 break;
2934 case WINED3DSPR_LOCALTHREADINDEX:
2935 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2936 sprintf(register_name, "int(gl_LocalInvocationIndex)");
2937 else
2938 sprintf(register_name, "ivec2(gl_LocalInvocationIndex, 0)");
2939 break;
2941 case WINED3DSPR_THREADID:
2942 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2943 break;
2945 case WINED3DSPR_THREADGROUPID:
2946 sprintf(register_name, "ivec3(gl_WorkGroupID)");
2947 break;
2949 case WINED3DSPR_LOCALTHREADID:
2950 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
2951 break;
2953 default:
2954 FIXME("Unhandled register type %#x.\n", reg->type);
2955 sprintf(register_name, "unrecognized_register");
2956 break;
2960 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2962 *str++ = '.';
2963 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2964 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2965 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2966 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2967 *str = '\0';
2970 /* Get the GLSL write mask for the destination register */
2971 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2973 DWORD mask = param->write_mask;
2975 if (shader_is_scalar(&param->reg))
2977 mask = WINED3DSP_WRITEMASK_0;
2978 *write_mask = '\0';
2980 else
2982 shader_glsl_write_mask_to_str(mask, write_mask);
2985 return mask;
2988 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
2990 unsigned int size = 0;
2992 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2993 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2994 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2995 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2997 return size;
3000 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3001 unsigned int component_idx)
3003 /* swizzle bits fields: wwzzyyxx */
3004 return (swizzle >> (2 * component_idx)) & 0x3;
3007 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3009 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3010 * but addressed as "rgba". To fix this we need to swap the register's x
3011 * and z components. */
3012 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3013 unsigned int i;
3015 *str++ = '.';
3016 for (i = 0; i < 4; ++i)
3018 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3019 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3021 *str = '\0';
3024 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3025 BOOL fixup, DWORD mask, char *swizzle_str)
3027 if (shader_is_scalar(&param->reg))
3028 *swizzle_str = '\0';
3029 else
3030 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3033 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3034 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3036 if (dst_data_type == src_data_type)
3038 string_buffer_sprintf(dst_param, "%s", src_param);
3039 return;
3042 if (src_data_type == WINED3D_DATA_FLOAT)
3044 switch (dst_data_type)
3046 case WINED3D_DATA_INT:
3047 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3048 return;
3049 case WINED3D_DATA_RESOURCE:
3050 case WINED3D_DATA_SAMPLER:
3051 case WINED3D_DATA_UINT:
3052 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3053 return;
3054 default:
3055 break;
3059 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3061 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3062 return;
3065 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3067 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3068 return;
3071 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3072 string_buffer_sprintf(dst_param, "%s", src_param);
3075 /* From a given parameter token, generate the corresponding GLSL string.
3076 * Also, return the actual register name and swizzle in case the
3077 * caller needs this information as well. */
3078 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3079 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3080 enum wined3d_data_type data_type)
3082 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3083 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3084 enum wined3d_data_type param_data_type;
3085 BOOL is_color = FALSE;
3086 char swizzle_str[6];
3088 glsl_src->reg_name[0] = '\0';
3089 glsl_src->param_str[0] = '\0';
3090 swizzle_str[0] = '\0';
3092 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
3093 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3095 switch (wined3d_src->reg.type)
3097 case WINED3DSPR_IMMCONST:
3098 param_data_type = data_type;
3099 break;
3100 case WINED3DSPR_PRIMID:
3101 param_data_type = WINED3D_DATA_UINT;
3102 break;
3103 case WINED3DSPR_LOCALTHREADINDEX:
3104 case WINED3DSPR_THREADID:
3105 case WINED3DSPR_THREADGROUPID:
3106 case WINED3DSPR_LOCALTHREADID:
3107 param_data_type = WINED3D_DATA_INT;
3108 break;
3109 default:
3110 param_data_type = WINED3D_DATA_FLOAT;
3111 break;
3114 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3115 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3117 string_buffer_release(priv->string_buffers, reg_name);
3120 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3121 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3123 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3126 /* From a given parameter token, generate the corresponding GLSL string.
3127 * Also, return the actual register name and swizzle in case the
3128 * caller needs this information as well. */
3129 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3130 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3132 BOOL is_color = FALSE;
3134 glsl_dst->mask_str[0] = '\0';
3135 glsl_dst->reg_name[0] = '\0';
3137 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
3138 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3141 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3142 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3143 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3144 enum wined3d_data_type data_type)
3146 struct glsl_dst_param glsl_dst;
3147 DWORD mask;
3149 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3151 switch (data_type)
3153 case WINED3D_DATA_FLOAT:
3154 shader_addline(buffer, "%s%s = %s(",
3155 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3156 break;
3157 case WINED3D_DATA_INT:
3158 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3159 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3160 break;
3161 case WINED3D_DATA_RESOURCE:
3162 case WINED3D_DATA_SAMPLER:
3163 case WINED3D_DATA_UINT:
3164 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3165 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3166 break;
3167 default:
3168 FIXME("Unhandled data type %#x.\n", data_type);
3169 shader_addline(buffer, "%s%s = %s(",
3170 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3171 break;
3175 return mask;
3178 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3179 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3181 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3184 /** Process GLSL instruction modifiers */
3185 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3187 struct glsl_dst_param dst_param;
3188 DWORD modifiers;
3190 if (!ins->dst_count) return;
3192 modifiers = ins->dst[0].modifiers;
3193 if (!modifiers) return;
3195 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3197 if (modifiers & WINED3DSPDM_SATURATE)
3199 /* _SAT means to clamp the value of the register to between 0 and 1 */
3200 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3201 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3204 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3206 FIXME("_centroid modifier not handled\n");
3209 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3211 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3215 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3217 switch (op)
3219 case WINED3D_SHADER_REL_OP_GT: return ">";
3220 case WINED3D_SHADER_REL_OP_EQ: return "==";
3221 case WINED3D_SHADER_REL_OP_GE: return ">=";
3222 case WINED3D_SHADER_REL_OP_LT: return "<";
3223 case WINED3D_SHADER_REL_OP_NE: return "!=";
3224 case WINED3D_SHADER_REL_OP_LE: return "<=";
3225 default:
3226 FIXME("Unrecognized operator %#x.\n", op);
3227 return "(\?\?)";
3231 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info,
3232 const struct wined3d_shader_version *version)
3234 return shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3237 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3238 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3240 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3241 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3242 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3243 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3244 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3245 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3246 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3247 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3248 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3249 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3250 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3251 const char *base = "texture", *type_part = "", *suffix = "";
3252 unsigned int coord_size, deriv_size;
3253 BOOL array;
3255 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3257 if (resource_type >= ARRAY_SIZE(resource_type_info))
3259 ERR("Unexpected resource type %#x.\n", resource_type);
3260 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3262 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3263 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3265 /* Note that there's no such thing as a projected cube texture. */
3266 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3267 projected = FALSE;
3269 if (needs_legacy_glsl_syntax(gl_info))
3271 if (shadow)
3272 base = "shadow";
3274 type_part = resource_type_info[resource_type].type_part;
3275 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3276 type_part = "2DRect";
3277 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3278 FIXME("Unhandled resource type %#x.\n", resource_type);
3280 if (!lod && grad && !shader_glsl_has_core_grad(gl_info, &ctx->shader->reg_maps.shader_version))
3282 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3283 suffix = "ARB";
3284 else
3285 FIXME("Unsupported grad function.\n");
3289 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3291 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3292 if (flags & ~texel_fetch_flags)
3293 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3295 base = "texelFetch";
3296 type_part = "";
3299 sample_function->name = string_buffer_get(priv->string_buffers);
3300 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3301 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3303 coord_size = resource_type_info[resource_type].coord_size;
3304 deriv_size = coord_size;
3305 if (shadow)
3306 ++coord_size;
3307 if (array)
3308 --deriv_size;
3309 sample_function->offset_size = offset ? deriv_size : 0;
3310 sample_function->coord_mask = (1u << coord_size) - 1;
3311 sample_function->deriv_mask = (1u << deriv_size) - 1;
3312 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3315 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3316 struct glsl_sample_function *sample_function)
3318 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3320 string_buffer_release(priv->string_buffers, sample_function->name);
3323 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3324 BOOL sign_fixup, enum fixup_channel_source channel_source)
3326 switch(channel_source)
3328 case CHANNEL_SOURCE_ZERO:
3329 strcat(arguments, "0.0");
3330 break;
3332 case CHANNEL_SOURCE_ONE:
3333 strcat(arguments, "1.0");
3334 break;
3336 case CHANNEL_SOURCE_X:
3337 strcat(arguments, reg_name);
3338 strcat(arguments, ".x");
3339 break;
3341 case CHANNEL_SOURCE_Y:
3342 strcat(arguments, reg_name);
3343 strcat(arguments, ".y");
3344 break;
3346 case CHANNEL_SOURCE_Z:
3347 strcat(arguments, reg_name);
3348 strcat(arguments, ".z");
3349 break;
3351 case CHANNEL_SOURCE_W:
3352 strcat(arguments, reg_name);
3353 strcat(arguments, ".w");
3354 break;
3356 default:
3357 FIXME("Unhandled channel source %#x\n", channel_source);
3358 strcat(arguments, "undefined");
3359 break;
3362 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3365 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3366 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3368 unsigned int mask_size, remaining;
3369 DWORD fixup_mask = 0;
3370 char arguments[256];
3371 char mask_str[6];
3373 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3374 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3375 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3376 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3377 if (!(mask &= fixup_mask))
3378 return;
3380 if (is_complex_fixup(fixup))
3382 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3383 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3384 return;
3387 shader_glsl_write_mask_to_str(mask, mask_str);
3388 mask_size = shader_glsl_get_write_mask_size(mask);
3390 arguments[0] = '\0';
3391 remaining = mask_size;
3392 if (mask & WINED3DSP_WRITEMASK_0)
3394 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3395 if (--remaining) strcat(arguments, ", ");
3397 if (mask & WINED3DSP_WRITEMASK_1)
3399 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3400 if (--remaining) strcat(arguments, ", ");
3402 if (mask & WINED3DSP_WRITEMASK_2)
3404 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3405 if (--remaining) strcat(arguments, ", ");
3407 if (mask & WINED3DSP_WRITEMASK_3)
3409 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3410 if (--remaining) strcat(arguments, ", ");
3413 if (mask_size > 1)
3414 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3415 else
3416 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3419 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3421 char reg_name[256];
3422 BOOL is_color;
3424 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3425 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3428 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3429 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3430 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3431 const char *coord_reg_fmt, ...)
3433 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3434 char dst_swizzle[6];
3435 struct color_fixup_desc fixup;
3436 BOOL np2_fixup = FALSE;
3437 va_list args;
3438 int ret;
3440 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3442 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3443 * We actually rely on it for vertex shaders and SM4+. */
3444 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3446 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3447 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3449 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3450 np2_fixup = TRUE;
3452 else
3454 fixup = COLOR_FIXUP_IDENTITY;
3457 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3459 if (sample_function->output_single_component)
3460 shader_addline(ins->ctx->buffer, "vec4(");
3462 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3463 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3465 for (;;)
3467 va_start(args, coord_reg_fmt);
3468 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3469 va_end(args);
3470 if (!ret)
3471 break;
3472 if (!string_buffer_resize(ins->ctx->buffer, ret))
3473 break;
3476 if (np2_fixup)
3478 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3479 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3481 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3483 case 1:
3484 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3485 idx >> 1, (idx % 2) ? "z" : "x");
3486 break;
3487 case 2:
3488 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3489 idx >> 1, (idx % 2) ? "zw" : "xy");
3490 break;
3491 case 3:
3492 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3493 idx >> 1, (idx % 2) ? "zw" : "xy");
3494 break;
3495 case 4:
3496 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3497 idx >> 1, (idx % 2) ? "zw" : "xy");
3498 break;
3501 if (dx && dy)
3502 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3503 else if (bias)
3504 shader_addline(ins->ctx->buffer, ", %s", bias);
3505 if (sample_function->offset_size)
3507 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3508 shader_addline(ins->ctx->buffer, ", ");
3509 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3511 shader_addline(ins->ctx->buffer, ")");
3513 if (sample_function->output_single_component)
3514 shader_addline(ins->ctx->buffer, ")");
3516 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3518 if (!is_identity_fixup(fixup))
3519 shader_glsl_color_correction(ins, fixup);
3522 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3524 /* Write the final position.
3526 * OpenGL coordinates specify the center of the pixel while D3D coords
3527 * specify the corner. The offsets are stored in z and w in
3528 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3529 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3530 * a MAD. */
3531 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3532 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3534 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3535 * in utils.c
3537 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3538 * shaders are run before the homogeneous divide, so we have to take the w
3539 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3540 * z = z * 2 - w. */
3541 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3544 /*****************************************************************************
3545 * Begin processing individual instruction opcodes
3546 ****************************************************************************/
3548 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3550 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3551 struct glsl_src_param src0_param;
3552 struct glsl_src_param src1_param;
3553 DWORD write_mask;
3554 const char *op;
3556 /* Determine the GLSL operator to use based on the opcode */
3557 switch (ins->handler_idx)
3559 case WINED3DSIH_ADD: op = "+"; break;
3560 case WINED3DSIH_AND: op = "&"; break;
3561 case WINED3DSIH_DIV: op = "/"; break;
3562 case WINED3DSIH_IADD: op = "+"; break;
3563 case WINED3DSIH_ISHL: op = "<<"; break;
3564 case WINED3DSIH_ISHR: op = ">>"; break;
3565 case WINED3DSIH_MUL: op = "*"; break;
3566 case WINED3DSIH_OR: op = "|"; break;
3567 case WINED3DSIH_SUB: op = "-"; break;
3568 case WINED3DSIH_USHR: op = ">>"; break;
3569 case WINED3DSIH_XOR: op = "^"; break;
3570 default:
3571 op = "<unhandled operator>";
3572 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3573 break;
3576 write_mask = shader_glsl_append_dst(buffer, ins);
3577 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3578 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3579 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3582 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3584 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3585 struct glsl_src_param src0_param;
3586 struct glsl_src_param src1_param;
3587 unsigned int mask_size;
3588 DWORD write_mask;
3589 const char *op;
3591 write_mask = shader_glsl_append_dst(buffer, ins);
3592 mask_size = shader_glsl_get_write_mask_size(write_mask);
3593 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3594 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3596 if (mask_size > 1)
3598 switch (ins->handler_idx)
3600 case WINED3DSIH_EQ: op = "equal"; break;
3601 case WINED3DSIH_IEQ: op = "equal"; break;
3602 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3603 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3604 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3605 case WINED3DSIH_LT: op = "lessThan"; break;
3606 case WINED3DSIH_ILT: op = "lessThan"; break;
3607 case WINED3DSIH_ULT: op = "lessThan"; break;
3608 case WINED3DSIH_NE: op = "notEqual"; break;
3609 case WINED3DSIH_INE: op = "notEqual"; break;
3610 default:
3611 op = "<unhandled operator>";
3612 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3613 break;
3616 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3617 mask_size, op, src0_param.param_str, src1_param.param_str);
3619 else
3621 switch (ins->handler_idx)
3623 case WINED3DSIH_EQ: op = "=="; break;
3624 case WINED3DSIH_IEQ: op = "=="; break;
3625 case WINED3DSIH_GE: op = ">="; break;
3626 case WINED3DSIH_IGE: op = ">="; break;
3627 case WINED3DSIH_UGE: op = ">="; break;
3628 case WINED3DSIH_LT: op = "<"; break;
3629 case WINED3DSIH_ILT: op = "<"; break;
3630 case WINED3DSIH_ULT: op = "<"; break;
3631 case WINED3DSIH_NE: op = "!="; break;
3632 case WINED3DSIH_INE: op = "!="; break;
3633 default:
3634 op = "<unhandled operator>";
3635 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3636 break;
3639 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3640 src0_param.param_str, op, src1_param.param_str);
3644 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3646 struct glsl_src_param src_param;
3647 DWORD write_mask;
3648 const char *op;
3650 switch (ins->handler_idx)
3652 case WINED3DSIH_INEG: op = "-"; break;
3653 case WINED3DSIH_NOT: op = "~"; break;
3654 default:
3655 op = "<unhandled operator>";
3656 ERR("Unhandled opcode %s.\n",
3657 debug_d3dshaderinstructionhandler(ins->handler_idx));
3658 break;
3661 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3662 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3663 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3666 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3668 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3669 struct glsl_src_param src0_param;
3670 struct glsl_src_param src1_param;
3671 DWORD write_mask;
3673 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3674 * If not, we can emulate it. */
3675 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3676 FIXME("64-bit integer multiplies not implemented.\n");
3678 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3680 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3681 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3682 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3684 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3685 src0_param.param_str, src1_param.param_str);
3689 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3691 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3692 struct glsl_src_param src0_param, src1_param;
3693 DWORD write_mask;
3695 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3697 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3699 char dst_mask[6];
3701 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3702 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3703 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3704 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3705 dst_mask, src0_param.param_str, src1_param.param_str);
3707 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3708 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3709 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3710 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3712 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3713 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3715 else
3717 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3718 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3719 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3720 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3723 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3725 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3726 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3727 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3728 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3732 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3733 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3735 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3736 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3737 struct glsl_src_param src0_param;
3738 DWORD write_mask;
3740 write_mask = shader_glsl_append_dst(buffer, ins);
3741 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3743 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3744 * shader versions WINED3DSIO_MOVA is used for this. */
3745 if (ins->ctx->reg_maps->shader_version.major == 1
3746 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3747 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3749 /* This is a simple floor() */
3750 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3751 if (mask_size > 1) {
3752 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3753 } else {
3754 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3757 else if (ins->handler_idx == WINED3DSIH_MOVA)
3759 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
3760 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3762 if (shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3764 if (mask_size > 1)
3765 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3766 else
3767 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3769 else
3771 if (mask_size > 1)
3772 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3773 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3774 else
3775 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3776 src0_param.param_str, src0_param.param_str);
3779 else
3781 shader_addline(buffer, "%s);\n", src0_param.param_str);
3785 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3786 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3788 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3789 struct glsl_src_param src0_param;
3790 struct glsl_src_param src1_param;
3791 DWORD dst_write_mask, src_write_mask;
3792 unsigned int dst_size;
3794 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3795 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3797 /* dp4 works on vec4, dp3 on vec3, etc. */
3798 if (ins->handler_idx == WINED3DSIH_DP4)
3799 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3800 else if (ins->handler_idx == WINED3DSIH_DP3)
3801 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3802 else
3803 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3805 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3806 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3808 if (dst_size > 1) {
3809 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3810 } else {
3811 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3815 /* Note that this instruction has some restrictions. The destination write mask
3816 * can't contain the w component, and the source swizzles have to be .xyzw */
3817 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3819 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3820 struct glsl_src_param src0_param;
3821 struct glsl_src_param src1_param;
3822 char dst_mask[6];
3824 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3825 shader_glsl_append_dst(ins->ctx->buffer, ins);
3826 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3827 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3828 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3831 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3833 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3835 if (!stream)
3836 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3837 else
3838 FIXME("Unhandled primitive stream %u.\n", stream);
3841 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3842 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3843 * GLSL uses the value as-is. */
3844 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3846 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3847 struct glsl_src_param src0_param;
3848 struct glsl_src_param src1_param;
3849 DWORD dst_write_mask;
3850 unsigned int dst_size;
3852 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3853 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3855 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3856 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3858 if (dst_size > 1)
3860 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3861 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3863 else
3865 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3866 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3870 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3871 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3873 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3874 struct glsl_src_param src_param;
3875 const char *instruction;
3876 DWORD write_mask;
3877 unsigned i;
3879 /* Determine the GLSL function to use based on the opcode */
3880 /* TODO: Possibly make this a table for faster lookups */
3881 switch (ins->handler_idx)
3883 case WINED3DSIH_ABS: instruction = "abs"; break;
3884 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
3885 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
3886 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3887 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3888 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3889 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3890 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3891 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3892 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
3893 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
3894 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
3895 case WINED3DSIH_FRC: instruction = "fract"; break;
3896 case WINED3DSIH_IMAX: instruction = "max"; break;
3897 case WINED3DSIH_IMIN: instruction = "min"; break;
3898 case WINED3DSIH_MAX: instruction = "max"; break;
3899 case WINED3DSIH_MIN: instruction = "min"; break;
3900 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3901 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3902 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3903 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3904 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3905 case WINED3DSIH_UMAX: instruction = "max"; break;
3906 case WINED3DSIH_UMIN: instruction = "min"; break;
3907 default: instruction = "";
3908 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3909 break;
3912 write_mask = shader_glsl_append_dst(buffer, ins);
3914 /* In D3D bits are numbered from the most significant bit. */
3915 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
3916 shader_addline(buffer, "31 - ");
3917 shader_addline(buffer, "%s(", instruction);
3919 if (ins->src_count)
3921 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3922 shader_addline(buffer, "%s", src_param.param_str);
3923 for (i = 1; i < ins->src_count; ++i)
3925 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3926 shader_addline(buffer, ", %s", src_param.param_str);
3930 shader_addline(buffer, "));\n");
3933 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
3935 struct wined3d_shader_dst_param dst;
3936 struct glsl_src_param src;
3937 DWORD write_mask;
3938 const char *fmt;
3939 unsigned int i;
3941 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
3942 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3944 dst = ins->dst[0];
3945 for (i = 0; i < 4; ++i)
3947 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3948 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3949 &dst, dst.reg.data_type)))
3950 continue;
3952 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
3953 shader_addline(ins->ctx->buffer, fmt, src.param_str);
3957 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
3959 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3960 struct wined3d_shader_dst_param dst;
3961 struct glsl_src_param src[4];
3962 const char *instruction;
3963 unsigned int i, j;
3964 DWORD write_mask;
3966 switch (ins->handler_idx)
3968 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
3969 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
3970 default:
3971 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3972 return;
3975 dst = ins->dst[0];
3976 for (i = 0; i < 4; ++i)
3978 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3979 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3980 &dst, dst.reg.data_type)))
3981 continue;
3983 for (j = 0; j < ins->src_count; ++j)
3984 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
3985 shader_addline(buffer, "%s(", instruction);
3986 for (j = 0; j < ins->src_count - 2; ++j)
3987 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
3988 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
3992 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3994 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3996 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3997 struct glsl_src_param src_param;
3998 unsigned int mask_size;
3999 DWORD write_mask;
4000 char dst_mask[6];
4002 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4003 mask_size = shader_glsl_get_write_mask_size(write_mask);
4004 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4006 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4007 src_param.param_str, src_param.param_str);
4008 shader_glsl_append_dst(buffer, ins);
4010 if (mask_size > 1)
4012 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4013 mask_size, src_param.param_str);
4015 else
4017 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4018 src_param.param_str);
4022 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4024 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4025 ins->ctx->reg_maps->shader_version.minor);
4026 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4027 struct glsl_src_param src0_param;
4028 const char *prefix, *suffix;
4029 unsigned int dst_size;
4030 DWORD dst_write_mask;
4032 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4033 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4035 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4036 dst_write_mask = WINED3DSP_WRITEMASK_3;
4038 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4040 switch (ins->handler_idx)
4042 case WINED3DSIH_EXP:
4043 case WINED3DSIH_EXPP:
4044 prefix = "exp2(";
4045 suffix = ")";
4046 break;
4048 case WINED3DSIH_LOG:
4049 case WINED3DSIH_LOGP:
4050 prefix = "log2(abs(";
4051 suffix = "))";
4052 break;
4054 case WINED3DSIH_RCP:
4055 prefix = "1.0 / ";
4056 suffix = "";
4057 break;
4059 case WINED3DSIH_RSQ:
4060 prefix = "inversesqrt(abs(";
4061 suffix = "))";
4062 break;
4064 default:
4065 prefix = "";
4066 suffix = "";
4067 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4068 break;
4071 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4072 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4073 else
4074 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4077 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4078 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4079 * dst.x = 2^(floor(src))
4080 * dst.y = src - floor(src)
4081 * dst.z = 2^src (partial precision is allowed, but optional)
4082 * dst.w = 1.0;
4083 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4084 * dst = 2^src; (partial precision is allowed, but optional)
4086 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4088 if (ins->ctx->reg_maps->shader_version.major < 2)
4090 struct glsl_src_param src_param;
4091 char dst_mask[6];
4093 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4095 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4096 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4097 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4098 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4100 shader_glsl_append_dst(ins->ctx->buffer, ins);
4101 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4102 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4103 return;
4106 shader_glsl_scalar_op(ins);
4109 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4110 const char *vector_constructor, const char *scalar_constructor)
4112 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4113 struct glsl_src_param src_param;
4114 unsigned int mask_size;
4115 DWORD write_mask;
4117 write_mask = shader_glsl_append_dst(buffer, ins);
4118 mask_size = shader_glsl_get_write_mask_size(write_mask);
4119 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4121 if (mask_size > 1)
4122 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4123 else
4124 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4127 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4129 shader_glsl_cast(ins, "ivec", "int");
4132 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4134 shader_glsl_cast(ins, "uvec", "uint");
4137 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4139 shader_glsl_cast(ins, "vec", "float");
4142 /** Process signed comparison opcodes in GLSL. */
4143 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4145 struct glsl_src_param src0_param;
4146 struct glsl_src_param src1_param;
4147 DWORD write_mask;
4148 unsigned int mask_size;
4150 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4151 mask_size = shader_glsl_get_write_mask_size(write_mask);
4152 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4153 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4155 if (mask_size > 1) {
4156 const char *compare;
4158 switch(ins->handler_idx)
4160 case WINED3DSIH_SLT: compare = "lessThan"; break;
4161 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4162 default: compare = "";
4163 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4166 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4167 src0_param.param_str, src1_param.param_str);
4168 } else {
4169 switch(ins->handler_idx)
4171 case WINED3DSIH_SLT:
4172 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4173 * to return 0.0 but step returns 1.0 because step is not < x
4174 * An alternative is a bvec compare padded with an unused second component.
4175 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4176 * issue. Playing with not() is not possible either because not() does not accept
4177 * a scalar.
4179 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4180 src0_param.param_str, src1_param.param_str);
4181 break;
4182 case WINED3DSIH_SGE:
4183 /* Here we can use the step() function and safe a conditional */
4184 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4185 break;
4186 default:
4187 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4193 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4195 const char *condition_prefix, *condition_suffix;
4196 struct wined3d_shader_dst_param dst;
4197 struct glsl_src_param src0_param;
4198 struct glsl_src_param src1_param;
4199 struct glsl_src_param src2_param;
4200 BOOL temp_destination = FALSE;
4201 DWORD cmp_channel = 0;
4202 unsigned int i, j;
4203 char mask_char[6];
4204 DWORD write_mask;
4206 switch (ins->handler_idx)
4208 case WINED3DSIH_CMP:
4209 condition_prefix = "";
4210 condition_suffix = " >= 0.0";
4211 break;
4213 case WINED3DSIH_CND:
4214 condition_prefix = "";
4215 condition_suffix = " > 0.5";
4216 break;
4218 case WINED3DSIH_MOVC:
4219 condition_prefix = "bool(";
4220 condition_suffix = ")";
4221 break;
4223 default:
4224 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4225 condition_prefix = "<unhandled prefix>";
4226 condition_suffix = "<unhandled suffix>";
4227 break;
4230 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4232 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4233 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4234 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4235 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4237 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4238 condition_prefix, src0_param.param_str, condition_suffix,
4239 src1_param.param_str, src2_param.param_str);
4240 return;
4243 dst = ins->dst[0];
4245 /* Splitting the instruction up in multiple lines imposes a problem:
4246 * The first lines may overwrite source parameters of the following lines.
4247 * Deal with that by using a temporary destination register if needed. */
4248 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4249 && ins->src[0].reg.type == dst.reg.type)
4250 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4251 && ins->src[1].reg.type == dst.reg.type)
4252 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4253 && ins->src[2].reg.type == dst.reg.type))
4254 temp_destination = TRUE;
4256 /* Cycle through all source0 channels. */
4257 for (i = 0; i < 4; ++i)
4259 write_mask = 0;
4260 /* Find the destination channels which use the current source0 channel. */
4261 for (j = 0; j < 4; ++j)
4263 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4265 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4266 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4269 dst.write_mask = ins->dst[0].write_mask & write_mask;
4271 if (temp_destination)
4273 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4274 continue;
4275 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4277 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4278 continue;
4280 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4281 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4282 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4284 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4285 condition_prefix, src0_param.param_str, condition_suffix,
4286 src1_param.param_str, src2_param.param_str);
4289 if (temp_destination)
4291 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4292 shader_glsl_append_dst(ins->ctx->buffer, ins);
4293 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4297 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4298 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4299 * the compare is done per component of src0. */
4300 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4302 struct glsl_src_param src0_param;
4303 struct glsl_src_param src1_param;
4304 struct glsl_src_param src2_param;
4305 DWORD write_mask;
4306 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4307 ins->ctx->reg_maps->shader_version.minor);
4309 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4311 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4312 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4313 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4314 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4316 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4317 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4318 else
4319 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4320 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4321 return;
4324 shader_glsl_conditional_move(ins);
4327 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4328 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4330 struct glsl_src_param src0_param;
4331 struct glsl_src_param src1_param;
4332 struct glsl_src_param src2_param;
4333 DWORD write_mask;
4335 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4336 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4337 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4338 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4339 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4340 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4343 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4344 Vertex shaders to GLSL codes */
4345 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4347 int i;
4348 int nComponents = 0;
4349 struct wined3d_shader_dst_param tmp_dst = {{0}};
4350 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4351 struct wined3d_shader_instruction tmp_ins;
4353 memset(&tmp_ins, 0, sizeof(tmp_ins));
4355 /* Set constants for the temporary argument */
4356 tmp_ins.ctx = ins->ctx;
4357 tmp_ins.dst_count = 1;
4358 tmp_ins.dst = &tmp_dst;
4359 tmp_ins.src_count = 2;
4360 tmp_ins.src = tmp_src;
4362 switch(ins->handler_idx)
4364 case WINED3DSIH_M4x4:
4365 nComponents = 4;
4366 tmp_ins.handler_idx = WINED3DSIH_DP4;
4367 break;
4368 case WINED3DSIH_M4x3:
4369 nComponents = 3;
4370 tmp_ins.handler_idx = WINED3DSIH_DP4;
4371 break;
4372 case WINED3DSIH_M3x4:
4373 nComponents = 4;
4374 tmp_ins.handler_idx = WINED3DSIH_DP3;
4375 break;
4376 case WINED3DSIH_M3x3:
4377 nComponents = 3;
4378 tmp_ins.handler_idx = WINED3DSIH_DP3;
4379 break;
4380 case WINED3DSIH_M3x2:
4381 nComponents = 2;
4382 tmp_ins.handler_idx = WINED3DSIH_DP3;
4383 break;
4384 default:
4385 break;
4388 tmp_dst = ins->dst[0];
4389 tmp_src[0] = ins->src[0];
4390 tmp_src[1] = ins->src[1];
4391 for (i = 0; i < nComponents; ++i)
4393 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4394 shader_glsl_dot(&tmp_ins);
4395 ++tmp_src[1].reg.idx[0].offset;
4400 The LRP instruction performs a component-wise linear interpolation
4401 between the second and third operands using the first operand as the
4402 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4403 This is equivalent to mix(src2, src1, src0);
4405 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4407 struct glsl_src_param src0_param;
4408 struct glsl_src_param src1_param;
4409 struct glsl_src_param src2_param;
4410 DWORD write_mask;
4412 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4414 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4415 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4416 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4418 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4419 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4422 /** Process the WINED3DSIO_LIT instruction in GLSL:
4423 * dst.x = dst.w = 1.0
4424 * dst.y = (src0.x > 0) ? src0.x
4425 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4426 * where src.w is clamped at +- 128
4428 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4430 struct glsl_src_param src0_param;
4431 struct glsl_src_param src1_param;
4432 struct glsl_src_param src3_param;
4433 char dst_mask[6];
4435 shader_glsl_append_dst(ins->ctx->buffer, ins);
4436 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4438 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4439 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4440 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4442 /* The sdk specifies the instruction like this
4443 * dst.x = 1.0;
4444 * if(src.x > 0.0) dst.y = src.x
4445 * else dst.y = 0.0.
4446 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4447 * else dst.z = 0.0;
4448 * dst.w = 1.0;
4449 * (where power = src.w clamped between -128 and 128)
4451 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4452 * dst.x = 1.0 ... No further explanation needed
4453 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4454 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4455 * dst.w = 1.0. ... Nothing fancy.
4457 * So we still have one conditional in there. So do this:
4458 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4460 * 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),
4461 * 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.
4462 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4464 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4465 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4466 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4468 shader_addline(ins->ctx->buffer,
4469 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4470 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4471 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4472 src0_param.param_str, src3_param.param_str, dst_mask);
4475 /** Process the WINED3DSIO_DST instruction in GLSL:
4476 * dst.x = 1.0
4477 * dst.y = src0.x * src0.y
4478 * dst.z = src0.z
4479 * dst.w = src1.w
4481 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4483 struct glsl_src_param src0y_param;
4484 struct glsl_src_param src0z_param;
4485 struct glsl_src_param src1y_param;
4486 struct glsl_src_param src1w_param;
4487 char dst_mask[6];
4489 shader_glsl_append_dst(ins->ctx->buffer, ins);
4490 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4492 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4493 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4494 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4495 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4497 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4498 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4501 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4502 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4503 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4505 * dst.x = cos(src0.?)
4506 * dst.y = sin(src0.?)
4507 * dst.z = dst.z
4508 * dst.w = dst.w
4510 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4512 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4513 struct glsl_src_param src0_param;
4514 DWORD write_mask;
4516 if (ins->ctx->reg_maps->shader_version.major < 4)
4518 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4520 write_mask = shader_glsl_append_dst(buffer, ins);
4521 switch (write_mask)
4523 case WINED3DSP_WRITEMASK_0:
4524 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4525 break;
4527 case WINED3DSP_WRITEMASK_1:
4528 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4529 break;
4531 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4532 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4533 src0_param.param_str, src0_param.param_str);
4534 break;
4536 default:
4537 ERR("Write mask should be .x, .y or .xy\n");
4538 break;
4541 return;
4544 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4547 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4549 char dst_mask[6];
4551 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4552 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4553 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4555 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4556 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4557 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4559 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4560 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4562 else
4564 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4565 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4566 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4569 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4571 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4572 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4573 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4577 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4578 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4579 * generate invalid code
4581 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4583 struct glsl_src_param src0_param;
4584 DWORD write_mask;
4586 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4587 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4589 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4592 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4593 * Start a for() loop where src1.y is the initial value of aL,
4594 * increment aL by src1.z for a total of src1.x iterations.
4595 * Need to use a temporary variable for this operation.
4597 /* FIXME: I don't think nested loops will work correctly this way. */
4598 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4600 struct wined3d_shader_parser_state *state = ins->ctx->state;
4601 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4602 const struct wined3d_shader *shader = ins->ctx->shader;
4603 const struct wined3d_shader_lconst *constant;
4604 struct glsl_src_param src1_param;
4605 const DWORD *control_values = NULL;
4607 if (ins->ctx->reg_maps->shader_version.major < 4)
4609 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4611 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4612 * class hardware doesn't support real varying indexing, but Microsoft
4613 * designed this feature for Shader model 2.x+. If the loop control is
4614 * known at compile time, the GLSL compiler can unroll the loop, and
4615 * replace indirect addressing with direct addressing. */
4616 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4618 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4620 if (constant->idx == ins->src[1].reg.idx[0].offset)
4622 control_values = constant->value;
4623 break;
4628 if (control_values)
4630 struct wined3d_shader_loop_control loop_control;
4631 loop_control.count = control_values[0];
4632 loop_control.start = control_values[1];
4633 loop_control.step = (int)control_values[2];
4635 if (loop_control.step > 0)
4637 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4638 state->current_loop_depth, loop_control.start,
4639 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4640 state->current_loop_depth, loop_control.step);
4642 else if (loop_control.step < 0)
4644 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4645 state->current_loop_depth, loop_control.start,
4646 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4647 state->current_loop_depth, loop_control.step);
4649 else
4651 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4652 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4653 state->current_loop_depth, loop_control.count,
4654 state->current_loop_depth);
4657 else
4659 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4660 state->current_loop_depth, state->current_loop_reg,
4661 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4662 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4665 ++state->current_loop_reg;
4667 else
4669 shader_addline(buffer, "for (;;)\n{\n");
4672 ++state->current_loop_depth;
4675 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4677 struct wined3d_shader_parser_state *state = ins->ctx->state;
4679 shader_addline(ins->ctx->buffer, "}\n");
4681 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4683 --state->current_loop_depth;
4684 --state->current_loop_reg;
4687 if (ins->handler_idx == WINED3DSIH_ENDREP)
4689 --state->current_loop_depth;
4693 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4695 struct wined3d_shader_parser_state *state = ins->ctx->state;
4696 const struct wined3d_shader *shader = ins->ctx->shader;
4697 const struct wined3d_shader_lconst *constant;
4698 struct glsl_src_param src0_param;
4699 const DWORD *control_values = NULL;
4701 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4702 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4704 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4706 if (constant->idx == ins->src[0].reg.idx[0].offset)
4708 control_values = constant->value;
4709 break;
4714 if (control_values)
4716 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4717 state->current_loop_depth, state->current_loop_depth,
4718 control_values[0], state->current_loop_depth);
4720 else
4722 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4723 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4724 state->current_loop_depth, state->current_loop_depth,
4725 src0_param.param_str, state->current_loop_depth);
4728 ++state->current_loop_depth;
4731 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4733 struct glsl_src_param src0_param;
4735 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4736 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4739 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4741 struct glsl_src_param src0_param;
4743 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4744 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4747 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4749 shader_addline(ins->ctx->buffer, "default:\n");
4752 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4754 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4755 struct glsl_src_param src0_param;
4757 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4758 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4761 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4763 struct glsl_src_param src0_param;
4764 struct glsl_src_param src1_param;
4766 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4767 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4769 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4770 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4773 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4775 shader_addline(ins->ctx->buffer, "} else {\n");
4778 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4780 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4782 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4783 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
4784 shader_glsl_fixup_position(ins->ctx->buffer);
4786 if (!stream)
4787 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4788 else
4789 FIXME("Unhandled primitive stream %u.\n", stream);
4792 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4794 shader_addline(ins->ctx->buffer, "break;\n");
4797 /* FIXME: According to MSDN the compare is done per component. */
4798 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4800 struct glsl_src_param src0_param;
4801 struct glsl_src_param src1_param;
4803 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4804 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4806 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4807 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4810 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4812 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4813 struct glsl_src_param src_param;
4815 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4816 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4819 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4821 shader_addline(ins->ctx->buffer, "continue;\n");
4824 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4826 shader_addline(ins->ctx->buffer, "}\n");
4827 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4829 /* Subroutines appear at the end of the shader. */
4830 ins->ctx->state->in_subroutine = TRUE;
4833 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4835 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4838 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4840 struct glsl_src_param src1_param;
4842 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4843 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4844 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4847 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4849 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
4851 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
4853 shader_glsl_generate_shader_epilogue(ins->ctx);
4854 shader_addline(ins->ctx->buffer, "return;\n");
4858 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4860 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4861 ins->ctx->reg_maps->shader_version.minor);
4862 struct glsl_sample_function sample_function;
4863 DWORD sample_flags = 0;
4864 DWORD resource_idx;
4865 DWORD mask = 0, swizzle;
4866 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4868 /* 1.0-1.4: Use destination register as sampler source.
4869 * 2.0+: Use provided sampler source. */
4870 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4871 resource_idx = ins->dst[0].reg.idx[0].offset;
4872 else
4873 resource_idx = ins->src[1].reg.idx[0].offset;
4875 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4877 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4878 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4879 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4881 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4882 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4884 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4885 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4887 case WINED3D_TTFF_COUNT1:
4888 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4889 break;
4890 case WINED3D_TTFF_COUNT2:
4891 mask = WINED3DSP_WRITEMASK_1;
4892 break;
4893 case WINED3D_TTFF_COUNT3:
4894 mask = WINED3DSP_WRITEMASK_2;
4895 break;
4896 case WINED3D_TTFF_COUNT4:
4897 case WINED3D_TTFF_DISABLE:
4898 mask = WINED3DSP_WRITEMASK_3;
4899 break;
4903 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4905 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4907 if (src_mod == WINED3DSPSM_DZ) {
4908 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4909 mask = WINED3DSP_WRITEMASK_2;
4910 } else if (src_mod == WINED3DSPSM_DW) {
4911 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4912 mask = WINED3DSP_WRITEMASK_3;
4915 else
4917 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4918 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4920 /* ps 2.0 texldp instruction always divides by the fourth component. */
4921 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4922 mask = WINED3DSP_WRITEMASK_3;
4926 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4927 mask |= sample_function.coord_mask;
4928 sample_function.coord_mask = mask;
4930 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4931 else swizzle = ins->src[1].swizzle;
4933 /* 1.0-1.3: Use destination register as coordinate source.
4934 1.4+: Use provided coordinate source register. */
4935 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4937 char coord_mask[6];
4938 shader_glsl_write_mask_to_str(mask, coord_mask);
4939 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4940 "T%u%s", resource_idx, coord_mask);
4942 else
4944 struct glsl_src_param coord_param;
4945 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4946 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4948 struct glsl_src_param bias;
4949 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4950 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4951 NULL, "%s", coord_param.param_str);
4952 } else {
4953 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4954 "%s", coord_param.param_str);
4957 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4960 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4962 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4963 struct glsl_src_param coord_param, dx_param, dy_param;
4964 struct glsl_sample_function sample_function;
4965 DWORD sampler_idx;
4966 DWORD swizzle = ins->src[1].swizzle;
4968 if (!shader_glsl_has_core_grad(gl_info, &ins->ctx->shader->reg_maps.shader_version)
4969 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4971 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4972 shader_glsl_tex(ins);
4973 return;
4976 sampler_idx = ins->src[1].reg.idx[0].offset;
4978 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4979 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4980 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4981 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4983 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4984 NULL, NULL, "%s", coord_param.param_str);
4985 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4988 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4990 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
4991 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4992 struct glsl_src_param coord_param, lod_param;
4993 struct glsl_sample_function sample_function;
4994 DWORD swizzle = ins->src[1].swizzle;
4995 DWORD sampler_idx;
4997 sampler_idx = ins->src[1].reg.idx[0].offset;
4999 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5000 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5002 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5004 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info, shader_version)
5005 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5007 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5008 * However, the NVIDIA drivers allow them in fragment shaders as well,
5009 * even without the appropriate extension. */
5010 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5012 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5013 "%s", coord_param.param_str);
5014 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5017 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5018 unsigned int resource_idx, unsigned int sampler_idx)
5020 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5021 unsigned int i;
5023 for (i = 0; i < sampler_map->count; ++i)
5025 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5026 return entries[i].bind_idx;
5029 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5031 return ~0u;
5034 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5036 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5037 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5038 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5039 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5040 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5041 struct glsl_src_param structure_idx, offset, data, data2;
5042 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5043 enum wined3d_shader_resource_type resource_type;
5044 struct wined3d_string_buffer *address;
5045 enum wined3d_data_type data_type;
5046 unsigned int resource_idx, stride;
5047 const char *op, *resource;
5048 DWORD coord_mask;
5049 BOOL is_tgsm;
5051 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5052 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5053 if (is_tgsm)
5055 if (resource_idx >= reg_maps->tgsm_count)
5057 ERR("Invalid TGSM index %u.\n", resource_idx);
5058 return;
5060 resource = "g";
5061 data_type = WINED3D_DATA_UINT;
5062 coord_mask = 1;
5063 stride = reg_maps->tgsm[resource_idx].stride;
5065 else
5067 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5069 ERR("Invalid UAV index %u.\n", resource_idx);
5070 return;
5072 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5073 if (resource_type >= ARRAY_SIZE(resource_type_info))
5075 ERR("Unexpected resource type %#x.\n", resource_type);
5076 return;
5078 resource = "image";
5079 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5080 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5081 stride = reg_maps->uav_resource_info[resource_idx].stride;
5084 switch (ins->handler_idx)
5086 case WINED3DSIH_ATOMIC_AND:
5087 case WINED3DSIH_IMM_ATOMIC_AND:
5088 if (is_tgsm)
5089 op = "atomicAnd";
5090 else
5091 op = "imageAtomicAnd";
5092 break;
5093 case WINED3DSIH_ATOMIC_CMP_STORE:
5094 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5095 if (is_tgsm)
5096 op = "atomicCompSwap";
5097 else
5098 op = "imageAtomicCompSwap";
5099 break;
5100 case WINED3DSIH_ATOMIC_IADD:
5101 case WINED3DSIH_IMM_ATOMIC_IADD:
5102 if (is_tgsm)
5103 op = "atomicAdd";
5104 else
5105 op = "imageAtomicAdd";
5106 break;
5107 case WINED3DSIH_ATOMIC_IMAX:
5108 case WINED3DSIH_IMM_ATOMIC_IMAX:
5109 if (is_tgsm)
5110 op = "atomicMax";
5111 else
5112 op = "imageAtomicMax";
5113 if (data_type != WINED3D_DATA_INT)
5115 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5116 return;
5118 break;
5119 case WINED3DSIH_ATOMIC_IMIN:
5120 case WINED3DSIH_IMM_ATOMIC_IMIN:
5121 if (is_tgsm)
5122 op = "atomicMin";
5123 else
5124 op = "imageAtomicMin";
5125 if (data_type != WINED3D_DATA_INT)
5127 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5128 return;
5130 break;
5131 case WINED3DSIH_ATOMIC_OR:
5132 case WINED3DSIH_IMM_ATOMIC_OR:
5133 if (is_tgsm)
5134 op = "atomicOr";
5135 else
5136 op = "imageAtomicOr";
5137 break;
5138 case WINED3DSIH_ATOMIC_UMAX:
5139 case WINED3DSIH_IMM_ATOMIC_UMAX:
5140 if (is_tgsm)
5141 op = "atomicMax";
5142 else
5143 op = "imageAtomicMax";
5144 if (data_type != WINED3D_DATA_UINT)
5146 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5147 return;
5149 break;
5150 case WINED3DSIH_ATOMIC_UMIN:
5151 case WINED3DSIH_IMM_ATOMIC_UMIN:
5152 if (is_tgsm)
5153 op = "atomicMin";
5154 else
5155 op = "imageAtomicMin";
5156 if (data_type != WINED3D_DATA_UINT)
5158 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5159 return;
5161 break;
5162 case WINED3DSIH_ATOMIC_XOR:
5163 case WINED3DSIH_IMM_ATOMIC_XOR:
5164 if (is_tgsm)
5165 op = "atomicXor";
5166 else
5167 op = "imageAtomicXor";
5168 break;
5169 case WINED3DSIH_IMM_ATOMIC_EXCH:
5170 if (is_tgsm)
5171 op = "atomicExchange";
5172 else
5173 op = "imageAtomicExchange";
5174 break;
5175 default:
5176 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5177 return;
5180 address = string_buffer_get(priv->string_buffers);
5181 if (stride)
5183 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5184 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5185 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5187 else
5189 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5190 string_buffer_sprintf(address, "%s", offset.param_str);
5191 if (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW)
5192 shader_addline(address, "/ 4");
5195 if (is_imm_instruction)
5196 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5198 if (is_tgsm)
5199 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5200 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5201 else
5202 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5203 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5205 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5206 shader_addline(buffer, "%s", data.param_str);
5207 if (ins->src_count >= 3)
5209 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5210 shader_addline(buffer, ", %s", data2.param_str);
5213 if (is_imm_instruction)
5214 shader_addline(buffer, ")");
5215 shader_addline(buffer, ");\n");
5217 string_buffer_release(priv->string_buffers, address);
5220 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5222 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5223 const char *op;
5225 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5226 op = "atomicCounterIncrement";
5227 else
5228 op = "atomicCounterDecrement";
5230 shader_glsl_append_dst(ins->ctx->buffer, ins);
5231 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5234 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5236 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5237 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5238 enum wined3d_shader_resource_type resource_type;
5239 struct glsl_src_param image_coord_param;
5240 enum wined3d_data_type data_type;
5241 DWORD coord_mask, write_mask;
5242 unsigned int uav_idx;
5243 char dst_swizzle[6];
5245 uav_idx = ins->src[1].reg.idx[0].offset;
5246 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5248 ERR("Invalid UAV index %u.\n", uav_idx);
5249 return;
5251 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5252 if (resource_type >= ARRAY_SIZE(resource_type_info))
5254 ERR("Unexpected resource type %#x.\n", resource_type);
5255 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5257 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5258 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5260 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5261 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5263 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5264 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5265 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5268 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5270 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5271 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5272 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5273 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5274 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5275 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5276 struct glsl_src_param structure_idx, offset;
5277 struct wined3d_string_buffer *address;
5278 struct wined3d_shader_dst_param dst;
5279 const char *function, *resource;
5281 resource_idx = src->reg.idx[0].offset;
5282 if (src->reg.type == WINED3DSPR_RESOURCE)
5284 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5286 ERR("Invalid resource index %u.\n", resource_idx);
5287 return;
5289 stride = reg_maps->resource_info[resource_idx].stride;
5290 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5291 function = "texelFetch";
5292 resource = "sampler";
5294 else if (src->reg.type == WINED3DSPR_UAV)
5296 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5298 ERR("Invalid UAV index %u.\n", resource_idx);
5299 return;
5301 stride = reg_maps->uav_resource_info[resource_idx].stride;
5302 bind_idx = resource_idx;
5303 function = "imageLoad";
5304 resource = "image";
5306 else
5308 if (resource_idx >= reg_maps->tgsm_count)
5310 ERR("Invalid TGSM index %u.\n", resource_idx);
5311 return;
5313 stride = reg_maps->tgsm[resource_idx].stride;
5314 bind_idx = resource_idx;
5315 function = NULL;
5316 resource = "g";
5319 address = string_buffer_get(priv->string_buffers);
5320 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5322 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5323 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5325 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5326 shader_addline(address, "%s / 4", offset.param_str);
5328 dst = ins->dst[0];
5329 for (i = 0; i < 4; ++i)
5331 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5332 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5333 continue;
5335 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5336 if (function)
5337 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5338 function, prefix, resource, bind_idx, address->buffer, swizzle);
5339 else
5340 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5341 prefix, resource, bind_idx, address->buffer, swizzle);
5344 string_buffer_release(priv->string_buffers, address);
5347 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5349 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5350 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5351 struct glsl_src_param image_coord_param, image_data_param;
5352 enum wined3d_shader_resource_type resource_type;
5353 enum wined3d_data_type data_type;
5354 unsigned int uav_idx;
5355 DWORD coord_mask;
5357 uav_idx = ins->dst[0].reg.idx[0].offset;
5358 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5360 ERR("Invalid UAV index %u.\n", uav_idx);
5361 return;
5363 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5364 if (resource_type >= ARRAY_SIZE(resource_type_info))
5366 ERR("Unexpected resource type %#x.\n", resource_type);
5367 return;
5369 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5370 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5372 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5373 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5374 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5375 shader_glsl_get_prefix(version->type), uav_idx,
5376 image_coord_param.param_str, image_data_param.param_str);
5379 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5381 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5382 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5383 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5384 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5385 struct glsl_src_param structure_idx, offset, data;
5386 unsigned int i, resource_idx, stride, src_idx = 0;
5387 struct wined3d_string_buffer *address;
5388 DWORD write_mask;
5389 BOOL is_tgsm;
5391 resource_idx = ins->dst[0].reg.idx[0].offset;
5392 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5393 if (is_tgsm)
5395 if (resource_idx >= reg_maps->tgsm_count)
5397 ERR("Invalid TGSM index %u.\n", resource_idx);
5398 return;
5400 stride = reg_maps->tgsm[resource_idx].stride;
5402 else
5404 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5406 ERR("Invalid UAV index %u.\n", resource_idx);
5407 return;
5409 stride = reg_maps->uav_resource_info[resource_idx].stride;
5412 address = string_buffer_get(priv->string_buffers);
5413 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5415 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5416 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5418 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5419 shader_addline(address, "%s / 4", offset.param_str);
5421 for (i = 0; i < 4; ++i)
5423 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5424 continue;
5426 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5428 if (is_tgsm)
5429 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5430 prefix, resource_idx, address->buffer, i, data.param_str);
5431 else
5432 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5433 prefix, resource_idx, address->buffer, i, data.param_str);
5436 string_buffer_release(priv->string_buffers, address);
5439 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5441 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5442 unsigned int sync_flags = ins->flags;
5444 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5446 shader_addline(buffer, "barrier();\n");
5447 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5450 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5452 shader_addline(buffer, "memoryBarrierShared();\n");
5453 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5456 if (sync_flags)
5457 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5460 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5461 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5463 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5464 unsigned int idx = reg->idx[0].offset;
5466 if (reg->type == WINED3DSPR_RESOURCE)
5468 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5470 ERR("Invalid resource index %u.\n", idx);
5471 return NULL;
5473 return &reg_maps->resource_info[idx];
5476 if (reg->type == WINED3DSPR_UAV)
5478 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5480 ERR("Invalid UAV index %u.\n", idx);
5481 return NULL;
5483 return &reg_maps->uav_resource_info[idx];
5486 FIXME("Unhandled register type %#x.\n", reg->type);
5487 return NULL;
5490 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5492 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5493 const struct wined3d_shader_resource_info *resource_info;
5494 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5495 unsigned int resource_idx;
5496 char dst_swizzle[6];
5497 DWORD write_mask;
5499 write_mask = shader_glsl_append_dst(buffer, ins);
5500 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5502 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5503 return;
5504 resource_idx = ins->src[0].reg.idx[0].offset;
5506 shader_addline(buffer, "ivec2(");
5507 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5509 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5510 resource_idx, WINED3D_SAMPLER_DEFAULT);
5511 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5513 else
5515 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5517 if (resource_info->stride)
5518 shader_addline(buffer, " / %u", resource_info->stride);
5519 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5520 shader_addline(buffer, " * 4");
5521 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5524 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5526 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5527 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5528 enum wined3d_shader_resource_type resource_type;
5529 enum wined3d_shader_register_type reg_type;
5530 unsigned int resource_idx, bind_idx, i;
5531 enum wined3d_data_type dst_data_type;
5532 struct glsl_src_param lod_param;
5533 char dst_swizzle[6];
5534 DWORD write_mask;
5536 dst_data_type = ins->dst[0].reg.data_type;
5537 if (ins->flags == WINED3DSI_RESINFO_UINT)
5538 dst_data_type = WINED3D_DATA_UINT;
5539 else if (ins->flags)
5540 FIXME("Unhandled flags %#x.\n", ins->flags);
5542 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5543 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5545 reg_type = ins->src[1].reg.type;
5546 resource_idx = ins->src[1].reg.idx[0].offset;
5547 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5548 if (reg_type == WINED3DSPR_RESOURCE)
5550 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5551 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5552 resource_idx, WINED3D_SAMPLER_DEFAULT);
5554 else
5556 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5557 bind_idx = resource_idx;
5560 if (resource_type >= ARRAY_SIZE(resource_type_info))
5562 ERR("Unexpected resource type %#x.\n", resource_type);
5563 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5566 if (dst_data_type == WINED3D_DATA_UINT)
5567 shader_addline(ins->ctx->buffer, "uvec4(");
5568 else
5569 shader_addline(ins->ctx->buffer, "vec4(");
5571 if (reg_type == WINED3DSPR_RESOURCE)
5573 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5574 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5576 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5577 shader_addline(ins->ctx->buffer, "0, ");
5579 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5581 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5582 shader_glsl_get_prefix(version->type), bind_idx);
5584 else
5586 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5587 shader_addline(ins->ctx->buffer, "1");
5590 else
5592 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5593 shader_glsl_get_prefix(version->type), bind_idx);
5595 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5596 shader_addline(ins->ctx->buffer, "0, ");
5598 /* For UAVs the returned miplevel count is always 1. */
5599 shader_addline(ins->ctx->buffer, "1");
5602 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5605 /* FIXME: The current implementation does not handle multisample textures correctly. */
5606 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5608 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5609 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5610 struct glsl_src_param coord_param, lod_param;
5611 struct glsl_sample_function sample_function;
5612 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5613 BOOL has_lod_param;
5615 if (wined3d_shader_instruction_has_texel_offset(ins))
5616 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5618 resource_idx = ins->src[1].reg.idx[0].offset;
5619 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5621 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5623 ERR("Invalid resource index %u.\n", resource_idx);
5624 return;
5626 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5628 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5629 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5630 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5631 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5632 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5633 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5634 "%s", coord_param.param_str);
5635 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5638 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5640 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5641 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5642 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5643 struct glsl_sample_function sample_function;
5644 DWORD flags = 0;
5646 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5647 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5648 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5649 flags |= WINED3D_GLSL_SAMPLE_LOD;
5650 if (wined3d_shader_instruction_has_texel_offset(ins))
5651 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5653 resource_idx = ins->src[1].reg.idx[0].offset;
5654 sampler_idx = ins->src[2].reg.idx[0].offset;
5656 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5657 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5659 switch (ins->handler_idx)
5661 case WINED3DSIH_SAMPLE:
5662 break;
5663 case WINED3DSIH_SAMPLE_B:
5664 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5665 lod_param_str = lod_param.param_str;
5666 break;
5667 case WINED3DSIH_SAMPLE_GRAD:
5668 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5669 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5670 dx_param_str = dx_param.param_str;
5671 dy_param_str = dy_param.param_str;
5672 break;
5673 case WINED3DSIH_SAMPLE_LOD:
5674 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5675 lod_param_str = lod_param.param_str;
5676 break;
5677 default:
5678 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5679 break;
5682 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5683 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5684 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5685 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5688 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5690 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5691 struct glsl_src_param coord_param, compare_param;
5692 struct glsl_sample_function sample_function;
5693 const char *lod_param = NULL;
5694 DWORD flags = 0;
5695 UINT coord_size;
5697 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5699 lod_param = "0";
5700 flags |= WINED3D_GLSL_SAMPLE_LOD;
5703 if (wined3d_shader_instruction_has_texel_offset(ins))
5704 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5706 resource_idx = ins->src[1].reg.idx[0].offset;
5707 sampler_idx = ins->src[2].reg.idx[0].offset;
5709 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5710 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5711 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5712 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5713 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5714 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5715 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5716 coord_size, coord_param.param_str, compare_param.param_str);
5717 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5720 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
5722 /* FIXME: Make this work for more than just 2D textures */
5723 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5724 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5726 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
5728 char dst_mask[6];
5730 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5731 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5732 ins->dst[0].reg.idx[0].offset, dst_mask);
5734 else
5736 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5737 DWORD reg = ins->src[0].reg.idx[0].offset;
5738 char dst_swizzle[6];
5740 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5742 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
5744 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
5745 struct glsl_src_param div_param;
5746 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
5748 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
5750 if (mask_size > 1)
5751 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
5752 else
5753 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
5755 else
5757 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
5762 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5763 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5764 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5765 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
5767 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5768 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5769 struct glsl_sample_function sample_function;
5770 struct glsl_src_param src0_param;
5771 UINT mask_size;
5773 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5775 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5776 * scalar, and projected sampling would require 4.
5778 * It is a dependent read - not valid with conditional NP2 textures
5780 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5781 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5783 switch(mask_size)
5785 case 1:
5786 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5787 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
5788 break;
5790 case 2:
5791 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5792 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
5793 break;
5795 case 3:
5796 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5797 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
5798 break;
5800 default:
5801 FIXME("Unexpected mask size %u\n", mask_size);
5802 break;
5804 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5807 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5808 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5809 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
5811 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5812 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5813 struct glsl_src_param src0_param;
5814 DWORD dst_mask;
5815 unsigned int mask_size;
5817 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5818 mask_size = shader_glsl_get_write_mask_size(dst_mask);
5819 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5821 if (mask_size > 1) {
5822 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
5823 } else {
5824 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
5828 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5829 * Calculate the depth as dst.x / dst.y */
5830 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
5832 struct glsl_dst_param dst_param;
5834 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5836 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5837 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5838 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5839 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5840 * >= 1.0 or < 0.0
5842 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5843 dst_param.reg_name, dst_param.reg_name);
5846 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5847 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5848 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5849 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5851 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
5853 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5854 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5855 struct glsl_src_param src0_param;
5857 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5859 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
5860 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5863 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5864 * Calculate the 1st of a 2-row matrix multiplication. */
5865 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
5867 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5868 DWORD reg = ins->dst[0].reg.idx[0].offset;
5869 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5870 struct glsl_src_param src0_param;
5872 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5873 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5876 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5877 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5878 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
5880 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5881 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5882 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5883 DWORD reg = ins->dst[0].reg.idx[0].offset;
5884 struct glsl_src_param src0_param;
5886 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5887 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
5888 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
5891 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
5893 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5894 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5895 struct glsl_sample_function sample_function;
5896 DWORD reg = ins->dst[0].reg.idx[0].offset;
5897 struct glsl_src_param src0_param;
5899 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5900 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5902 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5904 /* Sample the texture using the calculated coordinates */
5905 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5906 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5909 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5910 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5911 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5913 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5914 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5915 struct glsl_sample_function sample_function;
5916 DWORD reg = ins->dst[0].reg.idx[0].offset;
5917 struct glsl_src_param src0_param;
5919 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5920 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5922 /* Dependent read, not valid with conditional NP2 */
5923 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5925 /* Sample the texture using the calculated coordinates */
5926 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5927 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5929 tex_mx->current_row = 0;
5932 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5933 * Perform the 3rd row of a 3x3 matrix multiply */
5934 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5936 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5937 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5938 DWORD reg = ins->dst[0].reg.idx[0].offset;
5939 struct glsl_src_param src0_param;
5940 char dst_mask[6];
5942 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5944 shader_glsl_append_dst(ins->ctx->buffer, ins);
5945 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5946 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5948 tex_mx->current_row = 0;
5951 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5952 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5953 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5955 struct glsl_src_param src0_param;
5956 struct glsl_src_param src1_param;
5957 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5958 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5959 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5960 struct glsl_sample_function sample_function;
5961 DWORD reg = ins->dst[0].reg.idx[0].offset;
5962 char coord_mask[6];
5964 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5965 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5967 /* Perform the last matrix multiply operation */
5968 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5969 /* Reflection calculation */
5970 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5972 /* Dependent read, not valid with conditional NP2 */
5973 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5974 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5976 /* Sample the texture */
5977 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5978 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5979 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5981 tex_mx->current_row = 0;
5984 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5985 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5986 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5988 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5989 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5990 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5991 struct glsl_sample_function sample_function;
5992 DWORD reg = ins->dst[0].reg.idx[0].offset;
5993 struct glsl_src_param src0_param;
5994 char coord_mask[6];
5996 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5998 /* Perform the last matrix multiply operation */
5999 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6001 /* Construct the eye-ray vector from w coordinates */
6002 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6003 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6004 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6006 /* Dependent read, not valid with conditional NP2 */
6007 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6008 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6010 /* Sample the texture using the calculated coordinates */
6011 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6012 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6013 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6015 tex_mx->current_row = 0;
6018 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6019 * Apply a fake bump map transform.
6020 * texbem is pshader <= 1.3 only, this saves a few version checks
6022 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6024 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6025 struct glsl_sample_function sample_function;
6026 struct glsl_src_param coord_param;
6027 DWORD sampler_idx;
6028 DWORD mask;
6029 DWORD flags;
6030 char coord_mask[6];
6032 sampler_idx = ins->dst[0].reg.idx[0].offset;
6033 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6034 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6036 /* Dependent read, not valid with conditional NP2 */
6037 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6038 mask = sample_function.coord_mask;
6040 shader_glsl_write_mask_to_str(mask, coord_mask);
6042 /* With projected textures, texbem only divides the static texture coord,
6043 * not the displacement, so we can't let GL handle this. */
6044 if (flags & WINED3D_PSARGS_PROJECTED)
6046 DWORD div_mask=0;
6047 char coord_div_mask[3];
6048 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6050 case WINED3D_TTFF_COUNT1:
6051 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6052 break;
6053 case WINED3D_TTFF_COUNT2:
6054 div_mask = WINED3DSP_WRITEMASK_1;
6055 break;
6056 case WINED3D_TTFF_COUNT3:
6057 div_mask = WINED3DSP_WRITEMASK_2;
6058 break;
6059 case WINED3D_TTFF_COUNT4:
6060 case WINED3D_TTFF_DISABLE:
6061 div_mask = WINED3DSP_WRITEMASK_3;
6062 break;
6064 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6065 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6068 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6070 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6071 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6072 coord_param.param_str, coord_mask);
6074 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6076 struct glsl_src_param luminance_param;
6077 struct glsl_dst_param dst_param;
6079 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6080 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6082 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6083 dst_param.reg_name, dst_param.mask_str,
6084 luminance_param.param_str, sampler_idx, sampler_idx);
6086 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6089 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6091 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6092 struct glsl_src_param src0_param, src1_param;
6094 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6095 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6097 shader_glsl_append_dst(ins->ctx->buffer, ins);
6098 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6099 src0_param.param_str, sampler_idx, src1_param.param_str);
6102 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6103 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6104 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6106 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6107 struct glsl_sample_function sample_function;
6108 struct glsl_src_param src0_param;
6110 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6112 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6113 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6114 "%s.wx", src0_param.reg_name);
6115 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6118 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6119 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6120 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6122 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6123 struct glsl_sample_function sample_function;
6124 struct glsl_src_param src0_param;
6126 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6128 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6129 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6130 "%s.yz", src0_param.reg_name);
6131 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6134 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6135 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6136 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6138 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6139 struct glsl_sample_function sample_function;
6140 struct glsl_src_param src0_param;
6142 /* Dependent read, not valid with conditional NP2 */
6143 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6144 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6146 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6147 "%s", src0_param.param_str);
6148 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6151 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6152 * If any of the first 3 components are < 0, discard this pixel */
6153 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6155 if (ins->ctx->reg_maps->shader_version.major >= 4)
6157 struct glsl_src_param src_param;
6159 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
6160 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
6162 else
6164 struct glsl_dst_param dst_param;
6166 /* The argument is a destination parameter, and no writemasks are allowed */
6167 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6169 /* 2.0 shaders compare all 4 components in texkill. */
6170 if (ins->ctx->reg_maps->shader_version.major >= 2)
6171 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6172 /* 1.x shaders only compare the first 3 components, probably due to
6173 * the nature of the texkill instruction as a tex* instruction, and
6174 * phase, which kills all .w components. Even if all 4 components are
6175 * defined, only the first 3 are used. */
6176 else
6177 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6181 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6182 * dst = dot2(src0, src1) + src2 */
6183 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6185 struct glsl_src_param src0_param;
6186 struct glsl_src_param src1_param;
6187 struct glsl_src_param src2_param;
6188 DWORD write_mask;
6189 unsigned int mask_size;
6191 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6192 mask_size = shader_glsl_get_write_mask_size(write_mask);
6194 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6195 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6196 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6198 if (mask_size > 1) {
6199 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6200 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6201 } else {
6202 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6203 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6207 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6208 const struct wined3d_shader_signature *input_signature,
6209 const struct wined3d_shader_reg_maps *reg_maps,
6210 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
6212 unsigned int i;
6214 for (i = 0; i < input_signature->element_count; ++i)
6216 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6217 const char *semantic_name;
6218 UINT semantic_idx;
6219 char reg_mask[6];
6221 /* Unused */
6222 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6223 continue;
6225 semantic_name = input->semantic_name;
6226 semantic_idx = input->semantic_idx;
6227 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6229 if (args->vp_mode == vertexshader)
6231 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6233 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6234 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6236 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6238 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6240 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6242 shader_addline(buffer, "ps_in[%u] = vec4("
6243 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
6244 input->register_idx);
6246 else
6248 if (input->sysval_semantic)
6249 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6250 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
6251 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6252 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6255 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6257 if (args->pointsprite)
6258 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6259 shader->u.ps.input_reg_map[input->register_idx]);
6260 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6261 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6262 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6263 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
6264 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6265 else
6266 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6267 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6269 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6271 if (!semantic_idx)
6272 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6273 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6274 else if (semantic_idx == 1)
6275 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6276 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6277 else
6278 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6279 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6281 else
6283 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6284 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6289 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6291 struct glsl_program_key key;
6293 key.vs_id = entry->vs.id;
6294 key.gs_id = entry->gs.id;
6295 key.ps_id = entry->ps.id;
6296 key.cs_id = entry->cs.id;
6298 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6300 ERR("Failed to insert program entry.\n");
6304 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6305 const struct glsl_program_key *key)
6307 struct wine_rb_entry *entry;
6309 entry = wine_rb_get(&priv->program_lookup, key);
6310 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6313 /* Context activation is done by the caller. */
6314 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6315 struct glsl_shader_prog_link *entry)
6317 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6319 GL_EXTCALL(glDeleteProgram(entry->id));
6320 if (entry->vs.id)
6321 list_remove(&entry->vs.shader_entry);
6322 if (entry->gs.id)
6323 list_remove(&entry->gs.shader_entry);
6324 if (entry->ps.id)
6325 list_remove(&entry->ps.shader_entry);
6326 if (entry->cs.id)
6327 list_remove(&entry->cs.shader_entry);
6328 HeapFree(GetProcessHeap(), 0, entry);
6331 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6332 const struct wined3d_gl_info *gl_info, const DWORD *map,
6333 const struct wined3d_shader_signature *input_signature,
6334 const struct wined3d_shader_reg_maps *reg_maps_in,
6335 const struct wined3d_shader_signature *output_signature,
6336 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6338 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6339 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6340 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6341 unsigned int in_count = vec4_varyings(3, gl_info);
6342 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
6343 DWORD in_idx, *set = NULL;
6344 unsigned int i, j;
6345 char reg_mask[6];
6347 set = wined3d_calloc(max_varyings, sizeof(*set));
6349 for (i = 0; i < input_signature->element_count; ++i)
6351 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6353 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6354 continue;
6356 in_idx = map[input->register_idx];
6357 /* Declared, but not read register */
6358 if (in_idx == ~0u)
6359 continue;
6360 if (in_idx >= max_varyings)
6362 FIXME("More input varyings declared than supported, expect issues.\n");
6363 continue;
6366 if (in_idx == in_count)
6367 string_buffer_sprintf(destination, "gl_FrontColor");
6368 else if (in_idx == in_count + 1)
6369 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6370 else
6371 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6373 if (!set[in_idx])
6374 set[in_idx] = ~0u;
6376 for (j = 0; j < output_signature->element_count; ++j)
6378 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6379 DWORD mask;
6381 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6382 || input->semantic_idx != output->semantic_idx
6383 || strcmp(input->semantic_name, output->semantic_name)
6384 || !(mask = input->mask & output->mask))
6385 continue;
6387 if (set[in_idx] == ~0u)
6388 set[in_idx] = 0;
6389 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6390 shader_glsl_write_mask_to_str(mask, reg_mask);
6392 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6393 destination->buffer, reg_mask, output->register_idx, reg_mask);
6397 for (i = 0; i < max_varyings; ++i)
6399 unsigned int size;
6401 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6402 continue;
6404 if (set[i] == ~0u)
6405 set[i] = 0;
6407 size = 0;
6408 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6409 reg_mask[size++] = 'x';
6410 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6411 reg_mask[size++] = 'y';
6412 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6413 reg_mask[size++] = 'z';
6414 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6415 reg_mask[size++] = 'w';
6416 reg_mask[size] = '\0';
6418 if (i == in_count)
6419 string_buffer_sprintf(destination, "gl_FrontColor");
6420 else if (i == in_count + 1)
6421 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6422 else
6423 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6425 if (size == 1)
6426 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6427 else
6428 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6431 HeapFree(GetProcessHeap(), 0, set);
6432 string_buffer_release(&priv->string_buffers, destination);
6435 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6436 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6437 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6439 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6440 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6441 char reg_mask[6];
6442 unsigned int i;
6444 for (i = 0; i < output_signature->element_count; ++i)
6446 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6448 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6449 continue;
6451 if (output->register_idx >= input_count)
6452 continue;
6454 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
6456 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6458 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6459 destination->buffer, reg_mask, output->register_idx, reg_mask);
6462 string_buffer_release(&priv->string_buffers, destination);
6465 /* Context activation is done by the caller. */
6466 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
6467 const struct wined3d_shader *vs, unsigned int input_count,
6468 const struct wined3d_gl_info *gl_info)
6470 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6471 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6473 if (legacy_context)
6474 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
6475 else
6476 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
6477 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6479 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
6480 legacy_context ? "gs_in" : "gs_in.gs_in");
6482 shader_addline(buffer, "}\n");
6485 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6486 const struct wined3d_gl_info *gl_info, const DWORD *map,
6487 const struct wined3d_shader_signature *input_signature,
6488 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6489 const struct wined3d_shader_signature *output_signature,
6490 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6492 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6493 const char *semantic_name;
6494 UINT semantic_idx;
6495 char reg_mask[6];
6496 unsigned int i;
6498 /* First, sort out position and point size system values. */
6499 for (i = 0; i < output_signature->element_count; ++i)
6501 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6503 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6504 continue;
6506 semantic_name = output->semantic_name;
6507 semantic_idx = output->semantic_idx;
6508 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6510 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6512 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6513 reg_mask, output->register_idx, reg_mask);
6515 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6517 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6518 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6520 else if (output->sysval_semantic)
6522 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6526 /* Then, setup the pixel shader input. */
6527 if (reg_maps_out->shader_version.major < 4)
6528 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6529 output_signature, reg_maps_out, "ps_link");
6530 else
6531 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
6534 /* Context activation is done by the caller. */
6535 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6536 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6537 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6539 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6540 GLuint ret;
6541 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6542 unsigned int i;
6543 const char *semantic_name;
6544 UINT semantic_idx;
6545 char reg_mask[6];
6546 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6548 string_buffer_clear(buffer);
6550 shader_glsl_add_version_declaration(buffer, gl_info, &vs->reg_maps.shader_version);
6552 if (per_vertex_point_size)
6554 shader_addline(buffer, "uniform struct\n{\n");
6555 shader_addline(buffer, " float size_min;\n");
6556 shader_addline(buffer, " float size_max;\n");
6557 shader_addline(buffer, "} ffp_point;\n");
6560 if (ps_major < 3)
6562 DWORD colors_written_mask[2] = {0};
6563 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
6565 if (!legacy_context)
6567 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
6568 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
6569 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6570 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6573 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6575 for (i = 0; i < vs->output_signature.element_count; ++i)
6577 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
6578 DWORD write_mask;
6580 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
6581 continue;
6583 semantic_name = output->semantic_name;
6584 semantic_idx = output->semantic_idx;
6585 write_mask = output->mask;
6586 shader_glsl_write_mask_to_str(write_mask, reg_mask);
6588 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
6590 if (legacy_context)
6591 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6592 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
6593 else
6594 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6595 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
6597 colors_written_mask[semantic_idx] = write_mask;
6599 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
6601 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6602 reg_mask, output->register_idx, reg_mask);
6604 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6606 if (semantic_idx < MAX_TEXTURES)
6608 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
6609 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
6610 semantic_idx, reg_mask, output->register_idx, reg_mask);
6611 texcoords_written_mask[semantic_idx] = write_mask;
6614 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6616 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6617 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6619 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6621 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6622 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6623 output->register_idx, reg_mask[1]);
6627 for (i = 0; i < 2; ++i)
6629 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6631 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6632 if (!i)
6633 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6634 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
6635 reg_mask, reg_mask);
6636 else
6637 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6638 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6639 reg_mask, reg_mask);
6642 for (i = 0; i < MAX_TEXTURES; ++i)
6644 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6645 continue;
6647 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6649 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6650 && !texcoords_written_mask[i])
6651 continue;
6653 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6654 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6655 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6659 else
6661 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6663 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
6664 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6665 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6666 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6669 shader_addline(buffer, "}\n");
6671 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6672 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6673 shader_glsl_compile(gl_info, ret, buffer->buffer);
6675 return ret;
6678 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
6679 const struct wined3d_shader *shader, unsigned int input_count,
6680 const struct wined3d_gl_info *gl_info)
6682 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6684 if (input_count)
6685 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
6687 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6688 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
6690 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
6691 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
6693 shader_addline(buffer, "}\n");
6696 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
6697 const struct wined3d_gl_info *gl_info)
6699 const char *output = get_fragment_output(gl_info);
6701 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
6702 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6703 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
6704 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
6705 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
6706 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
6709 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
6710 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
6712 const char *output = get_fragment_output(gl_info);
6714 switch (mode)
6716 case WINED3D_FFP_PS_FOG_OFF:
6717 return;
6719 case WINED3D_FFP_PS_FOG_LINEAR:
6720 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6721 break;
6723 case WINED3D_FFP_PS_FOG_EXP:
6724 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6725 break;
6727 case WINED3D_FFP_PS_FOG_EXP2:
6728 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6729 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6730 break;
6732 default:
6733 ERR("Invalid fog mode %#x.\n", mode);
6734 return;
6737 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6738 output, output);
6741 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
6742 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
6744 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6745 * flipping all the operators here, just negate the comparison below. */
6746 static const char * const comparison_operator[] =
6748 "", /* WINED3D_CMP_NEVER */
6749 "<", /* WINED3D_CMP_LESS */
6750 "==", /* WINED3D_CMP_EQUAL */
6751 "<=", /* WINED3D_CMP_LESSEQUAL */
6752 ">", /* WINED3D_CMP_GREATER */
6753 "!=", /* WINED3D_CMP_NOTEQUAL */
6754 ">=", /* WINED3D_CMP_GREATEREQUAL */
6755 "" /* WINED3D_CMP_ALWAYS */
6758 if (alpha_func == WINED3D_CMP_ALWAYS)
6759 return;
6761 if (alpha_func != WINED3D_CMP_NEVER)
6762 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
6763 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
6764 shader_addline(buffer, " discard;\n");
6767 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
6768 const struct wined3d_gl_info *gl_info)
6770 if (gl_info->supported[ARB_GPU_SHADER5])
6771 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
6772 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
6773 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
6774 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
6775 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
6776 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
6777 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
6778 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
6779 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
6780 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
6781 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
6782 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
6783 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
6784 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
6785 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
6786 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
6787 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
6788 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6789 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
6790 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
6791 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
6792 if (gl_info->supported[EXT_GPU_SHADER4])
6793 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
6794 if (gl_info->supported[EXT_TEXTURE_ARRAY])
6795 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
6798 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
6799 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6800 const struct ps_compile_args *args)
6802 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6804 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6805 if (reg_maps->shader_version.major < 2)
6806 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6808 if (args->srgb_correction)
6809 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6811 /* SM < 3 does not replace the fog stage. */
6812 if (reg_maps->shader_version.major < 3)
6813 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6815 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6818 /* Context activation is done by the caller. */
6819 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
6820 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6821 const struct wined3d_shader *shader,
6822 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
6824 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6825 const struct wined3d_gl_info *gl_info = context->gl_info;
6826 struct shader_glsl_ctx_priv priv_ctx;
6827 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6828 GLuint shader_id;
6830 memset(&priv_ctx, 0, sizeof(priv_ctx));
6831 priv_ctx.cur_ps_args = args;
6832 priv_ctx.cur_np2fixup_info = np2fixup_info;
6833 priv_ctx.string_buffers = string_buffers;
6835 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6837 shader_glsl_enable_extensions(buffer, gl_info);
6838 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
6839 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
6840 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6841 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6842 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
6843 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
6844 /* The spec says that it doesn't have to be explicitly enabled, but the
6845 * nvidia drivers write a warning if we don't do so. */
6846 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6847 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6849 /* Base Declarations */
6850 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6852 shader_addline(buffer, "void main()\n{\n");
6854 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6855 * add approximately 0.5. This causes off-by-one problems as spotted by
6856 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6857 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6858 * causes precision troubles when we just subtract 0.5.
6860 * To deal with that, just floor() the position. This will eliminate the
6861 * fraction on all cards.
6863 * TODO: Test how this behaves with multisampling.
6865 * An advantage of floor is that it works even if the driver doesn't add
6866 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6867 * to return in gl_FragCoord, even though coordinates specify the pixel
6868 * centers instead of the pixel corners. This code will behave correctly
6869 * on drivers that returns integer values. */
6870 if (reg_maps->vpos)
6872 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6873 shader_addline(buffer, "vpos = gl_FragCoord;\n");
6874 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
6875 shader_addline(buffer,
6876 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6877 else
6878 shader_addline(buffer,
6879 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6882 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
6884 unsigned int i;
6885 WORD map = reg_maps->texcoord;
6887 if (legacy_context)
6889 if (glsl_is_color_reg_read(shader, 0))
6890 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6891 if (glsl_is_color_reg_read(shader, 1))
6892 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6895 for (i = 0; map; map >>= 1, ++i)
6897 if (map & 1)
6899 if (args->pointsprite)
6900 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
6901 else if (args->texcoords_initialized & (1u << i))
6902 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
6903 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
6904 else
6905 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
6906 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
6910 if (legacy_context)
6911 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6914 /* Pack 3.0 inputs */
6915 if (reg_maps->shader_version.major >= 3)
6916 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
6918 /* Base Shader Body */
6919 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
6920 return 0;
6922 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6923 if (reg_maps->shader_version.major < 4)
6924 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
6926 shader_addline(buffer, "}\n");
6928 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6929 TRACE("Compiling shader object %u.\n", shader_id);
6930 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6932 return shader_id;
6935 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
6936 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6937 const struct vs_compile_args *args)
6939 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6940 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6941 unsigned int i;
6943 /* Unpack outputs. */
6944 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6946 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6947 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6948 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6949 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6951 if (reg_maps->shader_version.major < 3)
6953 if (args->fog_src == VS_FOG_Z)
6954 shader_addline(buffer, "%s = gl_Position.z;\n",
6955 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6956 else if (!reg_maps->fog)
6957 shader_addline(buffer, "%s = 0.0;\n",
6958 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6961 /* We always store the clipplanes without y inversion. */
6962 if (args->clip_enabled)
6964 if (legacy_context)
6965 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6966 else
6967 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6968 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6971 if (args->point_size && !args->per_vertex_point_size)
6972 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6974 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6975 shader_glsl_fixup_position(buffer);
6978 /* Context activation is done by the caller. */
6979 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6980 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6982 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6983 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6984 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6985 const struct wined3d_gl_info *gl_info = context->gl_info;
6986 struct shader_glsl_ctx_priv priv_ctx;
6987 GLuint shader_id;
6989 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6991 shader_glsl_enable_extensions(buffer, gl_info);
6992 if (gl_info->supported[ARB_DRAW_INSTANCED])
6993 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6994 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6995 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6997 memset(&priv_ctx, 0, sizeof(priv_ctx));
6998 priv_ctx.cur_vs_args = args;
6999 priv_ctx.string_buffers = string_buffers;
7001 /* Base Declarations */
7002 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7004 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7005 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7007 if (reg_maps->shader_version.major >= 4)
7009 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
7010 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
7011 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
7012 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
7015 shader_addline(buffer, "void main()\n{\n");
7017 /* Base Shader Body */
7018 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
7019 return 0;
7021 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7022 if (reg_maps->shader_version.major < 4)
7023 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7025 shader_addline(buffer, "}\n");
7027 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7028 TRACE("Compiling shader object %u.\n", shader_id);
7029 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7031 return shader_id;
7034 /* Context activation is done by the caller. */
7035 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
7036 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
7038 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7039 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7040 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7041 const struct wined3d_gl_info *gl_info = context->gl_info;
7042 struct shader_glsl_ctx_priv priv_ctx;
7043 GLuint shader_id;
7045 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
7047 shader_glsl_enable_extensions(buffer, gl_info);
7048 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
7049 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
7051 memset(&priv_ctx, 0, sizeof(priv_ctx));
7052 priv_ctx.string_buffers = string_buffers;
7053 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7054 if (!gl_info->supported[ARB_CLIP_CONTROL])
7055 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7056 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
7057 shader_addline(buffer, "void main()\n{\n");
7058 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
7059 return 0;
7060 shader_addline(buffer, "}\n");
7062 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
7063 TRACE("Compiling shader object %u.\n", shader_id);
7064 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7066 return shader_id;
7069 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
7071 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
7072 const struct wined3d_gl_info *gl_info = ctx->gl_info;
7073 const struct wined3d_shader *shader = ctx->shader;
7075 switch (shader->reg_maps.shader_version.type)
7077 case WINED3D_SHADER_TYPE_PIXEL:
7078 shader_glsl_generate_ps_epilogue(gl_info, ctx->buffer, shader, priv->cur_ps_args);
7079 break;
7080 case WINED3D_SHADER_TYPE_VERTEX:
7081 shader_glsl_generate_vs_epilogue(gl_info, ctx->buffer, shader, priv->cur_vs_args);
7082 break;
7083 case WINED3D_SHADER_TYPE_GEOMETRY:
7084 case WINED3D_SHADER_TYPE_COMPUTE:
7085 break;
7086 default:
7087 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7088 break;
7092 /* Context activation is done by the caller. */
7093 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
7094 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7095 const struct wined3d_shader *shader)
7097 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
7098 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7099 const struct wined3d_gl_info *gl_info = context->gl_info;
7100 struct shader_glsl_ctx_priv priv_ctx;
7101 GLuint shader_id;
7102 unsigned int i;
7104 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
7106 shader_glsl_enable_extensions(buffer, gl_info);
7107 if (gl_info->supported[ARB_COMPUTE_SHADER])
7108 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
7110 memset(&priv_ctx, 0, sizeof(priv_ctx));
7111 priv_ctx.string_buffers = string_buffers;
7112 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7114 for (i = 0; i < reg_maps->tgsm_count; ++i)
7116 if (reg_maps->tgsm[i].size)
7117 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
7120 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
7121 thread_group_size->x, thread_group_size->y, thread_group_size->z);
7123 shader_addline(buffer, "void main()\n{\n");
7124 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
7125 shader_addline(buffer, "}\n");
7127 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
7128 TRACE("Compiling shader object %u.\n", shader_id);
7129 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7131 return shader_id;
7134 static GLuint find_glsl_pshader(const struct wined3d_context *context,
7135 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7136 struct wined3d_shader *shader,
7137 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
7139 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
7140 struct glsl_shader_private *shader_data;
7141 struct ps_np2fixup_info *np2fixup;
7142 UINT i;
7143 DWORD new_size;
7144 GLuint ret;
7146 if (!shader->backend_data)
7148 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
7149 if (!shader->backend_data)
7151 ERR("Failed to allocate backend data.\n");
7152 return 0;
7155 shader_data = shader->backend_data;
7156 gl_shaders = shader_data->gl_shaders.ps;
7158 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7159 * so a linear search is more performant than a hashmap or a binary search
7160 * (cache coherency etc)
7162 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7164 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
7166 if (args->np2_fixup)
7167 *np2fixup_info = &gl_shaders[i].np2fixup;
7168 return gl_shaders[i].id;
7172 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7173 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
7174 if (shader_data->num_gl_shaders)
7176 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
7177 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
7178 new_size * sizeof(*gl_shaders));
7180 else
7182 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
7183 new_size = 1;
7186 if(!new_array) {
7187 ERR("Out of memory\n");
7188 return 0;
7190 shader_data->gl_shaders.ps = new_array;
7191 shader_data->shader_array_size = new_size;
7192 gl_shaders = new_array;
7195 gl_shaders[shader_data->num_gl_shaders].args = *args;
7197 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
7198 memset(np2fixup, 0, sizeof(*np2fixup));
7199 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
7201 pixelshader_update_resource_types(shader, args->tex_types);
7203 string_buffer_clear(buffer);
7204 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
7205 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7207 return ret;
7210 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
7211 const DWORD use_map)
7213 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
7214 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
7215 if (stored->point_size != new->point_size)
7216 return FALSE;
7217 if (stored->per_vertex_point_size != new->per_vertex_point_size)
7218 return FALSE;
7219 if (stored->flatshading != new->flatshading)
7220 return FALSE;
7221 if (stored->next_shader_type != new->next_shader_type)
7222 return FALSE;
7223 if (stored->next_shader_input_count != new->next_shader_input_count)
7224 return FALSE;
7225 return stored->fog_src == new->fog_src;
7228 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
7229 struct wined3d_shader *shader, const struct vs_compile_args *args)
7231 UINT i;
7232 DWORD new_size;
7233 DWORD use_map = context->stream_info.use_map;
7234 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
7235 struct glsl_shader_private *shader_data;
7236 GLuint ret;
7238 if (!shader->backend_data)
7240 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
7241 if (!shader->backend_data)
7243 ERR("Failed to allocate backend data.\n");
7244 return 0;
7247 shader_data = shader->backend_data;
7248 gl_shaders = shader_data->gl_shaders.vs;
7250 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7251 * so a linear search is more performant than a hashmap or a binary search
7252 * (cache coherency etc)
7254 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7256 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
7257 return gl_shaders[i].id;
7260 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7262 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
7263 if (shader_data->num_gl_shaders)
7265 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
7266 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
7267 new_size * sizeof(*gl_shaders));
7269 else
7271 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
7272 new_size = 1;
7275 if(!new_array) {
7276 ERR("Out of memory\n");
7277 return 0;
7279 shader_data->gl_shaders.vs = new_array;
7280 shader_data->shader_array_size = new_size;
7281 gl_shaders = new_array;
7284 gl_shaders[shader_data->num_gl_shaders].args = *args;
7286 string_buffer_clear(&priv->shader_buffer);
7287 ret = shader_glsl_generate_vshader(context, priv, shader, args);
7288 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7290 return ret;
7293 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
7294 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
7296 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
7297 struct glsl_shader_private *shader_data;
7298 unsigned int i, new_size;
7299 GLuint ret;
7301 if (!shader->backend_data)
7303 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7305 ERR("Failed to allocate backend data.\n");
7306 return 0;
7309 shader_data = shader->backend_data;
7310 gl_shaders = shader_data->gl_shaders.gs;
7312 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7314 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
7315 return gl_shaders[i].id;
7318 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7320 if (shader_data->num_gl_shaders)
7322 new_size = shader_data->shader_array_size + 1;
7323 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
7324 new_size * sizeof(*new_array));
7326 else
7328 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
7329 new_size = 1;
7332 if (!new_array)
7334 ERR("Failed to allocate GL shaders array.\n");
7335 return 0;
7337 shader_data->gl_shaders.gs = new_array;
7338 shader_data->shader_array_size = new_size;
7339 gl_shaders = new_array;
7341 string_buffer_clear(&priv->shader_buffer);
7342 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
7343 gl_shaders[shader_data->num_gl_shaders].args = *args;
7344 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7346 return ret;
7349 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
7350 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7351 struct wined3d_shader *shader)
7353 struct glsl_cs_compiled_shader *gl_shaders;
7354 struct glsl_shader_private *shader_data;
7355 GLuint ret;
7357 if (!shader->backend_data)
7359 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7361 ERR("Failed to allocate backend data.\n");
7362 return 0;
7365 shader_data = shader->backend_data;
7366 gl_shaders = shader_data->gl_shaders.cs;
7368 /* No shader variants are used for compute shaders. */
7369 if (shader_data->num_gl_shaders)
7370 return gl_shaders[0].id;
7372 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7374 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
7376 ERR("Failed to allocate GL shader array.\n");
7377 return 0;
7379 shader_data->shader_array_size = 1;
7380 gl_shaders = shader_data->gl_shaders.cs;
7382 string_buffer_clear(buffer);
7383 ret = shader_glsl_generate_compute_shader(context, buffer, string_buffers, shader);
7384 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7386 return ret;
7389 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
7391 switch (mcs)
7393 case WINED3D_MCS_MATERIAL:
7394 return material;
7395 case WINED3D_MCS_COLOR1:
7396 return "ffp_attrib_diffuse";
7397 case WINED3D_MCS_COLOR2:
7398 return "ffp_attrib_specular";
7399 default:
7400 ERR("Invalid material color source %#x.\n", mcs);
7401 return "<invalid>";
7405 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
7406 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
7408 const char *diffuse, *specular, *emissive, *ambient;
7409 unsigned int i, idx;
7411 if (!settings->lighting)
7413 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
7414 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
7415 return;
7418 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
7419 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
7420 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
7421 shader_addline(buffer, "vec3 dir, dst;\n");
7422 shader_addline(buffer, "float att, t;\n");
7424 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
7425 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
7426 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
7427 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
7429 idx = 0;
7430 for (i = 0; i < settings->point_light_count; ++i, ++idx)
7432 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7433 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7434 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7435 shader_addline(buffer, "dst.x = 1.0;\n");
7436 if (legacy_lighting)
7438 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7439 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7441 else
7443 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7445 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7446 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
7447 if (!legacy_lighting)
7448 shader_addline(buffer, "att = 1.0 / att;\n");
7449 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7450 if (!settings->normal)
7452 if (!legacy_lighting)
7453 shader_addline(buffer, "}\n");
7454 continue;
7456 shader_addline(buffer, "dir = normalize(dir);\n");
7457 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7458 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7459 if (settings->localviewer)
7460 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7461 else
7462 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7463 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7464 " * ffp_light[%u].specular) * att;\n", idx);
7465 if (!legacy_lighting)
7466 shader_addline(buffer, "}\n");
7469 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
7471 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7472 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7473 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7474 shader_addline(buffer, "dst.x = 1.0;\n");
7475 if (legacy_lighting)
7477 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7478 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7480 else
7482 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7484 shader_addline(buffer, "dir = normalize(dir);\n");
7485 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
7486 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
7487 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
7488 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
7489 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
7490 idx, idx, idx, idx);
7491 if (legacy_lighting)
7492 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7493 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7494 idx, idx, idx);
7495 else
7496 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7497 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7498 idx, idx, idx);
7499 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7500 if (!settings->normal)
7502 if (!legacy_lighting)
7503 shader_addline(buffer, "}\n");
7504 continue;
7506 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7507 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7508 if (settings->localviewer)
7509 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7510 else
7511 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7512 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7513 " * ffp_light[%u].specular) * att;\n", idx);
7514 if (!legacy_lighting)
7515 shader_addline(buffer, "}\n");
7518 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
7520 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7521 if (!settings->normal)
7522 continue;
7523 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
7524 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7525 " * ffp_light[%u].diffuse.xyz;\n", idx);
7526 /* TODO: In the non-local viewer case the halfvector is constant
7527 * and could be precomputed and stored in a uniform. */
7528 if (settings->localviewer)
7529 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7530 else
7531 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7532 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7533 " * ffp_light[%u].specular;\n", idx);
7536 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
7538 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7539 if (!settings->normal)
7540 continue;
7541 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
7542 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7543 " * ffp_light[%u].diffuse.xyz;\n", idx);
7544 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7545 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7546 " * ffp_light[%u].specular;\n", idx);
7549 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
7550 ambient, diffuse, emissive);
7551 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
7552 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
7555 /* Context activation is done by the caller. */
7556 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
7557 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
7559 static const struct attrib_info
7561 const char type[6];
7562 const char name[24];
7564 attrib_info[] =
7566 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
7567 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
7568 /* TODO: Indexed vertex blending */
7569 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
7570 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
7571 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
7572 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
7573 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
7575 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7576 BOOL legacy_lighting = priv->legacy_lighting;
7577 GLuint shader_obj;
7578 unsigned int i;
7579 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7580 BOOL output_legacy_fogcoord = legacy_context;
7582 string_buffer_clear(buffer);
7584 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
7586 if (shader_glsl_use_explicit_attrib_location(gl_info))
7587 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7589 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
7591 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
7593 if (shader_glsl_use_explicit_attrib_location(gl_info))
7594 shader_addline(buffer, "layout(location = %u) ", i);
7595 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
7597 shader_addline(buffer, "\n");
7599 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
7600 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
7601 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
7602 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
7604 shader_addline(buffer, "uniform struct\n{\n");
7605 shader_addline(buffer, " vec4 emissive;\n");
7606 shader_addline(buffer, " vec4 ambient;\n");
7607 shader_addline(buffer, " vec4 diffuse;\n");
7608 shader_addline(buffer, " vec4 specular;\n");
7609 shader_addline(buffer, " float shininess;\n");
7610 shader_addline(buffer, "} ffp_material;\n");
7612 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
7613 shader_addline(buffer, "uniform struct\n{\n");
7614 shader_addline(buffer, " vec4 diffuse;\n");
7615 shader_addline(buffer, " vec4 specular;\n");
7616 shader_addline(buffer, " vec4 ambient;\n");
7617 shader_addline(buffer, " vec4 position;\n");
7618 shader_addline(buffer, " vec3 direction;\n");
7619 shader_addline(buffer, " float range;\n");
7620 shader_addline(buffer, " float falloff;\n");
7621 shader_addline(buffer, " float c_att;\n");
7622 shader_addline(buffer, " float l_att;\n");
7623 shader_addline(buffer, " float q_att;\n");
7624 shader_addline(buffer, " float cos_htheta;\n");
7625 shader_addline(buffer, " float cos_hphi;\n");
7626 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
7628 if (settings->point_size)
7630 shader_addline(buffer, "uniform struct\n{\n");
7631 shader_addline(buffer, " float size;\n");
7632 shader_addline(buffer, " float size_min;\n");
7633 shader_addline(buffer, " float size_max;\n");
7634 shader_addline(buffer, " float c_att;\n");
7635 shader_addline(buffer, " float l_att;\n");
7636 shader_addline(buffer, " float q_att;\n");
7637 shader_addline(buffer, "} ffp_point;\n");
7640 if (legacy_context)
7642 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7643 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7644 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7645 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7647 else
7649 if (settings->clipping)
7650 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7652 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7653 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7654 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7655 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7658 shader_addline(buffer, "\nvoid main()\n{\n");
7659 shader_addline(buffer, "float m;\n");
7660 shader_addline(buffer, "vec3 r;\n");
7662 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
7664 if (attrib_info[i].name[0])
7665 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
7666 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
7668 for (i = 0; i < MAX_TEXTURES; ++i)
7670 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
7671 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7672 && settings->texcoords & (1u << i))
7673 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
7676 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
7678 if (settings->transformed)
7680 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7681 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7682 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7684 else
7686 for (i = 0; i < settings->vertexblends; ++i)
7687 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
7689 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
7690 for (i = 0; i < settings->vertexblends + 1; ++i)
7691 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
7693 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7694 if (settings->clipping)
7696 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7697 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
7698 else
7699 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7700 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
7702 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
7705 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
7706 if (settings->normal)
7708 if (!settings->vertexblends)
7710 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7712 else
7714 for (i = 0; i < settings->vertexblends + 1; ++i)
7715 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
7718 if (settings->normalize)
7719 shader_addline(buffer, "normal = normalize(normal);\n");
7722 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
7723 if (legacy_context)
7725 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
7726 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7728 else
7730 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7731 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7734 for (i = 0; i < MAX_TEXTURES; ++i)
7736 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7738 switch (settings->texgen[i] & 0xffff0000)
7740 case WINED3DTSS_TCI_PASSTHRU:
7741 if (settings->texcoords & (1u << i))
7742 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7743 i, i, i);
7744 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
7745 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
7746 else
7747 output_legacy_texcoord = FALSE;
7748 break;
7750 case WINED3DTSS_TCI_CAMERASPACENORMAL:
7751 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
7752 break;
7754 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
7755 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
7756 break;
7758 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
7759 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7760 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
7761 break;
7763 case WINED3DTSS_TCI_SPHEREMAP:
7764 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7765 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7766 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7767 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
7768 break;
7770 default:
7771 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
7772 break;
7774 if (output_legacy_texcoord)
7775 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
7778 switch (settings->fog_mode)
7780 case WINED3D_FFP_VS_FOG_OFF:
7781 output_legacy_fogcoord = FALSE;
7782 break;
7784 case WINED3D_FFP_VS_FOG_FOGCOORD:
7785 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7786 break;
7788 case WINED3D_FFP_VS_FOG_RANGE:
7789 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7790 break;
7792 case WINED3D_FFP_VS_FOG_DEPTH:
7793 if (settings->ortho_fog)
7795 if (gl_info->supported[ARB_CLIP_CONTROL])
7796 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
7797 else
7798 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7799 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7801 else if (settings->transformed)
7803 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
7805 else
7807 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7809 break;
7811 default:
7812 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
7813 break;
7815 if (output_legacy_fogcoord)
7816 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7818 if (settings->point_size)
7820 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7821 " + ffp_point.l_att * length(ec_pos.xyz)"
7822 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7823 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
7824 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7827 shader_addline(buffer, "}\n");
7829 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7830 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
7832 return shader_obj;
7835 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
7836 DWORD argnum, unsigned int stage, DWORD arg)
7838 const char *ret;
7840 if (arg == ARG_UNUSED)
7841 return "<unused arg>";
7843 switch (arg & WINED3DTA_SELECTMASK)
7845 case WINED3DTA_DIFFUSE:
7846 ret = "ffp_varying_diffuse";
7847 break;
7849 case WINED3DTA_CURRENT:
7850 ret = "ret";
7851 break;
7853 case WINED3DTA_TEXTURE:
7854 switch (stage)
7856 case 0: ret = "tex0"; break;
7857 case 1: ret = "tex1"; break;
7858 case 2: ret = "tex2"; break;
7859 case 3: ret = "tex3"; break;
7860 case 4: ret = "tex4"; break;
7861 case 5: ret = "tex5"; break;
7862 case 6: ret = "tex6"; break;
7863 case 7: ret = "tex7"; break;
7864 default:
7865 ret = "<invalid texture>";
7866 break;
7868 break;
7870 case WINED3DTA_TFACTOR:
7871 ret = "tex_factor";
7872 break;
7874 case WINED3DTA_SPECULAR:
7875 ret = "ffp_varying_specular";
7876 break;
7878 case WINED3DTA_TEMP:
7879 ret = "temp_reg";
7880 break;
7882 case WINED3DTA_CONSTANT:
7883 switch (stage)
7885 case 0: ret = "tss_const0"; break;
7886 case 1: ret = "tss_const1"; break;
7887 case 2: ret = "tss_const2"; break;
7888 case 3: ret = "tss_const3"; break;
7889 case 4: ret = "tss_const4"; break;
7890 case 5: ret = "tss_const5"; break;
7891 case 6: ret = "tss_const6"; break;
7892 case 7: ret = "tss_const7"; break;
7893 default:
7894 ret = "<invalid constant>";
7895 break;
7897 break;
7899 default:
7900 return "<unhandled arg>";
7903 if (arg & WINED3DTA_COMPLEMENT)
7905 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
7906 if (argnum == 0)
7907 ret = "arg0";
7908 else if (argnum == 1)
7909 ret = "arg1";
7910 else if (argnum == 2)
7911 ret = "arg2";
7914 if (arg & WINED3DTA_ALPHAREPLICATE)
7916 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
7917 if (argnum == 0)
7918 ret = "arg0";
7919 else if (argnum == 1)
7920 ret = "arg1";
7921 else if (argnum == 2)
7922 ret = "arg2";
7925 return ret;
7928 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
7929 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
7931 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
7933 if (color && alpha)
7934 dstmask = "";
7935 else if (color)
7936 dstmask = ".xyz";
7937 else
7938 dstmask = ".w";
7940 if (dst == tempreg)
7941 dstreg = "temp_reg";
7942 else
7943 dstreg = "ret";
7945 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
7946 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
7947 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
7949 switch (op)
7951 case WINED3D_TOP_DISABLE:
7952 break;
7954 case WINED3D_TOP_SELECT_ARG1:
7955 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
7956 break;
7958 case WINED3D_TOP_SELECT_ARG2:
7959 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
7960 break;
7962 case WINED3D_TOP_MODULATE:
7963 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7964 break;
7966 case WINED3D_TOP_MODULATE_4X:
7967 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7968 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7969 break;
7971 case WINED3D_TOP_MODULATE_2X:
7972 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7973 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7974 break;
7976 case WINED3D_TOP_ADD:
7977 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7978 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7979 break;
7981 case WINED3D_TOP_ADD_SIGNED:
7982 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7983 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7984 break;
7986 case WINED3D_TOP_ADD_SIGNED_2X:
7987 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7988 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7989 break;
7991 case WINED3D_TOP_SUBTRACT:
7992 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7993 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7994 break;
7996 case WINED3D_TOP_ADD_SMOOTH:
7997 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7998 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
7999 break;
8001 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
8002 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
8003 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8004 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8005 break;
8007 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
8008 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
8009 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8010 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8011 break;
8013 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
8014 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
8015 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8016 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8017 break;
8019 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
8020 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
8021 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8022 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
8023 break;
8025 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
8026 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
8027 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8028 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8029 break;
8031 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
8032 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
8033 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
8034 break;
8036 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
8037 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
8038 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
8039 break;
8041 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
8042 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8043 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
8044 break;
8045 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
8046 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
8047 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
8048 break;
8050 case WINED3D_TOP_BUMPENVMAP:
8051 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
8052 /* These are handled in the first pass, nothing to do. */
8053 break;
8055 case WINED3D_TOP_DOTPRODUCT3:
8056 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
8057 dstreg, dstmask, arg1, arg2, dstmask);
8058 break;
8060 case WINED3D_TOP_MULTIPLY_ADD:
8061 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
8062 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
8063 break;
8065 case WINED3D_TOP_LERP:
8066 /* MSDN isn't quite right here. */
8067 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
8068 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
8069 break;
8071 default:
8072 FIXME("Unhandled operation %#x.\n", op);
8073 break;
8077 /* Context activation is done by the caller. */
8078 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
8079 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
8081 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
8082 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
8083 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8084 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8085 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
8086 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
8087 UINT lowest_disabled_stage;
8088 GLuint shader_id;
8089 DWORD arg0, arg1, arg2;
8090 unsigned int stage;
8092 string_buffer_clear(buffer);
8094 /* Find out which textures are read */
8095 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8097 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8098 break;
8100 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
8101 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
8102 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
8104 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
8105 || (stage == 0 && settings->color_key_enabled))
8106 tex_map |= 1u << stage;
8107 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
8108 tfactor_used = TRUE;
8109 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
8110 tempreg_used = TRUE;
8111 if (settings->op[stage].dst == tempreg)
8112 tempreg_used = TRUE;
8113 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
8114 tss_const_map |= 1u << stage;
8116 switch (settings->op[stage].cop)
8118 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
8119 lum_map |= 1u << stage;
8120 /* fall through */
8121 case WINED3D_TOP_BUMPENVMAP:
8122 bump_map |= 1u << stage;
8123 /* fall through */
8124 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
8125 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
8126 tex_map |= 1u << stage;
8127 break;
8129 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
8130 tfactor_used = TRUE;
8131 break;
8133 default:
8134 break;
8137 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8138 continue;
8140 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
8141 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
8142 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
8144 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
8145 tex_map |= 1u << stage;
8146 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
8147 tfactor_used = TRUE;
8148 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
8149 tempreg_used = TRUE;
8150 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
8151 tss_const_map |= 1u << stage;
8153 lowest_disabled_stage = stage;
8155 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
8157 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
8158 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
8160 if (!needs_legacy_glsl_syntax(gl_info))
8161 shader_addline(buffer, "out vec4 ps_out[1];\n");
8163 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
8164 shader_addline(buffer, "vec4 ret;\n");
8165 if (tempreg_used || settings->sRGB_write)
8166 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
8167 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
8169 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8171 if (tss_const_map & (1u << stage))
8172 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
8174 if (!(tex_map & (1u << stage)))
8175 continue;
8177 switch (settings->op[stage].tex_type)
8179 case WINED3D_GL_RES_TYPE_TEX_1D:
8180 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
8181 break;
8182 case WINED3D_GL_RES_TYPE_TEX_2D:
8183 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
8184 break;
8185 case WINED3D_GL_RES_TYPE_TEX_3D:
8186 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
8187 break;
8188 case WINED3D_GL_RES_TYPE_TEX_CUBE:
8189 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
8190 break;
8191 case WINED3D_GL_RES_TYPE_TEX_RECT:
8192 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
8193 break;
8194 default:
8195 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
8196 break;
8199 shader_addline(buffer, "vec4 tex%u;\n", stage);
8201 if (!(bump_map & (1u << stage)))
8202 continue;
8203 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
8205 if (!(lum_map & (1u << stage)))
8206 continue;
8207 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
8208 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
8210 if (tfactor_used)
8211 shader_addline(buffer, "uniform vec4 tex_factor;\n");
8212 if (settings->color_key_enabled)
8213 shader_addline(buffer, "uniform vec4 color_key[2];\n");
8214 shader_addline(buffer, "uniform vec4 specular_enable;\n");
8216 if (settings->sRGB_write)
8218 shader_addline(buffer, "const vec4 srgb_const0 = ");
8219 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
8220 shader_addline(buffer, ";\n");
8221 shader_addline(buffer, "const vec4 srgb_const1 = ");
8222 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
8223 shader_addline(buffer, ";\n");
8226 shader_addline(buffer, "uniform struct\n{\n");
8227 shader_addline(buffer, " vec4 color;\n");
8228 shader_addline(buffer, " float density;\n");
8229 shader_addline(buffer, " float end;\n");
8230 shader_addline(buffer, " float scale;\n");
8231 shader_addline(buffer, "} ffp_fog;\n");
8233 if (alpha_test_func != WINED3D_CMP_ALWAYS)
8234 shader_addline(buffer, "uniform float alpha_test_ref;\n");
8236 if (legacy_context)
8238 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8239 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8240 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8241 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
8242 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8244 else
8246 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8247 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8248 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8249 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
8250 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8253 shader_addline(buffer, "void main()\n{\n");
8255 if (legacy_context)
8257 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
8258 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
8261 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8263 if (tex_map & (1u << stage))
8265 if (settings->pointsprite)
8266 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
8267 else if (settings->texcoords_initialized & (1u << stage))
8268 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
8269 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
8270 else
8271 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
8275 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
8276 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
8278 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
8279 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
8281 /* Generate texture sampling instructions */
8282 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
8284 const char *texture_function, *coord_mask;
8285 BOOL proj;
8287 if (!(tex_map & (1u << stage)))
8288 continue;
8290 if (settings->op[stage].projected == proj_none)
8292 proj = FALSE;
8294 else if (settings->op[stage].projected == proj_count4
8295 || settings->op[stage].projected == proj_count3)
8297 proj = TRUE;
8299 else
8301 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
8302 proj = TRUE;
8305 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
8306 proj = FALSE;
8308 switch (settings->op[stage].tex_type)
8310 case WINED3D_GL_RES_TYPE_TEX_1D:
8311 if (proj)
8313 texture_function = "texture1DProj";
8314 coord_mask = "xw";
8316 else
8318 texture_function = "texture1D";
8319 coord_mask = "x";
8321 break;
8322 case WINED3D_GL_RES_TYPE_TEX_2D:
8323 if (proj)
8325 texture_function = "texture2DProj";
8326 coord_mask = "xyw";
8328 else
8330 texture_function = "texture2D";
8331 coord_mask = "xy";
8333 break;
8334 case WINED3D_GL_RES_TYPE_TEX_3D:
8335 if (proj)
8337 texture_function = "texture3DProj";
8338 coord_mask = "xyzw";
8340 else
8342 texture_function = "texture3D";
8343 coord_mask = "xyz";
8345 break;
8346 case WINED3D_GL_RES_TYPE_TEX_CUBE:
8347 texture_function = "textureCube";
8348 coord_mask = "xyz";
8349 break;
8350 case WINED3D_GL_RES_TYPE_TEX_RECT:
8351 if (proj)
8353 texture_function = "texture2DRectProj";
8354 coord_mask = "xyw";
8356 else
8358 texture_function = "texture2DRect";
8359 coord_mask = "xy";
8361 break;
8362 default:
8363 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
8364 texture_function = "";
8365 coord_mask = "xyzw";
8366 break;
8368 if (!needs_legacy_glsl_syntax(gl_info))
8369 texture_function = proj ? "textureProj" : "texture";
8371 if (stage > 0
8372 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
8373 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
8375 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
8377 /* With projective textures, texbem only divides the static
8378 * texture coord, not the displacement, so multiply the
8379 * displacement with the dividing parameter before passing it to
8380 * TXP. */
8381 if (settings->op[stage].projected != proj_none)
8383 if (settings->op[stage].projected == proj_count4)
8385 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
8386 stage, stage);
8387 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
8389 else
8391 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
8392 stage, stage);
8393 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
8396 else
8398 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
8401 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
8402 stage, texture_function, stage, coord_mask);
8404 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8405 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
8406 stage, stage - 1, stage - 1, stage - 1);
8408 else if (settings->op[stage].projected == proj_count3)
8410 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
8411 stage, texture_function, stage, stage);
8413 else
8415 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
8416 stage, texture_function, stage, stage, coord_mask);
8419 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
8420 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
8421 settings->op[stage].color_fixup);
8424 if (settings->color_key_enabled)
8426 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
8427 shader_addline(buffer, " discard;\n");
8430 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
8432 /* Generate the main shader */
8433 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8435 BOOL op_equal;
8437 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8438 break;
8440 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8441 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8442 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
8443 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8444 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8445 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
8446 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8447 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8448 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
8449 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8450 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8451 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
8452 else
8453 op_equal = settings->op[stage].aop == settings->op[stage].cop
8454 && settings->op[stage].carg0 == settings->op[stage].aarg0
8455 && settings->op[stage].carg1 == settings->op[stage].aarg1
8456 && settings->op[stage].carg2 == settings->op[stage].aarg2;
8458 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8460 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8461 settings->op[stage].cop, settings->op[stage].carg0,
8462 settings->op[stage].carg1, settings->op[stage].carg2);
8464 else if (op_equal)
8466 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
8467 settings->op[stage].cop, settings->op[stage].carg0,
8468 settings->op[stage].carg1, settings->op[stage].carg2);
8470 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
8471 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8473 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8474 settings->op[stage].cop, settings->op[stage].carg0,
8475 settings->op[stage].carg1, settings->op[stage].carg2);
8476 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
8477 settings->op[stage].aop, settings->op[stage].aarg0,
8478 settings->op[stage].aarg1, settings->op[stage].aarg2);
8482 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
8483 get_fragment_output(gl_info));
8485 if (settings->sRGB_write)
8486 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
8488 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
8490 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
8492 shader_addline(buffer, "}\n");
8494 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8495 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8497 string_buffer_release(&priv->string_buffers, tex_reg_name);
8498 return shader_id;
8501 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
8502 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
8504 struct glsl_ffp_vertex_shader *shader;
8505 const struct wine_rb_entry *entry;
8507 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
8508 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
8510 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
8511 return NULL;
8513 shader->desc.settings = *settings;
8514 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
8515 list_init(&shader->linked_programs);
8516 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
8517 ERR("Failed to insert ffp vertex shader.\n");
8519 return shader;
8522 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
8523 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
8525 struct glsl_ffp_fragment_shader *glsl_desc;
8526 const struct ffp_frag_desc *desc;
8528 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
8529 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
8531 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
8532 return NULL;
8534 glsl_desc->entry.settings = *args;
8535 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
8536 list_init(&glsl_desc->linked_programs);
8537 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
8539 return glsl_desc;
8543 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
8544 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
8546 unsigned int i;
8547 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8549 for (i = 0; i < vs_c_count; ++i)
8551 string_buffer_sprintf(name, "vs_c[%u]", i);
8552 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8554 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
8556 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8558 string_buffer_sprintf(name, "vs_i[%u]", i);
8559 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8562 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8564 string_buffer_sprintf(name, "vs_b[%u]", i);
8565 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8568 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8570 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
8572 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
8573 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8575 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
8576 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
8577 for (i = 0; i < MAX_TEXTURES; ++i)
8579 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
8580 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8582 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
8583 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
8584 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
8585 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
8586 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
8587 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
8588 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
8590 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
8591 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8592 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
8593 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8594 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
8595 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8596 string_buffer_sprintf(name, "ffp_light[%u].position", i);
8597 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8598 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
8599 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8600 string_buffer_sprintf(name, "ffp_light[%u].range", i);
8601 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8602 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
8603 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8604 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
8605 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8606 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
8607 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8608 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
8609 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8610 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
8611 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8612 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
8613 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8615 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
8616 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
8617 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
8618 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
8619 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
8620 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
8621 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
8623 string_buffer_release(&priv->string_buffers, name);
8626 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
8627 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
8629 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8632 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
8633 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
8635 unsigned int i;
8636 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8638 for (i = 0; i < ps_c_count; ++i)
8640 string_buffer_sprintf(name, "ps_c[%u]", i);
8641 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8643 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
8645 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8647 string_buffer_sprintf(name, "ps_i[%u]", i);
8648 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8651 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8653 string_buffer_sprintf(name, "ps_b[%u]", i);
8654 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8657 for (i = 0; i < MAX_TEXTURES; ++i)
8659 string_buffer_sprintf(name, "bumpenv_mat%u", i);
8660 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8661 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
8662 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8663 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
8664 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8665 string_buffer_sprintf(name, "tss_const%u", i);
8666 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8669 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
8670 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
8672 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
8673 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
8674 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
8675 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
8677 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
8679 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
8680 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
8681 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
8683 string_buffer_release(&priv->string_buffers, name);
8686 /* Context activation is done by the caller. */
8687 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
8688 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8690 const struct wined3d_gl_info *gl_info = context->gl_info;
8691 struct glsl_shader_prog_link *entry = NULL;
8692 struct wined3d_shader *shader;
8693 struct glsl_program_key key;
8694 GLuint program_id, cs_id;
8696 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
8697 return;
8699 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
8701 WARN("Compute shader is NULL.\n");
8702 ctx_data->glsl_program = NULL;
8703 return;
8706 cs_id = find_glsl_compute_shader(context, &priv->shader_buffer, &priv->string_buffers, shader);
8707 memset(&key, 0, sizeof(key));
8708 key.cs_id = cs_id;
8709 if ((entry = get_glsl_program_entry(priv, &key)))
8711 ctx_data->glsl_program = entry;
8712 return;
8715 program_id = GL_EXTCALL(glCreateProgram());
8716 TRACE("Created new GLSL shader program %u.\n", program_id);
8718 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
8720 ERR("Out of memory.\n");
8721 return;
8723 entry->id = program_id;
8724 entry->vs.id = 0;
8725 entry->gs.id = 0;
8726 entry->ps.id = 0;
8727 entry->cs.id = cs_id;
8728 entry->constant_version = 0;
8729 entry->ps.np2_fixup_info = NULL;
8730 add_glsl_program_entry(priv, entry);
8732 ctx_data->glsl_program = entry;
8734 TRACE("Attaching GLSL shader object %u to program %u.\n", cs_id, program_id);
8735 GL_EXTCALL(glAttachShader(program_id, cs_id));
8736 checkGLcall("glAttachShader");
8738 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
8740 TRACE("Linking GLSL shader program %u.\n", program_id);
8741 GL_EXTCALL(glLinkProgram(program_id));
8742 shader_glsl_validate_link(gl_info, program_id);
8744 GL_EXTCALL(glUseProgram(program_id));
8745 checkGLcall("glUseProgram");
8746 shader_glsl_load_program_resources(context, priv, program_id, shader);
8747 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
8749 entry->constant_update_mask = 0;
8752 /* Context activation is done by the caller. */
8753 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
8754 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8756 const struct wined3d_gl_info *gl_info = context->gl_info;
8757 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
8758 const struct ps_np2fixup_info *np2fixup_info = NULL;
8759 struct glsl_shader_prog_link *entry = NULL;
8760 struct wined3d_shader *vshader = NULL;
8761 struct wined3d_shader *gshader = NULL;
8762 struct wined3d_shader *pshader = NULL;
8763 GLuint reorder_shader_id = 0;
8764 struct glsl_program_key key;
8765 GLuint program_id;
8766 unsigned int i;
8767 GLuint vs_id = 0;
8768 GLuint gs_id = 0;
8769 GLuint ps_id = 0;
8770 struct list *ps_list, *vs_list;
8771 WORD attribs_map;
8772 struct wined3d_string_buffer *tmp_name;
8774 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
8776 vs_id = ctx_data->glsl_program->vs.id;
8777 vs_list = &ctx_data->glsl_program->vs.shader_entry;
8779 if (use_vs(state))
8781 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8782 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8784 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8785 && ctx_data->glsl_program->gs.id)
8787 gs_id = ctx_data->glsl_program->gs.id;
8789 else if (gshader)
8791 struct gs_compile_args args;
8793 find_gs_compile_args(state, gshader, &args);
8794 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
8798 else if (use_vs(state))
8800 struct vs_compile_args vs_compile_args;
8802 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8803 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8805 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
8806 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
8807 vs_list = &vshader->linked_programs;
8809 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8810 && ctx_data->glsl_program->gs.id)
8812 gs_id = ctx_data->glsl_program->gs.id;
8814 else if (gshader)
8816 struct gs_compile_args gs_compile_args;
8818 find_gs_compile_args(state, gshader, &gs_compile_args);
8819 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
8822 else if (priv->vertex_pipe == &glsl_vertex_pipe)
8824 struct glsl_ffp_vertex_shader *ffp_shader;
8825 struct wined3d_ffp_vs_settings settings;
8827 wined3d_ffp_get_vs_settings(context, state, &settings);
8828 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
8829 vs_id = ffp_shader->id;
8830 vs_list = &ffp_shader->linked_programs;
8833 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
8835 ps_id = ctx_data->glsl_program->ps.id;
8836 ps_list = &ctx_data->glsl_program->ps.shader_entry;
8838 if (use_ps(state))
8839 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8841 else if (use_ps(state))
8843 struct ps_compile_args ps_compile_args;
8844 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8845 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
8846 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
8847 pshader, &ps_compile_args, &np2fixup_info);
8848 ps_list = &pshader->linked_programs;
8850 else if (priv->fragment_pipe == &glsl_fragment_pipe)
8852 struct glsl_ffp_fragment_shader *ffp_shader;
8853 struct ffp_frag_settings settings;
8855 gen_ffp_frag_op(context, state, &settings, FALSE);
8856 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
8857 ps_id = ffp_shader->id;
8858 ps_list = &ffp_shader->linked_programs;
8861 key.vs_id = vs_id;
8862 key.gs_id = gs_id;
8863 key.ps_id = ps_id;
8864 key.cs_id = 0;
8865 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
8867 ctx_data->glsl_program = entry;
8868 return;
8871 /* If we get to this point, then no matching program exists, so we create one */
8872 program_id = GL_EXTCALL(glCreateProgram());
8873 TRACE("Created new GLSL shader program %u.\n", program_id);
8875 /* Create the entry */
8876 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
8877 entry->id = program_id;
8878 entry->vs.id = vs_id;
8879 entry->gs.id = gs_id;
8880 entry->ps.id = ps_id;
8881 entry->cs.id = 0;
8882 entry->constant_version = 0;
8883 entry->ps.np2_fixup_info = np2fixup_info;
8884 /* Add the hash table entry */
8885 add_glsl_program_entry(priv, entry);
8887 /* Set the current program */
8888 ctx_data->glsl_program = entry;
8890 /* Attach GLSL vshader */
8891 if (vs_id)
8893 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
8894 GL_EXTCALL(glAttachShader(program_id, vs_id));
8895 checkGLcall("glAttachShader");
8897 list_add_head(vs_list, &entry->vs.shader_entry);
8900 if (vshader)
8902 attribs_map = vshader->reg_maps.input_registers;
8903 if (vshader->reg_maps.shader_version.major < 4)
8905 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
8906 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
8907 d3d_info->emulated_flatshading
8908 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
8909 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
8910 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
8911 checkGLcall("glAttachShader");
8912 /* Flag the reorder function for deletion, it will be freed
8913 * automatically when the program is destroyed. */
8914 GL_EXTCALL(glDeleteShader(reorder_shader_id));
8917 else
8919 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
8922 if (!shader_glsl_use_explicit_attrib_location(gl_info))
8924 /* Bind vertex attributes to a corresponding index number to match
8925 * the same index numbers as ARB_vertex_programs (makes loading
8926 * vertex attributes simpler). With this method, we can use the
8927 * exact same code to load the attributes later for both ARB and
8928 * GLSL shaders.
8930 * We have to do this here because we need to know the Program ID
8931 * in order to make the bindings work, and it has to be done prior
8932 * to linking the GLSL program. */
8933 tmp_name = string_buffer_get(&priv->string_buffers);
8934 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
8936 if (!(attribs_map & 1))
8937 continue;
8939 string_buffer_sprintf(tmp_name, "vs_in%u", i);
8940 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8941 if (vshader && vshader->reg_maps.shader_version.major >= 4)
8943 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
8944 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8945 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
8946 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8949 checkGLcall("glBindAttribLocation");
8950 string_buffer_release(&priv->string_buffers, tmp_name);
8953 if (gshader)
8955 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
8956 GL_EXTCALL(glAttachShader(program_id, gs_id));
8957 checkGLcall("glAttachShader");
8959 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8961 TRACE("input type %s, output type %s, vertices out %u.\n",
8962 debug_d3dprimitivetype(gshader->u.gs.input_type),
8963 debug_d3dprimitivetype(gshader->u.gs.output_type),
8964 gshader->u.gs.vertices_out);
8965 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
8966 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
8967 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
8968 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
8969 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
8970 gshader->u.gs.vertices_out));
8971 checkGLcall("glProgramParameteriARB");
8974 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
8977 /* Attach GLSL pshader */
8978 if (ps_id)
8980 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
8981 GL_EXTCALL(glAttachShader(program_id, ps_id));
8982 checkGLcall("glAttachShader");
8984 list_add_head(ps_list, &entry->ps.shader_entry);
8987 /* Link the program */
8988 TRACE("Linking GLSL shader program %u.\n", program_id);
8989 GL_EXTCALL(glLinkProgram(program_id));
8990 shader_glsl_validate_link(gl_info, program_id);
8992 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
8993 vshader ? vshader->limits->constant_float : 0);
8994 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
8995 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
8996 pshader ? pshader->limits->constant_float : 0);
8997 checkGLcall("Find glsl program uniform locations");
8999 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9001 if (pshader && pshader->reg_maps.shader_version.major >= 3
9002 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
9004 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
9005 entry->vs.vertex_color_clamp = GL_FALSE;
9007 else
9009 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
9012 else
9014 /* With core profile we never change vertex_color_clamp from
9015 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
9016 * glClampColorARB(). */
9017 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
9020 /* Set the shader to allow uniform loading on it */
9021 GL_EXTCALL(glUseProgram(program_id));
9022 checkGLcall("glUseProgram");
9024 entry->constant_update_mask = 0;
9025 if (vshader)
9027 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
9028 if (vshader->reg_maps.integer_constants)
9029 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
9030 if (vshader->reg_maps.boolean_constants)
9031 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
9032 if (entry->vs.pos_fixup_location != -1)
9033 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9035 shader_glsl_load_program_resources(context, priv, program_id, vshader);
9037 else
9039 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9040 | WINED3D_SHADER_CONST_FFP_PROJ;
9042 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
9044 if (entry->vs.modelview_matrix_location[i] != -1)
9046 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9047 break;
9051 for (i = 0; i < MAX_TEXTURES; ++i)
9053 if (entry->vs.texture_matrix_location[i] != -1)
9055 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9056 break;
9059 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
9060 || entry->vs.material_specular_location != -1
9061 || entry->vs.material_emissive_location != -1
9062 || entry->vs.material_shininess_location != -1)
9063 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9064 if (entry->vs.light_ambient_location != -1)
9065 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9067 if (entry->vs.clip_planes_location != -1)
9068 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9069 if (entry->vs.pointsize_min_location != -1)
9070 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9072 if (gshader)
9074 if (entry->gs.pos_fixup_location != -1)
9075 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9077 shader_glsl_load_program_resources(context, priv, program_id, gshader);
9080 if (ps_id)
9082 if (pshader)
9084 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
9085 if (pshader->reg_maps.integer_constants)
9086 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
9087 if (pshader->reg_maps.boolean_constants)
9088 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
9089 if (entry->ps.ycorrection_location != -1)
9090 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
9092 shader_glsl_load_program_resources(context, priv, program_id, pshader);
9093 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
9095 else
9097 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9099 shader_glsl_load_samplers_range(gl_info, priv, program_id,
9100 shader_glsl_get_prefix(WINED3D_SHADER_TYPE_PIXEL),
9101 0, MAX_TEXTURES, context->tex_unit_map);
9104 for (i = 0; i < MAX_TEXTURES; ++i)
9106 if (entry->ps.bumpenv_mat_location[i] != -1)
9108 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
9109 break;
9113 if (entry->ps.fog_color_location != -1)
9114 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9115 if (entry->ps.alpha_test_ref_location != -1)
9116 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9117 if (entry->ps.np2_fixup_location != -1)
9118 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
9119 if (entry->ps.color_key_location != -1)
9120 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9124 /* Context activation is done by the caller. */
9125 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
9126 const struct wined3d_state *state)
9128 struct glsl_context_data *ctx_data = context->shader_backend_data;
9129 const struct wined3d_gl_info *gl_info = context->gl_info;
9130 struct shader_glsl_priv *priv = shader_priv;
9131 GLenum current_vertex_color_clamp;
9132 GLuint program_id, prev_id;
9134 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
9135 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
9137 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9139 set_glsl_shader_program(context, state, priv, ctx_data);
9141 if (ctx_data->glsl_program)
9143 program_id = ctx_data->glsl_program->id;
9144 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
9146 else
9148 program_id = 0;
9149 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
9152 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
9154 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
9155 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
9157 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
9158 checkGLcall("glClampColorARB");
9160 else
9162 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
9166 TRACE("Using GLSL program %u.\n", program_id);
9168 if (prev_id != program_id)
9170 GL_EXTCALL(glUseProgram(program_id));
9171 checkGLcall("glUseProgram");
9173 if (program_id)
9174 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
9177 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
9180 /* Context activation is done by the caller. */
9181 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
9182 const struct wined3d_state *state)
9184 struct glsl_context_data *ctx_data = context->shader_backend_data;
9185 const struct wined3d_gl_info *gl_info = context->gl_info;
9186 struct shader_glsl_priv *priv = shader_priv;
9187 GLuint program_id, prev_id;
9189 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9190 set_glsl_compute_shader_program(context, state, priv, ctx_data);
9191 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9193 TRACE("Using GLSL program %u.\n", program_id);
9195 if (prev_id != program_id)
9197 GL_EXTCALL(glUseProgram(program_id));
9198 checkGLcall("glUseProgram");
9201 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
9202 | (1u << WINED3D_SHADER_TYPE_VERTEX)
9203 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
9204 | (1u << WINED3D_SHADER_TYPE_HULL)
9205 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
9208 /* "context" is not necessarily the currently active context. */
9209 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
9211 struct glsl_context_data *ctx_data = context->shader_backend_data;
9213 ctx_data->glsl_program = NULL;
9214 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
9215 | (1u << WINED3D_SHADER_TYPE_VERTEX)
9216 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
9217 | (1u << WINED3D_SHADER_TYPE_HULL)
9218 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
9219 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
9222 /* Context activation is done by the caller. */
9223 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
9225 struct glsl_context_data *ctx_data = context->shader_backend_data;
9226 const struct wined3d_gl_info *gl_info = context->gl_info;
9227 struct shader_glsl_priv *priv = shader_priv;
9229 shader_glsl_invalidate_current_program(context);
9230 GL_EXTCALL(glUseProgram(0));
9231 checkGLcall("glUseProgram");
9233 priv->vertex_pipe->vp_enable(gl_info, FALSE);
9234 priv->fragment_pipe->enable_extension(gl_info, FALSE);
9236 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
9238 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9239 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
9240 checkGLcall("glClampColorARB");
9244 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
9245 const struct glsl_shader_prog_link *program)
9247 const struct glsl_context_data *ctx_data;
9248 struct wined3d_context *context;
9249 unsigned int i;
9251 for (i = 0; i < device->context_count; ++i)
9253 context = device->contexts[i];
9254 ctx_data = context->shader_backend_data;
9256 if (ctx_data->glsl_program == program)
9257 shader_glsl_invalidate_current_program(context);
9261 static void shader_glsl_destroy(struct wined3d_shader *shader)
9263 struct glsl_shader_private *shader_data = shader->backend_data;
9264 struct wined3d_device *device = shader->device;
9265 struct shader_glsl_priv *priv = device->shader_priv;
9266 const struct wined3d_gl_info *gl_info;
9267 const struct list *linked_programs;
9268 struct wined3d_context *context;
9270 if (!shader_data || !shader_data->num_gl_shaders)
9272 HeapFree(GetProcessHeap(), 0, shader_data);
9273 shader->backend_data = NULL;
9274 return;
9277 context = context_acquire(device, NULL, 0);
9278 gl_info = context->gl_info;
9280 TRACE("Deleting linked programs.\n");
9281 linked_programs = &shader->linked_programs;
9282 if (linked_programs->next)
9284 struct glsl_shader_prog_link *entry, *entry2;
9285 UINT i;
9287 switch (shader->reg_maps.shader_version.type)
9289 case WINED3D_SHADER_TYPE_PIXEL:
9291 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
9293 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9295 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
9296 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9297 checkGLcall("glDeleteShader");
9299 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
9301 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9302 struct glsl_shader_prog_link, ps.shader_entry)
9304 shader_glsl_invalidate_contexts_program(device, entry);
9305 delete_glsl_program_entry(priv, gl_info, entry);
9308 break;
9311 case WINED3D_SHADER_TYPE_VERTEX:
9313 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
9315 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9317 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
9318 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9319 checkGLcall("glDeleteShader");
9321 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
9323 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9324 struct glsl_shader_prog_link, vs.shader_entry)
9326 shader_glsl_invalidate_contexts_program(device, entry);
9327 delete_glsl_program_entry(priv, gl_info, entry);
9330 break;
9333 case WINED3D_SHADER_TYPE_GEOMETRY:
9335 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
9337 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9339 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
9340 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9341 checkGLcall("glDeleteShader");
9343 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
9345 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9346 struct glsl_shader_prog_link, gs.shader_entry)
9348 shader_glsl_invalidate_contexts_program(device, entry);
9349 delete_glsl_program_entry(priv, gl_info, entry);
9352 break;
9355 case WINED3D_SHADER_TYPE_COMPUTE:
9357 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
9359 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9361 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
9362 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9363 checkGLcall("glDeleteShader");
9365 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
9367 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9368 struct glsl_shader_prog_link, cs.shader_entry)
9370 shader_glsl_invalidate_contexts_program(device, entry);
9371 delete_glsl_program_entry(priv, gl_info, entry);
9374 break;
9377 default:
9378 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
9379 break;
9383 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9384 shader->backend_data = NULL;
9386 context_release(context);
9389 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
9391 const struct glsl_program_key *k = key;
9392 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
9393 const struct glsl_shader_prog_link, program_lookup_entry);
9395 if (k->vs_id > prog->vs.id) return 1;
9396 else if (k->vs_id < prog->vs.id) return -1;
9398 if (k->gs_id > prog->gs.id) return 1;
9399 else if (k->gs_id < prog->gs.id) return -1;
9401 if (k->ps_id > prog->ps.id) return 1;
9402 else if (k->ps_id < prog->ps.id) return -1;
9404 if (k->cs_id > prog->cs.id) return 1;
9405 else if (k->cs_id < prog->cs.id) return -1;
9407 return 0;
9410 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
9412 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
9413 + constant_count * sizeof(*heap->contained)
9414 + constant_count * sizeof(*heap->positions);
9415 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
9417 if (!mem)
9419 ERR("Failed to allocate memory\n");
9420 return FALSE;
9423 heap->entries = mem;
9424 heap->entries[1].version = 0;
9425 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
9426 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
9427 heap->positions = (unsigned int *)(heap->contained + constant_count);
9428 heap->size = 1;
9430 return TRUE;
9433 static void constant_heap_free(struct constant_heap *heap)
9435 HeapFree(GetProcessHeap(), 0, heap->entries);
9438 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
9439 const struct fragment_pipeline *fragment_pipe)
9441 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
9442 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
9443 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
9444 struct fragment_caps fragment_caps;
9445 void *vertex_priv, *fragment_priv;
9447 string_buffer_list_init(&priv->string_buffers);
9449 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
9451 ERR("Failed to initialize vertex pipe.\n");
9452 HeapFree(GetProcessHeap(), 0, priv);
9453 return E_FAIL;
9456 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
9458 ERR("Failed to initialize fragment pipe.\n");
9459 vertex_pipe->vp_free(device);
9460 HeapFree(GetProcessHeap(), 0, priv);
9461 return E_FAIL;
9464 if (!string_buffer_init(&priv->shader_buffer))
9466 ERR("Failed to initialize shader buffer.\n");
9467 goto fail;
9470 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
9472 ERR("Failed to allocate memory.\n");
9473 goto fail;
9476 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
9478 ERR("Failed to initialize vertex shader constant heap\n");
9479 goto fail;
9482 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
9484 ERR("Failed to initialize pixel shader constant heap\n");
9485 goto fail;
9488 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
9490 priv->next_constant_version = 1;
9491 priv->vertex_pipe = vertex_pipe;
9492 priv->fragment_pipe = fragment_pipe;
9493 fragment_pipe->get_caps(gl_info, &fragment_caps);
9494 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
9495 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
9497 device->vertex_priv = vertex_priv;
9498 device->fragment_priv = fragment_priv;
9499 device->shader_priv = priv;
9501 return WINED3D_OK;
9503 fail:
9504 constant_heap_free(&priv->pconst_heap);
9505 constant_heap_free(&priv->vconst_heap);
9506 HeapFree(GetProcessHeap(), 0, priv->stack);
9507 string_buffer_free(&priv->shader_buffer);
9508 fragment_pipe->free_private(device);
9509 vertex_pipe->vp_free(device);
9510 HeapFree(GetProcessHeap(), 0, priv);
9511 return E_OUTOFMEMORY;
9514 /* Context activation is done by the caller. */
9515 static void shader_glsl_free(struct wined3d_device *device)
9517 struct shader_glsl_priv *priv = device->shader_priv;
9519 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
9520 constant_heap_free(&priv->pconst_heap);
9521 constant_heap_free(&priv->vconst_heap);
9522 HeapFree(GetProcessHeap(), 0, priv->stack);
9523 string_buffer_list_cleanup(&priv->string_buffers);
9524 string_buffer_free(&priv->shader_buffer);
9525 priv->fragment_pipe->free_private(device);
9526 priv->vertex_pipe->vp_free(device);
9528 HeapFree(GetProcessHeap(), 0, device->shader_priv);
9529 device->shader_priv = NULL;
9532 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
9534 struct glsl_context_data *ctx_data;
9535 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
9536 return FALSE;
9537 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9538 context->shader_backend_data = ctx_data;
9539 return TRUE;
9542 static void shader_glsl_free_context_data(struct wined3d_context *context)
9544 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
9547 static void shader_glsl_init_context_state(struct wined3d_context *context)
9549 const struct wined3d_gl_info *gl_info = context->gl_info;
9551 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
9552 checkGLcall("GL_PROGRAM_POINT_SIZE");
9555 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
9557 UINT shader_model;
9559 /* FIXME: Check for the specific extensions required for SM5 support
9560 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
9561 * soon as we introduce them, adjusting the GL / GLSL version checks
9562 * accordingly. */
9563 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
9564 && gl_info->supported[ARB_COMPUTE_SHADER]
9565 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
9566 && gl_info->supported[ARB_GPU_SHADER5]
9567 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
9568 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
9569 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
9570 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
9571 shader_model = 5;
9572 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
9573 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
9574 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
9575 shader_model = 4;
9576 /* Support for texldd and texldl instructions in pixel shaders is required
9577 * for SM3. */
9578 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
9579 shader_model = 3;
9580 else
9581 shader_model = 2;
9582 TRACE("Shader model %u.\n", shader_model);
9584 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
9585 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
9586 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
9587 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
9588 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
9589 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
9591 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
9592 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
9594 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
9595 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
9596 caps->varying_count = gl_info->limits.glsl_varyings;
9598 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
9599 * Direct3D minimum requirement.
9601 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
9602 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
9604 * The problem is that the refrast clamps temporary results in the shader to
9605 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
9606 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
9607 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
9608 * offer a way to query this.
9610 if (shader_model >= 4)
9611 caps->ps_1x_max_value = FLT_MAX;
9612 else
9613 caps->ps_1x_max_value = 1024.0f;
9615 /* Ideally we'd only set caps like sRGB writes here if supported by both
9616 * the shader backend and the fragment pipe, but we can get called before
9617 * shader_glsl_alloc(). */
9618 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
9619 | WINED3D_SHADER_CAP_SRGB_WRITE;
9622 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
9624 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
9626 TRACE("Checking support for fixup:\n");
9627 dump_color_fixup_desc(fixup);
9630 /* We support everything except YUV conversions. */
9631 if (!is_complex_fixup(fixup))
9633 TRACE("[OK]\n");
9634 return TRUE;
9637 TRACE("[FAILED]\n");
9638 return FALSE;
9641 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
9643 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
9644 /* WINED3DSIH_ADD */ shader_glsl_binop,
9645 /* WINED3DSIH_AND */ shader_glsl_binop,
9646 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
9647 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
9648 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
9649 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
9650 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
9651 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
9652 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
9653 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
9654 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
9655 /* WINED3DSIH_BEM */ shader_glsl_bem,
9656 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
9657 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
9658 /* WINED3DSIH_BREAK */ shader_glsl_break,
9659 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
9660 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
9661 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
9662 /* WINED3DSIH_CALL */ shader_glsl_call,
9663 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
9664 /* WINED3DSIH_CASE */ shader_glsl_case,
9665 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
9666 /* WINED3DSIH_CND */ shader_glsl_cnd,
9667 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
9668 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
9669 /* WINED3DSIH_CRS */ shader_glsl_cross,
9670 /* WINED3DSIH_CUT */ shader_glsl_cut,
9671 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
9672 /* WINED3DSIH_DCL */ shader_glsl_nop,
9673 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
9674 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
9675 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
9676 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
9677 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
9678 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ NULL,
9679 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
9680 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
9681 /* WINED3DSIH_DCL_INDEX_RANGE */ NULL,
9682 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
9683 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
9684 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
9685 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
9686 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
9687 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
9688 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
9689 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
9690 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
9691 /* WINED3DSIH_DCL_INTERFACE */ NULL,
9692 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
9693 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
9694 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
9695 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
9696 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
9697 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
9698 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
9699 /* WINED3DSIH_DCL_STREAM */ NULL,
9700 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
9701 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
9702 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
9703 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
9704 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
9705 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
9706 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
9707 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
9708 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
9709 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
9710 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
9711 /* WINED3DSIH_DEF */ shader_glsl_nop,
9712 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
9713 /* WINED3DSIH_DEFB */ shader_glsl_nop,
9714 /* WINED3DSIH_DEFI */ shader_glsl_nop,
9715 /* WINED3DSIH_DIV */ shader_glsl_binop,
9716 /* WINED3DSIH_DP2 */ shader_glsl_dot,
9717 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
9718 /* WINED3DSIH_DP3 */ shader_glsl_dot,
9719 /* WINED3DSIH_DP4 */ shader_glsl_dot,
9720 /* WINED3DSIH_DST */ shader_glsl_dst,
9721 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
9722 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
9723 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
9724 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
9725 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
9726 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
9727 /* WINED3DSIH_ELSE */ shader_glsl_else,
9728 /* WINED3DSIH_EMIT */ shader_glsl_emit,
9729 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
9730 /* WINED3DSIH_ENDIF */ shader_glsl_end,
9731 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
9732 /* WINED3DSIH_ENDREP */ shader_glsl_end,
9733 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
9734 /* WINED3DSIH_EQ */ shader_glsl_relop,
9735 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
9736 /* WINED3DSIH_EXPP */ shader_glsl_expp,
9737 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
9738 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
9739 /* WINED3DSIH_FCALL */ NULL,
9740 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
9741 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
9742 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
9743 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
9744 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
9745 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
9746 /* WINED3DSIH_GATHER4 */ NULL,
9747 /* WINED3DSIH_GATHER4_C */ NULL,
9748 /* WINED3DSIH_GE */ shader_glsl_relop,
9749 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
9750 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
9751 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
9752 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
9753 /* WINED3DSIH_IADD */ shader_glsl_binop,
9754 /* WINED3DSIH_IEQ */ shader_glsl_relop,
9755 /* WINED3DSIH_IF */ shader_glsl_if,
9756 /* WINED3DSIH_IFC */ shader_glsl_ifc,
9757 /* WINED3DSIH_IGE */ shader_glsl_relop,
9758 /* WINED3DSIH_ILT */ shader_glsl_relop,
9759 /* WINED3DSIH_IMAD */ shader_glsl_mad,
9760 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
9761 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
9762 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
9763 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
9764 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
9765 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
9766 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
9767 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
9768 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
9769 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
9770 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
9771 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
9772 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
9773 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
9774 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
9775 /* WINED3DSIH_INE */ shader_glsl_relop,
9776 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
9777 /* WINED3DSIH_ISHL */ shader_glsl_binop,
9778 /* WINED3DSIH_ISHR */ shader_glsl_binop,
9779 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
9780 /* WINED3DSIH_LABEL */ shader_glsl_label,
9781 /* WINED3DSIH_LD */ shader_glsl_ld,
9782 /* WINED3DSIH_LD2DMS */ NULL,
9783 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
9784 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
9785 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
9786 /* WINED3DSIH_LIT */ shader_glsl_lit,
9787 /* WINED3DSIH_LOD */ NULL,
9788 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
9789 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
9790 /* WINED3DSIH_LOOP */ shader_glsl_loop,
9791 /* WINED3DSIH_LRP */ shader_glsl_lrp,
9792 /* WINED3DSIH_LT */ shader_glsl_relop,
9793 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
9794 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
9795 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
9796 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
9797 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
9798 /* WINED3DSIH_MAD */ shader_glsl_mad,
9799 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
9800 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
9801 /* WINED3DSIH_MOV */ shader_glsl_mov,
9802 /* WINED3DSIH_MOVA */ shader_glsl_mov,
9803 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
9804 /* WINED3DSIH_MUL */ shader_glsl_binop,
9805 /* WINED3DSIH_NE */ shader_glsl_relop,
9806 /* WINED3DSIH_NOP */ shader_glsl_nop,
9807 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
9808 /* WINED3DSIH_NRM */ shader_glsl_nrm,
9809 /* WINED3DSIH_OR */ shader_glsl_binop,
9810 /* WINED3DSIH_PHASE */ shader_glsl_nop,
9811 /* WINED3DSIH_POW */ shader_glsl_pow,
9812 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
9813 /* WINED3DSIH_REP */ shader_glsl_rep,
9814 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
9815 /* WINED3DSIH_RET */ shader_glsl_ret,
9816 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
9817 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
9818 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
9819 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
9820 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
9821 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
9822 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
9823 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
9824 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
9825 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
9826 /* WINED3DSIH_SAMPLE_INFO */ NULL,
9827 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
9828 /* WINED3DSIH_SAMPLE_POS */ NULL,
9829 /* WINED3DSIH_SETP */ NULL,
9830 /* WINED3DSIH_SGE */ shader_glsl_compare,
9831 /* WINED3DSIH_SGN */ shader_glsl_sgn,
9832 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
9833 /* WINED3DSIH_SLT */ shader_glsl_compare,
9834 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
9835 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
9836 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
9837 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
9838 /* WINED3DSIH_SUB */ shader_glsl_binop,
9839 /* WINED3DSIH_SWAPC */ NULL,
9840 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
9841 /* WINED3DSIH_SYNC */ shader_glsl_sync,
9842 /* WINED3DSIH_TEX */ shader_glsl_tex,
9843 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
9844 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
9845 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
9846 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
9847 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
9848 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
9849 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
9850 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
9851 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
9852 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
9853 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
9854 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
9855 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
9856 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
9857 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
9858 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
9859 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
9860 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
9861 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
9862 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
9863 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
9864 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
9865 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
9866 /* WINED3DSIH_UGE */ shader_glsl_relop,
9867 /* WINED3DSIH_ULT */ shader_glsl_relop,
9868 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
9869 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
9870 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
9871 /* WINED3DSIH_USHR */ shader_glsl_binop,
9872 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
9873 /* WINED3DSIH_XOR */ shader_glsl_binop,
9876 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
9877 SHADER_HANDLER hw_fct;
9879 /* Select handler */
9880 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
9882 /* Unhandled opcode */
9883 if (!hw_fct)
9885 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
9886 return;
9888 hw_fct(ins);
9890 shader_glsl_add_instruction_modifiers(ins);
9893 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
9895 struct shader_glsl_priv *priv = shader_priv;
9897 return priv->ffp_proj_control;
9900 const struct wined3d_shader_backend_ops glsl_shader_backend =
9902 shader_glsl_handle_instruction,
9903 shader_glsl_select,
9904 shader_glsl_select_compute,
9905 shader_glsl_disable,
9906 shader_glsl_update_float_vertex_constants,
9907 shader_glsl_update_float_pixel_constants,
9908 shader_glsl_load_constants,
9909 shader_glsl_destroy,
9910 shader_glsl_alloc,
9911 shader_glsl_free,
9912 shader_glsl_allocate_context_data,
9913 shader_glsl_free_context_data,
9914 shader_glsl_init_context_state,
9915 shader_glsl_get_caps,
9916 shader_glsl_color_fixup_supported,
9917 shader_glsl_has_ffp_proj_control,
9920 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
9922 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
9924 caps->xyzrhw = TRUE;
9925 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9926 caps->ffp_generic_attributes = TRUE;
9927 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
9928 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
9929 caps->max_vertex_blend_matrix_index = 0;
9930 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
9931 | WINED3DVTXPCAPS_MATERIALSOURCE7
9932 | WINED3DVTXPCAPS_VERTEXFOG
9933 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9934 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9935 | WINED3DVTXPCAPS_LOCALVIEWER
9936 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
9937 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
9938 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
9939 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
9942 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
9944 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9945 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9946 return 0;
9949 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9951 struct shader_glsl_priv *priv;
9953 if (shader_backend == &glsl_shader_backend)
9955 priv = shader_priv;
9956 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
9957 return priv;
9960 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9962 return NULL;
9965 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
9967 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9968 struct glsl_ffp_vertex_shader, desc.entry);
9969 struct glsl_shader_prog_link *program, *program2;
9970 struct glsl_ffp_destroy_ctx *ctx = context;
9972 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9973 struct glsl_shader_prog_link, vs.shader_entry)
9975 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9977 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9978 HeapFree(GetProcessHeap(), 0, shader);
9981 /* Context activation is done by the caller. */
9982 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
9984 struct shader_glsl_priv *priv = device->vertex_priv;
9985 struct glsl_ffp_destroy_ctx ctx;
9987 ctx.priv = priv;
9988 ctx.gl_info = &device->adapter->gl_info;
9989 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9992 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9993 const struct wined3d_state *state, DWORD state_id) {}
9995 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9996 const struct wined3d_state *state, DWORD state_id)
9998 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10001 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
10002 const struct wined3d_state *state, DWORD state_id)
10004 const struct wined3d_gl_info *gl_info = context->gl_info;
10005 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
10006 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
10007 BOOL transformed = context->stream_info.position_transformed;
10008 BOOL wasrhw = context->last_was_rhw;
10009 unsigned int i;
10011 context->last_was_rhw = transformed;
10013 /* If the vertex declaration contains a transformed position attribute,
10014 * the draw uses the fixed function vertex pipeline regardless of any
10015 * vertex shader set by the application. */
10016 if (transformed != wasrhw
10017 || context->stream_info.swizzle_map != context->last_swizzle_map)
10018 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10020 context->last_swizzle_map = context->stream_info.swizzle_map;
10022 if (!use_vs(state))
10024 if (context->last_was_vshader)
10026 if (legacy_context)
10027 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
10028 clipplane(context, state, STATE_CLIPPLANE(i));
10029 else
10030 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10033 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10035 /* Because of settings->texcoords, we have to regenerate the vertex
10036 * shader on a vdecl change if there aren't enough varyings to just
10037 * always output all the texture coordinates. */
10038 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
10039 || normal != context->last_was_normal)
10040 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10042 if (use_ps(state)
10043 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
10044 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
10045 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10047 else
10049 if (!context->last_was_vshader)
10051 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
10052 if (legacy_context)
10053 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
10054 clipplane(context, state, STATE_CLIPPLANE(i));
10055 else
10056 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10060 context->last_was_vshader = use_vs(state);
10061 context->last_was_normal = normal;
10064 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
10065 const struct wined3d_state *state, DWORD state_id)
10067 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10068 /* Different vertex shaders potentially require a different vertex attributes setup. */
10069 if (!isStateDirty(context, STATE_VDECL))
10070 context_apply_state(context, state, STATE_VDECL);
10073 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
10074 const struct wined3d_state *state, DWORD state_id)
10076 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
10077 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
10078 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10081 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
10082 const struct wined3d_state *state, DWORD state_id)
10084 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
10085 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
10086 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
10087 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
10088 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10091 static void glsl_vertex_pipe_world(struct wined3d_context *context,
10092 const struct wined3d_state *state, DWORD state_id)
10094 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
10097 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
10098 const struct wined3d_state *state, DWORD state_id)
10100 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10103 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
10105 const struct wined3d_gl_info *gl_info = context->gl_info;
10106 unsigned int k;
10108 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10109 | WINED3D_SHADER_CONST_FFP_LIGHTS
10110 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10112 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10114 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
10116 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
10117 clipplane(context, state, STATE_CLIPPLANE(k));
10120 else
10122 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10126 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
10127 const struct wined3d_state *state, DWORD state_id)
10129 /* Table fog behavior depends on the projection matrix. */
10130 if (state->render_states[WINED3D_RS_FOGENABLE]
10131 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
10132 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10133 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
10136 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
10137 const struct wined3d_state *state, DWORD state_id)
10139 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
10140 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
10141 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
10142 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
10143 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10144 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10147 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
10148 const struct wined3d_state *state, DWORD state_id)
10150 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10153 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
10154 const struct wined3d_state *state, DWORD state_id)
10156 DWORD sampler = state_id - STATE_SAMPLER(0);
10157 const struct wined3d_texture *texture = state->textures[sampler];
10158 BOOL np2;
10160 if (!texture)
10161 return;
10163 if (sampler >= MAX_TEXTURES)
10164 return;
10166 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
10167 || context->lastWasPow2Texture & (1u << sampler))
10169 if (np2)
10170 context->lastWasPow2Texture |= 1u << sampler;
10171 else
10172 context->lastWasPow2Texture &= ~(1u << sampler);
10174 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10178 static void glsl_vertex_pipe_material(struct wined3d_context *context,
10179 const struct wined3d_state *state, DWORD state_id)
10181 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10184 static void glsl_vertex_pipe_light(struct wined3d_context *context,
10185 const struct wined3d_state *state, DWORD state_id)
10187 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10190 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
10191 const struct wined3d_state *state, DWORD state_id)
10193 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10196 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
10197 const struct wined3d_state *state, DWORD state_id)
10199 if (!use_vs(state))
10200 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10203 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
10204 const struct wined3d_state *state, DWORD state_id)
10206 static unsigned int once;
10208 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
10209 FIXME("Non-point sprite points not supported in core profile.\n");
10212 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
10213 const struct wined3d_state *state, DWORD state_id)
10215 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10218 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
10219 const struct wined3d_state *state, DWORD state_id)
10221 const struct wined3d_gl_info *gl_info = context->gl_info;
10222 UINT index = state_id - STATE_CLIPPLANE(0);
10224 if (index >= gl_info->limits.user_clip_distances)
10225 return;
10227 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10230 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
10232 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10233 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
10234 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
10235 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
10236 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
10237 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
10238 /* Clip planes */
10239 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10240 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10241 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10242 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10243 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10244 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10245 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10246 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10247 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10248 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10249 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10250 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10251 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10252 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10253 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10254 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10255 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10256 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10257 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10258 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10259 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10260 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10261 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10262 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10263 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10264 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10265 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10266 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10267 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10268 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10269 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10270 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10271 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10272 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10273 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10274 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10275 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10276 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10277 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10278 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10279 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10280 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10281 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10282 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10283 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10284 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10285 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10286 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10287 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10288 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10289 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10290 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10291 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10292 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10293 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10294 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10295 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10296 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10297 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10298 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10299 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10300 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10301 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10302 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10303 /* Lights */
10304 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10305 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10306 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10307 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10308 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10309 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10310 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10311 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10312 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10313 /* Viewport */
10314 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
10315 /* Transform states */
10316 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
10317 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
10318 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10319 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10320 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10321 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10322 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10323 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10324 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10325 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10326 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
10327 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10328 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10329 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10330 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10331 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10332 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10333 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10334 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10335 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10336 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10337 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10338 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10339 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10340 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10341 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10342 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10343 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10344 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10345 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10346 /* Fog */
10347 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10348 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10349 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10350 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10351 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
10352 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
10353 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10354 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10355 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10356 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10357 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10358 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10359 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10360 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10361 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10362 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10363 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10364 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
10365 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
10366 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
10367 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
10368 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
10369 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10370 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10371 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10372 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10373 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10374 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10375 /* NP2 texture matrix fixups. They are not needed if
10376 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
10377 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
10378 * matrix. */
10379 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10380 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10381 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10382 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10383 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10384 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10385 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10386 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10387 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10388 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10389 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10390 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10391 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10392 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10393 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10394 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10395 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10396 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10397 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10398 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10399 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10400 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10401 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10402 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10403 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10404 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
10405 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
10406 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
10409 /* TODO:
10410 * - Implement vertex tweening. */
10411 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
10413 glsl_vertex_pipe_vp_enable,
10414 glsl_vertex_pipe_vp_get_caps,
10415 glsl_vertex_pipe_vp_get_emul_mask,
10416 glsl_vertex_pipe_vp_alloc,
10417 glsl_vertex_pipe_vp_free,
10418 glsl_vertex_pipe_vp_states,
10421 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
10423 /* Nothing to do. */
10426 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
10428 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
10429 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
10430 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
10431 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
10432 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
10433 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
10434 | WINED3DTEXOPCAPS_SELECTARG1
10435 | WINED3DTEXOPCAPS_SELECTARG2
10436 | WINED3DTEXOPCAPS_MODULATE4X
10437 | WINED3DTEXOPCAPS_MODULATE2X
10438 | WINED3DTEXOPCAPS_MODULATE
10439 | WINED3DTEXOPCAPS_ADDSIGNED2X
10440 | WINED3DTEXOPCAPS_ADDSIGNED
10441 | WINED3DTEXOPCAPS_ADD
10442 | WINED3DTEXOPCAPS_SUBTRACT
10443 | WINED3DTEXOPCAPS_ADDSMOOTH
10444 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
10445 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
10446 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
10447 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
10448 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
10449 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
10450 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
10451 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
10452 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
10453 | WINED3DTEXOPCAPS_DOTPRODUCT3
10454 | WINED3DTEXOPCAPS_MULTIPLYADD
10455 | WINED3DTEXOPCAPS_LERP
10456 | WINED3DTEXOPCAPS_BUMPENVMAP
10457 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
10458 caps->MaxTextureBlendStages = MAX_TEXTURES;
10459 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
10462 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
10464 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10465 return GL_EXT_EMUL_ARB_MULTITEXTURE;
10466 return 0;
10469 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
10471 struct shader_glsl_priv *priv;
10473 if (shader_backend == &glsl_shader_backend)
10475 priv = shader_priv;
10476 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
10477 return priv;
10480 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
10482 return NULL;
10485 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
10487 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
10488 struct glsl_ffp_fragment_shader, entry.entry);
10489 struct glsl_shader_prog_link *program, *program2;
10490 struct glsl_ffp_destroy_ctx *ctx = context;
10492 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
10493 struct glsl_shader_prog_link, ps.shader_entry)
10495 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
10497 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
10498 HeapFree(GetProcessHeap(), 0, shader);
10501 /* Context activation is done by the caller. */
10502 static void glsl_fragment_pipe_free(struct wined3d_device *device)
10504 struct shader_glsl_priv *priv = device->fragment_priv;
10505 struct glsl_ffp_destroy_ctx ctx;
10507 ctx.priv = priv;
10508 ctx.gl_info = &device->adapter->gl_info;
10509 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
10512 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
10513 const struct wined3d_state *state, DWORD state_id)
10515 context->last_was_pshader = use_ps(state);
10517 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10520 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
10521 const struct wined3d_state *state, DWORD state_id)
10523 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10526 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
10527 const struct wined3d_state *state, DWORD state_id)
10529 BOOL use_vshader = use_vs(state);
10530 enum fogsource new_source;
10531 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
10532 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
10534 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10536 if (!state->render_states[WINED3D_RS_FOGENABLE])
10537 return;
10539 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
10541 if (use_vshader)
10542 new_source = FOGSOURCE_VS;
10543 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
10544 new_source = FOGSOURCE_COORD;
10545 else
10546 new_source = FOGSOURCE_FFP;
10548 else
10550 new_source = FOGSOURCE_FFP;
10553 if (new_source != context->fog_source || fogstart == fogend)
10555 context->fog_source = new_source;
10556 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10560 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
10561 const struct wined3d_state *state, DWORD state_id)
10563 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10564 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10565 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10567 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
10568 glsl_fragment_pipe_fog(context, state, state_id);
10571 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
10572 const struct wined3d_state *state, DWORD state_id)
10574 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10575 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10576 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10579 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
10580 const struct wined3d_state *state, DWORD state_id)
10582 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10585 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
10586 const struct wined3d_state *state, DWORD state_id)
10588 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10591 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
10592 const struct wined3d_state *state, DWORD state_id)
10594 const struct wined3d_gl_info *gl_info = context->gl_info;
10595 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
10596 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
10598 if (func)
10600 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
10601 checkGLcall("glAlphaFunc");
10605 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
10606 const struct wined3d_state *state, DWORD state_id)
10608 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10611 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
10612 const struct wined3d_state *state, DWORD state_id)
10614 const struct wined3d_gl_info *gl_info = context->gl_info;
10616 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
10618 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
10619 checkGLcall("glEnable(GL_ALPHA_TEST)");
10621 else
10623 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
10624 checkGLcall("glDisable(GL_ALPHA_TEST)");
10628 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
10629 const struct wined3d_state *state, DWORD state_id)
10631 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10634 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
10635 const struct wined3d_state *state, DWORD state_id)
10637 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10640 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
10641 const struct wined3d_state *state, DWORD state_id)
10643 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10646 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
10648 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10649 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
10650 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10651 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10652 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10653 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10654 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10655 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10656 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10657 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10658 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10659 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10660 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10661 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10662 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10663 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10664 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10665 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10666 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10667 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10668 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10669 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10670 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10671 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10672 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10673 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10674 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10675 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10676 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10677 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10678 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10679 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10680 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10681 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10682 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10683 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10684 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10685 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10686 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10687 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10688 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10689 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10690 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10691 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10692 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10693 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10694 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10695 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10696 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10697 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10698 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10699 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10700 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10701 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10702 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10703 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10704 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10705 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10706 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10707 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10708 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10709 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10710 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10711 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10712 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10713 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10714 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10715 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10716 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10717 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10718 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10719 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10720 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10721 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10722 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10723 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10724 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
10725 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
10726 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
10727 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
10728 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
10729 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
10730 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10731 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
10732 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
10733 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10734 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10735 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10736 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
10737 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
10738 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10739 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10740 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10741 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
10742 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
10743 {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 },
10744 {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 },
10745 {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 },
10746 {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 },
10747 {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 },
10748 {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 },
10749 {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 },
10750 {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 },
10751 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10752 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10753 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10754 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10755 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10756 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10757 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10758 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10759 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10760 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10761 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
10762 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
10763 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
10766 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
10768 return TRUE;
10771 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
10775 const struct fragment_pipeline glsl_fragment_pipe =
10777 glsl_fragment_pipe_enable,
10778 glsl_fragment_pipe_get_caps,
10779 glsl_fragment_pipe_get_emul_mask,
10780 glsl_fragment_pipe_alloc,
10781 glsl_fragment_pipe_free,
10782 glsl_fragment_pipe_alloc_context_data,
10783 glsl_fragment_pipe_free_context_data,
10784 shader_glsl_color_fixup_supported,
10785 glsl_fragment_pipe_state_template,