wined3d: Pass a wined3d_shader_context structure to shader_glsl_get_register_name().
[wine.git] / dlls / wined3d / glsl_shader.c
blob8aa3f2df250d76900623b767f218e336bebda07e
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 static const struct
76 enum wined3d_data_type data_type;
77 const char *glsl_scalar_type;
78 const char *glsl_vector_type;
80 component_type_info[] =
82 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_UNKNOWN */
83 {WINED3D_DATA_UINT, "uint", "uvec"}, /* WINED3D_TYPE_UINT */
84 {WINED3D_DATA_INT, "int", "ivec"}, /* WINED3D_TYPE_INT */
85 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_FLOAT */
88 struct glsl_dst_param
90 char reg_name[150];
91 char mask_str[6];
94 struct glsl_src_param
96 char reg_name[150];
97 char param_str[200];
100 struct glsl_sample_function
102 struct wined3d_string_buffer *name;
103 unsigned int coord_mask;
104 unsigned int deriv_mask;
105 enum wined3d_data_type data_type;
106 BOOL output_single_component;
107 unsigned int offset_size;
110 enum heap_node_op
112 HEAP_NODE_TRAVERSE_LEFT,
113 HEAP_NODE_TRAVERSE_RIGHT,
114 HEAP_NODE_POP,
117 struct constant_entry
119 unsigned int idx;
120 unsigned int version;
123 struct constant_heap
125 struct constant_entry *entries;
126 BOOL *contained;
127 unsigned int *positions;
128 unsigned int size;
131 /* GLSL shader private data */
132 struct shader_glsl_priv
134 struct wined3d_string_buffer shader_buffer;
135 struct wined3d_string_buffer_list string_buffers;
136 struct wine_rb_tree program_lookup;
137 struct constant_heap vconst_heap;
138 struct constant_heap pconst_heap;
139 unsigned char *stack;
140 UINT next_constant_version;
142 const struct wined3d_vertex_pipe_ops *vertex_pipe;
143 const struct fragment_pipeline *fragment_pipe;
144 struct wine_rb_tree ffp_vertex_shaders;
145 struct wine_rb_tree ffp_fragment_shaders;
146 BOOL ffp_proj_control;
147 BOOL legacy_lighting;
150 struct glsl_vs_program
152 struct list shader_entry;
153 GLuint id;
154 GLenum vertex_color_clamp;
155 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
156 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
157 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
158 GLint pos_fixup_location;
160 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
161 GLint projection_matrix_location;
162 GLint normal_matrix_location;
163 GLint texture_matrix_location[MAX_TEXTURES];
164 GLint material_ambient_location;
165 GLint material_diffuse_location;
166 GLint material_specular_location;
167 GLint material_emissive_location;
168 GLint material_shininess_location;
169 GLint light_ambient_location;
170 struct
172 GLint diffuse;
173 GLint specular;
174 GLint ambient;
175 GLint position;
176 GLint direction;
177 GLint range;
178 GLint falloff;
179 GLint c_att;
180 GLint l_att;
181 GLint q_att;
182 GLint cos_htheta;
183 GLint cos_hphi;
184 } light_location[MAX_ACTIVE_LIGHTS];
185 GLint pointsize_location;
186 GLint pointsize_min_location;
187 GLint pointsize_max_location;
188 GLint pointsize_c_att_location;
189 GLint pointsize_l_att_location;
190 GLint pointsize_q_att_location;
191 GLint clip_planes_location;
194 struct glsl_hs_program
196 struct list shader_entry;
197 GLuint id;
200 struct glsl_ds_program
202 struct list shader_entry;
203 GLuint id;
205 GLint pos_fixup_location;
208 struct glsl_gs_program
210 struct list shader_entry;
211 GLuint id;
213 GLint pos_fixup_location;
216 struct glsl_ps_program
218 struct list shader_entry;
219 GLuint id;
220 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
221 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
222 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
223 GLint bumpenv_mat_location[MAX_TEXTURES];
224 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
225 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
226 GLint tss_constant_location[MAX_TEXTURES];
227 GLint tex_factor_location;
228 GLint specular_enable_location;
229 GLint fog_color_location;
230 GLint fog_density_location;
231 GLint fog_end_location;
232 GLint fog_scale_location;
233 GLint alpha_test_ref_location;
234 GLint ycorrection_location;
235 GLint np2_fixup_location;
236 GLint color_key_location;
237 const struct ps_np2fixup_info *np2_fixup_info;
240 struct glsl_cs_program
242 struct list shader_entry;
243 GLuint id;
246 /* Struct to maintain data about a linked GLSL program */
247 struct glsl_shader_prog_link
249 struct wine_rb_entry program_lookup_entry;
250 struct glsl_vs_program vs;
251 struct glsl_hs_program hs;
252 struct glsl_ds_program ds;
253 struct glsl_gs_program gs;
254 struct glsl_ps_program ps;
255 struct glsl_cs_program cs;
256 GLuint id;
257 DWORD constant_update_mask;
258 unsigned int constant_version;
259 DWORD shader_controlled_clip_distances : 1;
260 DWORD clip_distance_mask : 8; /* MAX_CLIP_DISTANCES, 8 */
261 DWORD padding : 23;
264 struct glsl_program_key
266 GLuint vs_id;
267 GLuint hs_id;
268 GLuint ds_id;
269 GLuint gs_id;
270 GLuint ps_id;
271 GLuint cs_id;
274 struct shader_glsl_ctx_priv {
275 const struct vs_compile_args *cur_vs_args;
276 const struct ds_compile_args *cur_ds_args;
277 const struct ps_compile_args *cur_ps_args;
278 struct ps_np2fixup_info *cur_np2fixup_info;
279 struct wined3d_string_buffer_list *string_buffers;
282 struct glsl_context_data
284 struct glsl_shader_prog_link *glsl_program;
285 GLenum vertex_color_clamp;
286 BOOL rasterization_disabled;
289 struct glsl_ps_compiled_shader
291 struct ps_compile_args args;
292 struct ps_np2fixup_info np2fixup;
293 GLuint id;
296 struct glsl_vs_compiled_shader
298 struct vs_compile_args args;
299 GLuint id;
302 struct glsl_hs_compiled_shader
304 GLuint id;
307 struct glsl_ds_compiled_shader
309 struct ds_compile_args args;
310 GLuint id;
313 struct glsl_gs_compiled_shader
315 struct gs_compile_args args;
316 GLuint id;
319 struct glsl_cs_compiled_shader
321 GLuint id;
324 struct glsl_shader_private
326 union
328 struct glsl_vs_compiled_shader *vs;
329 struct glsl_hs_compiled_shader *hs;
330 struct glsl_ds_compiled_shader *ds;
331 struct glsl_gs_compiled_shader *gs;
332 struct glsl_ps_compiled_shader *ps;
333 struct glsl_cs_compiled_shader *cs;
334 } gl_shaders;
335 unsigned int num_gl_shaders, shader_array_size;
338 struct glsl_ffp_vertex_shader
340 struct wined3d_ffp_vs_desc desc;
341 GLuint id;
342 struct list linked_programs;
345 struct glsl_ffp_fragment_shader
347 struct ffp_frag_desc entry;
348 GLuint id;
349 struct list linked_programs;
352 struct glsl_ffp_destroy_ctx
354 struct shader_glsl_priv *priv;
355 const struct wined3d_gl_info *gl_info;
358 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
360 static const char *debug_gl_shader_type(GLenum type)
362 switch (type)
364 #define WINED3D_TO_STR(u) case u: return #u
365 WINED3D_TO_STR(GL_VERTEX_SHADER);
366 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
367 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
368 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
369 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
370 WINED3D_TO_STR(GL_COMPUTE_SHADER);
371 #undef WINED3D_TO_STR
372 default:
373 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
377 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
379 switch (type)
381 case WINED3D_SHADER_TYPE_VERTEX:
382 return "vs";
384 case WINED3D_SHADER_TYPE_HULL:
385 return "hs";
387 case WINED3D_SHADER_TYPE_DOMAIN:
388 return "ds";
390 case WINED3D_SHADER_TYPE_GEOMETRY:
391 return "gs";
393 case WINED3D_SHADER_TYPE_PIXEL:
394 return "ps";
396 case WINED3D_SHADER_TYPE_COMPUTE:
397 return "cs";
399 default:
400 FIXME("Unhandled shader type %#x.\n", type);
401 return "unknown";
405 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
407 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
408 return 440;
409 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
410 return 150;
411 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
412 return 130;
413 else
414 return 120;
417 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
418 const struct wined3d_gl_info *gl_info)
420 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
423 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
425 char str[4][17];
427 wined3d_ftoa(values[0], str[0]);
428 wined3d_ftoa(values[1], str[1]);
429 wined3d_ftoa(values[2], str[2]);
430 wined3d_ftoa(values[3], str[3]);
431 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
434 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
435 const int *values, unsigned int size)
437 int i;
439 if (!size || size > 4)
441 ERR("Invalid vector size %u.\n", size);
442 return;
445 if (size > 1)
446 shader_addline(buffer, "ivec%u(", size);
448 for (i = 0; i < size; ++i)
449 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
451 if (size > 1)
452 shader_addline(buffer, ")");
455 static const char *get_info_log_line(const char **ptr)
457 const char *p, *q;
459 p = *ptr;
460 if (!(q = strstr(p, "\n")))
462 if (!*p) return NULL;
463 *ptr += strlen(p);
464 return p;
466 *ptr = q + 1;
468 return p;
471 /* Context activation is done by the caller. */
472 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
474 int length = 0;
475 char *log;
477 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
478 return;
480 if (program)
481 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
482 else
483 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
485 /* A size of 1 is just a null-terminated string, so the log should be bigger than
486 * that if there are errors. */
487 if (length > 1)
489 const char *ptr, *line;
491 log = heap_alloc(length);
492 /* The info log is supposed to be zero-terminated, but at least some
493 * versions of fglrx don't terminate the string properly. The reported
494 * length does include the terminator, so explicitly set it to zero
495 * here. */
496 log[length - 1] = 0;
497 if (program)
498 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
499 else
500 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
502 ptr = log;
503 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
505 WARN("Info log received from GLSL shader #%u:\n", id);
506 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
508 else
510 FIXME("Info log received from GLSL shader #%u:\n", id);
511 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
513 heap_free(log);
517 /* Context activation is done by the caller. */
518 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
520 const char *ptr, *line;
522 TRACE("Compiling shader object %u.\n", shader);
524 if (TRACE_ON(d3d_shader))
526 ptr = src;
527 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
530 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
531 checkGLcall("glShaderSource");
532 GL_EXTCALL(glCompileShader(shader));
533 checkGLcall("glCompileShader");
534 print_glsl_info_log(gl_info, shader, FALSE);
537 /* Context activation is done by the caller. */
538 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
540 GLint i, shader_count, source_size = -1;
541 GLuint *shaders;
542 char *source = NULL;
544 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
545 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
547 ERR("Failed to allocate shader array memory.\n");
548 return;
551 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
552 for (i = 0; i < shader_count; ++i)
554 const char *ptr, *line;
555 GLint tmp;
557 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
559 if (source_size < tmp)
561 heap_free(source);
563 if (!(source = heap_alloc_zero(tmp)))
565 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
566 heap_free(shaders);
567 return;
569 source_size = tmp;
572 FIXME("Shader %u:\n", shaders[i]);
573 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
574 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
575 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
576 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
577 FIXME("\n");
579 ptr = source;
580 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
581 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
582 FIXME("\n");
585 heap_free(source);
586 heap_free(shaders);
589 /* Context activation is done by the caller. */
590 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
592 GLint tmp;
594 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
595 return;
597 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
598 if (!tmp)
600 FIXME("Program %u link status invalid.\n", program);
601 shader_glsl_dump_program_source(gl_info, program);
604 print_glsl_info_log(gl_info, program, TRUE);
607 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
609 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
610 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
611 * versions. */
612 return shader_glsl_get_version(gl_info) >= 140;
615 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
617 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
620 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
621 struct shader_glsl_priv *priv, GLuint program_id,
622 const struct wined3d_shader_reg_maps *reg_maps)
624 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
625 struct wined3d_string_buffer *name;
626 unsigned int i, base, count;
627 GLuint block_idx;
629 if (shader_glsl_use_layout_binding_qualifier(gl_info))
630 return;
632 name = string_buffer_get(&priv->string_buffers);
633 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
634 for (i = 0; i < count; ++i)
636 if (!reg_maps->cb_sizes[i])
637 continue;
639 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
640 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
641 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
643 checkGLcall("glUniformBlockBinding");
644 string_buffer_release(&priv->string_buffers, name);
647 /* Context activation is done by the caller. */
648 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
649 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
650 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
652 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
653 unsigned int i, mapped_unit;
654 GLint name_loc;
656 for (i = 0; i < count; ++i)
658 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
659 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
660 if (name_loc == -1)
661 continue;
663 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
664 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
666 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
667 continue;
670 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
671 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
673 checkGLcall("Load sampler bindings");
674 string_buffer_release(&priv->string_buffers, sampler_name);
677 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
678 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
680 const DWORD *tex_unit_map;
681 unsigned int base, count;
683 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
684 if (sampler_idx >= count)
685 return WINED3D_UNMAPPED_STAGE;
686 if (!tex_unit_map)
687 return base + sampler_idx;
688 return tex_unit_map[base + sampler_idx];
691 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
692 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
693 unsigned int sampler_idx)
695 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
696 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
697 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
698 else
699 ERR("Unmapped sampler %u.\n", sampler_idx);
702 /* Context activation is done by the caller. */
703 static void shader_glsl_load_samplers(const struct wined3d_context *context,
704 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
706 const struct wined3d_gl_info *gl_info = context->gl_info;
707 const struct wined3d_shader_version *shader_version;
708 const DWORD *tex_unit_map;
709 unsigned int base, count;
710 const char *prefix;
712 if (shader_glsl_use_layout_binding_qualifier(gl_info))
713 return;
715 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
716 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
717 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
718 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
721 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
722 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
724 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
726 if (icb)
728 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
729 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
730 GLint icb_location;
732 string_buffer_sprintf(icb_name, "%s_icb", prefix);
733 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
734 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
735 checkGLcall("Load immediate constant buffer");
737 string_buffer_release(&priv->string_buffers, icb_name);
741 /* Context activation is done by the caller. */
742 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
743 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
745 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
746 struct wined3d_string_buffer *name;
747 GLint location;
748 unsigned int i;
750 if (shader_glsl_use_layout_binding_qualifier(gl_info))
751 return;
753 name = string_buffer_get(&priv->string_buffers);
754 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
756 if (!reg_maps->uav_resource_info[i].type)
757 continue;
759 string_buffer_sprintf(name, "%s_image%u", prefix, i);
760 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
761 if (location == -1)
762 continue;
764 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
765 GL_EXTCALL(glUniform1i(location, i));
767 checkGLcall("Load image bindings");
768 string_buffer_release(&priv->string_buffers, name);
771 /* Context activation is done by the caller. */
772 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
773 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
775 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
777 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
778 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
779 /* Texture unit mapping is set up to be the same each time the shader
780 * program is used so we can hardcode the sampler uniform values. */
781 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
784 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
785 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
787 if (varyings && *strings)
789 char *ptr = *strings;
791 varyings[*varying_count] = ptr;
793 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
794 ptr += buffer->content_size + 1;
796 *strings = ptr;
799 *strings_length += buffer->content_size + 1;
800 ++(*varying_count);
803 static void append_transform_feedback_skip_components(const char **varyings,
804 unsigned int *varying_count, char **strings, unsigned int *strings_length,
805 struct wined3d_string_buffer *buffer, unsigned int component_count)
807 unsigned int j;
809 for (j = 0; j < component_count / 4; ++j)
811 string_buffer_sprintf(buffer, "gl_SkipComponents4");
812 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
814 if (component_count % 4)
816 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
817 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
821 static BOOL shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
822 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
823 char *strings, unsigned int *strings_length, GLenum buffer_mode)
825 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
826 BOOL have_varyings_to_record = FALSE;
828 count = length = 0;
829 highest_output_slot = 0;
830 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
832 stride = 0;
834 for (i = 0; i < so_desc->element_count; ++i)
836 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
838 highest_output_slot = max(highest_output_slot, e->output_slot);
839 if (e->output_slot != buffer_idx)
840 continue;
842 if (e->stream_idx)
844 FIXME("Unhandled stream %u.\n", e->stream_idx);
845 continue;
848 stride += e->component_count;
850 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
852 append_transform_feedback_skip_components(varyings, &count,
853 &strings, &length, buffer, e->component_count);
854 continue;
857 if (e->component_idx || e->component_count != 4)
859 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
861 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
862 append_transform_feedback_skip_components(varyings, &count,
863 &strings, &length, buffer, e->component_count);
864 continue;
867 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
868 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
869 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
871 else
873 string_buffer_sprintf(buffer, "shader_in_out.reg%u", e->register_idx);
874 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
877 have_varyings_to_record = TRUE;
880 if (buffer_idx < so_desc->buffer_stride_count
881 && stride < so_desc->buffer_strides[buffer_idx] / 4)
883 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
884 append_transform_feedback_skip_components(varyings, &count,
885 &strings, &length, buffer, component_count);
888 if (highest_output_slot <= buffer_idx)
889 break;
891 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
893 string_buffer_sprintf(buffer, "gl_NextBuffer");
894 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
898 if (varying_count)
899 *varying_count = count;
900 if (strings_length)
901 *strings_length = length;
903 return have_varyings_to_record;
906 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
907 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
909 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
910 const struct wined3d_gl_info *gl_info = context->gl_info;
911 struct wined3d_string_buffer *buffer;
912 unsigned int i, count, length;
913 const char **varyings;
914 char *strings;
915 GLenum mode;
917 if (!so_desc->element_count)
918 return;
920 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
922 mode = GL_INTERLEAVED_ATTRIBS;
924 else
926 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
928 for (i = 0; i < so_desc->element_count; ++i)
930 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
932 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
933 return;
935 ++element_count[so_desc->elements[i].output_slot];
938 if (element_count[0] == so_desc->element_count)
940 mode = GL_INTERLEAVED_ATTRIBS;
942 else
944 mode = GL_SEPARATE_ATTRIBS;
945 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
947 if (element_count[i] != 1)
948 break;
950 for (; i < ARRAY_SIZE(element_count); ++i)
952 if (element_count[i])
954 FIXME("Only single element per buffer is allowed in separate mode.\n");
955 return;
961 buffer = string_buffer_get(&priv->string_buffers);
963 if (!shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode))
965 FIXME("No varyings to record, disabling transform feedback.\n");
966 shader->u.gs.so_desc.element_count = 0;
967 string_buffer_release(&priv->string_buffers, buffer);
968 return;
971 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
973 ERR("Out of memory.\n");
974 string_buffer_release(&priv->string_buffers, buffer);
975 return;
977 if (!(strings = heap_calloc(length, sizeof(*strings))))
979 ERR("Out of memory.\n");
980 heap_free(varyings);
981 string_buffer_release(&priv->string_buffers, buffer);
982 return;
985 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
986 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
987 checkGLcall("glTransformFeedbackVaryings");
989 heap_free(varyings);
990 heap_free(strings);
991 string_buffer_release(&priv->string_buffers, buffer);
994 /* Context activation is done by the caller. */
995 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
996 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
998 unsigned int start = ~0U, end = 0;
999 int stack_idx = 0;
1000 unsigned int heap_idx = 1;
1001 unsigned int idx;
1003 if (heap->entries[heap_idx].version <= version) return;
1005 idx = heap->entries[heap_idx].idx;
1006 if (constant_locations[idx] != -1)
1007 start = end = idx;
1008 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1010 while (stack_idx >= 0)
1012 /* Note that we fall through to the next case statement. */
1013 switch(stack[stack_idx])
1015 case HEAP_NODE_TRAVERSE_LEFT:
1017 unsigned int left_idx = heap_idx << 1;
1018 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1020 heap_idx = left_idx;
1021 idx = heap->entries[heap_idx].idx;
1022 if (constant_locations[idx] != -1)
1024 if (start > idx)
1025 start = idx;
1026 if (end < idx)
1027 end = idx;
1030 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1031 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1032 break;
1036 case HEAP_NODE_TRAVERSE_RIGHT:
1038 unsigned int right_idx = (heap_idx << 1) + 1;
1039 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1041 heap_idx = right_idx;
1042 idx = heap->entries[heap_idx].idx;
1043 if (constant_locations[idx] != -1)
1045 if (start > idx)
1046 start = idx;
1047 if (end < idx)
1048 end = idx;
1051 stack[stack_idx++] = HEAP_NODE_POP;
1052 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1053 break;
1057 case HEAP_NODE_POP:
1058 heap_idx >>= 1;
1059 --stack_idx;
1060 break;
1063 if (start <= end)
1064 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1065 checkGLcall("walk_constant_heap()");
1068 /* Context activation is done by the caller. */
1069 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1070 GLint location, const struct wined3d_vec4 *data)
1072 GLfloat clamped_constant[4];
1074 if (location == -1) return;
1076 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1077 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1078 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1079 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1081 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1084 /* Context activation is done by the caller. */
1085 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1086 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1087 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1089 int stack_idx = 0;
1090 unsigned int heap_idx = 1;
1091 unsigned int idx;
1093 if (heap->entries[heap_idx].version <= version) return;
1095 idx = heap->entries[heap_idx].idx;
1096 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1097 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1099 while (stack_idx >= 0)
1101 /* Note that we fall through to the next case statement. */
1102 switch(stack[stack_idx])
1104 case HEAP_NODE_TRAVERSE_LEFT:
1106 unsigned int left_idx = heap_idx << 1;
1107 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1109 heap_idx = left_idx;
1110 idx = heap->entries[heap_idx].idx;
1111 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1113 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1114 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1115 break;
1119 case HEAP_NODE_TRAVERSE_RIGHT:
1121 unsigned int right_idx = (heap_idx << 1) + 1;
1122 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1124 heap_idx = right_idx;
1125 idx = heap->entries[heap_idx].idx;
1126 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1128 stack[stack_idx++] = HEAP_NODE_POP;
1129 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1130 break;
1134 case HEAP_NODE_POP:
1135 heap_idx >>= 1;
1136 --stack_idx;
1137 break;
1140 checkGLcall("walk_constant_heap_clamped()");
1143 /* Context activation is done by the caller. */
1144 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1145 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1146 unsigned char *stack, unsigned int version)
1148 const struct wined3d_shader_lconst *lconst;
1150 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1151 if (shader->reg_maps.shader_version.major == 1
1152 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1153 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1154 else
1155 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1157 if (!shader->load_local_constsF)
1159 TRACE("No need to load local float constants for this shader.\n");
1160 return;
1163 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1164 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1166 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1168 checkGLcall("glUniform4fv()");
1171 /* Context activation is done by the caller. */
1172 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1173 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1175 unsigned int i;
1176 struct list* ptr;
1178 for (i = 0; constants_set; constants_set >>= 1, ++i)
1180 if (!(constants_set & 1)) continue;
1182 /* We found this uniform name in the program - go ahead and send the data */
1183 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1186 /* Load immediate constants */
1187 ptr = list_head(&shader->constantsI);
1188 while (ptr)
1190 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1191 unsigned int idx = lconst->idx;
1192 const GLint *values = (const GLint *)lconst->value;
1194 /* We found this uniform name in the program - go ahead and send the data */
1195 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1196 ptr = list_next(&shader->constantsI, ptr);
1198 checkGLcall("glUniform4iv()");
1201 /* Context activation is done by the caller. */
1202 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1203 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1205 unsigned int i;
1206 struct list* ptr;
1208 for (i = 0; constants_set; constants_set >>= 1, ++i)
1210 if (!(constants_set & 1)) continue;
1212 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1215 /* Load immediate constants */
1216 ptr = list_head(&shader->constantsB);
1217 while (ptr)
1219 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1220 unsigned int idx = lconst->idx;
1221 const GLint *values = (const GLint *)lconst->value;
1223 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1224 ptr = list_next(&shader->constantsB, ptr);
1226 checkGLcall("glUniform1iv()");
1229 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1231 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1234 /* Context activation is done by the caller (state handler). */
1235 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1236 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1238 struct
1240 float sx, sy;
1242 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1243 UINT fixup = ps->np2_fixup_info->active;
1244 UINT i;
1246 for (i = 0; fixup; fixup >>= 1, ++i)
1248 const struct wined3d_texture *tex = state->textures[i];
1249 unsigned char idx = ps->np2_fixup_info->idx[i];
1251 if (!tex)
1253 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1254 continue;
1257 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1258 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1261 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1264 /* Taken and adapted from Mesa. */
1265 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1267 float pos, neg, t, det;
1268 struct wined3d_matrix temp;
1270 /* Calculate the determinant of upper left 3x3 submatrix and
1271 * determine if the matrix is singular. */
1272 pos = neg = 0.0f;
1273 t = in->_11 * in->_22 * in->_33;
1274 if (t >= 0.0f)
1275 pos += t;
1276 else
1277 neg += t;
1279 t = in->_21 * in->_32 * in->_13;
1280 if (t >= 0.0f)
1281 pos += t;
1282 else
1283 neg += t;
1284 t = in->_31 * in->_12 * in->_23;
1285 if (t >= 0.0f)
1286 pos += t;
1287 else
1288 neg += t;
1290 t = -in->_31 * in->_22 * in->_13;
1291 if (t >= 0.0f)
1292 pos += t;
1293 else
1294 neg += t;
1295 t = -in->_21 * in->_12 * in->_33;
1296 if (t >= 0.0f)
1297 pos += t;
1298 else
1299 neg += t;
1301 t = -in->_11 * in->_32 * in->_23;
1302 if (t >= 0.0f)
1303 pos += t;
1304 else
1305 neg += t;
1307 det = pos + neg;
1309 if (fabsf(det) < 1e-25f)
1310 return FALSE;
1312 det = 1.0f / det;
1313 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1314 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1315 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1316 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1317 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1318 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1319 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1320 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1321 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1323 *out = temp;
1324 return TRUE;
1327 static void swap_rows(float **a, float **b)
1329 float *tmp = *a;
1331 *a = *b;
1332 *b = tmp;
1335 static BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1337 float wtmp[4][8];
1338 float m0, m1, m2, m3, s;
1339 float *r0, *r1, *r2, *r3;
1341 r0 = wtmp[0];
1342 r1 = wtmp[1];
1343 r2 = wtmp[2];
1344 r3 = wtmp[3];
1346 r0[0] = m->_11;
1347 r0[1] = m->_12;
1348 r0[2] = m->_13;
1349 r0[3] = m->_14;
1350 r0[4] = 1.0f;
1351 r0[5] = r0[6] = r0[7] = 0.0f;
1353 r1[0] = m->_21;
1354 r1[1] = m->_22;
1355 r1[2] = m->_23;
1356 r1[3] = m->_24;
1357 r1[5] = 1.0f;
1358 r1[4] = r1[6] = r1[7] = 0.0f;
1360 r2[0] = m->_31;
1361 r2[1] = m->_32;
1362 r2[2] = m->_33;
1363 r2[3] = m->_34;
1364 r2[6] = 1.0f;
1365 r2[4] = r2[5] = r2[7] = 0.0f;
1367 r3[0] = m->_41;
1368 r3[1] = m->_42;
1369 r3[2] = m->_43;
1370 r3[3] = m->_44;
1371 r3[7] = 1.0f;
1372 r3[4] = r3[5] = r3[6] = 0.0f;
1374 /* Choose pivot - or die. */
1375 if (fabsf(r3[0]) > fabsf(r2[0]))
1376 swap_rows(&r3, &r2);
1377 if (fabsf(r2[0]) > fabsf(r1[0]))
1378 swap_rows(&r2, &r1);
1379 if (fabsf(r1[0]) > fabsf(r0[0]))
1380 swap_rows(&r1, &r0);
1381 if (r0[0] == 0.0f)
1382 return FALSE;
1384 /* Eliminate first variable. */
1385 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1386 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1387 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1388 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1389 s = r0[4];
1390 if (s != 0.0f)
1392 r1[4] -= m1 * s;
1393 r2[4] -= m2 * s;
1394 r3[4] -= m3 * s;
1396 s = r0[5];
1397 if (s != 0.0f)
1399 r1[5] -= m1 * s;
1400 r2[5] -= m2 * s;
1401 r3[5] -= m3 * s;
1403 s = r0[6];
1404 if (s != 0.0f)
1406 r1[6] -= m1 * s;
1407 r2[6] -= m2 * s;
1408 r3[6] -= m3 * s;
1410 s = r0[7];
1411 if (s != 0.0f)
1413 r1[7] -= m1 * s;
1414 r2[7] -= m2 * s;
1415 r3[7] -= m3 * s;
1418 /* Choose pivot - or die. */
1419 if (fabsf(r3[1]) > fabsf(r2[1]))
1420 swap_rows(&r3, &r2);
1421 if (fabsf(r2[1]) > fabsf(r1[1]))
1422 swap_rows(&r2, &r1);
1423 if (r1[1] == 0.0f)
1424 return FALSE;
1426 /* Eliminate second variable. */
1427 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1428 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1429 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1430 s = r1[4];
1431 if (s != 0.0f)
1433 r2[4] -= m2 * s;
1434 r3[4] -= m3 * s;
1436 s = r1[5];
1437 if (s != 0.0f)
1439 r2[5] -= m2 * s;
1440 r3[5] -= m3 * s;
1442 s = r1[6];
1443 if (s != 0.0f)
1445 r2[6] -= m2 * s;
1446 r3[6] -= m3 * s;
1448 s = r1[7];
1449 if (s != 0.0f)
1451 r2[7] -= m2 * s;
1452 r3[7] -= m3 * s;
1455 /* Choose pivot - or die. */
1456 if (fabsf(r3[2]) > fabsf(r2[2]))
1457 swap_rows(&r3, &r2);
1458 if (r2[2] == 0.0f)
1459 return FALSE;
1461 /* Eliminate third variable. */
1462 m3 = r3[2] / r2[2];
1463 r3[3] -= m3 * r2[3];
1464 r3[4] -= m3 * r2[4];
1465 r3[5] -= m3 * r2[5];
1466 r3[6] -= m3 * r2[6];
1467 r3[7] -= m3 * r2[7];
1469 /* Last check. */
1470 if (r3[3] == 0.0f)
1471 return FALSE;
1473 /* Back substitute row 3. */
1474 s = 1.0f / r3[3];
1475 r3[4] *= s;
1476 r3[5] *= s;
1477 r3[6] *= s;
1478 r3[7] *= s;
1480 /* Back substitute row 2. */
1481 m2 = r2[3];
1482 s = 1.0f / r2[2];
1483 r2[4] = s * (r2[4] - r3[4] * m2);
1484 r2[5] = s * (r2[5] - r3[5] * m2);
1485 r2[6] = s * (r2[6] - r3[6] * m2);
1486 r2[7] = s * (r2[7] - r3[7] * m2);
1487 m1 = r1[3];
1488 r1[4] -= r3[4] * m1;
1489 r1[5] -= r3[5] * m1;
1490 r1[6] -= r3[6] * m1;
1491 r1[7] -= r3[7] * m1;
1492 m0 = r0[3];
1493 r0[4] -= r3[4] * m0;
1494 r0[5] -= r3[5] * m0;
1495 r0[6] -= r3[6] * m0;
1496 r0[7] -= r3[7] * m0;
1498 /* Back substitute row 1. */
1499 m1 = r1[2];
1500 s = 1.0f / r1[1];
1501 r1[4] = s * (r1[4] - r2[4] * m1);
1502 r1[5] = s * (r1[5] - r2[5] * m1);
1503 r1[6] = s * (r1[6] - r2[6] * m1);
1504 r1[7] = s * (r1[7] - r2[7] * m1);
1505 m0 = r0[2];
1506 r0[4] -= r2[4] * m0;
1507 r0[5] -= r2[5] * m0;
1508 r0[6] -= r2[6] * m0;
1509 r0[7] -= r2[7] * m0;
1511 /* Back substitute row 0. */
1512 m0 = r0[1];
1513 s = 1.0f / r0[0];
1514 r0[4] = s * (r0[4] - r1[4] * m0);
1515 r0[5] = s * (r0[5] - r1[5] * m0);
1516 r0[6] = s * (r0[6] - r1[6] * m0);
1517 r0[7] = s * (r0[7] - r1[7] * m0);
1519 out->_11 = r0[4];
1520 out->_12 = r0[5];
1521 out->_13 = r0[6];
1522 out->_14 = r0[7];
1523 out->_21 = r1[4];
1524 out->_22 = r1[5];
1525 out->_23 = r1[6];
1526 out->_24 = r1[7];
1527 out->_31 = r2[4];
1528 out->_32 = r2[5];
1529 out->_33 = r2[6];
1530 out->_34 = r2[7];
1531 out->_41 = r3[4];
1532 out->_42 = r3[5];
1533 out->_43 = r3[6];
1534 out->_44 = r3[7];
1536 return TRUE;
1539 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1541 struct wined3d_matrix temp;
1542 unsigned int i, j;
1544 for (i = 0; i < 4; ++i)
1545 for (j = 0; j < 4; ++j)
1546 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1548 *out = temp;
1551 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1552 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1554 const struct wined3d_gl_info *gl_info = context->gl_info;
1555 float mat[3 * 3];
1556 struct wined3d_matrix mv;
1557 unsigned int i, j;
1559 if (prog->vs.normal_matrix_location == -1)
1560 return;
1562 get_modelview_matrix(context, state, 0, &mv);
1563 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1564 invert_matrix_3d(&mv, &mv);
1565 else
1566 invert_matrix(&mv, &mv);
1567 /* Tests show that singular modelview matrices are used unchanged as normal
1568 * matrices on D3D3 and older. There seems to be no clearly consistent
1569 * behavior on newer D3D versions so always follow older ddraw behavior. */
1570 for (i = 0; i < 3; ++i)
1571 for (j = 0; j < 3; ++j)
1572 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1574 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1575 checkGLcall("glUniformMatrix3fv");
1578 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1579 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1581 const struct wined3d_gl_info *gl_info = context->gl_info;
1582 struct wined3d_matrix mat;
1584 if (tex >= MAX_TEXTURES)
1585 return;
1586 if (prog->vs.texture_matrix_location[tex] == -1)
1587 return;
1589 get_texture_matrix(context, state, tex, &mat);
1590 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1591 checkGLcall("glUniformMatrix4fv");
1594 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1595 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1597 const struct wined3d_gl_info *gl_info = context->gl_info;
1599 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1601 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1602 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1604 else
1606 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1608 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1610 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1611 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1612 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1613 checkGLcall("setting FFP material uniforms");
1616 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1617 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1619 const struct wined3d_gl_info *gl_info = context->gl_info;
1620 struct wined3d_color color;
1622 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1623 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1624 checkGLcall("glUniform3fv");
1627 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1628 const struct wined3d_matrix *src2)
1630 struct wined3d_vec4 temp;
1632 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1633 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1634 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1635 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1637 *dest = temp;
1640 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1641 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1642 struct glsl_shader_prog_link *prog)
1644 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1645 const struct wined3d_gl_info *gl_info = context->gl_info;
1646 struct wined3d_vec4 vec4;
1648 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1649 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1650 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1652 switch (light_info->OriginalParms.type)
1654 case WINED3D_LIGHT_POINT:
1655 multiply_vector_matrix(&vec4, &light_info->position, view);
1656 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1657 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1658 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1659 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1660 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1661 break;
1663 case WINED3D_LIGHT_SPOT:
1664 multiply_vector_matrix(&vec4, &light_info->position, view);
1665 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1667 multiply_vector_matrix(&vec4, &light_info->direction, view);
1668 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1670 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1671 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1672 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1673 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1674 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1675 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1676 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1677 break;
1679 case WINED3D_LIGHT_DIRECTIONAL:
1680 multiply_vector_matrix(&vec4, &light_info->direction, view);
1681 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1682 break;
1684 case WINED3D_LIGHT_PARALLELPOINT:
1685 multiply_vector_matrix(&vec4, &light_info->position, view);
1686 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1687 break;
1689 default:
1690 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1692 checkGLcall("setting FFP lights uniforms");
1695 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1696 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1698 const struct wined3d_gl_info *gl_info = context->gl_info;
1699 float min, max;
1700 float size, att[3];
1702 get_pointsize_minmax(context, state, &min, &max);
1704 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1705 checkGLcall("glUniform1f");
1706 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1707 checkGLcall("glUniform1f");
1709 get_pointsize(context, state, &size, att);
1711 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1712 checkGLcall("glUniform1f");
1713 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1714 checkGLcall("glUniform1f");
1715 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1716 checkGLcall("glUniform1f");
1717 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1718 checkGLcall("glUniform1f");
1721 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1722 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1724 const struct wined3d_gl_info *gl_info = context->gl_info;
1725 struct wined3d_color color;
1726 float start, end, scale;
1727 union
1729 DWORD d;
1730 float f;
1731 } tmpvalue;
1733 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1734 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1735 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1736 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1737 get_fog_start_end(context, state, &start, &end);
1738 scale = 1.0f / (end - start);
1739 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1740 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1741 checkGLcall("fog emulation uniforms");
1744 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1745 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1747 const struct wined3d_gl_info *gl_info = context->gl_info;
1748 struct wined3d_matrix matrix;
1749 struct wined3d_vec4 plane;
1751 plane = state->clip_planes[index];
1753 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1754 if (!use_vs(state))
1756 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1757 transpose_matrix(&matrix, &matrix);
1758 multiply_vector_matrix(&plane, &plane, &matrix);
1761 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1764 /* Context activation is done by the caller (state handler). */
1765 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1766 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1768 struct wined3d_color float_key[2];
1769 const struct wined3d_texture *texture = state->textures[0];
1771 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1772 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1775 /* Context activation is done by the caller (state handler). */
1776 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1777 const struct wined3d_state *state)
1779 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1780 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1781 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1782 const struct wined3d_gl_info *gl_info = context->gl_info;
1783 struct shader_glsl_priv *priv = shader_priv;
1784 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1785 DWORD update_mask;
1787 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1788 UINT constant_version;
1789 int i;
1791 if (!prog) {
1792 /* No GLSL program set - nothing to do. */
1793 return;
1795 constant_version = prog->constant_version;
1796 update_mask = context->constant_update_mask & prog->constant_update_mask;
1798 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1799 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1800 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1802 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1803 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1804 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1806 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1807 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1808 vshader->reg_maps.boolean_constants);
1810 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1812 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1813 shader_glsl_clip_plane_uniform(context, state, i, prog);
1816 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1817 shader_glsl_pointsize_uniform(context, state, prog);
1819 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1821 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1822 max(state->viewport_count, 1) : 1;
1823 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1824 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1825 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1826 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1827 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1828 else
1829 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1830 checkGLcall("glUniform4fv");
1833 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1835 struct wined3d_matrix mat;
1837 get_modelview_matrix(context, state, 0, &mat);
1838 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1839 checkGLcall("glUniformMatrix4fv");
1841 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1844 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1846 struct wined3d_matrix mat;
1848 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1850 if (prog->vs.modelview_matrix_location[i] == -1)
1851 break;
1853 get_modelview_matrix(context, state, i, &mat);
1854 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1855 checkGLcall("glUniformMatrix4fv");
1859 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1861 struct wined3d_matrix projection;
1863 get_projection_matrix(context, state, &projection);
1864 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1865 checkGLcall("glUniformMatrix4fv");
1868 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1870 for (i = 0; i < MAX_TEXTURES; ++i)
1871 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1874 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1875 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1877 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1879 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1880 DWORD point_count = 0;
1881 DWORD spot_count = 0;
1882 DWORD directional_count = 0;
1883 DWORD parallel_point_count = 0;
1885 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1887 if (!state->lights[i])
1888 continue;
1890 switch (state->lights[i]->OriginalParms.type)
1892 case WINED3D_LIGHT_POINT:
1893 ++point_count;
1894 break;
1895 case WINED3D_LIGHT_SPOT:
1896 ++spot_count;
1897 break;
1898 case WINED3D_LIGHT_DIRECTIONAL:
1899 ++directional_count;
1900 break;
1901 case WINED3D_LIGHT_PARALLELPOINT:
1902 ++parallel_point_count;
1903 break;
1904 default:
1905 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1906 break;
1909 point_idx = 0;
1910 spot_idx = point_idx + point_count;
1911 directional_idx = spot_idx + spot_count;
1912 parallel_point_idx = directional_idx + directional_count;
1914 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1915 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1917 const struct wined3d_light_info *light_info = state->lights[i];
1918 unsigned int idx;
1920 if (!light_info)
1921 continue;
1923 switch (light_info->OriginalParms.type)
1925 case WINED3D_LIGHT_POINT:
1926 idx = point_idx++;
1927 break;
1928 case WINED3D_LIGHT_SPOT:
1929 idx = spot_idx++;
1930 break;
1931 case WINED3D_LIGHT_DIRECTIONAL:
1932 idx = directional_idx++;
1933 break;
1934 case WINED3D_LIGHT_PARALLELPOINT:
1935 idx = parallel_point_idx++;
1936 break;
1937 default:
1938 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1939 continue;
1941 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1945 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1946 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1947 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1949 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1950 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1951 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1953 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1954 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1955 pshader->reg_maps.boolean_constants);
1957 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1959 for (i = 0; i < MAX_TEXTURES; ++i)
1961 if (prog->ps.bumpenv_mat_location[i] == -1)
1962 continue;
1964 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1965 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1967 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1969 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1970 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1971 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1972 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1976 checkGLcall("bump env uniforms");
1979 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1981 const struct wined3d_vec4 correction_params =
1983 /* Position is relative to the framebuffer, not the viewport. */
1984 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1985 context->render_offscreen ? 1.0f : -1.0f,
1986 0.0f,
1987 0.0f,
1990 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1993 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1994 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1995 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1996 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1998 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
2000 struct wined3d_color color;
2002 if (prog->ps.tex_factor_location != -1)
2004 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
2005 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
2008 if (state->render_states[WINED3D_RS_SPECULARENABLE])
2009 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
2010 else
2011 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
2013 for (i = 0; i < MAX_TEXTURES; ++i)
2015 if (prog->ps.tss_constant_location[i] == -1)
2016 continue;
2018 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
2019 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
2022 checkGLcall("fixed function uniforms");
2025 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
2026 shader_glsl_load_fog_uniform(context, state, prog);
2028 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
2030 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
2032 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
2033 checkGLcall("alpha test emulation uniform");
2036 if (priv->next_constant_version == UINT_MAX)
2038 TRACE("Max constant version reached, resetting to 0.\n");
2039 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
2040 priv->next_constant_version = 1;
2042 else
2044 prog->constant_version = priv->next_constant_version++;
2048 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
2050 struct constant_entry *entries = heap->entries;
2051 unsigned int *positions = heap->positions;
2052 unsigned int heap_idx, parent_idx;
2054 if (!heap->contained[idx])
2056 heap_idx = heap->size++;
2057 heap->contained[idx] = TRUE;
2059 else
2061 heap_idx = positions[idx];
2064 while (heap_idx > 1)
2066 parent_idx = heap_idx >> 1;
2068 if (new_version <= entries[parent_idx].version) break;
2070 entries[heap_idx] = entries[parent_idx];
2071 positions[entries[parent_idx].idx] = heap_idx;
2072 heap_idx = parent_idx;
2075 entries[heap_idx].version = new_version;
2076 entries[heap_idx].idx = idx;
2077 positions[idx] = heap_idx;
2080 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2082 struct shader_glsl_priv *priv = device->shader_priv;
2083 struct constant_heap *heap = &priv->vconst_heap;
2084 UINT i;
2086 for (i = start; i < count + start; ++i)
2088 update_heap_entry(heap, i, priv->next_constant_version);
2092 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2094 struct shader_glsl_priv *priv = device->shader_priv;
2095 struct constant_heap *heap = &priv->pconst_heap;
2096 UINT i;
2098 for (i = start; i < count + start; ++i)
2100 update_heap_entry(heap, i, priv->next_constant_version);
2104 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2106 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2107 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2108 if(shader_major > 3) return ret;
2110 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2111 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2112 return ret;
2115 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2117 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2120 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2122 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2123 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2126 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2128 return shader_glsl_get_version(gl_info) >= 150;
2131 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2133 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2136 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2137 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2139 va_list args;
2140 int ret;
2142 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2143 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2144 for (;;)
2146 va_start(args, format);
2147 ret = shader_vaddline(buffer, format, args);
2148 va_end(args);
2149 if (!ret)
2150 return;
2151 if (!string_buffer_resize(buffer, ret))
2152 return;
2156 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2157 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2159 va_list args;
2160 int ret;
2162 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2163 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2164 for (;;)
2166 va_start(args, format);
2167 ret = shader_vaddline(buffer, format, args);
2168 va_end(args);
2169 if (!ret)
2170 return;
2171 if (!string_buffer_resize(buffer, ret))
2172 return;
2176 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2178 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2181 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2183 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2186 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2188 switch (mode)
2190 case WINED3DSIM_CONSTANT:
2191 return "flat ";
2192 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2193 return "noperspective ";
2194 default:
2195 FIXME("Unhandled interpolation mode %#x.\n", mode);
2196 case WINED3DSIM_NONE:
2197 case WINED3DSIM_LINEAR:
2198 return "";
2202 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2203 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2205 return wined3d_extract_bits(packed_interpolation_mode,
2206 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2209 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2210 struct wined3d_string_buffer *buffer, unsigned int element_count,
2211 const DWORD *interpolation_mode, BOOL unroll)
2213 enum wined3d_shader_interpolation_mode mode;
2214 unsigned int i;
2216 if (shader_glsl_use_interface_blocks(gl_info))
2218 if (unroll)
2220 shader_addline(buffer, "in shader_in_out {\n");
2221 for (i = 0; i < element_count; ++i)
2223 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2224 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2226 shader_addline(buffer, "} shader_in;\n");
2228 else
2230 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2233 else
2235 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2239 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2240 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2241 const DWORD *interpolation_mode)
2243 enum wined3d_shader_interpolation_mode mode;
2244 unsigned int i;
2246 if (shader_glsl_use_interface_blocks(gl_info))
2248 if (rasterizer_setup)
2250 shader_addline(buffer, "out shader_in_out {\n");
2251 for (i = 0; i < element_count; ++i)
2253 const char *interpolation_qualifiers = "";
2254 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2256 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2257 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2259 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2261 shader_addline(buffer, "} shader_out;\n");
2263 else
2265 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2268 else
2270 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2274 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2276 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2279 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2281 switch (primitive_type)
2283 case WINED3D_PT_POINTLIST:
2284 return "points";
2286 case WINED3D_PT_LINELIST:
2287 return "lines";
2289 case WINED3D_PT_LINESTRIP:
2290 return "line_strip";
2292 case WINED3D_PT_TRIANGLELIST:
2293 return "triangles";
2295 case WINED3D_PT_TRIANGLESTRIP:
2296 return "triangle_strip";
2298 case WINED3D_PT_LINELIST_ADJ:
2299 return "lines_adjacency";
2301 case WINED3D_PT_TRIANGLELIST_ADJ:
2302 return "triangles_adjacency";
2304 default:
2305 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2306 return "";
2310 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2312 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2313 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2314 DWORD input_reg_used = shader->u.ps.input_reg_used;
2315 unsigned int i;
2317 if (reg_maps->shader_version.major < 3)
2318 return input_reg_used & (1u << idx);
2320 for (i = 0; i < input_signature->element_count; ++i)
2322 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2324 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2325 continue;
2327 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2328 && input->semantic_idx == idx)
2329 return input_reg_used & (1u << input->register_idx);
2331 return FALSE;
2334 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2335 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2337 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2339 if (version->major >= 4)
2340 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2341 else
2342 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2345 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2346 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2347 unsigned int index)
2349 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2350 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2351 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2352 index, scalar_type, scalar_type, index);
2355 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2356 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2358 unsigned int index = e->register_idx;
2359 enum wined3d_component_type type;
2361 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2363 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2364 index);
2365 return;
2367 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2369 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2370 index);
2371 return;
2373 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2374 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2376 if (shader_glsl_use_explicit_attrib_location(gl_info))
2377 shader_addline(buffer, "layout(location = %u) ", index);
2379 type = e->component_type;
2380 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2382 FIXME("Unhandled type %#x.\n", type);
2383 type = WINED3D_TYPE_FLOAT;
2385 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2386 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2387 else
2388 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2389 component_type_info[type].glsl_vector_type,
2390 component_type_info[type].glsl_scalar_type, index);
2393 /** Generate the variable & register declarations for the GLSL output target */
2394 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2395 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2396 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2398 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2399 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2400 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2401 const struct wined3d_gl_info *gl_info = context->gl_info;
2402 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2403 unsigned int uniform_block_base, uniform_block_count;
2404 const struct wined3d_shader_lconst *lconst;
2405 const char *prefix;
2406 unsigned int i;
2407 DWORD map;
2409 prefix = shader_glsl_get_prefix(version->type);
2411 /* Prototype the subroutines */
2412 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2414 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2417 /* Declare the constants (aka uniforms) */
2418 if (shader->limits->constant_float > 0)
2420 unsigned max_constantsF;
2422 /* Unless the shader uses indirect addressing, always declare the
2423 * maximum array size and ignore that we need some uniforms privately.
2424 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2425 * and immediate values, still declare VC[256]. If the shader needs
2426 * more uniforms than we have it won't work in any case. If it uses
2427 * less, the compiler will figure out which uniforms are really used
2428 * and strip them out. This allows a shader to use c255 on a dx9 card,
2429 * as long as it doesn't also use all the other constants.
2431 * If the shader uses indirect addressing the compiler must assume
2432 * that all declared uniforms are used. In this case, declare only the
2433 * amount that we're assured to have.
2435 * Thus we run into problems in these two cases:
2436 * 1) The shader really uses more uniforms than supported.
2437 * 2) The shader uses indirect addressing, less constants than
2438 * supported, but uses a constant index > #supported consts. */
2439 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2441 /* No indirect addressing here. */
2442 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2444 else
2446 if (reg_maps->usesrelconstF)
2448 /* Subtract the other potential uniforms from the max
2449 * available (bools, ints, and 1 row of projection matrix).
2450 * Subtract another uniform for immediate values, which have
2451 * to be loaded via uniform by the driver as well. The shader
2452 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2453 * shader code, so one vec4 should be enough. (Unfortunately
2454 * the Nvidia driver doesn't store 128 and -128 in one float).
2456 * Writing gl_ClipVertex requires one uniform for each
2457 * clipplane as well. */
2458 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2459 if (vs_args->clip_enabled)
2460 max_constantsF -= gl_info->limits.user_clip_distances;
2461 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2462 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2463 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2464 * for now take this into account when calculating the number of available constants
2466 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2467 /* Set by driver quirks in directx.c */
2468 max_constantsF -= gl_info->reserved_glsl_constants;
2470 if (max_constantsF < shader->limits->constant_float)
2472 static unsigned int once;
2474 if (!once++)
2475 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2476 " it may not render correctly.\n");
2477 else
2478 WARN("The hardware does not support enough uniform components to run this shader.\n");
2481 else
2483 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2486 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2487 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2490 /* Always declare the full set of constants, the compiler can remove the
2491 * unused ones because d3d doesn't (yet) support indirect int and bool
2492 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2493 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2494 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2496 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2497 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2499 /* Declare immediate constant buffer */
2500 if (reg_maps->icb)
2501 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2503 /* Declare constant buffers */
2504 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2505 &uniform_block_base, &uniform_block_count);
2506 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2508 if (reg_maps->cb_sizes[i])
2510 shader_addline(buffer, "layout(std140");
2511 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2512 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2513 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2514 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2518 /* Declare texture samplers */
2519 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2521 struct wined3d_shader_sampler_map_entry *entry;
2522 const char *sampler_type_prefix, *sampler_type;
2523 BOOL shadow_sampler, tex_rect;
2525 entry = &reg_maps->sampler_map.entries[i];
2527 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2529 ERR("Invalid resource index %u.\n", entry->resource_idx);
2530 continue;
2533 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2535 case WINED3D_DATA_FLOAT:
2536 case WINED3D_DATA_UNORM:
2537 case WINED3D_DATA_SNORM:
2538 sampler_type_prefix = "";
2539 break;
2541 case WINED3D_DATA_INT:
2542 sampler_type_prefix = "i";
2543 break;
2545 case WINED3D_DATA_UINT:
2546 sampler_type_prefix = "u";
2547 break;
2549 default:
2550 sampler_type_prefix = "";
2551 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2552 break;
2555 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2556 switch (reg_maps->resource_info[entry->resource_idx].type)
2558 case WINED3D_SHADER_RESOURCE_BUFFER:
2559 sampler_type = "samplerBuffer";
2560 break;
2562 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2563 if (shadow_sampler)
2564 sampler_type = "sampler1DShadow";
2565 else
2566 sampler_type = "sampler1D";
2567 break;
2569 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2570 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2571 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2572 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2573 if (shadow_sampler)
2575 if (tex_rect)
2576 sampler_type = "sampler2DRectShadow";
2577 else
2578 sampler_type = "sampler2DShadow";
2580 else
2582 if (tex_rect)
2583 sampler_type = "sampler2DRect";
2584 else
2585 sampler_type = "sampler2D";
2587 break;
2589 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2590 if (shadow_sampler)
2591 FIXME("Unsupported 3D shadow sampler.\n");
2592 sampler_type = "sampler3D";
2593 break;
2595 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2596 if (shadow_sampler)
2597 sampler_type = "samplerCubeShadow";
2598 else
2599 sampler_type = "samplerCube";
2600 break;
2602 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2603 if (shadow_sampler)
2604 sampler_type = "sampler1DArrayShadow";
2605 else
2606 sampler_type = "sampler1DArray";
2607 break;
2609 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2610 if (shadow_sampler)
2611 sampler_type = "sampler2DArrayShadow";
2612 else
2613 sampler_type = "sampler2DArray";
2614 break;
2616 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2617 if (shadow_sampler)
2618 sampler_type = "samplerCubeArrayShadow";
2619 else
2620 sampler_type = "samplerCubeArray";
2621 break;
2623 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2624 sampler_type = "sampler2DMS";
2625 break;
2627 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2628 sampler_type = "sampler2DMSArray";
2629 break;
2631 default:
2632 sampler_type = "unsupported_sampler";
2633 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2634 break;
2637 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2638 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2639 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2640 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2643 /* Declare images */
2644 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2646 const char *image_type_prefix, *image_type, *read_format;
2648 if (!reg_maps->uav_resource_info[i].type)
2649 continue;
2651 switch (reg_maps->uav_resource_info[i].data_type)
2653 case WINED3D_DATA_FLOAT:
2654 case WINED3D_DATA_UNORM:
2655 case WINED3D_DATA_SNORM:
2656 image_type_prefix = "";
2657 read_format = "r32f";
2658 break;
2660 case WINED3D_DATA_INT:
2661 image_type_prefix = "i";
2662 read_format = "r32i";
2663 break;
2665 case WINED3D_DATA_UINT:
2666 image_type_prefix = "u";
2667 read_format = "r32ui";
2668 break;
2670 default:
2671 image_type_prefix = "";
2672 read_format = "";
2673 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2674 break;
2677 switch (reg_maps->uav_resource_info[i].type)
2679 case WINED3D_SHADER_RESOURCE_BUFFER:
2680 image_type = "imageBuffer";
2681 break;
2683 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2684 image_type = "image2D";
2685 break;
2687 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2688 image_type = "image3D";
2689 break;
2691 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2692 image_type = "image2DArray";
2693 break;
2695 default:
2696 image_type = "unsupported_image";
2697 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2698 break;
2701 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2702 shader_addline(buffer, "layout(binding = %u)\n", i);
2703 if (reg_maps->uav_read_mask & (1u << i))
2704 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2705 read_format, image_type_prefix, image_type, prefix, i);
2706 else
2707 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2708 image_type_prefix, image_type, prefix, i);
2710 if (reg_maps->uav_counter_mask & (1u << i))
2711 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2712 i, prefix, i);
2715 /* Declare address variables */
2716 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2718 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2721 /* Declare output register temporaries */
2722 if (shader->limits->packed_output)
2723 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2725 /* Declare temporary variables */
2726 if (reg_maps->temporary_count)
2728 for (i = 0; i < reg_maps->temporary_count; ++i)
2729 shader_addline(buffer, "vec4 R%u;\n", i);
2731 else if (version->major < 4)
2733 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2735 if (map & 1)
2736 shader_addline(buffer, "vec4 R%u;\n", i);
2740 /* Declare indexable temporary variables */
2741 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2743 if (idx_temp_reg->component_count != 4)
2744 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2745 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2748 /* Declare loop registers aLx */
2749 if (version->major < 4)
2751 for (i = 0; i < reg_maps->loop_depth; ++i)
2753 shader_addline(buffer, "int aL%u;\n", i);
2754 shader_addline(buffer, "int tmpInt%u;\n", i);
2758 /* Temporary variables for matrix operations */
2759 shader_addline(buffer, "vec4 tmp0;\n");
2760 shader_addline(buffer, "vec4 tmp1;\n");
2762 if (!shader->load_local_constsF)
2764 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2766 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2767 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2768 shader_addline(buffer, ";\n");
2773 /* Prototypes */
2774 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2775 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2776 enum wined3d_data_type data_type);
2778 /** Used for opcode modifiers - They multiply the result by the specified amount */
2779 static const char * const shift_glsl_tab[] = {
2780 "", /* 0 (none) */
2781 "2.0 * ", /* 1 (x2) */
2782 "4.0 * ", /* 2 (x4) */
2783 "8.0 * ", /* 3 (x8) */
2784 "16.0 * ", /* 4 (x16) */
2785 "32.0 * ", /* 5 (x32) */
2786 "", /* 6 (x64) */
2787 "", /* 7 (x128) */
2788 "", /* 8 (d256) */
2789 "", /* 9 (d128) */
2790 "", /* 10 (d64) */
2791 "", /* 11 (d32) */
2792 "0.0625 * ", /* 12 (d16) */
2793 "0.125 * ", /* 13 (d8) */
2794 "0.25 * ", /* 14 (d4) */
2795 "0.5 * " /* 15 (d2) */
2798 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2799 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2800 const char *in_reg, const char *in_regswizzle, char *out_str)
2802 switch (src_modifier)
2804 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2805 case WINED3DSPSM_DW:
2806 case WINED3DSPSM_NONE:
2807 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2808 break;
2809 case WINED3DSPSM_NEG:
2810 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2811 break;
2812 case WINED3DSPSM_NOT:
2813 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2814 break;
2815 case WINED3DSPSM_BIAS:
2816 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2817 break;
2818 case WINED3DSPSM_BIASNEG:
2819 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2820 break;
2821 case WINED3DSPSM_SIGN:
2822 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2823 break;
2824 case WINED3DSPSM_SIGNNEG:
2825 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2826 break;
2827 case WINED3DSPSM_COMP:
2828 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2829 break;
2830 case WINED3DSPSM_X2:
2831 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2832 break;
2833 case WINED3DSPSM_X2NEG:
2834 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2835 break;
2836 case WINED3DSPSM_ABS:
2837 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2838 break;
2839 case WINED3DSPSM_ABSNEG:
2840 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2841 break;
2842 default:
2843 FIXME("Unhandled modifier %u\n", src_modifier);
2844 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2848 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2849 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2851 /* The ARB_shading_language_420pack extension allows swizzle operations on
2852 * scalars. */
2853 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2854 sprintf(register_name, "%s", glsl_variable);
2855 else
2856 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2859 /** Writes the GLSL variable name that corresponds to the register that the
2860 * DX opcode parameter is trying to access */
2861 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2862 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2863 const struct wined3d_shader_context *ctx)
2865 /* oPos, oFog and oPts in D3D */
2866 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2868 const struct wined3d_shader *shader = ctx->shader;
2869 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2870 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2871 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2872 const char *prefix = shader_glsl_get_prefix(version->type);
2873 struct glsl_src_param rel_param0, rel_param1;
2874 char imm_str[4][17];
2876 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2877 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2878 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2879 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2880 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2881 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2882 *is_color = FALSE;
2884 switch (reg->type)
2886 case WINED3DSPR_TEMP:
2887 sprintf(register_name, "R%u", reg->idx[0].offset);
2888 break;
2890 case WINED3DSPR_INPUT:
2891 case WINED3DSPR_INCONTROLPOINT:
2892 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2894 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2896 if (reg->idx[0].rel_addr)
2897 FIXME("VS3 input registers relative addressing.\n");
2898 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2899 *is_color = TRUE;
2900 if (reg->idx[0].rel_addr)
2902 sprintf(register_name, "%s_in[%s + %u]",
2903 prefix, rel_param0.param_str, reg->idx[0].offset);
2905 else
2907 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2909 break;
2912 if (version->type == WINED3D_SHADER_TYPE_HULL
2913 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2914 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2916 if (reg->idx[0].rel_addr)
2918 if (reg->idx[1].rel_addr)
2919 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2920 rel_param0.param_str, reg->idx[0].offset,
2921 rel_param1.param_str, reg->idx[1].offset);
2922 else
2923 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2924 rel_param0.param_str, reg->idx[0].offset,
2925 reg->idx[1].offset);
2927 else if (reg->idx[1].rel_addr)
2928 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2929 rel_param1.param_str, reg->idx[1].offset);
2930 else
2931 sprintf(register_name, "shader_in[%u].reg[%u]",
2932 reg->idx[0].offset, reg->idx[1].offset);
2933 break;
2936 /* pixel shaders >= 3.0 */
2937 if (version->major >= 3)
2939 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2940 unsigned int in_count = vec4_varyings(version->major, gl_info);
2942 if (reg->idx[0].rel_addr)
2944 /* Removing a + 0 would be an obvious optimization, but
2945 * OS X doesn't see the NOP operation there. */
2946 if (idx)
2948 if (needs_legacy_glsl_syntax(gl_info)
2949 && shader->u.ps.declared_in_count > in_count)
2951 sprintf(register_name,
2952 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2953 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2954 prefix, rel_param0.param_str, idx);
2956 else
2958 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2961 else
2963 if (needs_legacy_glsl_syntax(gl_info)
2964 && shader->u.ps.declared_in_count > in_count)
2966 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2967 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2968 prefix, rel_param0.param_str);
2970 else
2972 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2976 else
2978 if (idx == in_count) sprintf(register_name, "gl_Color");
2979 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2980 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2983 else
2985 if (!reg->idx[0].offset)
2986 strcpy(register_name, "ffp_varying_diffuse");
2987 else
2988 strcpy(register_name, "ffp_varying_specular");
2989 break;
2991 break;
2993 case WINED3DSPR_CONST:
2995 /* Relative addressing */
2996 if (reg->idx[0].rel_addr)
2998 if (wined3d_settings.check_float_constants)
2999 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
3000 rel_param0.param_str, reg->idx[0].offset,
3001 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
3002 prefix, rel_param0.param_str, reg->idx[0].offset);
3003 else if (reg->idx[0].offset)
3004 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3005 else
3006 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
3008 else
3010 if (shader_constant_is_local(shader, reg->idx[0].offset))
3011 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
3012 else
3013 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
3016 break;
3018 case WINED3DSPR_CONSTINT:
3019 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
3020 break;
3022 case WINED3DSPR_CONSTBOOL:
3023 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
3024 break;
3026 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
3027 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
3028 sprintf(register_name, "T%u", reg->idx[0].offset);
3029 else
3030 sprintf(register_name, "A%u", reg->idx[0].offset);
3031 break;
3033 case WINED3DSPR_LOOP:
3034 sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
3035 break;
3037 case WINED3DSPR_SAMPLER:
3038 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
3039 break;
3041 case WINED3DSPR_COLOROUT:
3042 if (reg->idx[0].offset >= gl_info->limits.buffers)
3043 WARN("Write to render target %u, only %d supported.\n",
3044 reg->idx[0].offset, gl_info->limits.buffers);
3046 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
3047 break;
3049 case WINED3DSPR_RASTOUT:
3050 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
3051 break;
3053 case WINED3DSPR_DEPTHOUT:
3054 case WINED3DSPR_DEPTHOUTGE:
3055 case WINED3DSPR_DEPTHOUTLE:
3056 sprintf(register_name, "gl_FragDepth");
3057 break;
3059 case WINED3DSPR_ATTROUT:
3060 if (!reg->idx[0].offset)
3061 sprintf(register_name, "%s_out[8]", prefix);
3062 else
3063 sprintf(register_name, "%s_out[9]", prefix);
3064 break;
3066 case WINED3DSPR_TEXCRDOUT:
3067 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
3068 if (reg->idx[0].rel_addr)
3069 sprintf(register_name, "%s_out[%s + %u]",
3070 prefix, rel_param0.param_str, reg->idx[0].offset);
3071 else
3072 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3073 break;
3075 case WINED3DSPR_MISCTYPE:
3076 if (!reg->idx[0].offset)
3078 /* vPos */
3079 sprintf(register_name, "vpos");
3081 else if (reg->idx[0].offset == 1)
3083 /* Note that gl_FrontFacing is a bool, while vFace is
3084 * a float for which the sign determines front/back */
3085 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3087 else
3089 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3090 sprintf(register_name, "unrecognized_register");
3092 break;
3094 case WINED3DSPR_IMMCONST:
3095 switch (reg->immconst_type)
3097 case WINED3D_IMMCONST_SCALAR:
3098 switch (data_type)
3100 case WINED3D_DATA_UNORM:
3101 case WINED3D_DATA_SNORM:
3102 case WINED3D_DATA_FLOAT:
3103 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3104 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3105 else
3106 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3107 break;
3108 case WINED3D_DATA_INT:
3109 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3110 break;
3111 case WINED3D_DATA_RESOURCE:
3112 case WINED3D_DATA_SAMPLER:
3113 case WINED3D_DATA_UINT:
3114 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3115 break;
3116 default:
3117 sprintf(register_name, "<unhandled data type %#x>", data_type);
3118 break;
3120 break;
3122 case WINED3D_IMMCONST_VEC4:
3123 switch (data_type)
3125 case WINED3D_DATA_UNORM:
3126 case WINED3D_DATA_SNORM:
3127 case WINED3D_DATA_FLOAT:
3128 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3130 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3131 reg->u.immconst_data[0], reg->u.immconst_data[1],
3132 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3134 else
3136 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3137 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3138 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3139 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3140 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3141 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3143 break;
3144 case WINED3D_DATA_INT:
3145 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3146 reg->u.immconst_data[0], reg->u.immconst_data[1],
3147 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3148 break;
3149 case WINED3D_DATA_RESOURCE:
3150 case WINED3D_DATA_SAMPLER:
3151 case WINED3D_DATA_UINT:
3152 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3153 reg->u.immconst_data[0], reg->u.immconst_data[1],
3154 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3155 break;
3156 default:
3157 sprintf(register_name, "<unhandled data type %#x>", data_type);
3158 break;
3160 break;
3162 default:
3163 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3164 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3166 break;
3168 case WINED3DSPR_CONSTBUFFER:
3169 if (reg->idx[1].rel_addr)
3170 sprintf(register_name, "%s_cb%u[%s + %u]",
3171 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3172 else
3173 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3174 break;
3176 case WINED3DSPR_IMMCONSTBUFFER:
3177 if (reg->idx[0].rel_addr)
3178 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3179 else
3180 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3181 break;
3183 case WINED3DSPR_PRIMID:
3184 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3185 sprintf(register_name, "gl_PrimitiveIDIn");
3186 else
3187 sprintf(register_name, "gl_PrimitiveID");
3188 break;
3190 case WINED3DSPR_IDXTEMP:
3191 if (reg->idx[1].rel_addr)
3192 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3193 else
3194 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3195 break;
3197 case WINED3DSPR_LOCALTHREADINDEX:
3198 shader_glsl_fixup_scalar_register_variable(register_name,
3199 "int(gl_LocalInvocationIndex)", gl_info);
3200 break;
3202 case WINED3DSPR_GSINSTID:
3203 case WINED3DSPR_OUTPOINTID:
3204 shader_glsl_fixup_scalar_register_variable(register_name,
3205 "gl_InvocationID", gl_info);
3206 break;
3208 case WINED3DSPR_THREADID:
3209 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3210 break;
3212 case WINED3DSPR_THREADGROUPID:
3213 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3214 break;
3216 case WINED3DSPR_LOCALTHREADID:
3217 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3218 break;
3220 case WINED3DSPR_FORKINSTID:
3221 case WINED3DSPR_JOININSTID:
3222 shader_glsl_fixup_scalar_register_variable(register_name,
3223 "phase_instance_id", gl_info);
3224 break;
3226 case WINED3DSPR_TESSCOORD:
3227 sprintf(register_name, "gl_TessCoord");
3228 break;
3230 case WINED3DSPR_OUTCONTROLPOINT:
3231 if (reg->idx[0].rel_addr)
3233 if (reg->idx[1].rel_addr)
3234 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3235 rel_param0.param_str, reg->idx[0].offset,
3236 rel_param1.param_str, reg->idx[1].offset);
3237 else
3238 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3239 rel_param0.param_str, reg->idx[0].offset,
3240 reg->idx[1].offset);
3242 else if (reg->idx[1].rel_addr)
3244 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3245 reg->idx[0].offset, rel_param1.param_str,
3246 reg->idx[1].offset);
3248 else
3250 sprintf(register_name, "shader_out[%u].reg[%u]",
3251 reg->idx[0].offset, reg->idx[1].offset);
3253 break;
3255 case WINED3DSPR_PATCHCONST:
3256 if (version->type == WINED3D_SHADER_TYPE_HULL)
3257 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3258 else
3259 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3260 break;
3262 case WINED3DSPR_SAMPLEMASK:
3263 sprintf(register_name, "sample_mask");
3264 break;
3266 default:
3267 FIXME("Unhandled register type %#x.\n", reg->type);
3268 sprintf(register_name, "unrecognized_register");
3269 break;
3273 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3275 *str++ = '.';
3276 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3277 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3278 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3279 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3280 *str = '\0';
3283 /* Get the GLSL write mask for the destination register */
3284 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3286 DWORD mask = param->write_mask;
3288 if (shader_is_scalar(&param->reg))
3290 mask = WINED3DSP_WRITEMASK_0;
3291 *write_mask = '\0';
3293 else
3295 shader_glsl_write_mask_to_str(mask, write_mask);
3298 return mask;
3301 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3303 unsigned int size = 0;
3305 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3306 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3307 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3308 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3310 return size;
3313 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3314 unsigned int component_idx)
3316 /* swizzle bits fields: wwzzyyxx */
3317 return (swizzle >> (2 * component_idx)) & 0x3;
3320 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3322 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3323 * but addressed as "rgba". To fix this we need to swap the register's x
3324 * and z components. */
3325 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3326 unsigned int i;
3328 *str++ = '.';
3329 for (i = 0; i < 4; ++i)
3331 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3332 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3334 *str = '\0';
3337 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3338 BOOL fixup, DWORD mask, char *swizzle_str)
3340 if (shader_is_scalar(&param->reg))
3341 *swizzle_str = '\0';
3342 else
3343 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3346 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3347 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3349 if (dst_data_type == src_data_type)
3351 string_buffer_sprintf(dst_param, "%s", src_param);
3352 return;
3355 if (src_data_type == WINED3D_DATA_FLOAT)
3357 switch (dst_data_type)
3359 case WINED3D_DATA_INT:
3360 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3361 return;
3362 case WINED3D_DATA_RESOURCE:
3363 case WINED3D_DATA_SAMPLER:
3364 case WINED3D_DATA_UINT:
3365 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3366 return;
3367 default:
3368 break;
3372 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3374 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3375 return;
3378 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3380 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3381 return;
3384 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3385 string_buffer_sprintf(dst_param, "%s", src_param);
3388 /* From a given parameter token, generate the corresponding GLSL string.
3389 * Also, return the actual register name and swizzle in case the
3390 * caller needs this information as well. */
3391 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3392 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3393 enum wined3d_data_type data_type)
3395 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3396 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3397 enum wined3d_data_type param_data_type;
3398 BOOL is_color = FALSE;
3399 char swizzle_str[6];
3401 glsl_src->reg_name[0] = '\0';
3402 glsl_src->param_str[0] = '\0';
3403 swizzle_str[0] = '\0';
3405 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ctx);
3406 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3408 switch (wined3d_src->reg.type)
3410 case WINED3DSPR_IMMCONST:
3411 param_data_type = data_type;
3412 break;
3413 case WINED3DSPR_FORKINSTID:
3414 case WINED3DSPR_GSINSTID:
3415 case WINED3DSPR_JOININSTID:
3416 case WINED3DSPR_LOCALTHREADID:
3417 case WINED3DSPR_LOCALTHREADINDEX:
3418 case WINED3DSPR_OUTPOINTID:
3419 case WINED3DSPR_PRIMID:
3420 case WINED3DSPR_THREADGROUPID:
3421 case WINED3DSPR_THREADID:
3422 param_data_type = WINED3D_DATA_INT;
3423 break;
3424 default:
3425 param_data_type = WINED3D_DATA_FLOAT;
3426 break;
3429 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3430 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3432 string_buffer_release(priv->string_buffers, reg_name);
3435 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3436 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3438 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3441 /* From a given parameter token, generate the corresponding GLSL string.
3442 * Also, return the actual register name and swizzle in case the
3443 * caller needs this information as well. */
3444 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3445 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3447 BOOL is_color = FALSE;
3449 glsl_dst->mask_str[0] = '\0';
3450 glsl_dst->reg_name[0] = '\0';
3452 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3453 glsl_dst->reg_name, &is_color, ins->ctx);
3454 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3457 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3458 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3459 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3460 enum wined3d_data_type data_type)
3462 struct glsl_dst_param glsl_dst;
3463 DWORD mask;
3465 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3467 switch (data_type)
3469 case WINED3D_DATA_FLOAT:
3470 shader_addline(buffer, "%s%s = %s(",
3471 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3472 break;
3473 case WINED3D_DATA_INT:
3474 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3475 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3476 break;
3477 case WINED3D_DATA_RESOURCE:
3478 case WINED3D_DATA_SAMPLER:
3479 case WINED3D_DATA_UINT:
3480 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3481 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3482 break;
3483 default:
3484 FIXME("Unhandled data type %#x.\n", data_type);
3485 shader_addline(buffer, "%s%s = %s(",
3486 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3487 break;
3491 return mask;
3494 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3495 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3497 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3500 /** Process GLSL instruction modifiers */
3501 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3503 struct glsl_dst_param dst_param;
3504 DWORD modifiers;
3506 if (!ins->dst_count) return;
3508 modifiers = ins->dst[0].modifiers;
3509 if (!modifiers) return;
3511 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3513 if (modifiers & WINED3DSPDM_SATURATE)
3515 /* _SAT means to clamp the value of the register to between 0 and 1 */
3516 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3517 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3520 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3522 FIXME("_centroid modifier not handled\n");
3525 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3527 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3531 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3533 switch (op)
3535 case WINED3D_SHADER_REL_OP_GT: return ">";
3536 case WINED3D_SHADER_REL_OP_EQ: return "==";
3537 case WINED3D_SHADER_REL_OP_GE: return ">=";
3538 case WINED3D_SHADER_REL_OP_LT: return "<";
3539 case WINED3D_SHADER_REL_OP_NE: return "!=";
3540 case WINED3D_SHADER_REL_OP_LE: return "<=";
3541 default:
3542 FIXME("Unrecognized operator %#x.\n", op);
3543 return "(\?\?)";
3547 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3549 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3552 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3553 unsigned int *coord_size, unsigned int *deriv_size)
3555 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3556 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3558 *coord_size = resource_type_info[resource_type].coord_size;
3559 *deriv_size = *coord_size;
3560 if (is_array)
3561 --(*deriv_size);
3564 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3565 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3567 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3568 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3569 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3570 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3571 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3572 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3573 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3574 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3575 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3576 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3577 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3578 const char *base = "texture", *type_part = "", *suffix = "";
3579 unsigned int coord_size, deriv_size;
3581 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3583 if (resource_type >= ARRAY_SIZE(resource_type_info))
3585 ERR("Unexpected resource type %#x.\n", resource_type);
3586 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3589 /* Note that there's no such thing as a projected cube texture. */
3590 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3591 projected = FALSE;
3593 if (needs_legacy_glsl_syntax(gl_info))
3595 if (shadow)
3596 base = "shadow";
3598 type_part = resource_type_info[resource_type].type_part;
3599 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3600 type_part = "2DRect";
3601 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3602 FIXME("Unhandled resource type %#x.\n", resource_type);
3604 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3606 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3607 suffix = "ARB";
3608 else
3609 FIXME("Unsupported grad function.\n");
3613 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3615 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3616 if (flags & ~texel_fetch_flags)
3617 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3619 base = "texelFetch";
3620 type_part = "";
3623 sample_function->name = string_buffer_get(priv->string_buffers);
3624 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3625 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3627 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3628 if (shadow)
3629 ++coord_size;
3630 sample_function->offset_size = offset ? deriv_size : 0;
3631 sample_function->coord_mask = (1u << coord_size) - 1;
3632 sample_function->deriv_mask = (1u << deriv_size) - 1;
3633 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3636 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3637 struct glsl_sample_function *sample_function)
3639 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3641 string_buffer_release(priv->string_buffers, sample_function->name);
3644 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3645 BOOL sign_fixup, enum fixup_channel_source channel_source)
3647 switch(channel_source)
3649 case CHANNEL_SOURCE_ZERO:
3650 strcat(arguments, "0.0");
3651 break;
3653 case CHANNEL_SOURCE_ONE:
3654 strcat(arguments, "1.0");
3655 break;
3657 case CHANNEL_SOURCE_X:
3658 strcat(arguments, reg_name);
3659 strcat(arguments, ".x");
3660 break;
3662 case CHANNEL_SOURCE_Y:
3663 strcat(arguments, reg_name);
3664 strcat(arguments, ".y");
3665 break;
3667 case CHANNEL_SOURCE_Z:
3668 strcat(arguments, reg_name);
3669 strcat(arguments, ".z");
3670 break;
3672 case CHANNEL_SOURCE_W:
3673 strcat(arguments, reg_name);
3674 strcat(arguments, ".w");
3675 break;
3677 default:
3678 FIXME("Unhandled channel source %#x\n", channel_source);
3679 strcat(arguments, "undefined");
3680 break;
3683 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3686 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3687 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3689 unsigned int mask_size, remaining;
3690 DWORD fixup_mask = 0;
3691 char arguments[256];
3692 char mask_str[6];
3694 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3695 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3696 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3697 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3698 if (!(mask &= fixup_mask))
3699 return;
3701 if (is_complex_fixup(fixup))
3703 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3704 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3705 return;
3708 shader_glsl_write_mask_to_str(mask, mask_str);
3709 mask_size = shader_glsl_get_write_mask_size(mask);
3711 arguments[0] = '\0';
3712 remaining = mask_size;
3713 if (mask & WINED3DSP_WRITEMASK_0)
3715 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3716 if (--remaining) strcat(arguments, ", ");
3718 if (mask & WINED3DSP_WRITEMASK_1)
3720 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3721 if (--remaining) strcat(arguments, ", ");
3723 if (mask & WINED3DSP_WRITEMASK_2)
3725 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3726 if (--remaining) strcat(arguments, ", ");
3728 if (mask & WINED3DSP_WRITEMASK_3)
3730 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3731 if (--remaining) strcat(arguments, ", ");
3734 if (mask_size > 1)
3735 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3736 else
3737 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3740 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3742 char reg_name[256];
3743 BOOL is_color;
3745 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins->ctx);
3746 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3749 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3750 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3751 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3752 const char *coord_reg_fmt, ...)
3754 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3755 char dst_swizzle[6];
3756 struct color_fixup_desc fixup;
3757 BOOL np2_fixup = FALSE;
3758 va_list args;
3759 int ret;
3761 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3763 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3764 * We actually rely on it for vertex shaders and SM4+. */
3765 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3767 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3768 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3770 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3771 np2_fixup = TRUE;
3773 else
3775 fixup = COLOR_FIXUP_IDENTITY;
3778 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3780 if (sample_function->output_single_component)
3781 shader_addline(ins->ctx->buffer, "vec4(");
3783 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3784 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3786 for (;;)
3788 va_start(args, coord_reg_fmt);
3789 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3790 va_end(args);
3791 if (!ret)
3792 break;
3793 if (!string_buffer_resize(ins->ctx->buffer, ret))
3794 break;
3797 if (np2_fixup)
3799 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3800 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3802 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3804 case 1:
3805 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3806 idx >> 1, (idx % 2) ? "z" : "x");
3807 break;
3808 case 2:
3809 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3810 idx >> 1, (idx % 2) ? "zw" : "xy");
3811 break;
3812 case 3:
3813 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3814 idx >> 1, (idx % 2) ? "zw" : "xy");
3815 break;
3816 case 4:
3817 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3818 idx >> 1, (idx % 2) ? "zw" : "xy");
3819 break;
3822 if (dx && dy)
3823 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3824 else if (bias)
3825 shader_addline(ins->ctx->buffer, ", %s", bias);
3826 if (sample_function->offset_size)
3828 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3829 shader_addline(ins->ctx->buffer, ", ");
3830 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3832 shader_addline(ins->ctx->buffer, ")");
3834 if (sample_function->output_single_component)
3835 shader_addline(ins->ctx->buffer, ")");
3837 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3839 if (!is_identity_fixup(fixup))
3840 shader_glsl_color_correction(ins, fixup);
3843 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3845 /* Write the final position.
3847 * OpenGL coordinates specify the center of the pixel while D3D coords
3848 * specify the corner. The offsets are stored in z and w in
3849 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3850 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3851 * a MAD. */
3852 if (use_viewport_index)
3854 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3855 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3857 else
3859 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3860 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3863 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3864 * in utils.c
3866 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3867 * shaders are run before the homogeneous divide, so we have to take the w
3868 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3869 * z = z * 2 - w. */
3870 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3873 /*****************************************************************************
3874 * Begin processing individual instruction opcodes
3875 ****************************************************************************/
3877 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3879 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3880 struct glsl_src_param src0_param;
3881 struct glsl_src_param src1_param;
3882 DWORD write_mask;
3883 const char *op;
3885 /* Determine the GLSL operator to use based on the opcode */
3886 switch (ins->handler_idx)
3888 case WINED3DSIH_ADD: op = "+"; break;
3889 case WINED3DSIH_AND: op = "&"; break;
3890 case WINED3DSIH_DIV: op = "/"; break;
3891 case WINED3DSIH_IADD: op = "+"; break;
3892 case WINED3DSIH_ISHL: op = "<<"; break;
3893 case WINED3DSIH_ISHR: op = ">>"; break;
3894 case WINED3DSIH_MUL: op = "*"; break;
3895 case WINED3DSIH_OR: op = "|"; break;
3896 case WINED3DSIH_SUB: op = "-"; break;
3897 case WINED3DSIH_USHR: op = ">>"; break;
3898 case WINED3DSIH_XOR: op = "^"; break;
3899 default:
3900 op = "<unhandled operator>";
3901 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3902 break;
3905 write_mask = shader_glsl_append_dst(buffer, ins);
3906 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3907 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3908 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3911 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3913 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3914 struct glsl_src_param src0_param;
3915 struct glsl_src_param src1_param;
3916 unsigned int mask_size;
3917 DWORD write_mask;
3918 const char *op;
3920 write_mask = shader_glsl_append_dst(buffer, ins);
3921 mask_size = shader_glsl_get_write_mask_size(write_mask);
3922 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3923 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3925 if (mask_size > 1)
3927 switch (ins->handler_idx)
3929 case WINED3DSIH_EQ: op = "equal"; break;
3930 case WINED3DSIH_IEQ: op = "equal"; break;
3931 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3932 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3933 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3934 case WINED3DSIH_LT: op = "lessThan"; break;
3935 case WINED3DSIH_ILT: op = "lessThan"; break;
3936 case WINED3DSIH_ULT: op = "lessThan"; break;
3937 case WINED3DSIH_NE: op = "notEqual"; break;
3938 case WINED3DSIH_INE: op = "notEqual"; break;
3939 default:
3940 op = "<unhandled operator>";
3941 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3942 break;
3945 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3946 mask_size, op, src0_param.param_str, src1_param.param_str);
3948 else
3950 switch (ins->handler_idx)
3952 case WINED3DSIH_EQ: op = "=="; break;
3953 case WINED3DSIH_IEQ: op = "=="; break;
3954 case WINED3DSIH_GE: op = ">="; break;
3955 case WINED3DSIH_IGE: op = ">="; break;
3956 case WINED3DSIH_UGE: op = ">="; break;
3957 case WINED3DSIH_LT: op = "<"; break;
3958 case WINED3DSIH_ILT: op = "<"; break;
3959 case WINED3DSIH_ULT: op = "<"; break;
3960 case WINED3DSIH_NE: op = "!="; break;
3961 case WINED3DSIH_INE: op = "!="; break;
3962 default:
3963 op = "<unhandled operator>";
3964 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3965 break;
3968 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3969 src0_param.param_str, op, src1_param.param_str);
3973 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3975 struct glsl_src_param src_param;
3976 DWORD write_mask;
3977 const char *op;
3979 switch (ins->handler_idx)
3981 case WINED3DSIH_INEG: op = "-"; break;
3982 case WINED3DSIH_NOT: op = "~"; break;
3983 default:
3984 op = "<unhandled operator>";
3985 ERR("Unhandled opcode %s.\n",
3986 debug_d3dshaderinstructionhandler(ins->handler_idx));
3987 break;
3990 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3991 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3992 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3995 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3997 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3998 struct glsl_src_param src0_param;
3999 struct glsl_src_param src1_param;
4000 DWORD write_mask;
4002 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
4003 * If not, we can emulate it. */
4004 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4005 FIXME("64-bit integer multiplies not implemented.\n");
4007 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4009 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4010 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4011 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4013 shader_addline(ins->ctx->buffer, "%s * %s);\n",
4014 src0_param.param_str, src1_param.param_str);
4018 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
4020 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4021 struct glsl_src_param src0_param, src1_param;
4022 DWORD write_mask;
4024 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4026 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4028 char dst_mask[6];
4030 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4031 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4032 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4033 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
4034 dst_mask, src0_param.param_str, src1_param.param_str);
4036 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4037 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4038 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4039 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4041 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4042 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4044 else
4046 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4047 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4048 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4049 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
4052 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4054 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4055 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4056 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4057 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4061 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
4062 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
4064 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4065 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4066 struct glsl_src_param src0_param;
4067 DWORD write_mask;
4069 write_mask = shader_glsl_append_dst(buffer, ins);
4070 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4072 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
4073 * shader versions WINED3DSIO_MOVA is used for this. */
4074 if (ins->ctx->reg_maps->shader_version.major == 1
4075 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
4076 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
4078 /* This is a simple floor() */
4079 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4080 if (mask_size > 1) {
4081 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4082 } else {
4083 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4086 else if (ins->handler_idx == WINED3DSIH_MOVA)
4088 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4090 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4092 if (mask_size > 1)
4093 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4094 else
4095 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4097 else
4099 if (mask_size > 1)
4100 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4101 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4102 else
4103 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4104 src0_param.param_str, src0_param.param_str);
4107 else
4109 shader_addline(buffer, "%s);\n", src0_param.param_str);
4113 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4114 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4116 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4117 struct glsl_src_param src0_param;
4118 struct glsl_src_param src1_param;
4119 DWORD dst_write_mask, src_write_mask;
4120 unsigned int dst_size;
4122 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4123 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4125 /* dp4 works on vec4, dp3 on vec3, etc. */
4126 if (ins->handler_idx == WINED3DSIH_DP4)
4127 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4128 else if (ins->handler_idx == WINED3DSIH_DP3)
4129 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4130 else
4131 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4133 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4134 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4136 if (dst_size > 1) {
4137 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4138 } else {
4139 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4143 /* Note that this instruction has some restrictions. The destination write mask
4144 * can't contain the w component, and the source swizzles have to be .xyzw */
4145 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4147 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4148 struct glsl_src_param src0_param;
4149 struct glsl_src_param src1_param;
4150 char dst_mask[6];
4152 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4153 shader_glsl_append_dst(ins->ctx->buffer, ins);
4154 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4155 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4156 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4159 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4161 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4163 if (!stream)
4164 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4165 else
4166 FIXME("Unhandled primitive stream %u.\n", stream);
4169 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4170 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4171 * GLSL uses the value as-is. */
4172 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4174 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4175 struct glsl_src_param src0_param;
4176 struct glsl_src_param src1_param;
4177 DWORD dst_write_mask;
4178 unsigned int dst_size;
4180 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4181 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4183 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4184 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4186 if (dst_size > 1)
4188 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4189 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4191 else
4193 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4194 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4198 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4199 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4201 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4202 struct glsl_src_param src_param;
4203 const char *instruction;
4204 DWORD write_mask;
4205 unsigned i;
4207 /* Determine the GLSL function to use based on the opcode */
4208 /* TODO: Possibly make this a table for faster lookups */
4209 switch (ins->handler_idx)
4211 case WINED3DSIH_ABS: instruction = "abs"; break;
4212 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4213 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4214 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4215 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4216 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4217 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4218 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4219 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4220 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4221 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4222 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4223 case WINED3DSIH_FRC: instruction = "fract"; break;
4224 case WINED3DSIH_IMAX: instruction = "max"; break;
4225 case WINED3DSIH_IMIN: instruction = "min"; break;
4226 case WINED3DSIH_MAX: instruction = "max"; break;
4227 case WINED3DSIH_MIN: instruction = "min"; break;
4228 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4229 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4230 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4231 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4232 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4233 case WINED3DSIH_UMAX: instruction = "max"; break;
4234 case WINED3DSIH_UMIN: instruction = "min"; break;
4235 default: instruction = "";
4236 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4237 break;
4240 write_mask = shader_glsl_append_dst(buffer, ins);
4242 /* In D3D bits are numbered from the most significant bit. */
4243 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4244 shader_addline(buffer, "31 - ");
4245 shader_addline(buffer, "%s(", instruction);
4247 if (ins->src_count)
4249 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4250 shader_addline(buffer, "%s", src_param.param_str);
4251 for (i = 1; i < ins->src_count; ++i)
4253 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4254 shader_addline(buffer, ", %s", src_param.param_str);
4258 shader_addline(buffer, "));\n");
4261 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4263 struct wined3d_shader_dst_param dst;
4264 struct glsl_src_param src;
4265 DWORD write_mask;
4266 const char *fmt;
4267 unsigned int i;
4269 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4270 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4272 dst = ins->dst[0];
4273 for (i = 0; i < 4; ++i)
4275 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4276 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4277 &dst, dst.reg.data_type)))
4278 continue;
4280 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4281 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4285 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4287 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4288 struct wined3d_shader_dst_param dst;
4289 struct glsl_src_param src[4];
4290 const char *instruction;
4291 BOOL tmp_dst = FALSE;
4292 char mask_char[6];
4293 unsigned int i, j;
4294 DWORD write_mask;
4296 switch (ins->handler_idx)
4298 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4299 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4300 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4301 default:
4302 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4303 return;
4306 for (i = 0; i < ins->src_count; ++i)
4308 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4309 && ins->dst[0].reg.type == ins->src[i].reg.type)
4310 tmp_dst = TRUE;
4313 dst = ins->dst[0];
4314 for (i = 0; i < 4; ++i)
4316 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4317 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4318 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4319 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4320 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4321 continue;
4323 for (j = 0; j < ins->src_count; ++j)
4324 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4325 shader_addline(buffer, "%s(", instruction);
4326 for (j = 0; j < ins->src_count - 2; ++j)
4327 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4328 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4331 if (tmp_dst)
4333 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4334 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4335 shader_addline(buffer, "tmp0%s);\n", mask_char);
4339 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4341 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4343 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4344 struct glsl_src_param src_param;
4345 unsigned int mask_size;
4346 DWORD write_mask;
4347 char dst_mask[6];
4349 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4350 mask_size = shader_glsl_get_write_mask_size(write_mask);
4351 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4353 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4354 src_param.param_str, src_param.param_str);
4355 shader_glsl_append_dst(buffer, ins);
4357 if (mask_size > 1)
4359 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4360 mask_size, src_param.param_str);
4362 else
4364 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4365 src_param.param_str);
4369 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4371 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4372 ins->ctx->reg_maps->shader_version.minor);
4373 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4374 struct glsl_src_param src0_param;
4375 const char *prefix, *suffix;
4376 unsigned int dst_size;
4377 DWORD dst_write_mask;
4379 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4380 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4382 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4383 dst_write_mask = WINED3DSP_WRITEMASK_3;
4385 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4387 switch (ins->handler_idx)
4389 case WINED3DSIH_EXP:
4390 case WINED3DSIH_EXPP:
4391 prefix = "exp2(";
4392 suffix = ")";
4393 break;
4395 case WINED3DSIH_LOG:
4396 case WINED3DSIH_LOGP:
4397 prefix = "log2(abs(";
4398 suffix = "))";
4399 break;
4401 case WINED3DSIH_RCP:
4402 prefix = "1.0 / ";
4403 suffix = "";
4404 break;
4406 case WINED3DSIH_RSQ:
4407 prefix = "inversesqrt(abs(";
4408 suffix = "))";
4409 break;
4411 default:
4412 prefix = "";
4413 suffix = "";
4414 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4415 break;
4418 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4419 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4420 else
4421 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4424 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4425 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4426 * dst.x = 2^(floor(src))
4427 * dst.y = src - floor(src)
4428 * dst.z = 2^src (partial precision is allowed, but optional)
4429 * dst.w = 1.0;
4430 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4431 * dst = 2^src; (partial precision is allowed, but optional)
4433 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4435 if (ins->ctx->reg_maps->shader_version.major < 2)
4437 struct glsl_src_param src_param;
4438 char dst_mask[6];
4440 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4442 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4443 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4444 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4445 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4447 shader_glsl_append_dst(ins->ctx->buffer, ins);
4448 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4449 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4450 return;
4453 shader_glsl_scalar_op(ins);
4456 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4457 const char *vector_constructor, const char *scalar_constructor)
4459 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4460 struct glsl_src_param src_param;
4461 unsigned int mask_size;
4462 DWORD write_mask;
4464 write_mask = shader_glsl_append_dst(buffer, ins);
4465 mask_size = shader_glsl_get_write_mask_size(write_mask);
4466 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4468 if (mask_size > 1)
4469 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4470 else
4471 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4474 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4476 shader_glsl_cast(ins, "ivec", "int");
4479 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4481 shader_glsl_cast(ins, "uvec", "uint");
4484 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4486 shader_glsl_cast(ins, "vec", "float");
4489 /** Process signed comparison opcodes in GLSL. */
4490 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4492 struct glsl_src_param src0_param;
4493 struct glsl_src_param src1_param;
4494 DWORD write_mask;
4495 unsigned int mask_size;
4497 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4498 mask_size = shader_glsl_get_write_mask_size(write_mask);
4499 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4500 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4502 if (mask_size > 1) {
4503 const char *compare;
4505 switch(ins->handler_idx)
4507 case WINED3DSIH_SLT: compare = "lessThan"; break;
4508 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4509 default: compare = "";
4510 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4513 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4514 src0_param.param_str, src1_param.param_str);
4515 } else {
4516 switch(ins->handler_idx)
4518 case WINED3DSIH_SLT:
4519 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4520 * to return 0.0 but step returns 1.0 because step is not < x
4521 * An alternative is a bvec compare padded with an unused second component.
4522 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4523 * issue. Playing with not() is not possible either because not() does not accept
4524 * a scalar.
4526 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4527 src0_param.param_str, src1_param.param_str);
4528 break;
4529 case WINED3DSIH_SGE:
4530 /* Here we can use the step() function and safe a conditional */
4531 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4532 break;
4533 default:
4534 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4540 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4542 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4543 struct wined3d_shader_dst_param dst[2];
4544 struct glsl_src_param src[3];
4545 unsigned int i, j, k;
4546 char mask_char[6];
4547 DWORD write_mask;
4548 BOOL tmp_dst[2];
4550 for (i = 0; i < ins->dst_count; ++i)
4552 tmp_dst[i] = FALSE;
4553 for (j = 0; j < ins->src_count; ++j)
4555 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4556 && ins->dst[i].reg.type == ins->src[j].reg.type)
4557 tmp_dst[i] = TRUE;
4561 dst[0] = ins->dst[0];
4562 dst[1] = ins->dst[1];
4563 for (i = 0; i < 4; ++i)
4565 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4567 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4568 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4569 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4570 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4571 continue;
4573 for (k = 0; k < ARRAY_SIZE(src); ++k)
4574 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4576 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4577 src[0].param_str, src[1].param_str, src[2].param_str);
4581 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4583 if (tmp_dst[i])
4585 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4586 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4587 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4592 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4594 const char *condition_prefix, *condition_suffix;
4595 struct wined3d_shader_dst_param dst;
4596 struct glsl_src_param src0_param;
4597 struct glsl_src_param src1_param;
4598 struct glsl_src_param src2_param;
4599 BOOL temp_destination = FALSE;
4600 DWORD cmp_channel = 0;
4601 unsigned int i, j;
4602 char mask_char[6];
4603 DWORD write_mask;
4605 switch (ins->handler_idx)
4607 case WINED3DSIH_CMP:
4608 condition_prefix = "";
4609 condition_suffix = " >= 0.0";
4610 break;
4612 case WINED3DSIH_CND:
4613 condition_prefix = "";
4614 condition_suffix = " > 0.5";
4615 break;
4617 case WINED3DSIH_MOVC:
4618 condition_prefix = "bool(";
4619 condition_suffix = ")";
4620 break;
4622 default:
4623 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4624 condition_prefix = "<unhandled prefix>";
4625 condition_suffix = "<unhandled suffix>";
4626 break;
4629 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4631 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4632 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4633 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4634 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4636 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4637 condition_prefix, src0_param.param_str, condition_suffix,
4638 src1_param.param_str, src2_param.param_str);
4639 return;
4642 dst = ins->dst[0];
4644 /* Splitting the instruction up in multiple lines imposes a problem:
4645 * The first lines may overwrite source parameters of the following lines.
4646 * Deal with that by using a temporary destination register if needed. */
4647 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4648 && ins->src[0].reg.type == dst.reg.type)
4649 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4650 && ins->src[1].reg.type == dst.reg.type)
4651 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4652 && ins->src[2].reg.type == dst.reg.type))
4653 temp_destination = TRUE;
4655 /* Cycle through all source0 channels. */
4656 for (i = 0; i < 4; ++i)
4658 write_mask = 0;
4659 /* Find the destination channels which use the current source0 channel. */
4660 for (j = 0; j < 4; ++j)
4662 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4664 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4665 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4668 dst.write_mask = ins->dst[0].write_mask & write_mask;
4670 if (temp_destination)
4672 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4673 continue;
4674 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4676 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4677 continue;
4679 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4680 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4681 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4683 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4684 condition_prefix, src0_param.param_str, condition_suffix,
4685 src1_param.param_str, src2_param.param_str);
4688 if (temp_destination)
4690 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4691 shader_glsl_append_dst(ins->ctx->buffer, ins);
4692 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4696 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4697 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4698 * the compare is done per component of src0. */
4699 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4701 struct glsl_src_param src0_param;
4702 struct glsl_src_param src1_param;
4703 struct glsl_src_param src2_param;
4704 DWORD write_mask;
4705 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4706 ins->ctx->reg_maps->shader_version.minor);
4708 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4710 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4711 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4712 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4713 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4715 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4716 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4717 else
4718 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4719 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4720 return;
4723 shader_glsl_conditional_move(ins);
4726 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4727 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4729 struct glsl_src_param src0_param;
4730 struct glsl_src_param src1_param;
4731 struct glsl_src_param src2_param;
4732 DWORD write_mask;
4734 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4735 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4736 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4737 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4738 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4739 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4742 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4743 Vertex shaders to GLSL codes */
4744 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4746 int i;
4747 int nComponents = 0;
4748 struct wined3d_shader_dst_param tmp_dst = {{0}};
4749 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4750 struct wined3d_shader_instruction tmp_ins;
4752 memset(&tmp_ins, 0, sizeof(tmp_ins));
4754 /* Set constants for the temporary argument */
4755 tmp_ins.ctx = ins->ctx;
4756 tmp_ins.dst_count = 1;
4757 tmp_ins.dst = &tmp_dst;
4758 tmp_ins.src_count = 2;
4759 tmp_ins.src = tmp_src;
4761 switch(ins->handler_idx)
4763 case WINED3DSIH_M4x4:
4764 nComponents = 4;
4765 tmp_ins.handler_idx = WINED3DSIH_DP4;
4766 break;
4767 case WINED3DSIH_M4x3:
4768 nComponents = 3;
4769 tmp_ins.handler_idx = WINED3DSIH_DP4;
4770 break;
4771 case WINED3DSIH_M3x4:
4772 nComponents = 4;
4773 tmp_ins.handler_idx = WINED3DSIH_DP3;
4774 break;
4775 case WINED3DSIH_M3x3:
4776 nComponents = 3;
4777 tmp_ins.handler_idx = WINED3DSIH_DP3;
4778 break;
4779 case WINED3DSIH_M3x2:
4780 nComponents = 2;
4781 tmp_ins.handler_idx = WINED3DSIH_DP3;
4782 break;
4783 default:
4784 break;
4787 tmp_dst = ins->dst[0];
4788 tmp_src[0] = ins->src[0];
4789 tmp_src[1] = ins->src[1];
4790 for (i = 0; i < nComponents; ++i)
4792 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4793 shader_glsl_dot(&tmp_ins);
4794 ++tmp_src[1].reg.idx[0].offset;
4799 The LRP instruction performs a component-wise linear interpolation
4800 between the second and third operands using the first operand as the
4801 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4802 This is equivalent to mix(src2, src1, src0);
4804 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4806 struct glsl_src_param src0_param;
4807 struct glsl_src_param src1_param;
4808 struct glsl_src_param src2_param;
4809 DWORD write_mask;
4811 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4813 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4814 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4815 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4817 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4818 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4821 /** Process the WINED3DSIO_LIT instruction in GLSL:
4822 * dst.x = dst.w = 1.0
4823 * dst.y = (src0.x > 0) ? src0.x
4824 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4825 * where src.w is clamped at +- 128
4827 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4829 struct glsl_src_param src0_param;
4830 struct glsl_src_param src1_param;
4831 struct glsl_src_param src3_param;
4832 char dst_mask[6];
4834 shader_glsl_append_dst(ins->ctx->buffer, ins);
4835 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4837 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4838 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4839 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4841 /* The sdk specifies the instruction like this
4842 * dst.x = 1.0;
4843 * if(src.x > 0.0) dst.y = src.x
4844 * else dst.y = 0.0.
4845 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4846 * else dst.z = 0.0;
4847 * dst.w = 1.0;
4848 * (where power = src.w clamped between -128 and 128)
4850 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4851 * dst.x = 1.0 ... No further explanation needed
4852 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4853 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4854 * dst.w = 1.0. ... Nothing fancy.
4856 * So we still have one conditional in there. So do this:
4857 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4859 * 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),
4860 * 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.
4861 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4863 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4864 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4865 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4867 shader_addline(ins->ctx->buffer,
4868 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4869 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4870 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4871 src0_param.param_str, src3_param.param_str, dst_mask);
4874 /** Process the WINED3DSIO_DST instruction in GLSL:
4875 * dst.x = 1.0
4876 * dst.y = src0.x * src0.y
4877 * dst.z = src0.z
4878 * dst.w = src1.w
4880 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4882 struct glsl_src_param src0y_param;
4883 struct glsl_src_param src0z_param;
4884 struct glsl_src_param src1y_param;
4885 struct glsl_src_param src1w_param;
4886 char dst_mask[6];
4888 shader_glsl_append_dst(ins->ctx->buffer, ins);
4889 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4891 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4892 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4893 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4894 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4896 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4897 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4900 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4901 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4902 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4904 * dst.x = cos(src0.?)
4905 * dst.y = sin(src0.?)
4906 * dst.z = dst.z
4907 * dst.w = dst.w
4909 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4911 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4912 struct glsl_src_param src0_param;
4913 DWORD write_mask;
4915 if (ins->ctx->reg_maps->shader_version.major < 4)
4917 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4919 write_mask = shader_glsl_append_dst(buffer, ins);
4920 switch (write_mask)
4922 case WINED3DSP_WRITEMASK_0:
4923 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4924 break;
4926 case WINED3DSP_WRITEMASK_1:
4927 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4928 break;
4930 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4931 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4932 src0_param.param_str, src0_param.param_str);
4933 break;
4935 default:
4936 ERR("Write mask should be .x, .y or .xy\n");
4937 break;
4940 return;
4943 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4946 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4948 char dst_mask[6];
4950 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4951 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4952 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4954 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4955 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4956 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4958 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4959 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4961 else
4963 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4964 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4965 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4968 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4970 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4971 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4972 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4976 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4977 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4978 * generate invalid code
4980 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4982 struct glsl_src_param src0_param;
4983 DWORD write_mask;
4985 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4986 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4988 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4991 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4992 * Start a for() loop where src1.y is the initial value of aL,
4993 * increment aL by src1.z for a total of src1.x iterations.
4994 * Need to use a temporary variable for this operation.
4996 /* FIXME: I don't think nested loops will work correctly this way. */
4997 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4999 struct wined3d_shader_parser_state *state = ins->ctx->state;
5000 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5001 const struct wined3d_shader *shader = ins->ctx->shader;
5002 const struct wined3d_shader_lconst *constant;
5003 struct glsl_src_param src1_param;
5004 const DWORD *control_values = NULL;
5006 if (ins->ctx->reg_maps->shader_version.major < 4)
5008 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
5010 /* Try to hardcode the loop control parameters if possible. Direct3D 9
5011 * class hardware doesn't support real varying indexing, but Microsoft
5012 * designed this feature for Shader model 2.x+. If the loop control is
5013 * known at compile time, the GLSL compiler can unroll the loop, and
5014 * replace indirect addressing with direct addressing. */
5015 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
5017 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5019 if (constant->idx == ins->src[1].reg.idx[0].offset)
5021 control_values = constant->value;
5022 break;
5027 if (control_values)
5029 struct wined3d_shader_loop_control loop_control;
5030 loop_control.count = control_values[0];
5031 loop_control.start = control_values[1];
5032 loop_control.step = (int)control_values[2];
5034 if (loop_control.step > 0)
5036 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
5037 state->current_loop_depth, loop_control.start,
5038 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
5039 state->current_loop_depth, loop_control.step);
5041 else if (loop_control.step < 0)
5043 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
5044 state->current_loop_depth, loop_control.start,
5045 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
5046 state->current_loop_depth, loop_control.step);
5048 else
5050 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
5051 state->current_loop_depth, loop_control.start, state->current_loop_depth,
5052 state->current_loop_depth, loop_control.count,
5053 state->current_loop_depth);
5056 else
5058 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
5059 state->current_loop_depth, state->current_loop_reg,
5060 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
5061 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
5064 ++state->current_loop_reg;
5066 else
5068 shader_addline(buffer, "for (;;)\n{\n");
5071 ++state->current_loop_depth;
5074 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
5076 struct wined3d_shader_parser_state *state = ins->ctx->state;
5078 shader_addline(ins->ctx->buffer, "}\n");
5080 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5082 --state->current_loop_depth;
5083 --state->current_loop_reg;
5086 if (ins->handler_idx == WINED3DSIH_ENDREP)
5088 --state->current_loop_depth;
5092 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5094 struct wined3d_shader_parser_state *state = ins->ctx->state;
5095 const struct wined3d_shader *shader = ins->ctx->shader;
5096 const struct wined3d_shader_lconst *constant;
5097 struct glsl_src_param src0_param;
5098 const DWORD *control_values = NULL;
5100 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5101 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5103 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5105 if (constant->idx == ins->src[0].reg.idx[0].offset)
5107 control_values = constant->value;
5108 break;
5113 if (control_values)
5115 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5116 state->current_loop_depth, state->current_loop_depth,
5117 control_values[0], state->current_loop_depth);
5119 else
5121 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5122 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5123 state->current_loop_depth, state->current_loop_depth,
5124 src0_param.param_str, state->current_loop_depth);
5127 ++state->current_loop_depth;
5130 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5132 struct glsl_src_param src0_param;
5134 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5135 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5138 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5140 struct glsl_src_param src0_param;
5142 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5143 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5146 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5148 shader_addline(ins->ctx->buffer, "default:\n");
5151 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
5153 struct glsl_src_param src_param;
5154 const char *condition;
5156 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5157 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5158 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5161 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5163 shader_glsl_generate_condition(ins);
5164 shader_addline(ins->ctx->buffer, "{\n");
5167 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5169 struct glsl_src_param src0_param;
5170 struct glsl_src_param src1_param;
5172 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5173 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5175 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5176 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5179 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5181 shader_addline(ins->ctx->buffer, "} else {\n");
5184 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5186 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5187 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5189 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5190 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5191 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5193 if (!stream)
5194 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5195 else
5196 FIXME("Unhandled primitive stream %u.\n", stream);
5199 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5201 shader_addline(ins->ctx->buffer, "break;\n");
5204 /* FIXME: According to MSDN the compare is done per component. */
5205 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5207 struct glsl_src_param src0_param;
5208 struct glsl_src_param src1_param;
5210 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5211 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5213 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5214 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5217 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5219 const char *op;
5221 switch (ins->handler_idx)
5223 case WINED3DSIH_BREAKP:
5224 op = "break;";
5225 break;
5226 case WINED3DSIH_CONTINUEP:
5227 op = "continue;";
5228 break;
5229 case WINED3DSIH_RETP:
5230 op = "return;";
5231 break;
5232 default:
5233 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5234 return;
5237 shader_glsl_generate_condition(ins);
5238 if (ins->handler_idx == WINED3DSIH_RETP)
5240 shader_addline(ins->ctx->buffer, "{\n");
5241 shader_glsl_generate_shader_epilogue(ins->ctx);
5243 shader_addline(ins->ctx->buffer, " %s\n", op);
5244 if (ins->handler_idx == WINED3DSIH_RETP)
5245 shader_addline(ins->ctx->buffer, "}\n");
5248 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5250 shader_addline(ins->ctx->buffer, "continue;\n");
5253 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5255 shader_addline(ins->ctx->buffer, "}\n");
5256 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5258 /* Subroutines appear at the end of the shader. */
5259 ins->ctx->state->in_subroutine = TRUE;
5262 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5264 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5267 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5269 struct glsl_src_param src1_param;
5271 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5272 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5273 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5276 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5278 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5280 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5282 shader_glsl_generate_shader_epilogue(ins->ctx);
5283 shader_addline(ins->ctx->buffer, "return;\n");
5287 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5289 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5290 ins->ctx->reg_maps->shader_version.minor);
5291 struct glsl_sample_function sample_function;
5292 DWORD sample_flags = 0;
5293 DWORD resource_idx;
5294 DWORD mask = 0, swizzle;
5295 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5297 /* 1.0-1.4: Use destination register as sampler source.
5298 * 2.0+: Use provided sampler source. */
5299 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5300 resource_idx = ins->dst[0].reg.idx[0].offset;
5301 else
5302 resource_idx = ins->src[1].reg.idx[0].offset;
5304 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5306 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5307 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5308 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5310 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5311 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5313 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5314 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5316 case WINED3D_TTFF_COUNT1:
5317 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5318 break;
5319 case WINED3D_TTFF_COUNT2:
5320 mask = WINED3DSP_WRITEMASK_1;
5321 break;
5322 case WINED3D_TTFF_COUNT3:
5323 mask = WINED3DSP_WRITEMASK_2;
5324 break;
5325 case WINED3D_TTFF_COUNT4:
5326 case WINED3D_TTFF_DISABLE:
5327 mask = WINED3DSP_WRITEMASK_3;
5328 break;
5332 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5334 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5336 if (src_mod == WINED3DSPSM_DZ) {
5337 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5338 mask = WINED3DSP_WRITEMASK_2;
5339 } else if (src_mod == WINED3DSPSM_DW) {
5340 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5341 mask = WINED3DSP_WRITEMASK_3;
5344 else
5346 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5347 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5349 /* ps 2.0 texldp instruction always divides by the fourth component. */
5350 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5351 mask = WINED3DSP_WRITEMASK_3;
5355 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5356 mask |= sample_function.coord_mask;
5357 sample_function.coord_mask = mask;
5359 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5360 else swizzle = ins->src[1].swizzle;
5362 /* 1.0-1.3: Use destination register as coordinate source.
5363 1.4+: Use provided coordinate source register. */
5364 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5366 char coord_mask[6];
5367 shader_glsl_write_mask_to_str(mask, coord_mask);
5368 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5369 "T%u%s", resource_idx, coord_mask);
5371 else
5373 struct glsl_src_param coord_param;
5374 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5375 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5377 struct glsl_src_param bias;
5378 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5379 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5380 NULL, "%s", coord_param.param_str);
5381 } else {
5382 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5383 "%s", coord_param.param_str);
5386 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5389 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5391 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5392 struct glsl_src_param coord_param, dx_param, dy_param;
5393 struct glsl_sample_function sample_function;
5394 DWORD sampler_idx;
5395 DWORD swizzle = ins->src[1].swizzle;
5397 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5399 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5400 shader_glsl_tex(ins);
5401 return;
5404 sampler_idx = ins->src[1].reg.idx[0].offset;
5406 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5407 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5408 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5409 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5411 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5412 NULL, NULL, "%s", coord_param.param_str);
5413 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5416 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5418 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5419 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5420 struct glsl_src_param coord_param, lod_param;
5421 struct glsl_sample_function sample_function;
5422 DWORD swizzle = ins->src[1].swizzle;
5423 DWORD sampler_idx;
5425 sampler_idx = ins->src[1].reg.idx[0].offset;
5427 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5428 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5430 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5432 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5433 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5435 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5436 * However, the NVIDIA drivers allow them in fragment shaders as well,
5437 * even without the appropriate extension. */
5438 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5440 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5441 "%s", coord_param.param_str);
5442 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5445 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5446 unsigned int resource_idx, unsigned int sampler_idx)
5448 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5449 unsigned int i;
5451 for (i = 0; i < sampler_map->count; ++i)
5453 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5454 return entries[i].bind_idx;
5457 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5459 return ~0u;
5462 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5464 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5465 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5466 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5467 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5468 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5469 struct glsl_src_param structure_idx, offset, data, data2;
5470 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5471 enum wined3d_shader_resource_type resource_type;
5472 struct wined3d_string_buffer *address;
5473 enum wined3d_data_type data_type;
5474 unsigned int resource_idx, stride;
5475 const char *op, *resource;
5476 DWORD coord_mask;
5477 BOOL is_tgsm;
5479 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5480 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5481 if (is_tgsm)
5483 if (resource_idx >= reg_maps->tgsm_count)
5485 ERR("Invalid TGSM index %u.\n", resource_idx);
5486 return;
5488 resource = "g";
5489 data_type = WINED3D_DATA_UINT;
5490 coord_mask = 1;
5491 stride = reg_maps->tgsm[resource_idx].stride;
5493 else
5495 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5497 ERR("Invalid UAV index %u.\n", resource_idx);
5498 return;
5500 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5501 if (resource_type >= ARRAY_SIZE(resource_type_info))
5503 ERR("Unexpected resource type %#x.\n", resource_type);
5504 return;
5506 resource = "image";
5507 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5508 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5509 stride = reg_maps->uav_resource_info[resource_idx].stride;
5512 switch (ins->handler_idx)
5514 case WINED3DSIH_ATOMIC_AND:
5515 case WINED3DSIH_IMM_ATOMIC_AND:
5516 if (is_tgsm)
5517 op = "atomicAnd";
5518 else
5519 op = "imageAtomicAnd";
5520 break;
5521 case WINED3DSIH_ATOMIC_CMP_STORE:
5522 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5523 if (is_tgsm)
5524 op = "atomicCompSwap";
5525 else
5526 op = "imageAtomicCompSwap";
5527 break;
5528 case WINED3DSIH_ATOMIC_IADD:
5529 case WINED3DSIH_IMM_ATOMIC_IADD:
5530 if (is_tgsm)
5531 op = "atomicAdd";
5532 else
5533 op = "imageAtomicAdd";
5534 break;
5535 case WINED3DSIH_ATOMIC_IMAX:
5536 case WINED3DSIH_IMM_ATOMIC_IMAX:
5537 if (is_tgsm)
5538 op = "atomicMax";
5539 else
5540 op = "imageAtomicMax";
5541 if (data_type != WINED3D_DATA_INT)
5543 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5544 return;
5546 break;
5547 case WINED3DSIH_ATOMIC_IMIN:
5548 case WINED3DSIH_IMM_ATOMIC_IMIN:
5549 if (is_tgsm)
5550 op = "atomicMin";
5551 else
5552 op = "imageAtomicMin";
5553 if (data_type != WINED3D_DATA_INT)
5555 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5556 return;
5558 break;
5559 case WINED3DSIH_ATOMIC_OR:
5560 case WINED3DSIH_IMM_ATOMIC_OR:
5561 if (is_tgsm)
5562 op = "atomicOr";
5563 else
5564 op = "imageAtomicOr";
5565 break;
5566 case WINED3DSIH_ATOMIC_UMAX:
5567 case WINED3DSIH_IMM_ATOMIC_UMAX:
5568 if (is_tgsm)
5569 op = "atomicMax";
5570 else
5571 op = "imageAtomicMax";
5572 if (data_type != WINED3D_DATA_UINT)
5574 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5575 return;
5577 break;
5578 case WINED3DSIH_ATOMIC_UMIN:
5579 case WINED3DSIH_IMM_ATOMIC_UMIN:
5580 if (is_tgsm)
5581 op = "atomicMin";
5582 else
5583 op = "imageAtomicMin";
5584 if (data_type != WINED3D_DATA_UINT)
5586 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5587 return;
5589 break;
5590 case WINED3DSIH_ATOMIC_XOR:
5591 case WINED3DSIH_IMM_ATOMIC_XOR:
5592 if (is_tgsm)
5593 op = "atomicXor";
5594 else
5595 op = "imageAtomicXor";
5596 break;
5597 case WINED3DSIH_IMM_ATOMIC_EXCH:
5598 if (is_tgsm)
5599 op = "atomicExchange";
5600 else
5601 op = "imageAtomicExchange";
5602 break;
5603 default:
5604 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5605 return;
5608 address = string_buffer_get(priv->string_buffers);
5609 if (stride)
5611 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5612 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5613 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5615 else
5617 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5618 string_buffer_sprintf(address, "%s", offset.param_str);
5619 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5620 shader_addline(address, "/ 4");
5623 if (is_imm_instruction)
5624 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5626 if (is_tgsm)
5627 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5628 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5629 else
5630 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5631 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5633 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5634 shader_addline(buffer, "%s", data.param_str);
5635 if (ins->src_count >= 3)
5637 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5638 shader_addline(buffer, ", %s", data2.param_str);
5641 if (is_imm_instruction)
5642 shader_addline(buffer, ")");
5643 shader_addline(buffer, ");\n");
5645 string_buffer_release(priv->string_buffers, address);
5648 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5650 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5651 const char *op;
5653 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5654 op = "atomicCounterIncrement";
5655 else
5656 op = "atomicCounterDecrement";
5658 shader_glsl_append_dst(ins->ctx->buffer, ins);
5659 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5662 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5664 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5665 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5666 enum wined3d_shader_resource_type resource_type;
5667 struct glsl_src_param image_coord_param;
5668 enum wined3d_data_type data_type;
5669 DWORD coord_mask, write_mask;
5670 unsigned int uav_idx;
5671 char dst_swizzle[6];
5673 uav_idx = ins->src[1].reg.idx[0].offset;
5674 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5676 ERR("Invalid UAV index %u.\n", uav_idx);
5677 return;
5679 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5680 if (resource_type >= ARRAY_SIZE(resource_type_info))
5682 ERR("Unexpected resource type %#x.\n", resource_type);
5683 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5685 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5686 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5688 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5689 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5691 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5692 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5693 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5696 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5698 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5699 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5700 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5701 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5702 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5703 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5704 struct glsl_src_param structure_idx, offset;
5705 struct wined3d_string_buffer *address;
5706 struct wined3d_shader_dst_param dst;
5707 const char *function, *resource;
5709 resource_idx = src->reg.idx[0].offset;
5710 if (src->reg.type == WINED3DSPR_RESOURCE)
5712 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5714 ERR("Invalid resource index %u.\n", resource_idx);
5715 return;
5717 stride = reg_maps->resource_info[resource_idx].stride;
5718 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5719 function = "texelFetch";
5720 resource = "sampler";
5722 else if (src->reg.type == WINED3DSPR_UAV)
5724 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5726 ERR("Invalid UAV index %u.\n", resource_idx);
5727 return;
5729 stride = reg_maps->uav_resource_info[resource_idx].stride;
5730 bind_idx = resource_idx;
5731 function = "imageLoad";
5732 resource = "image";
5734 else
5736 if (resource_idx >= reg_maps->tgsm_count)
5738 ERR("Invalid TGSM index %u.\n", resource_idx);
5739 return;
5741 stride = reg_maps->tgsm[resource_idx].stride;
5742 bind_idx = resource_idx;
5743 function = NULL;
5744 resource = "g";
5747 address = string_buffer_get(priv->string_buffers);
5748 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5750 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5751 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5753 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5754 shader_addline(address, "%s / 4", offset.param_str);
5756 dst = ins->dst[0];
5757 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5759 /* The instruction is split into multiple lines. The first lines may
5760 * overwrite source parameters of the following lines. */
5761 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5762 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5765 for (i = 0; i < 4; ++i)
5767 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5768 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5769 continue;
5771 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5772 if (function)
5773 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5774 function, prefix, resource, bind_idx, address->buffer, swizzle);
5775 else
5776 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5777 prefix, resource, bind_idx, address->buffer, swizzle);
5780 string_buffer_release(priv->string_buffers, address);
5783 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5785 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5786 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5787 struct glsl_src_param image_coord_param, image_data_param;
5788 enum wined3d_shader_resource_type resource_type;
5789 enum wined3d_data_type data_type;
5790 unsigned int uav_idx;
5791 DWORD coord_mask;
5793 uav_idx = ins->dst[0].reg.idx[0].offset;
5794 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5796 ERR("Invalid UAV index %u.\n", uav_idx);
5797 return;
5799 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5800 if (resource_type >= ARRAY_SIZE(resource_type_info))
5802 ERR("Unexpected resource type %#x.\n", resource_type);
5803 return;
5805 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5806 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5808 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5809 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5810 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5811 shader_glsl_get_prefix(version->type), uav_idx,
5812 image_coord_param.param_str, image_data_param.param_str);
5815 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5817 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5818 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5819 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5820 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5821 struct glsl_src_param structure_idx, offset, data;
5822 unsigned int i, resource_idx, stride, src_idx = 0;
5823 struct wined3d_string_buffer *address;
5824 DWORD write_mask;
5825 BOOL is_tgsm;
5827 resource_idx = ins->dst[0].reg.idx[0].offset;
5828 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5829 if (is_tgsm)
5831 if (resource_idx >= reg_maps->tgsm_count)
5833 ERR("Invalid TGSM index %u.\n", resource_idx);
5834 return;
5836 stride = reg_maps->tgsm[resource_idx].stride;
5838 else
5840 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5842 ERR("Invalid UAV index %u.\n", resource_idx);
5843 return;
5845 stride = reg_maps->uav_resource_info[resource_idx].stride;
5848 address = string_buffer_get(priv->string_buffers);
5849 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5851 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5852 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5854 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5855 shader_addline(address, "%s / 4", offset.param_str);
5857 for (i = 0; i < 4; ++i)
5859 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5860 continue;
5862 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5864 if (is_tgsm)
5865 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5866 prefix, resource_idx, address->buffer, i, data.param_str);
5867 else
5868 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5869 prefix, resource_idx, address->buffer, i, data.param_str);
5872 string_buffer_release(priv->string_buffers, address);
5875 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5877 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5878 unsigned int sync_flags = ins->flags;
5880 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5882 shader_addline(buffer, "barrier();\n");
5883 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5886 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5888 shader_addline(buffer, "memoryBarrierShared();\n");
5889 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5892 if (sync_flags)
5893 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5896 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5897 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5899 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5900 unsigned int idx = reg->idx[0].offset;
5902 if (reg->type == WINED3DSPR_RESOURCE)
5904 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5906 ERR("Invalid resource index %u.\n", idx);
5907 return NULL;
5909 return &reg_maps->resource_info[idx];
5912 if (reg->type == WINED3DSPR_UAV)
5914 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5916 ERR("Invalid UAV index %u.\n", idx);
5917 return NULL;
5919 return &reg_maps->uav_resource_info[idx];
5922 FIXME("Unhandled register type %#x.\n", reg->type);
5923 return NULL;
5926 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5928 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5929 const struct wined3d_shader_resource_info *resource_info;
5930 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5931 unsigned int resource_idx;
5932 char dst_swizzle[6];
5933 DWORD write_mask;
5935 write_mask = shader_glsl_append_dst(buffer, ins);
5936 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5938 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5939 return;
5940 resource_idx = ins->src[0].reg.idx[0].offset;
5942 shader_addline(buffer, "ivec2(");
5943 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5945 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5946 resource_idx, WINED3D_SAMPLER_DEFAULT);
5947 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5949 else
5951 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5953 if (resource_info->stride)
5954 shader_addline(buffer, " / %u", resource_info->stride);
5955 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5956 shader_addline(buffer, " * 4");
5957 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5960 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5962 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5963 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5966 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5968 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5971 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5973 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5974 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5975 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5976 enum wined3d_shader_resource_type resource_type;
5977 enum wined3d_shader_register_type reg_type;
5978 unsigned int resource_idx, bind_idx, i;
5979 enum wined3d_data_type dst_data_type;
5980 struct glsl_src_param lod_param;
5981 BOOL supports_mipmaps;
5982 char dst_swizzle[6];
5983 DWORD write_mask;
5985 dst_data_type = ins->dst[0].reg.data_type;
5986 if (ins->flags == WINED3DSI_RESINFO_UINT)
5987 dst_data_type = WINED3D_DATA_UINT;
5988 else if (ins->flags)
5989 FIXME("Unhandled flags %#x.\n", ins->flags);
5991 reg_type = ins->src[1].reg.type;
5992 resource_idx = ins->src[1].reg.idx[0].offset;
5993 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5994 if (reg_type == WINED3DSPR_RESOURCE)
5996 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5997 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5998 resource_idx, WINED3D_SAMPLER_DEFAULT);
6000 else
6002 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
6003 bind_idx = resource_idx;
6006 if (resource_type >= ARRAY_SIZE(resource_type_info))
6008 ERR("Unexpected resource type %#x.\n", resource_type);
6009 return;
6012 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], dst_data_type);
6013 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
6015 if (dst_data_type == WINED3D_DATA_UINT)
6016 shader_addline(buffer, "uvec4(");
6017 else
6018 shader_addline(buffer, "vec4(");
6020 if (reg_type == WINED3DSPR_RESOURCE)
6022 shader_addline(buffer, "textureSize(%s_sampler%u",
6023 shader_glsl_get_prefix(version->type), bind_idx);
6025 else
6027 shader_addline(buffer, "imageSize(%s_image%u",
6028 shader_glsl_get_prefix(version->type), bind_idx);
6031 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
6032 if (supports_mipmaps)
6033 shader_addline(buffer, ", %s", lod_param.param_str);
6034 shader_addline(buffer, "), ");
6036 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
6037 shader_addline(buffer, "0, ");
6039 if (supports_mipmaps)
6041 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6043 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
6044 shader_glsl_get_prefix(version->type), bind_idx);
6046 else
6048 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
6049 shader_addline(buffer, "1");
6052 else
6054 shader_addline(buffer, "1");
6057 shader_addline(buffer, ")%s);\n", dst_swizzle);
6060 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
6062 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6063 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6064 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6065 const struct wined3d_shader_dst_param *dst = ins->dst;
6066 const struct wined3d_shader_src_param *src = ins->src;
6067 enum wined3d_shader_resource_type resource_type;
6068 enum wined3d_data_type dst_data_type;
6069 unsigned int resource_idx, bind_idx;
6070 char dst_swizzle[6];
6071 DWORD write_mask;
6073 dst_data_type = dst->reg.data_type;
6074 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
6075 dst_data_type = WINED3D_DATA_UINT;
6076 else if (ins->flags)
6077 FIXME("Unhandled flags %#x.\n", ins->flags);
6079 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, dst_data_type);
6080 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
6082 if (dst_data_type == WINED3D_DATA_UINT)
6083 shader_addline(buffer, "uvec4(");
6084 else
6085 shader_addline(buffer, "vec4(");
6087 if (src->reg.type == WINED3DSPR_RASTERIZER)
6089 if (gl_info->supported[ARB_SAMPLE_SHADING])
6091 shader_addline(buffer, "gl_NumSamples");
6093 else
6095 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
6096 shader_addline(buffer, "1");
6099 else
6101 resource_idx = src->reg.idx[0].offset;
6102 resource_type = reg_maps->resource_info[resource_idx].type;
6103 if (resource_type >= ARRAY_SIZE(resource_type_info))
6105 ERR("Unexpected resource type %#x.\n", resource_type);
6106 return;
6108 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
6110 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
6112 shader_addline(buffer, "textureSamples(%s_sampler%u)",
6113 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
6115 else
6117 FIXME("textureSamples() is not supported.\n");
6118 shader_addline(buffer, "1");
6122 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
6125 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6127 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6128 struct glsl_src_param coord_param, lod_param, sample_param;
6129 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6130 struct glsl_sample_function sample_function;
6131 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6132 BOOL has_lod_param;
6134 if (wined3d_shader_instruction_has_texel_offset(ins))
6135 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6137 resource_idx = ins->src[1].reg.idx[0].offset;
6138 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6140 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6142 ERR("Invalid resource index %u.\n", resource_idx);
6143 return;
6145 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6147 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6148 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6149 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6150 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6151 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6153 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6154 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6155 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6157 else
6159 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6160 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6161 "%s", coord_param.param_str);
6163 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6166 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6168 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6169 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6170 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6171 struct glsl_sample_function sample_function;
6172 DWORD flags = 0;
6174 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6175 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6176 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6177 flags |= WINED3D_GLSL_SAMPLE_LOD;
6178 if (wined3d_shader_instruction_has_texel_offset(ins))
6179 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6181 resource_idx = ins->src[1].reg.idx[0].offset;
6182 sampler_idx = ins->src[2].reg.idx[0].offset;
6184 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6185 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6187 switch (ins->handler_idx)
6189 case WINED3DSIH_SAMPLE:
6190 break;
6191 case WINED3DSIH_SAMPLE_B:
6192 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6193 lod_param_str = lod_param.param_str;
6194 break;
6195 case WINED3DSIH_SAMPLE_GRAD:
6196 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6197 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6198 dx_param_str = dx_param.param_str;
6199 dy_param_str = dy_param.param_str;
6200 break;
6201 case WINED3DSIH_SAMPLE_LOD:
6202 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6203 lod_param_str = lod_param.param_str;
6204 break;
6205 default:
6206 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6207 break;
6210 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6211 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6212 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6213 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6216 /* GLSL doesn't provide a function to sample from level zero with depth
6217 * comparison for array textures and cube textures. We use textureGrad*()
6218 * to implement sample_c_lz.
6220 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6221 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6222 unsigned int coord_size, const char *coord_param, const char *ref_param)
6224 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6225 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6226 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6227 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6228 char dst_swizzle[6];
6230 WARN("Emitting textureGrad() for sample_c_lz.\n");
6232 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6233 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6234 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6235 sample_function->offset_size ? "Offset" : "",
6236 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6237 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6238 if (sample_function->offset_size)
6240 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6241 shader_addline(buffer, ", ");
6242 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6244 shader_addline(buffer, "))%s);\n", dst_swizzle);
6247 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6249 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6250 const struct wined3d_shader_resource_info *resource_info;
6251 struct glsl_src_param coord_param, compare_param;
6252 struct glsl_sample_function sample_function;
6253 const char *lod_param = NULL;
6254 unsigned int coord_size;
6255 DWORD flags = 0;
6257 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6259 lod_param = "0";
6260 flags |= WINED3D_GLSL_SAMPLE_LOD;
6263 if (wined3d_shader_instruction_has_texel_offset(ins))
6264 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6266 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6267 return;
6268 resource_idx = ins->src[1].reg.idx[0].offset;
6269 sampler_idx = ins->src[2].reg.idx[0].offset;
6271 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6272 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6273 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6274 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6275 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6276 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6277 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6278 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6280 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6281 coord_size, coord_param.param_str, compare_param.param_str);
6283 else
6285 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6286 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6287 coord_size, coord_param.param_str, compare_param.param_str);
6289 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6292 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6294 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6295 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6296 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6297 struct glsl_src_param coord_param, compare_param, offset_param;
6298 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6299 const struct wined3d_shader_resource_info *resource_info;
6300 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6301 unsigned int coord_size, offset_size;
6302 char dst_swizzle[6];
6303 BOOL has_offset;
6305 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6307 FIXME("OpenGL implementation does not support textureGather.\n");
6308 return;
6311 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6312 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6313 || wined3d_shader_instruction_has_texel_offset(ins);
6315 resource_param_idx =
6316 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6317 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6318 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6319 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6320 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6322 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6323 return;
6325 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6327 ERR("Unexpected resource type %#x.\n", resource_info->type);
6328 return;
6330 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6332 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6333 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6335 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6337 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6338 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6339 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6341 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6342 shader_addline(buffer, ", %s", compare_param.param_str);
6344 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6346 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6347 shader_addline(buffer, ", %s", offset_param.param_str);
6349 else if (has_offset)
6351 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6352 shader_addline(buffer, ", ");
6353 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6355 if (component_idx)
6356 shader_addline(buffer, ", %u", component_idx);
6358 shader_addline(buffer, ")%s);\n", dst_swizzle);
6361 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6363 /* FIXME: Make this work for more than just 2D textures */
6364 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6365 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6367 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6369 char dst_mask[6];
6371 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6372 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6373 ins->dst[0].reg.idx[0].offset, dst_mask);
6375 else
6377 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6378 DWORD reg = ins->src[0].reg.idx[0].offset;
6379 char dst_swizzle[6];
6381 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6383 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6385 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6386 struct glsl_src_param div_param;
6387 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6389 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6391 if (mask_size > 1)
6392 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6393 else
6394 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6396 else
6398 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6403 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6404 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6405 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6406 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6408 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6409 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6410 struct glsl_sample_function sample_function;
6411 struct glsl_src_param src0_param;
6412 UINT mask_size;
6414 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6416 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6417 * scalar, and projected sampling would require 4.
6419 * It is a dependent read - not valid with conditional NP2 textures
6421 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6422 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6424 switch(mask_size)
6426 case 1:
6427 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6428 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6429 break;
6431 case 2:
6432 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6433 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6434 break;
6436 case 3:
6437 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6438 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6439 break;
6441 default:
6442 FIXME("Unexpected mask size %u\n", mask_size);
6443 break;
6445 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6448 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6449 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6450 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6452 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6453 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6454 struct glsl_src_param src0_param;
6455 DWORD dst_mask;
6456 unsigned int mask_size;
6458 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6459 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6460 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6462 if (mask_size > 1) {
6463 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6464 } else {
6465 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6469 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6470 * Calculate the depth as dst.x / dst.y */
6471 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6473 struct glsl_dst_param dst_param;
6475 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6477 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6478 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6479 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6480 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6481 * >= 1.0 or < 0.0
6483 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6484 dst_param.reg_name, dst_param.reg_name);
6487 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6488 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6489 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6490 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6492 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6494 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6495 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6496 struct glsl_src_param src0_param;
6498 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6500 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6501 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6504 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6505 * Calculate the 1st of a 2-row matrix multiplication. */
6506 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6508 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6509 DWORD reg = ins->dst[0].reg.idx[0].offset;
6510 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6511 struct glsl_src_param src0_param;
6513 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6514 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6517 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6518 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6519 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6521 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6522 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6523 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6524 DWORD reg = ins->dst[0].reg.idx[0].offset;
6525 struct glsl_src_param src0_param;
6527 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6528 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6529 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6532 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6534 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6535 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6536 struct glsl_sample_function sample_function;
6537 DWORD reg = ins->dst[0].reg.idx[0].offset;
6538 struct glsl_src_param src0_param;
6540 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6541 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6543 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6545 /* Sample the texture using the calculated coordinates */
6546 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6547 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6550 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6551 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6552 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6554 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6555 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6556 struct glsl_sample_function sample_function;
6557 DWORD reg = ins->dst[0].reg.idx[0].offset;
6558 struct glsl_src_param src0_param;
6560 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6561 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6563 /* Dependent read, not valid with conditional NP2 */
6564 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6566 /* Sample the texture using the calculated coordinates */
6567 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6568 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6570 tex_mx->current_row = 0;
6573 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6574 * Perform the 3rd row of a 3x3 matrix multiply */
6575 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6577 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6578 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6579 DWORD reg = ins->dst[0].reg.idx[0].offset;
6580 struct glsl_src_param src0_param;
6581 char dst_mask[6];
6583 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6585 shader_glsl_append_dst(ins->ctx->buffer, ins);
6586 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6587 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6589 tex_mx->current_row = 0;
6592 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6593 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6594 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6596 struct glsl_src_param src0_param;
6597 struct glsl_src_param src1_param;
6598 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6599 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6600 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6601 struct glsl_sample_function sample_function;
6602 DWORD reg = ins->dst[0].reg.idx[0].offset;
6603 char coord_mask[6];
6605 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6606 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6608 /* Perform the last matrix multiply operation */
6609 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6610 /* Reflection calculation */
6611 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6613 /* Dependent read, not valid with conditional NP2 */
6614 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6615 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6617 /* Sample the texture */
6618 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6619 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6620 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6622 tex_mx->current_row = 0;
6625 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6626 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6627 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6629 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6630 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6631 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6632 struct glsl_sample_function sample_function;
6633 DWORD reg = ins->dst[0].reg.idx[0].offset;
6634 struct glsl_src_param src0_param;
6635 char coord_mask[6];
6637 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6639 /* Perform the last matrix multiply operation */
6640 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6642 /* Construct the eye-ray vector from w coordinates */
6643 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6644 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6645 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6647 /* Dependent read, not valid with conditional NP2 */
6648 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6649 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6651 /* Sample the texture using the calculated coordinates */
6652 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6653 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6654 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6656 tex_mx->current_row = 0;
6659 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6660 * Apply a fake bump map transform.
6661 * texbem is pshader <= 1.3 only, this saves a few version checks
6663 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6665 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6666 struct glsl_sample_function sample_function;
6667 struct glsl_src_param coord_param;
6668 DWORD sampler_idx;
6669 DWORD mask;
6670 DWORD flags;
6671 char coord_mask[6];
6673 sampler_idx = ins->dst[0].reg.idx[0].offset;
6674 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6675 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6677 /* Dependent read, not valid with conditional NP2 */
6678 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6679 mask = sample_function.coord_mask;
6681 shader_glsl_write_mask_to_str(mask, coord_mask);
6683 /* With projected textures, texbem only divides the static texture coord,
6684 * not the displacement, so we can't let GL handle this. */
6685 if (flags & WINED3D_PSARGS_PROJECTED)
6687 DWORD div_mask=0;
6688 char coord_div_mask[3];
6689 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6691 case WINED3D_TTFF_COUNT1:
6692 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6693 break;
6694 case WINED3D_TTFF_COUNT2:
6695 div_mask = WINED3DSP_WRITEMASK_1;
6696 break;
6697 case WINED3D_TTFF_COUNT3:
6698 div_mask = WINED3DSP_WRITEMASK_2;
6699 break;
6700 case WINED3D_TTFF_COUNT4:
6701 case WINED3D_TTFF_DISABLE:
6702 div_mask = WINED3DSP_WRITEMASK_3;
6703 break;
6705 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6706 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6709 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6711 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6712 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6713 coord_param.param_str, coord_mask);
6715 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6717 struct glsl_src_param luminance_param;
6718 struct glsl_dst_param dst_param;
6720 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6721 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6723 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6724 dst_param.reg_name, dst_param.mask_str,
6725 luminance_param.param_str, sampler_idx, sampler_idx);
6727 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6730 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6732 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6733 struct glsl_src_param src0_param, src1_param;
6735 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6736 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6738 shader_glsl_append_dst(ins->ctx->buffer, ins);
6739 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6740 src0_param.param_str, sampler_idx, src1_param.param_str);
6743 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6744 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6745 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6747 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6748 struct glsl_sample_function sample_function;
6749 struct glsl_src_param src0_param;
6751 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6753 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6754 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6755 "%s.wx", src0_param.reg_name);
6756 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6759 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6760 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6761 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6763 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6764 struct glsl_sample_function sample_function;
6765 struct glsl_src_param src0_param;
6767 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6769 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6770 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6771 "%s.yz", src0_param.reg_name);
6772 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6775 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6776 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6777 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6779 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6780 struct glsl_sample_function sample_function;
6781 struct glsl_src_param src0_param;
6783 /* Dependent read, not valid with conditional NP2 */
6784 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6785 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6787 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6788 "%s", src0_param.param_str);
6789 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6792 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6793 * If any of the first 3 components are < 0, discard this pixel */
6794 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6796 if (ins->ctx->reg_maps->shader_version.major >= 4)
6798 shader_glsl_generate_condition(ins);
6799 shader_addline(ins->ctx->buffer, " discard;\n");
6801 else
6803 struct glsl_dst_param dst_param;
6805 /* The argument is a destination parameter, and no writemasks are allowed */
6806 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6808 /* 2.0 shaders compare all 4 components in texkill. */
6809 if (ins->ctx->reg_maps->shader_version.major >= 2)
6810 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6811 /* 1.x shaders only compare the first 3 components, probably due to
6812 * the nature of the texkill instruction as a tex* instruction, and
6813 * phase, which kills all .w components. Even if all 4 components are
6814 * defined, only the first 3 are used. */
6815 else
6816 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6820 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6821 * dst = dot2(src0, src1) + src2 */
6822 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6824 struct glsl_src_param src0_param;
6825 struct glsl_src_param src1_param;
6826 struct glsl_src_param src2_param;
6827 DWORD write_mask;
6828 unsigned int mask_size;
6830 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6831 mask_size = shader_glsl_get_write_mask_size(write_mask);
6833 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6834 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6835 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6837 if (mask_size > 1) {
6838 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6839 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6840 } else {
6841 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6842 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6846 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6847 const struct wined3d_shader_signature *input_signature,
6848 const struct wined3d_shader_reg_maps *reg_maps,
6849 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6851 unsigned int i;
6853 for (i = 0; i < input_signature->element_count; ++i)
6855 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6856 const char *semantic_name;
6857 UINT semantic_idx;
6858 char reg_mask[6];
6860 /* Unused */
6861 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6862 continue;
6864 semantic_name = input->semantic_name;
6865 semantic_idx = input->semantic_idx;
6866 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6868 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6870 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6872 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6873 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6875 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6877 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6879 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6881 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6882 input->register_idx, reg_mask);
6884 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6886 if (gl_info->supported[ARB_SAMPLE_SHADING])
6887 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6888 input->register_idx, reg_mask);
6889 else
6890 FIXME("ARB_sample_shading is not supported.\n");
6892 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6894 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6895 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6896 input->register_idx, reg_mask);
6897 else
6898 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6900 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6902 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6903 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6904 input->register_idx, reg_mask);
6905 else
6906 FIXME("ARB_viewport_array is not supported.\n");
6908 else
6910 if (input->sysval_semantic)
6911 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6912 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6913 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6914 shader_glsl_shader_input_name(gl_info),
6915 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6918 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6920 if (args->pointsprite)
6921 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6922 shader->u.ps.input_reg_map[input->register_idx]);
6923 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6924 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6925 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6926 needs_legacy_glsl_syntax(gl_info)
6927 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6928 else
6929 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6930 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6932 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6934 if (!semantic_idx)
6935 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6936 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6937 else if (semantic_idx == 1)
6938 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6939 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6940 else
6941 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6942 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6944 else
6946 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6947 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6952 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6954 struct glsl_program_key key;
6956 key.vs_id = entry->vs.id;
6957 key.hs_id = entry->hs.id;
6958 key.ds_id = entry->ds.id;
6959 key.gs_id = entry->gs.id;
6960 key.ps_id = entry->ps.id;
6961 key.cs_id = entry->cs.id;
6963 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6965 ERR("Failed to insert program entry.\n");
6969 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6970 const struct glsl_program_key *key)
6972 struct wine_rb_entry *entry;
6974 entry = wine_rb_get(&priv->program_lookup, key);
6975 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6978 /* Context activation is done by the caller. */
6979 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6980 struct glsl_shader_prog_link *entry)
6982 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6984 GL_EXTCALL(glDeleteProgram(entry->id));
6985 if (entry->vs.id)
6986 list_remove(&entry->vs.shader_entry);
6987 if (entry->hs.id)
6988 list_remove(&entry->hs.shader_entry);
6989 if (entry->ds.id)
6990 list_remove(&entry->ds.shader_entry);
6991 if (entry->gs.id)
6992 list_remove(&entry->gs.shader_entry);
6993 if (entry->ps.id)
6994 list_remove(&entry->ps.shader_entry);
6995 if (entry->cs.id)
6996 list_remove(&entry->cs.shader_entry);
6997 heap_free(entry);
7000 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
7001 const struct wined3d_gl_info *gl_info, const DWORD *map,
7002 const struct wined3d_shader_signature *input_signature,
7003 const struct wined3d_shader_reg_maps *reg_maps_in,
7004 const struct wined3d_shader_signature *output_signature,
7005 const struct wined3d_shader_reg_maps *reg_maps_out)
7007 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
7008 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
7009 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7010 unsigned int in_count = vec4_varyings(3, gl_info);
7011 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
7012 DWORD in_idx, *set = NULL;
7013 unsigned int i, j;
7014 char reg_mask[6];
7016 set = heap_calloc(max_varyings, sizeof(*set));
7018 for (i = 0; i < input_signature->element_count; ++i)
7020 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
7022 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
7023 continue;
7025 in_idx = map[input->register_idx];
7026 /* Declared, but not read register */
7027 if (in_idx == ~0u)
7028 continue;
7029 if (in_idx >= max_varyings)
7031 FIXME("More input varyings declared than supported, expect issues.\n");
7032 continue;
7035 if (in_idx == in_count)
7036 string_buffer_sprintf(destination, "gl_FrontColor");
7037 else if (in_idx == in_count + 1)
7038 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7039 else
7040 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
7042 if (!set[in_idx])
7043 set[in_idx] = ~0u;
7045 for (j = 0; j < output_signature->element_count; ++j)
7047 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
7048 DWORD mask;
7050 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
7051 || input->semantic_idx != output->semantic_idx
7052 || strcmp(input->semantic_name, output->semantic_name)
7053 || !(mask = input->mask & output->mask))
7054 continue;
7056 if (set[in_idx] == ~0u)
7057 set[in_idx] = 0;
7058 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
7059 shader_glsl_write_mask_to_str(mask, reg_mask);
7061 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
7062 destination->buffer, reg_mask, output->register_idx, reg_mask);
7066 for (i = 0; i < max_varyings; ++i)
7068 unsigned int size;
7070 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
7071 continue;
7073 if (set[i] == ~0u)
7074 set[i] = 0;
7076 size = 0;
7077 if (!(set[i] & WINED3DSP_WRITEMASK_0))
7078 reg_mask[size++] = 'x';
7079 if (!(set[i] & WINED3DSP_WRITEMASK_1))
7080 reg_mask[size++] = 'y';
7081 if (!(set[i] & WINED3DSP_WRITEMASK_2))
7082 reg_mask[size++] = 'z';
7083 if (!(set[i] & WINED3DSP_WRITEMASK_3))
7084 reg_mask[size++] = 'w';
7085 reg_mask[size] = '\0';
7087 if (i == in_count)
7088 string_buffer_sprintf(destination, "gl_FrontColor");
7089 else if (i == in_count + 1)
7090 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7091 else
7092 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
7094 if (size == 1)
7095 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
7096 else
7097 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
7100 heap_free(set);
7101 string_buffer_release(&priv->string_buffers, destination);
7104 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
7105 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
7106 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
7107 BOOL rasterizer_setup)
7109 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7110 char reg_mask[6];
7111 unsigned int i;
7113 for (i = 0; i < output_signature->element_count; ++i)
7115 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7117 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7118 continue;
7120 if (output->stream_idx)
7121 continue;
7123 if (output->register_idx >= input_count)
7124 continue;
7126 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7128 shader_addline(buffer,
7129 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7130 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7134 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7135 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7137 unsigned int i, clip_or_cull_index;
7138 const char *name;
7139 char reg_mask[6];
7141 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7142 /* Assign consecutive indices starting from 0. */
7143 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7144 for (i = 0; i < 4; ++i)
7146 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7147 continue;
7149 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7150 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7151 name, clip_or_cull_index, element->register_idx, reg_mask);
7152 ++clip_or_cull_index;
7156 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7157 const struct wined3d_gl_info *gl_info, const DWORD *map,
7158 const struct wined3d_shader_signature *input_signature,
7159 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7160 const struct wined3d_shader_signature *output_signature,
7161 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7163 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7164 const char *semantic_name;
7165 unsigned int semantic_idx;
7166 char reg_mask[6];
7167 unsigned int i;
7169 /* First, sort out position and point size system values. */
7170 for (i = 0; i < output_signature->element_count; ++i)
7172 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7174 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7175 continue;
7177 if (output->stream_idx)
7178 continue;
7180 semantic_name = output->semantic_name;
7181 semantic_idx = output->semantic_idx;
7182 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7184 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7186 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7187 reg_mask, output->register_idx, reg_mask);
7189 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7191 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7192 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7194 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7196 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7197 output->register_idx, reg_mask);
7199 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7201 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7202 output->register_idx, reg_mask);
7204 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7206 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7208 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7210 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7212 else if (output->sysval_semantic)
7214 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7218 /* Then, setup the pixel shader input. */
7219 if (reg_maps_out->shader_version.major < 4)
7220 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7221 output_signature, reg_maps_out);
7222 else
7223 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7226 /* Context activation is done by the caller. */
7227 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7228 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7229 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7231 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7232 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7233 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7234 const char *semantic_name;
7235 UINT semantic_idx;
7236 char reg_mask[6];
7237 unsigned int i;
7238 GLuint ret;
7240 string_buffer_clear(buffer);
7242 shader_glsl_add_version_declaration(buffer, gl_info);
7244 if (per_vertex_point_size)
7246 shader_addline(buffer, "uniform struct\n{\n");
7247 shader_addline(buffer, " float size_min;\n");
7248 shader_addline(buffer, " float size_max;\n");
7249 shader_addline(buffer, "} ffp_point;\n");
7252 if (ps_major < 3)
7254 DWORD colors_written_mask[2] = {0};
7255 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7257 if (!legacy_syntax)
7259 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7260 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7261 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7262 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7265 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7267 for (i = 0; i < vs->output_signature.element_count; ++i)
7269 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7270 DWORD write_mask;
7272 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7273 continue;
7275 semantic_name = output->semantic_name;
7276 semantic_idx = output->semantic_idx;
7277 write_mask = output->mask;
7278 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7280 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7282 if (legacy_syntax)
7283 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7284 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7285 else
7286 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7287 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7289 colors_written_mask[semantic_idx] = write_mask;
7291 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7293 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7294 reg_mask, output->register_idx, reg_mask);
7296 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7298 if (semantic_idx < MAX_TEXTURES)
7300 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7301 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7302 semantic_idx, reg_mask, output->register_idx, reg_mask);
7303 texcoords_written_mask[semantic_idx] = write_mask;
7306 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7308 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7309 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7311 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7313 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7314 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7315 output->register_idx, reg_mask[1]);
7319 for (i = 0; i < 2; ++i)
7321 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7323 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7324 if (!i)
7325 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7326 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7327 reg_mask, reg_mask);
7328 else
7329 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7330 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7331 reg_mask, reg_mask);
7334 for (i = 0; i < MAX_TEXTURES; ++i)
7336 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7337 continue;
7339 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7341 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7342 && !texcoords_written_mask[i])
7343 continue;
7345 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7346 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7347 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7351 else
7353 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7355 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7356 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7357 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7358 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7361 shader_addline(buffer, "}\n");
7363 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7364 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7365 shader_glsl_compile(gl_info, ret, buffer->buffer);
7367 return ret;
7370 static void shader_glsl_generate_stream_output_setup(struct shader_glsl_priv *priv,
7371 const struct wined3d_shader *shader, const struct wined3d_stream_output_desc *so_desc)
7373 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7374 unsigned int i;
7376 shader_addline(buffer, "out shader_in_out\n{\n");
7377 for (i = 0; i < so_desc->element_count; ++i)
7379 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7381 if (e->stream_idx)
7383 FIXME("Unhandled stream %u.\n", e->stream_idx);
7384 continue;
7386 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7387 continue;
7389 if (e->component_idx || e->component_count != 4)
7391 if (e->component_count == 1)
7392 shader_addline(buffer, "float");
7393 else
7394 shader_addline(buffer, "vec%u", e->component_count);
7395 shader_addline(buffer, " reg%u_%u_%u;\n",
7396 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
7398 else
7400 shader_addline(buffer, "vec4 reg%u;\n", e->register_idx);
7403 shader_addline(buffer, "} shader_out;\n");
7405 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7406 shader->limits->packed_output);
7407 for (i = 0; i < so_desc->element_count; ++i)
7409 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7411 if (e->stream_idx)
7413 FIXME("Unhandled stream %u.\n", e->stream_idx);
7414 continue;
7416 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7417 continue;
7419 if (e->component_idx || e->component_count != 4)
7421 DWORD write_mask;
7422 char str_mask[6];
7424 write_mask = ((1u << e->component_count) - 1) << e->component_idx;
7425 shader_glsl_write_mask_to_str(write_mask, str_mask);
7426 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7427 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1,
7428 e->register_idx, str_mask);
7430 else
7432 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n",
7433 e->register_idx, e->register_idx);
7436 shader_addline(buffer, "}\n");
7439 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7440 const struct wined3d_shader *shader, unsigned int input_count,
7441 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7443 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7444 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7446 if (rasterizer_setup)
7447 input_count = min(vec4_varyings(4, gl_info), input_count);
7449 if (input_count)
7450 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7452 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7453 prefix, shader->limits->packed_output);
7455 if (rasterizer_setup)
7456 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7457 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7458 else
7459 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7460 &shader->reg_maps, "shader_out", rasterizer_setup);
7462 shader_addline(buffer, "}\n");
7465 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7466 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7467 const char *reg_mask)
7469 if (!constant->sysval_semantic)
7471 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7472 return;
7475 switch (constant->sysval_semantic)
7477 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7478 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7479 case WINED3D_SV_TESS_FACTOR_LINEDET:
7480 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7481 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7482 break;
7483 case WINED3D_SV_TESS_FACTOR_QUADINT:
7484 case WINED3D_SV_TESS_FACTOR_TRIINT:
7485 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7486 break;
7487 default:
7488 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7489 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7493 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7494 const struct wined3d_shader_signature *signature, BOOL input_setup)
7496 unsigned int i, register_count, user_constant_index, user_constant_count;
7498 register_count = user_constant_count = 0;
7499 for (i = 0; i < signature->element_count; ++i)
7501 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7502 register_count = max(constant->register_idx + 1, register_count);
7503 if (!constant->sysval_semantic)
7504 ++user_constant_count;
7507 if (user_constant_count)
7508 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7509 input_setup ? "in" : "out", user_constant_count);
7510 if (input_setup)
7511 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7513 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7514 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7516 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7517 char reg_mask[6];
7519 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7521 if (input_setup)
7522 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7523 else
7524 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7526 shader_addline(buffer, " = ");
7528 if (input_setup)
7529 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7530 else
7531 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7533 shader_addline(buffer, ";\n");
7535 shader_addline(buffer, "}\n");
7538 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7539 const struct wined3d_gl_info *gl_info)
7541 const char *output = get_fragment_output(gl_info);
7543 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7544 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7545 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7546 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7547 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7548 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7551 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7552 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7554 const char *output = get_fragment_output(gl_info);
7556 switch (mode)
7558 case WINED3D_FFP_PS_FOG_OFF:
7559 return;
7561 case WINED3D_FFP_PS_FOG_LINEAR:
7562 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7563 break;
7565 case WINED3D_FFP_PS_FOG_EXP:
7566 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7567 break;
7569 case WINED3D_FFP_PS_FOG_EXP2:
7570 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7571 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7572 break;
7574 default:
7575 ERR("Invalid fog mode %#x.\n", mode);
7576 return;
7579 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7580 output, output);
7583 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7584 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7586 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7587 * flipping all the operators here, just negate the comparison below. */
7588 static const char * const comparison_operator[] =
7590 "", /* WINED3D_CMP_NEVER */
7591 "<", /* WINED3D_CMP_LESS */
7592 "==", /* WINED3D_CMP_EQUAL */
7593 "<=", /* WINED3D_CMP_LESSEQUAL */
7594 ">", /* WINED3D_CMP_GREATER */
7595 "!=", /* WINED3D_CMP_NOTEQUAL */
7596 ">=", /* WINED3D_CMP_GREATEREQUAL */
7597 "" /* WINED3D_CMP_ALWAYS */
7600 if (alpha_func == WINED3D_CMP_ALWAYS)
7601 return;
7603 if (alpha_func != WINED3D_CMP_NEVER)
7604 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7605 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7606 shader_addline(buffer, " discard;\n");
7609 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7610 const struct wined3d_gl_info *gl_info)
7612 if (gl_info->supported[ARB_CULL_DISTANCE])
7613 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7614 if (gl_info->supported[ARB_GPU_SHADER5])
7615 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7616 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7617 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7618 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7619 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7620 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7621 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7622 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7623 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7624 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7625 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7626 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7627 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7628 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7629 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7630 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7631 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7632 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7633 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7634 if (gl_info->supported[ARB_TEXTURE_GATHER])
7635 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7636 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7637 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7638 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7639 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7640 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7641 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7642 if (gl_info->supported[EXT_GPU_SHADER4])
7643 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7644 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7645 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7648 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7649 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7650 struct wined3d_string_buffer_list *string_buffers)
7652 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7653 struct wined3d_string_buffer *src, *assignment;
7654 enum wined3d_data_type dst_data_type;
7655 unsigned int i;
7657 if (output_signature->element_count)
7659 src = string_buffer_get(string_buffers);
7660 assignment = string_buffer_get(string_buffers);
7661 for (i = 0; i < output_signature->element_count; ++i)
7663 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7665 /* register_idx is set to ~0u for non-color outputs. */
7666 if (output->register_idx == ~0u)
7667 continue;
7668 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7670 FIXME("Unhandled component type %#x.\n", output->component_type);
7671 continue;
7673 dst_data_type = component_type_info[output->component_type].data_type;
7674 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7675 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7676 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT);
7677 shader_addline(buffer, "%s;\n", assignment->buffer);
7679 string_buffer_release(string_buffers, src);
7680 string_buffer_release(string_buffers, assignment);
7682 else
7684 DWORD mask = shader->reg_maps.rt_mask;
7686 while (mask)
7688 i = wined3d_bit_scan(&mask);
7689 shader_addline(buffer, "color_out%u = ps_out[%u];\n", i, i);
7694 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7695 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7696 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7698 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7700 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7701 if (reg_maps->shader_version.major < 2)
7702 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7704 if (args->srgb_correction)
7705 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7707 /* SM < 3 does not replace the fog stage. */
7708 if (reg_maps->shader_version.major < 3)
7709 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7711 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7713 if (reg_maps->sample_mask)
7714 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7716 if (!needs_legacy_glsl_syntax(gl_info))
7717 shader_glsl_generate_color_output(buffer, gl_info, shader, string_buffers);
7720 /* Context activation is done by the caller. */
7721 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7722 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7723 const struct wined3d_shader *shader,
7724 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7726 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7727 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7728 const char *prefix = shader_glsl_get_prefix(version->type);
7729 const struct wined3d_gl_info *gl_info = context->gl_info;
7730 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7731 unsigned int i, extra_constants_needed = 0;
7732 struct shader_glsl_ctx_priv priv_ctx;
7733 GLuint shader_id;
7734 DWORD map;
7736 memset(&priv_ctx, 0, sizeof(priv_ctx));
7737 priv_ctx.cur_ps_args = args;
7738 priv_ctx.cur_np2fixup_info = np2fixup_info;
7739 priv_ctx.string_buffers = string_buffers;
7741 shader_glsl_add_version_declaration(buffer, gl_info);
7743 shader_glsl_enable_extensions(buffer, gl_info);
7744 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7745 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7746 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7747 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7748 if (shader_glsl_use_explicit_attrib_location(gl_info))
7749 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7750 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7751 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7752 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7753 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7754 if (gl_info->supported[ARB_SAMPLE_SHADING])
7755 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7756 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7757 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7758 /* The spec says that it doesn't have to be explicitly enabled, but the
7759 * nvidia drivers write a warning if we don't do so. */
7760 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7761 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7763 /* Base Declarations */
7764 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7766 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7768 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7769 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7770 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7771 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7774 /* Declare uniforms for NP2 texcoord fixup:
7775 * This is NOT done inside the loop that declares the texture samplers
7776 * since the NP2 fixup code is currently only used for the GeforceFX
7777 * series and when forcing the ARB_npot extension off. Modern cards just
7778 * skip the code anyway, so put it inside a separate loop. */
7779 if (args->np2_fixup)
7781 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7782 unsigned int cur = 0;
7784 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7785 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7786 * samplerNP2Fixup stores texture dimensions and is updated through
7787 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7789 for (i = 0; i < shader->limits->sampler; ++i)
7791 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7792 continue;
7794 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7796 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7797 continue;
7800 fixup->idx[i] = cur++;
7803 fixup->num_consts = (cur + 1) >> 1;
7804 fixup->active = args->np2_fixup;
7805 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7808 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7810 shader_addline(buffer, "uniform struct\n{\n");
7811 shader_addline(buffer, " vec4 color;\n");
7812 shader_addline(buffer, " float density;\n");
7813 shader_addline(buffer, " float end;\n");
7814 shader_addline(buffer, " float scale;\n");
7815 shader_addline(buffer, "} ffp_fog;\n");
7817 if (needs_legacy_glsl_syntax(gl_info))
7819 if (glsl_is_color_reg_read(shader, 0))
7820 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7821 if (glsl_is_color_reg_read(shader, 1))
7822 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7823 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7824 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7826 else
7828 if (glsl_is_color_reg_read(shader, 0))
7829 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7830 if (glsl_is_color_reg_read(shader, 1))
7831 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7832 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7833 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7834 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7838 if (version->major >= 3)
7840 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7842 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7843 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7844 shader->u.ps.interpolation_mode, version->major >= 4);
7845 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7848 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7850 if (!(map & 1))
7851 continue;
7853 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7855 if (reg_maps->luminanceparams & (1u << i))
7857 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7858 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7859 extra_constants_needed++;
7862 extra_constants_needed++;
7865 if (args->srgb_correction)
7867 shader_addline(buffer, "const vec4 srgb_const0 = ");
7868 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7869 shader_addline(buffer, ";\n");
7870 shader_addline(buffer, "const vec4 srgb_const1 = ");
7871 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7872 shader_addline(buffer, ";\n");
7874 if (reg_maps->vpos || reg_maps->usesdsy)
7876 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7878 ++extra_constants_needed;
7879 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7881 if (reg_maps->vpos)
7883 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7885 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7886 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7887 args->render_offscreen ? "" : "origin_upper_left, ");
7888 else if (!args->render_offscreen)
7889 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7891 shader_addline(buffer, "vec4 vpos;\n");
7895 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7896 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7898 if (!needs_legacy_glsl_syntax(gl_info))
7900 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7902 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7903 if (output_signature->element_count)
7905 for (i = 0; i < output_signature->element_count; ++i)
7907 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7909 if (output->register_idx == ~0u)
7910 continue;
7911 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7913 FIXME("Unhandled component type %#x.\n", output->component_type);
7914 continue;
7916 if (shader_glsl_use_explicit_attrib_location(gl_info))
7917 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7918 shader_addline(buffer, "out %s4 color_out%u;\n",
7919 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7922 else
7924 DWORD mask = reg_maps->rt_mask;
7926 while (mask)
7928 i = wined3d_bit_scan(&mask);
7929 if (shader_glsl_use_explicit_attrib_location(gl_info))
7930 shader_addline(buffer, "layout(location = %u) ", i);
7931 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7936 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7937 FIXME("Insufficient uniforms to run this shader.\n");
7939 if (shader->u.ps.force_early_depth_stencil)
7940 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7942 shader_addline(buffer, "void main()\n{\n");
7944 if (reg_maps->sample_mask)
7945 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7947 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7948 * add approximately 0.5. This causes off-by-one problems as spotted by
7949 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7950 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7951 * causes precision troubles when we just subtract 0.5.
7953 * To deal with that, just floor() the position. This will eliminate the
7954 * fraction on all cards.
7956 * TODO: Test how this behaves with multisampling.
7958 * An advantage of floor is that it works even if the driver doesn't add
7959 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7960 * to return in gl_FragCoord, even though coordinates specify the pixel
7961 * centers instead of the pixel corners. This code will behave correctly
7962 * on drivers that returns integer values. */
7963 if (reg_maps->vpos)
7965 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7966 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7967 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7968 shader_addline(buffer,
7969 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7970 else
7971 shader_addline(buffer,
7972 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7975 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7977 unsigned int i;
7978 WORD map = reg_maps->texcoord;
7980 if (legacy_syntax)
7982 if (glsl_is_color_reg_read(shader, 0))
7983 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7984 if (glsl_is_color_reg_read(shader, 1))
7985 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7988 for (i = 0; map; map >>= 1, ++i)
7990 if (map & 1)
7992 if (args->pointsprite)
7993 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7994 else if (args->texcoords_initialized & (1u << i))
7995 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7996 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7997 else
7998 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7999 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
8003 if (legacy_syntax)
8004 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
8007 /* Pack 3.0 inputs */
8008 if (reg_maps->shader_version.major >= 3)
8009 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
8010 reg_maps->shader_version.major >= 4);
8012 /* Base Shader Body */
8013 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8014 return 0;
8016 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8017 if (reg_maps->shader_version.major < 4)
8018 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
8020 shader_addline(buffer, "}\n");
8022 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8023 TRACE("Compiling shader object %u.\n", shader_id);
8024 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8026 return shader_id;
8029 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
8030 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8031 const struct vs_compile_args *args)
8033 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8034 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8035 unsigned int i;
8037 /* Unpack outputs. */
8038 shader_addline(buffer, "setup_vs_output(vs_out);\n");
8040 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
8041 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
8042 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
8043 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
8045 if (reg_maps->shader_version.major < 3)
8047 if (args->fog_src == VS_FOG_Z)
8048 shader_addline(buffer, "%s = gl_Position.z;\n",
8049 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
8050 else if (!reg_maps->fog)
8051 shader_addline(buffer, "%s = 0.0;\n",
8052 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
8055 /* We always store the clipplanes without y inversion. */
8056 if (args->clip_enabled)
8058 if (legacy_syntax)
8059 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
8060 else
8061 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8062 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
8065 if (args->point_size && !args->per_vertex_point_size)
8066 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
8068 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8069 shader_glsl_fixup_position(buffer, FALSE);
8072 /* Context activation is done by the caller. */
8073 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
8074 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
8076 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8077 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8078 const struct wined3d_shader_version *version = &reg_maps->shader_version;
8079 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8080 const struct wined3d_gl_info *gl_info = context->gl_info;
8081 struct shader_glsl_ctx_priv priv_ctx;
8082 GLuint shader_id;
8083 unsigned int i;
8085 memset(&priv_ctx, 0, sizeof(priv_ctx));
8086 priv_ctx.cur_vs_args = args;
8087 priv_ctx.string_buffers = string_buffers;
8089 shader_glsl_add_version_declaration(buffer, gl_info);
8091 shader_glsl_enable_extensions(buffer, gl_info);
8092 if (gl_info->supported[ARB_DRAW_INSTANCED])
8093 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8094 if (shader_glsl_use_explicit_attrib_location(gl_info))
8095 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8097 /* Base Declarations */
8098 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8100 for (i = 0; i < shader->input_signature.element_count; ++i)
8101 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8103 if (args->point_size && !args->per_vertex_point_size)
8105 shader_addline(buffer, "uniform struct\n{\n");
8106 shader_addline(buffer, " float size;\n");
8107 shader_addline(buffer, " float size_min;\n");
8108 shader_addline(buffer, " float size_max;\n");
8109 shader_addline(buffer, "} ffp_point;\n");
8112 if (!needs_legacy_glsl_syntax(gl_info))
8114 if (args->clip_enabled)
8115 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8117 if (version->major < 3)
8119 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8120 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8121 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8122 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8126 if (version->major < 4)
8127 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8129 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8130 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8132 if (reg_maps->shader_version.major >= 4)
8133 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8134 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8136 shader_addline(buffer, "void main()\n{\n");
8138 if (reg_maps->input_rel_addressing)
8140 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8141 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8142 for (i = 0; i < shader->input_signature.element_count; ++i)
8144 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8145 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8149 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8150 return 0;
8152 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8153 if (reg_maps->shader_version.major < 4)
8154 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8156 shader_addline(buffer, "}\n");
8158 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8159 TRACE("Compiling shader object %u.\n", shader_id);
8160 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8162 return shader_id;
8165 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8166 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8168 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8169 char reg_mask[6];
8170 unsigned int i;
8172 for (i = 0; i < output_signature->element_count; ++i)
8174 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8176 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8177 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8178 output->register_idx, reg_mask, output->register_idx, reg_mask);
8182 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8183 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8184 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8185 const char *phase_name, unsigned phase_idx)
8187 unsigned int i;
8188 HRESULT hr;
8190 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8191 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8192 for (i = 0; i < phase->temporary_count; ++i)
8193 shader_addline(buffer, "vec4 R%u;\n", i);
8194 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8195 shader_addline(buffer, "}\n");
8196 return hr;
8199 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8200 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8202 if (phase->instance_count)
8204 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8205 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8206 shader_addline(buffer, "}\n");
8208 else
8210 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8214 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
8215 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8217 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8218 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8219 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8220 const struct wined3d_gl_info *gl_info = context->gl_info;
8221 const struct wined3d_hull_shader *hs = &shader->u.hs;
8222 const struct wined3d_shader_phase *phase;
8223 struct shader_glsl_ctx_priv priv_ctx;
8224 GLuint shader_id;
8225 unsigned int i;
8227 memset(&priv_ctx, 0, sizeof(priv_ctx));
8228 priv_ctx.string_buffers = string_buffers;
8230 shader_glsl_add_version_declaration(buffer, gl_info);
8232 shader_glsl_enable_extensions(buffer, gl_info);
8233 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8235 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8237 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8239 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8240 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8242 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8244 if (hs->phases.control_point)
8246 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8247 shader->limits->packed_output);
8248 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8249 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8250 shader_addline(buffer, "}\n");
8253 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8254 if ((phase = hs->phases.control_point))
8256 for (i = 0; i < phase->temporary_count; ++i)
8257 shader_addline(buffer, "vec4 R%u;\n", i);
8258 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8259 return 0;
8260 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8262 else
8264 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8266 shader_addline(buffer, "}\n");
8268 for (i = 0; i < hs->phases.fork_count; ++i)
8270 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8271 &hs->phases.fork[i], "fork", i)))
8272 return 0;
8275 for (i = 0; i < hs->phases.join_count; ++i)
8277 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8278 &hs->phases.join[i], "join", i)))
8279 return 0;
8282 shader_addline(buffer, "void main()\n{\n");
8283 shader_addline(buffer, "hs_control_point_phase();\n");
8284 if (reg_maps->vocp)
8285 shader_addline(buffer, "barrier();\n");
8286 for (i = 0; i < hs->phases.fork_count; ++i)
8287 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8288 for (i = 0; i < hs->phases.join_count; ++i)
8289 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8290 shader_addline(buffer, "setup_patch_constant_output();\n");
8291 shader_addline(buffer, "}\n");
8293 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8294 TRACE("Compiling shader object %u.\n", shader_id);
8295 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8297 return shader_id;
8300 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8301 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8302 const struct ds_compile_args *args)
8304 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8306 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8307 shader_glsl_fixup_position(buffer, FALSE);
8310 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
8311 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8313 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8314 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8315 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8316 const struct wined3d_gl_info *gl_info = context->gl_info;
8317 struct shader_glsl_ctx_priv priv_ctx;
8318 GLuint shader_id;
8320 memset(&priv_ctx, 0, sizeof(priv_ctx));
8321 priv_ctx.cur_ds_args = args;
8322 priv_ctx.string_buffers = string_buffers;
8324 shader_glsl_add_version_declaration(buffer, gl_info);
8326 shader_glsl_enable_extensions(buffer, gl_info);
8327 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8329 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8331 shader_addline(buffer, "layout(");
8332 switch (shader->u.ds.tessellator_domain)
8334 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8335 shader_addline(buffer, "isolines");
8336 break;
8337 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8338 shader_addline(buffer, "quads");
8339 break;
8340 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8341 shader_addline(buffer, "triangles");
8342 break;
8344 switch (args->tessellator_output_primitive)
8346 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8347 if (args->render_offscreen)
8348 shader_addline(buffer, ", ccw");
8349 else
8350 shader_addline(buffer, ", cw");
8351 break;
8352 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8353 if (args->render_offscreen)
8354 shader_addline(buffer, ", cw");
8355 else
8356 shader_addline(buffer, ", ccw");
8357 break;
8358 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8359 shader_addline(buffer, ", point_mode");
8360 break;
8361 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8362 break;
8364 switch (args->tessellator_partitioning)
8366 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8367 shader_addline(buffer, ", fractional_odd_spacing");
8368 break;
8369 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8370 shader_addline(buffer, ", fractional_even_spacing");
8371 break;
8372 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8373 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8374 shader_addline(buffer, ", equal_spacing");
8375 break;
8377 shader_addline(buffer, ") in;\n");
8379 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8381 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8382 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8384 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8385 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8386 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8388 shader_addline(buffer, "void main()\n{\n");
8389 shader_addline(buffer, "setup_patch_constant_input();\n");
8391 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8392 return 0;
8394 shader_addline(buffer, "}\n");
8396 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8397 TRACE("Compiling shader object %u.\n", shader_id);
8398 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8400 return shader_id;
8403 /* Context activation is done by the caller. */
8404 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
8405 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8407 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8408 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8409 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8410 const struct wined3d_gl_info *gl_info = context->gl_info;
8411 const struct wined3d_shader_signature_element *output;
8412 enum wined3d_primitive_type primitive_type;
8413 struct shader_glsl_ctx_priv priv_ctx;
8414 unsigned int max_vertices;
8415 unsigned int i, j;
8416 GLuint shader_id;
8418 memset(&priv_ctx, 0, sizeof(priv_ctx));
8419 priv_ctx.string_buffers = string_buffers;
8421 shader_glsl_add_version_declaration(buffer, gl_info);
8423 shader_glsl_enable_extensions(buffer, gl_info);
8425 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8427 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8428 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8429 if (shader->u.gs.instance_count > 1)
8430 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8431 shader_addline(buffer, ") in;\n");
8433 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8434 if (!(max_vertices = shader->u.gs.vertices_out))
8436 switch (args->primitive_type)
8438 case WINED3D_PT_POINTLIST:
8439 max_vertices = 1;
8440 break;
8441 case WINED3D_PT_LINELIST:
8442 max_vertices = 2;
8443 break;
8444 case WINED3D_PT_TRIANGLELIST:
8445 max_vertices = 3;
8446 break;
8447 default:
8448 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8449 break;
8452 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8453 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8454 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8456 if (!gl_info->supported[ARB_CLIP_CONTROL])
8458 shader_addline(buffer, "uniform vec4 pos_fixup");
8459 if (reg_maps->viewport_array)
8460 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8461 shader_addline(buffer, ";\n");
8464 if (is_rasterization_disabled(shader))
8466 shader_glsl_generate_stream_output_setup(priv, shader, &shader->u.gs.so_desc);
8468 else
8470 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8471 gl_info, TRUE, args->interpolation_mode);
8474 shader_addline(buffer, "void main()\n{\n");
8475 if (shader->function)
8477 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8478 return 0;
8480 else
8482 for (i = 0; i < max_vertices; ++i)
8484 for (j = 0; j < shader->output_signature.element_count; ++j)
8486 output = &shader->output_signature.elements[j];
8487 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8488 output->register_idx, i, output->register_idx);
8490 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8491 if (!gl_info->supported[ARB_CLIP_CONTROL])
8492 shader_glsl_fixup_position(buffer, FALSE);
8493 shader_addline(buffer, "EmitVertex();\n");
8496 shader_addline(buffer, "}\n");
8498 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8499 TRACE("Compiling shader object %u.\n", shader_id);
8500 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8502 return shader_id;
8505 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8507 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8508 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8509 struct wined3d_string_buffer *buffer = ctx->buffer;
8510 const struct wined3d_shader *shader = ctx->shader;
8512 switch (shader->reg_maps.shader_version.type)
8514 case WINED3D_SHADER_TYPE_PIXEL:
8515 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8516 break;
8517 case WINED3D_SHADER_TYPE_VERTEX:
8518 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8519 break;
8520 case WINED3D_SHADER_TYPE_DOMAIN:
8521 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8522 break;
8523 case WINED3D_SHADER_TYPE_GEOMETRY:
8524 case WINED3D_SHADER_TYPE_COMPUTE:
8525 break;
8526 default:
8527 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8528 break;
8532 /* Context activation is done by the caller. */
8533 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8534 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8535 const struct wined3d_shader *shader)
8537 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8538 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8539 const struct wined3d_gl_info *gl_info = context->gl_info;
8540 struct shader_glsl_ctx_priv priv_ctx;
8541 GLuint shader_id;
8542 unsigned int i;
8544 memset(&priv_ctx, 0, sizeof(priv_ctx));
8545 priv_ctx.string_buffers = string_buffers;
8547 shader_glsl_add_version_declaration(buffer, gl_info);
8549 shader_glsl_enable_extensions(buffer, gl_info);
8550 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8552 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8554 for (i = 0; i < reg_maps->tgsm_count; ++i)
8556 if (reg_maps->tgsm[i].size)
8557 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8560 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8561 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8563 shader_addline(buffer, "void main()\n{\n");
8564 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8565 shader_addline(buffer, "}\n");
8567 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8568 TRACE("Compiling shader object %u.\n", shader_id);
8569 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8571 return shader_id;
8574 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8575 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8576 struct wined3d_shader *shader,
8577 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8579 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8580 struct glsl_shader_private *shader_data;
8581 struct ps_np2fixup_info *np2fixup;
8582 UINT i;
8583 DWORD new_size;
8584 GLuint ret;
8586 if (!shader->backend_data)
8588 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8590 ERR("Failed to allocate backend data.\n");
8591 return 0;
8594 shader_data = shader->backend_data;
8595 gl_shaders = shader_data->gl_shaders.ps;
8597 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8598 * so a linear search is more performant than a hashmap or a binary search
8599 * (cache coherency etc)
8601 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8603 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8605 if (args->np2_fixup)
8606 *np2fixup_info = &gl_shaders[i].np2fixup;
8607 return gl_shaders[i].id;
8611 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8612 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8614 if (shader_data->num_gl_shaders)
8616 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8617 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8619 else
8621 new_array = heap_alloc(sizeof(*gl_shaders));
8622 new_size = 1;
8625 if(!new_array) {
8626 ERR("Out of memory\n");
8627 return 0;
8629 shader_data->gl_shaders.ps = new_array;
8630 shader_data->shader_array_size = new_size;
8631 gl_shaders = new_array;
8634 gl_shaders[shader_data->num_gl_shaders].args = *args;
8636 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8637 memset(np2fixup, 0, sizeof(*np2fixup));
8638 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8640 pixelshader_update_resource_types(shader, args->tex_types);
8642 string_buffer_clear(buffer);
8643 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8644 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8646 return ret;
8649 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8650 const DWORD use_map)
8652 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8653 return FALSE;
8654 if ((stored->clip_enabled) != new->clip_enabled)
8655 return FALSE;
8656 if (stored->point_size != new->point_size)
8657 return FALSE;
8658 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8659 return FALSE;
8660 if (stored->flatshading != new->flatshading)
8661 return FALSE;
8662 if (stored->next_shader_type != new->next_shader_type)
8663 return FALSE;
8664 if (stored->next_shader_input_count != new->next_shader_input_count)
8665 return FALSE;
8666 if (stored->fog_src != new->fog_src)
8667 return FALSE;
8668 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8671 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8672 struct wined3d_shader *shader, const struct vs_compile_args *args)
8674 UINT i;
8675 DWORD new_size;
8676 DWORD use_map = context->stream_info.use_map;
8677 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8678 struct glsl_shader_private *shader_data;
8679 GLuint ret;
8681 if (!shader->backend_data)
8683 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8685 ERR("Failed to allocate backend data.\n");
8686 return 0;
8689 shader_data = shader->backend_data;
8690 gl_shaders = shader_data->gl_shaders.vs;
8692 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8693 * so a linear search is more performant than a hashmap or a binary search
8694 * (cache coherency etc)
8696 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8698 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8699 return gl_shaders[i].id;
8702 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8704 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8706 if (shader_data->num_gl_shaders)
8708 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8709 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8711 else
8713 new_array = heap_alloc(sizeof(*gl_shaders));
8714 new_size = 1;
8717 if(!new_array) {
8718 ERR("Out of memory\n");
8719 return 0;
8721 shader_data->gl_shaders.vs = new_array;
8722 shader_data->shader_array_size = new_size;
8723 gl_shaders = new_array;
8726 gl_shaders[shader_data->num_gl_shaders].args = *args;
8728 string_buffer_clear(&priv->shader_buffer);
8729 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8730 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8732 return ret;
8735 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8736 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8738 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8739 struct glsl_shader_private *shader_data;
8740 unsigned int new_size;
8741 GLuint ret;
8743 if (!shader->backend_data)
8745 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8747 ERR("Failed to allocate backend data.\n");
8748 return 0;
8751 shader_data = shader->backend_data;
8752 gl_shaders = shader_data->gl_shaders.hs;
8754 if (shader_data->num_gl_shaders > 0)
8756 assert(shader_data->num_gl_shaders == 1);
8757 return gl_shaders[0].id;
8760 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8762 assert(!shader_data->gl_shaders.hs);
8763 new_size = 1;
8764 if (!(new_array = heap_alloc(sizeof(*new_array))))
8766 ERR("Failed to allocate GL shaders array.\n");
8767 return 0;
8769 shader_data->gl_shaders.hs = new_array;
8770 shader_data->shader_array_size = new_size;
8771 gl_shaders = new_array;
8773 string_buffer_clear(&priv->shader_buffer);
8774 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8775 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8777 return ret;
8780 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8781 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8783 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8784 struct glsl_shader_private *shader_data;
8785 unsigned int i, new_size;
8786 GLuint ret;
8788 if (!shader->backend_data)
8790 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8792 ERR("Failed to allocate backend data.\n");
8793 return 0;
8796 shader_data = shader->backend_data;
8797 gl_shaders = shader_data->gl_shaders.ds;
8799 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8801 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8802 return gl_shaders[i].id;
8805 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8807 if (shader_data->num_gl_shaders)
8809 new_size = shader_data->shader_array_size + 1;
8810 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8812 else
8814 new_array = heap_alloc(sizeof(*new_array));
8815 new_size = 1;
8818 if (!new_array)
8820 ERR("Failed to allocate GL shaders array.\n");
8821 return 0;
8823 shader_data->gl_shaders.ds = new_array;
8824 shader_data->shader_array_size = new_size;
8825 gl_shaders = new_array;
8827 string_buffer_clear(&priv->shader_buffer);
8828 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8829 gl_shaders[shader_data->num_gl_shaders].args = *args;
8830 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8832 return ret;
8835 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8836 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8838 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8839 struct glsl_shader_private *shader_data;
8840 unsigned int i, new_size;
8841 GLuint ret;
8843 if (!shader->backend_data)
8845 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8847 ERR("Failed to allocate backend data.\n");
8848 return 0;
8851 shader_data = shader->backend_data;
8852 gl_shaders = shader_data->gl_shaders.gs;
8854 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8856 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8857 return gl_shaders[i].id;
8860 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8862 if (shader_data->num_gl_shaders)
8864 new_size = shader_data->shader_array_size + 1;
8865 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8867 else
8869 new_array = heap_alloc(sizeof(*new_array));
8870 new_size = 1;
8873 if (!new_array)
8875 ERR("Failed to allocate GL shaders array.\n");
8876 return 0;
8878 shader_data->gl_shaders.gs = new_array;
8879 shader_data->shader_array_size = new_size;
8880 gl_shaders = new_array;
8882 string_buffer_clear(&priv->shader_buffer);
8883 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8884 gl_shaders[shader_data->num_gl_shaders].args = *args;
8885 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8887 return ret;
8890 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8892 switch (mcs)
8894 case WINED3D_MCS_MATERIAL:
8895 return material;
8896 case WINED3D_MCS_COLOR1:
8897 return "ffp_attrib_diffuse";
8898 case WINED3D_MCS_COLOR2:
8899 return "ffp_attrib_specular";
8900 default:
8901 ERR("Invalid material color source %#x.\n", mcs);
8902 return "<invalid>";
8906 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8907 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8909 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8910 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8911 if (settings->localviewer)
8912 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8913 else
8914 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8915 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8916 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8919 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8920 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8922 const char *diffuse, *specular, *emissive, *ambient;
8923 unsigned int i, idx;
8925 if (!settings->lighting)
8927 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8928 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8929 return;
8932 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8933 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8934 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8935 shader_addline(buffer, "vec3 dir, dst;\n");
8936 shader_addline(buffer, "float att, t;\n");
8938 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8939 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8940 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8941 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8943 idx = 0;
8944 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8946 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8947 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8948 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8949 shader_addline(buffer, "dst.x = 1.0;\n");
8950 if (legacy_lighting)
8952 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8953 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8954 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8956 else
8958 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8960 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8961 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8962 if (!legacy_lighting)
8963 shader_addline(buffer, "att = 1.0 / att;\n");
8964 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8965 if (!settings->normal)
8967 shader_addline(buffer, "}\n");
8968 continue;
8970 shader_addline(buffer, "dir = normalize(dir);\n");
8971 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8972 shader_addline(buffer, "}\n");
8975 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8977 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8978 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8979 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8980 shader_addline(buffer, "dst.x = 1.0;\n");
8981 if (legacy_lighting)
8983 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8984 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8985 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8987 else
8989 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8991 shader_addline(buffer, "dir = normalize(dir);\n");
8992 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8993 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8994 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8995 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8996 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8997 idx, idx, idx, idx);
8998 if (legacy_lighting)
8999 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
9000 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
9001 idx, idx, idx);
9002 else
9003 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
9004 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
9005 idx, idx, idx);
9006 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
9007 if (!settings->normal)
9009 shader_addline(buffer, "}\n");
9010 continue;
9012 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
9013 shader_addline(buffer, "}\n");
9016 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
9018 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
9019 if (!settings->normal)
9020 continue;
9021 shader_addline(buffer, "att = 1.0;\n");
9022 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
9023 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
9026 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
9028 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
9029 if (!settings->normal)
9030 continue;
9031 shader_addline(buffer, "att = 1.0;\n");
9032 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
9033 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
9036 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
9037 ambient, diffuse, emissive);
9038 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
9039 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
9042 /* Context activation is done by the caller. */
9043 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
9044 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
9046 static const struct attrib_info
9048 const char type[6];
9049 const char name[24];
9051 attrib_info[] =
9053 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
9054 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
9055 /* TODO: Indexed vertex blending */
9056 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
9057 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
9058 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
9059 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
9060 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
9062 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9063 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9064 BOOL output_legacy_fogcoord = legacy_syntax;
9065 BOOL legacy_lighting = priv->legacy_lighting;
9066 GLuint shader_obj;
9067 unsigned int i;
9069 string_buffer_clear(buffer);
9071 shader_glsl_add_version_declaration(buffer, gl_info);
9073 if (shader_glsl_use_explicit_attrib_location(gl_info))
9074 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9076 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
9078 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9080 if (shader_glsl_use_explicit_attrib_location(gl_info))
9081 shader_addline(buffer, "layout(location = %u) ", i);
9082 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9084 shader_addline(buffer, "\n");
9086 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9087 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9088 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9089 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
9091 shader_addline(buffer, "uniform struct\n{\n");
9092 shader_addline(buffer, " vec4 emissive;\n");
9093 shader_addline(buffer, " vec4 ambient;\n");
9094 shader_addline(buffer, " vec4 diffuse;\n");
9095 shader_addline(buffer, " vec4 specular;\n");
9096 shader_addline(buffer, " float shininess;\n");
9097 shader_addline(buffer, "} ffp_material;\n");
9099 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9100 shader_addline(buffer, "uniform struct\n{\n");
9101 shader_addline(buffer, " vec4 diffuse;\n");
9102 shader_addline(buffer, " vec4 specular;\n");
9103 shader_addline(buffer, " vec4 ambient;\n");
9104 shader_addline(buffer, " vec4 position;\n");
9105 shader_addline(buffer, " vec3 direction;\n");
9106 shader_addline(buffer, " float range;\n");
9107 shader_addline(buffer, " float falloff;\n");
9108 shader_addline(buffer, " float c_att;\n");
9109 shader_addline(buffer, " float l_att;\n");
9110 shader_addline(buffer, " float q_att;\n");
9111 shader_addline(buffer, " float cos_htheta;\n");
9112 shader_addline(buffer, " float cos_hphi;\n");
9113 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
9115 if (settings->point_size)
9117 shader_addline(buffer, "uniform struct\n{\n");
9118 shader_addline(buffer, " float size;\n");
9119 shader_addline(buffer, " float size_min;\n");
9120 shader_addline(buffer, " float size_max;\n");
9121 shader_addline(buffer, " float c_att;\n");
9122 shader_addline(buffer, " float l_att;\n");
9123 shader_addline(buffer, " float q_att;\n");
9124 shader_addline(buffer, "} ffp_point;\n");
9127 if (legacy_syntax)
9129 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9130 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9131 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9132 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9134 else
9136 if (settings->clipping)
9137 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9139 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9140 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9141 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9142 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9145 shader_addline(buffer, "\nvoid main()\n{\n");
9146 shader_addline(buffer, "float m;\n");
9147 shader_addline(buffer, "vec3 r;\n");
9149 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9151 if (attrib_info[i].name[0])
9152 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9153 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9155 for (i = 0; i < MAX_TEXTURES; ++i)
9157 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9158 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9159 && settings->texcoords & (1u << i))
9160 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9163 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9165 if (settings->transformed)
9167 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9168 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9169 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9171 else
9173 for (i = 0; i < settings->vertexblends; ++i)
9174 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9176 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9177 for (i = 0; i < settings->vertexblends + 1; ++i)
9178 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9180 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9181 if (settings->clipping)
9183 if (legacy_syntax)
9184 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9185 else
9186 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9187 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9189 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9192 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9193 if (settings->normal)
9195 if (!settings->vertexblends)
9197 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9199 else
9201 for (i = 0; i < settings->vertexblends + 1; ++i)
9202 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9205 if (settings->normalize)
9206 shader_addline(buffer, "normal = normalize(normal);\n");
9209 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9210 if (legacy_syntax)
9212 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9213 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9215 else
9217 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9218 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9221 for (i = 0; i < MAX_TEXTURES; ++i)
9223 BOOL output_legacy_texcoord = legacy_syntax;
9225 switch (settings->texgen[i] & 0xffff0000)
9227 case WINED3DTSS_TCI_PASSTHRU:
9228 if (settings->texcoords & (1u << i))
9229 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9230 i, i, i);
9231 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
9232 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9233 else
9234 output_legacy_texcoord = FALSE;
9235 break;
9237 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9238 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9239 break;
9241 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9242 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9243 break;
9245 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9246 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9247 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9248 break;
9250 case WINED3DTSS_TCI_SPHEREMAP:
9251 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9252 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9253 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9254 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9255 break;
9257 default:
9258 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9259 break;
9261 if (output_legacy_texcoord)
9262 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9265 switch (settings->fog_mode)
9267 case WINED3D_FFP_VS_FOG_OFF:
9268 output_legacy_fogcoord = FALSE;
9269 break;
9271 case WINED3D_FFP_VS_FOG_FOGCOORD:
9272 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9273 break;
9275 case WINED3D_FFP_VS_FOG_RANGE:
9276 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9277 break;
9279 case WINED3D_FFP_VS_FOG_DEPTH:
9280 if (settings->ortho_fog)
9282 if (gl_info->supported[ARB_CLIP_CONTROL])
9283 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9284 else
9285 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9286 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9288 else if (settings->transformed)
9290 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9292 else
9294 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9296 break;
9298 default:
9299 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9300 break;
9302 if (output_legacy_fogcoord)
9303 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9305 if (settings->point_size)
9307 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9308 " + ffp_point.l_att * length(ec_pos.xyz)"
9309 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9310 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9311 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9314 shader_addline(buffer, "}\n");
9316 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9317 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9319 return shader_obj;
9322 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9323 DWORD argnum, unsigned int stage, DWORD arg)
9325 const char *ret;
9327 if (arg == ARG_UNUSED)
9328 return "<unused arg>";
9330 switch (arg & WINED3DTA_SELECTMASK)
9332 case WINED3DTA_DIFFUSE:
9333 ret = "ffp_varying_diffuse";
9334 break;
9336 case WINED3DTA_CURRENT:
9337 ret = "ret";
9338 break;
9340 case WINED3DTA_TEXTURE:
9341 switch (stage)
9343 case 0: ret = "tex0"; break;
9344 case 1: ret = "tex1"; break;
9345 case 2: ret = "tex2"; break;
9346 case 3: ret = "tex3"; break;
9347 case 4: ret = "tex4"; break;
9348 case 5: ret = "tex5"; break;
9349 case 6: ret = "tex6"; break;
9350 case 7: ret = "tex7"; break;
9351 default:
9352 ret = "<invalid texture>";
9353 break;
9355 break;
9357 case WINED3DTA_TFACTOR:
9358 ret = "tex_factor";
9359 break;
9361 case WINED3DTA_SPECULAR:
9362 ret = "ffp_varying_specular";
9363 break;
9365 case WINED3DTA_TEMP:
9366 ret = "temp_reg";
9367 break;
9369 case WINED3DTA_CONSTANT:
9370 switch (stage)
9372 case 0: ret = "tss_const0"; break;
9373 case 1: ret = "tss_const1"; break;
9374 case 2: ret = "tss_const2"; break;
9375 case 3: ret = "tss_const3"; break;
9376 case 4: ret = "tss_const4"; break;
9377 case 5: ret = "tss_const5"; break;
9378 case 6: ret = "tss_const6"; break;
9379 case 7: ret = "tss_const7"; break;
9380 default:
9381 ret = "<invalid constant>";
9382 break;
9384 break;
9386 default:
9387 return "<unhandled arg>";
9390 if (arg & WINED3DTA_COMPLEMENT)
9392 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9393 if (argnum == 0)
9394 ret = "arg0";
9395 else if (argnum == 1)
9396 ret = "arg1";
9397 else if (argnum == 2)
9398 ret = "arg2";
9401 if (arg & WINED3DTA_ALPHAREPLICATE)
9403 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9404 if (argnum == 0)
9405 ret = "arg0";
9406 else if (argnum == 1)
9407 ret = "arg1";
9408 else if (argnum == 2)
9409 ret = "arg2";
9412 return ret;
9415 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9416 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9418 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9420 if (color && alpha)
9421 dstmask = "";
9422 else if (color)
9423 dstmask = ".xyz";
9424 else
9425 dstmask = ".w";
9427 dstreg = tmp_dst ? "temp_reg" : "ret";
9429 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9430 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9431 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9433 switch (op)
9435 case WINED3D_TOP_DISABLE:
9436 break;
9438 case WINED3D_TOP_SELECT_ARG1:
9439 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9440 break;
9442 case WINED3D_TOP_SELECT_ARG2:
9443 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9444 break;
9446 case WINED3D_TOP_MODULATE:
9447 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9448 break;
9450 case WINED3D_TOP_MODULATE_4X:
9451 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9452 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9453 break;
9455 case WINED3D_TOP_MODULATE_2X:
9456 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9457 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9458 break;
9460 case WINED3D_TOP_ADD:
9461 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9462 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9463 break;
9465 case WINED3D_TOP_ADD_SIGNED:
9466 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9467 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9468 break;
9470 case WINED3D_TOP_ADD_SIGNED_2X:
9471 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9472 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9473 break;
9475 case WINED3D_TOP_SUBTRACT:
9476 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9477 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9478 break;
9480 case WINED3D_TOP_ADD_SMOOTH:
9481 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9482 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9483 break;
9485 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9486 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9487 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9488 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9489 break;
9491 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9492 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9493 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9494 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9495 break;
9497 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9498 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9499 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9500 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9501 break;
9503 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9504 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9505 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9506 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9507 break;
9509 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9510 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9511 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9512 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9513 break;
9515 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9516 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9517 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9518 break;
9520 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9521 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9522 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9523 break;
9525 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9526 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9527 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9528 break;
9529 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9530 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9531 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9532 break;
9534 case WINED3D_TOP_BUMPENVMAP:
9535 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9536 /* These are handled in the first pass, nothing to do. */
9537 break;
9539 case WINED3D_TOP_DOTPRODUCT3:
9540 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9541 dstreg, dstmask, arg1, arg2, dstmask);
9542 break;
9544 case WINED3D_TOP_MULTIPLY_ADD:
9545 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9546 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9547 break;
9549 case WINED3D_TOP_LERP:
9550 /* MSDN isn't quite right here. */
9551 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9552 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9553 break;
9555 default:
9556 FIXME("Unhandled operation %#x.\n", op);
9557 break;
9561 /* Context activation is done by the caller. */
9562 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9563 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9565 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9566 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9567 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9568 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9569 const struct wined3d_gl_info *gl_info = context->gl_info;
9570 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9571 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9572 UINT lowest_disabled_stage;
9573 GLuint shader_id;
9574 DWORD arg0, arg1, arg2;
9575 unsigned int stage;
9577 string_buffer_clear(buffer);
9579 /* Find out which textures are read */
9580 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9582 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9583 break;
9585 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9586 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9587 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9589 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9590 || (stage == 0 && settings->color_key_enabled))
9591 tex_map |= 1u << stage;
9592 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9593 tfactor_used = TRUE;
9594 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9595 tempreg_used = TRUE;
9596 if (settings->op[stage].tmp_dst)
9597 tempreg_used = TRUE;
9598 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9599 tss_const_map |= 1u << stage;
9601 switch (settings->op[stage].cop)
9603 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9604 lum_map |= 1u << stage;
9605 /* fall through */
9606 case WINED3D_TOP_BUMPENVMAP:
9607 bump_map |= 1u << stage;
9608 /* fall through */
9609 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9610 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9611 tex_map |= 1u << stage;
9612 break;
9614 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9615 tfactor_used = TRUE;
9616 break;
9618 default:
9619 break;
9622 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9623 continue;
9625 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9626 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9627 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9629 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9630 tex_map |= 1u << stage;
9631 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9632 tfactor_used = TRUE;
9633 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9634 tempreg_used = TRUE;
9635 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9636 tss_const_map |= 1u << stage;
9638 lowest_disabled_stage = stage;
9640 shader_glsl_add_version_declaration(buffer, gl_info);
9642 if (shader_glsl_use_explicit_attrib_location(gl_info))
9643 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9644 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9645 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9646 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9647 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9649 if (!needs_legacy_glsl_syntax(gl_info))
9651 shader_addline(buffer, "vec4 ps_out[1];\n");
9652 if (shader_glsl_use_explicit_attrib_location(gl_info))
9653 shader_addline(buffer, "layout(location = 0) ");
9654 shader_addline(buffer, "out vec4 color_out0;\n");
9657 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9658 shader_addline(buffer, "vec4 ret;\n");
9659 if (tempreg_used || settings->sRGB_write)
9660 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9661 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9663 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9665 const char *sampler_type;
9667 if (tss_const_map & (1u << stage))
9668 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9670 if (!(tex_map & (1u << stage)))
9671 continue;
9673 switch (settings->op[stage].tex_type)
9675 case WINED3D_GL_RES_TYPE_TEX_1D:
9676 sampler_type = "1D";
9677 break;
9678 case WINED3D_GL_RES_TYPE_TEX_2D:
9679 sampler_type = "2D";
9680 break;
9681 case WINED3D_GL_RES_TYPE_TEX_3D:
9682 sampler_type = "3D";
9683 break;
9684 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9685 sampler_type = "Cube";
9686 break;
9687 case WINED3D_GL_RES_TYPE_TEX_RECT:
9688 sampler_type = "2DRect";
9689 break;
9690 default:
9691 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9692 sampler_type = NULL;
9693 break;
9695 if (sampler_type)
9697 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9698 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9699 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9702 shader_addline(buffer, "vec4 tex%u;\n", stage);
9704 if (!(bump_map & (1u << stage)))
9705 continue;
9706 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9708 if (!(lum_map & (1u << stage)))
9709 continue;
9710 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9711 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9713 if (tfactor_used)
9714 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9715 if (settings->color_key_enabled)
9716 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9717 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9719 if (settings->sRGB_write)
9721 shader_addline(buffer, "const vec4 srgb_const0 = ");
9722 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9723 shader_addline(buffer, ";\n");
9724 shader_addline(buffer, "const vec4 srgb_const1 = ");
9725 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9726 shader_addline(buffer, ";\n");
9729 shader_addline(buffer, "uniform struct\n{\n");
9730 shader_addline(buffer, " vec4 color;\n");
9731 shader_addline(buffer, " float density;\n");
9732 shader_addline(buffer, " float end;\n");
9733 shader_addline(buffer, " float scale;\n");
9734 shader_addline(buffer, "} ffp_fog;\n");
9736 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9737 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9739 if (legacy_syntax)
9741 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9742 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9743 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9744 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9745 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9747 else
9749 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9750 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9751 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9752 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9753 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9756 shader_addline(buffer, "void main()\n{\n");
9758 if (legacy_syntax)
9760 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9761 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9764 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9766 if (tex_map & (1u << stage))
9768 if (settings->pointsprite)
9769 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9770 else if (settings->texcoords_initialized & (1u << stage))
9771 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9772 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9773 else
9774 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9778 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9779 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9781 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9782 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9784 /* Generate texture sampling instructions */
9785 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9787 const char *texture_function, *coord_mask;
9788 BOOL proj;
9790 if (!(tex_map & (1u << stage)))
9791 continue;
9793 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9795 proj = FALSE;
9797 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9798 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9800 proj = TRUE;
9802 else
9804 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9805 proj = TRUE;
9808 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9809 proj = FALSE;
9811 switch (settings->op[stage].tex_type)
9813 case WINED3D_GL_RES_TYPE_TEX_1D:
9814 if (proj)
9816 texture_function = "texture1DProj";
9817 coord_mask = "xw";
9819 else
9821 texture_function = "texture1D";
9822 coord_mask = "x";
9824 break;
9825 case WINED3D_GL_RES_TYPE_TEX_2D:
9826 if (proj)
9828 texture_function = "texture2DProj";
9829 coord_mask = "xyw";
9831 else
9833 texture_function = "texture2D";
9834 coord_mask = "xy";
9836 break;
9837 case WINED3D_GL_RES_TYPE_TEX_3D:
9838 if (proj)
9840 texture_function = "texture3DProj";
9841 coord_mask = "xyzw";
9843 else
9845 texture_function = "texture3D";
9846 coord_mask = "xyz";
9848 break;
9849 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9850 texture_function = "textureCube";
9851 coord_mask = "xyz";
9852 break;
9853 case WINED3D_GL_RES_TYPE_TEX_RECT:
9854 if (proj)
9856 texture_function = "texture2DRectProj";
9857 coord_mask = "xyw";
9859 else
9861 texture_function = "texture2DRect";
9862 coord_mask = "xy";
9864 break;
9865 default:
9866 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9867 texture_function = "";
9868 coord_mask = "xyzw";
9869 break;
9871 if (!legacy_syntax)
9872 texture_function = proj ? "textureProj" : "texture";
9874 if (stage > 0
9875 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9876 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9878 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9880 /* With projective textures, texbem only divides the static
9881 * texture coordinate, not the displacement, so multiply the
9882 * displacement with the dividing parameter before passing it to
9883 * TXP. */
9884 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9886 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9888 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9889 stage, stage);
9890 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9892 else
9894 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9895 stage, stage);
9896 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9899 else
9901 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9904 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9905 stage, texture_function, stage, coord_mask);
9907 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9908 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9909 stage, stage - 1, stage - 1, stage - 1);
9911 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9913 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9914 stage, texture_function, stage, stage);
9916 else
9918 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9919 stage, texture_function, stage, stage, coord_mask);
9922 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9923 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9924 settings->op[stage].color_fixup);
9927 if (settings->color_key_enabled)
9929 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9930 shader_addline(buffer, " discard;\n");
9933 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9935 /* Generate the main shader */
9936 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9938 BOOL op_equal;
9940 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9941 break;
9943 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9944 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9945 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9946 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9947 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9948 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9949 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9950 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9951 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9952 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9953 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9954 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9955 else
9956 op_equal = settings->op[stage].aop == settings->op[stage].cop
9957 && settings->op[stage].carg0 == settings->op[stage].aarg0
9958 && settings->op[stage].carg1 == settings->op[stage].aarg1
9959 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9961 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9963 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9964 settings->op[stage].cop, settings->op[stage].carg0,
9965 settings->op[stage].carg1, settings->op[stage].carg2);
9967 else if (op_equal)
9969 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9970 settings->op[stage].cop, settings->op[stage].carg0,
9971 settings->op[stage].carg1, settings->op[stage].carg2);
9973 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9974 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9976 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9977 settings->op[stage].cop, settings->op[stage].carg0,
9978 settings->op[stage].carg1, settings->op[stage].carg2);
9979 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9980 settings->op[stage].aop, settings->op[stage].aarg0,
9981 settings->op[stage].aarg1, settings->op[stage].aarg2);
9985 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9986 get_fragment_output(gl_info));
9988 if (settings->sRGB_write)
9989 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9991 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9993 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9994 if (!needs_legacy_glsl_syntax(gl_info))
9995 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9997 shader_addline(buffer, "}\n");
9999 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
10000 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
10002 string_buffer_release(&priv->string_buffers, tex_reg_name);
10003 return shader_id;
10006 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
10007 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
10009 struct glsl_ffp_vertex_shader *shader;
10010 const struct wine_rb_entry *entry;
10012 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
10013 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
10015 if (!(shader = heap_alloc(sizeof(*shader))))
10016 return NULL;
10018 shader->desc.settings = *settings;
10019 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
10020 list_init(&shader->linked_programs);
10021 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
10022 ERR("Failed to insert ffp vertex shader.\n");
10024 return shader;
10027 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
10028 const struct ffp_frag_settings *args, const struct wined3d_context *context)
10030 struct glsl_ffp_fragment_shader *glsl_desc;
10031 const struct ffp_frag_desc *desc;
10033 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
10034 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
10036 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
10037 return NULL;
10039 glsl_desc->entry.settings = *args;
10040 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
10041 list_init(&glsl_desc->linked_programs);
10042 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
10044 return glsl_desc;
10048 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
10049 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
10051 unsigned int i;
10052 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10054 for (i = 0; i < vs_c_count; ++i)
10056 string_buffer_sprintf(name, "vs_c[%u]", i);
10057 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10059 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
10061 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10063 string_buffer_sprintf(name, "vs_i[%u]", i);
10064 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10067 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10069 string_buffer_sprintf(name, "vs_b[%u]", i);
10070 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10073 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10075 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
10077 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
10078 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10080 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
10081 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
10082 for (i = 0; i < MAX_TEXTURES; ++i)
10084 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
10085 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10087 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
10088 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
10089 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
10090 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
10091 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
10092 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
10093 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
10095 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
10096 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10097 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
10098 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10099 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
10100 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10101 string_buffer_sprintf(name, "ffp_light[%u].position", i);
10102 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10103 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
10104 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10105 string_buffer_sprintf(name, "ffp_light[%u].range", i);
10106 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10107 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
10108 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10109 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
10110 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10111 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10112 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10113 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10114 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10115 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10116 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10117 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10118 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10120 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10121 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10122 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10123 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10124 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10125 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10126 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10128 string_buffer_release(&priv->string_buffers, name);
10131 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10132 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10134 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10137 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10138 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10140 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10143 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10144 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10146 unsigned int i;
10147 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10149 for (i = 0; i < ps_c_count; ++i)
10151 string_buffer_sprintf(name, "ps_c[%u]", i);
10152 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10154 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10156 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10158 string_buffer_sprintf(name, "ps_i[%u]", i);
10159 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10162 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10164 string_buffer_sprintf(name, "ps_b[%u]", i);
10165 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10168 for (i = 0; i < MAX_TEXTURES; ++i)
10170 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10171 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10172 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10173 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10174 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10175 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10176 string_buffer_sprintf(name, "tss_const%u", i);
10177 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10180 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10181 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10183 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10184 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10185 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10186 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10188 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10190 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10191 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10192 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10194 string_buffer_release(&priv->string_buffers, name);
10197 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10198 const struct wined3d_context *context, struct wined3d_shader *shader)
10200 struct glsl_context_data *ctx_data = context->shader_backend_data;
10201 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10202 const struct wined3d_gl_info *gl_info = context->gl_info;
10203 struct glsl_cs_compiled_shader *gl_shaders;
10204 struct glsl_shader_private *shader_data;
10205 struct glsl_shader_prog_link *entry;
10206 GLuint shader_id, program_id;
10208 if (!(entry = heap_alloc(sizeof(*entry))))
10210 ERR("Out of memory.\n");
10211 return E_OUTOFMEMORY;
10214 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10216 ERR("Failed to allocate backend data.\n");
10217 heap_free(entry);
10218 return E_OUTOFMEMORY;
10220 shader_data = shader->backend_data;
10221 gl_shaders = shader_data->gl_shaders.cs;
10223 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10225 ERR("Failed to allocate GL shader array.\n");
10226 heap_free(entry);
10227 heap_free(shader->backend_data);
10228 shader->backend_data = NULL;
10229 return E_OUTOFMEMORY;
10231 shader_data->shader_array_size = 1;
10232 gl_shaders = shader_data->gl_shaders.cs;
10234 TRACE("Compiling compute shader %p.\n", shader);
10236 string_buffer_clear(buffer);
10237 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
10238 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10240 program_id = GL_EXTCALL(glCreateProgram());
10241 TRACE("Created new GLSL shader program %u.\n", program_id);
10243 entry->id = program_id;
10244 entry->vs.id = 0;
10245 entry->hs.id = 0;
10246 entry->ds.id = 0;
10247 entry->gs.id = 0;
10248 entry->ps.id = 0;
10249 entry->cs.id = shader_id;
10250 entry->constant_version = 0;
10251 entry->shader_controlled_clip_distances = 0;
10252 entry->ps.np2_fixup_info = NULL;
10253 add_glsl_program_entry(priv, entry);
10255 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10256 GL_EXTCALL(glAttachShader(program_id, shader_id));
10257 checkGLcall("glAttachShader");
10259 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10261 TRACE("Linking GLSL shader program %u.\n", program_id);
10262 GL_EXTCALL(glLinkProgram(program_id));
10263 shader_glsl_validate_link(gl_info, program_id);
10265 GL_EXTCALL(glUseProgram(program_id));
10266 checkGLcall("glUseProgram");
10267 shader_glsl_load_program_resources(context, priv, program_id, shader);
10268 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10270 entry->constant_update_mask = 0;
10272 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10273 checkGLcall("glUseProgram");
10274 return WINED3D_OK;
10277 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
10278 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10280 struct glsl_shader_private *shader_data;
10282 if (!shader->backend_data)
10284 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10285 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
10287 ERR("Failed to compile compute shader %p.\n", shader);
10288 return 0;
10291 shader_data = shader->backend_data;
10292 return shader_data->gl_shaders.cs[0].id;
10295 /* Context activation is done by the caller. */
10296 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
10297 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10299 struct glsl_shader_prog_link *entry;
10300 struct wined3d_shader *shader;
10301 struct glsl_program_key key;
10302 GLuint cs_id;
10304 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10305 return;
10307 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10309 WARN("Compute shader is NULL.\n");
10310 ctx_data->glsl_program = NULL;
10311 return;
10314 cs_id = find_glsl_compute_shader(context, priv, shader);
10315 memset(&key, 0, sizeof(key));
10316 key.cs_id = cs_id;
10317 if (!(entry = get_glsl_program_entry(priv, &key)))
10318 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10319 ctx_data->glsl_program = entry;
10322 /* Context activation is done by the caller. */
10323 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
10324 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10326 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
10327 const struct wined3d_gl_info *gl_info = context->gl_info;
10328 const struct wined3d_shader *pre_rasterization_shader;
10329 const struct ps_np2fixup_info *np2fixup_info = NULL;
10330 struct wined3d_shader *hshader, *dshader, *gshader;
10331 struct glsl_shader_prog_link *entry = NULL;
10332 struct wined3d_shader *vshader = NULL;
10333 struct wined3d_shader *pshader = NULL;
10334 GLuint reorder_shader_id = 0;
10335 struct glsl_program_key key;
10336 GLuint program_id;
10337 unsigned int i;
10338 GLuint vs_id = 0;
10339 GLuint hs_id = 0;
10340 GLuint ds_id = 0;
10341 GLuint gs_id = 0;
10342 GLuint ps_id = 0;
10343 struct list *ps_list, *vs_list;
10344 WORD attribs_map;
10345 struct wined3d_string_buffer *tmp_name;
10347 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10349 vs_id = ctx_data->glsl_program->vs.id;
10350 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10352 if (use_vs(state))
10353 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10355 else if (use_vs(state))
10357 struct vs_compile_args vs_compile_args;
10359 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10361 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
10362 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
10363 vs_list = &vshader->linked_programs;
10365 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10367 struct glsl_ffp_vertex_shader *ffp_shader;
10368 struct wined3d_ffp_vs_settings settings;
10370 wined3d_ffp_get_vs_settings(context, state, &settings);
10371 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10372 vs_id = ffp_shader->id;
10373 vs_list = &ffp_shader->linked_programs;
10376 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10377 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10378 hs_id = ctx_data->glsl_program->hs.id;
10379 else if (hshader)
10380 hs_id = find_glsl_hull_shader(context, priv, hshader);
10382 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10383 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10385 ds_id = ctx_data->glsl_program->ds.id;
10387 else if (dshader)
10389 struct ds_compile_args args;
10391 find_ds_compile_args(state, dshader, &args, context);
10392 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
10395 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10396 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10398 gs_id = ctx_data->glsl_program->gs.id;
10400 else if (gshader)
10402 struct gs_compile_args args;
10404 find_gs_compile_args(state, gshader, &args, context);
10405 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
10408 /* A pixel shader is not used when rasterization is disabled. */
10409 if (is_rasterization_disabled(gshader))
10411 ps_id = 0;
10412 ps_list = NULL;
10414 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10416 ps_id = ctx_data->glsl_program->ps.id;
10417 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10419 if (use_ps(state))
10420 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10422 else if (use_ps(state))
10424 struct ps_compile_args ps_compile_args;
10425 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10426 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
10427 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
10428 pshader, &ps_compile_args, &np2fixup_info);
10429 ps_list = &pshader->linked_programs;
10431 else if (priv->fragment_pipe == &glsl_fragment_pipe
10432 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10434 struct glsl_ffp_fragment_shader *ffp_shader;
10435 struct ffp_frag_settings settings;
10437 gen_ffp_frag_op(context, state, &settings, FALSE);
10438 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
10439 ps_id = ffp_shader->id;
10440 ps_list = &ffp_shader->linked_programs;
10443 key.vs_id = vs_id;
10444 key.hs_id = hs_id;
10445 key.ds_id = ds_id;
10446 key.gs_id = gs_id;
10447 key.ps_id = ps_id;
10448 key.cs_id = 0;
10449 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10451 ctx_data->glsl_program = entry;
10452 return;
10455 /* If we get to this point, then no matching program exists, so we create one */
10456 program_id = GL_EXTCALL(glCreateProgram());
10457 TRACE("Created new GLSL shader program %u.\n", program_id);
10459 /* Create the entry */
10460 entry = heap_alloc(sizeof(*entry));
10461 entry->id = program_id;
10462 entry->vs.id = vs_id;
10463 entry->hs.id = hs_id;
10464 entry->ds.id = ds_id;
10465 entry->gs.id = gs_id;
10466 entry->ps.id = ps_id;
10467 entry->cs.id = 0;
10468 entry->constant_version = 0;
10469 entry->shader_controlled_clip_distances = 0;
10470 entry->ps.np2_fixup_info = np2fixup_info;
10471 /* Add the hash table entry */
10472 add_glsl_program_entry(priv, entry);
10474 /* Set the current program */
10475 ctx_data->glsl_program = entry;
10477 /* Attach GLSL vshader */
10478 if (vs_id)
10480 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10481 GL_EXTCALL(glAttachShader(program_id, vs_id));
10482 checkGLcall("glAttachShader");
10484 list_add_head(vs_list, &entry->vs.shader_entry);
10487 if (vshader)
10489 attribs_map = vshader->reg_maps.input_registers;
10490 if (vshader->reg_maps.shader_version.major < 4)
10492 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10493 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10494 d3d_info->emulated_flatshading
10495 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10496 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10497 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10498 checkGLcall("glAttachShader");
10499 /* Flag the reorder function for deletion, it will be freed
10500 * automatically when the program is destroyed. */
10501 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10504 else
10506 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10509 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10511 /* Bind vertex attributes to a corresponding index number to match
10512 * the same index numbers as ARB_vertex_programs (makes loading
10513 * vertex attributes simpler). With this method, we can use the
10514 * exact same code to load the attributes later for both ARB and
10515 * GLSL shaders.
10517 * We have to do this here because we need to know the Program ID
10518 * in order to make the bindings work, and it has to be done prior
10519 * to linking the GLSL program. */
10520 tmp_name = string_buffer_get(&priv->string_buffers);
10521 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10523 if (!(attribs_map & 1))
10524 continue;
10526 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10527 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10528 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10530 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10531 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10532 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10533 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10536 checkGLcall("glBindAttribLocation");
10538 if (!needs_legacy_glsl_syntax(gl_info))
10540 for (i = 0; i < MAX_RENDER_TARGET_VIEWS; ++i)
10542 string_buffer_sprintf(tmp_name, "color_out%u", i);
10543 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10544 checkGLcall("glBindFragDataLocation");
10547 string_buffer_release(&priv->string_buffers, tmp_name);
10550 if (hshader)
10552 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10553 GL_EXTCALL(glAttachShader(program_id, hs_id));
10554 checkGLcall("glAttachShader");
10556 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10559 if (dshader)
10561 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10562 GL_EXTCALL(glAttachShader(program_id, ds_id));
10563 checkGLcall("glAttachShader");
10565 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10568 if (gshader)
10570 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10571 GL_EXTCALL(glAttachShader(program_id, gs_id));
10572 checkGLcall("glAttachShader");
10574 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10576 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10579 /* Attach GLSL pshader */
10580 if (ps_id)
10582 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10583 GL_EXTCALL(glAttachShader(program_id, ps_id));
10584 checkGLcall("glAttachShader");
10586 list_add_head(ps_list, &entry->ps.shader_entry);
10589 /* Link the program */
10590 TRACE("Linking GLSL shader program %u.\n", program_id);
10591 GL_EXTCALL(glLinkProgram(program_id));
10592 shader_glsl_validate_link(gl_info, program_id);
10594 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10595 vshader ? vshader->limits->constant_float : 0);
10596 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10597 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10598 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10599 pshader ? pshader->limits->constant_float : 0);
10600 checkGLcall("find glsl program uniform locations");
10602 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10603 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10605 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10606 entry->shader_controlled_clip_distances = 1;
10607 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10610 if (needs_legacy_glsl_syntax(gl_info))
10612 if (pshader && pshader->reg_maps.shader_version.major >= 3
10613 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10615 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10616 entry->vs.vertex_color_clamp = GL_FALSE;
10618 else
10620 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10623 else
10625 /* With core profile we never change vertex_color_clamp from
10626 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10627 * glClampColorARB(). */
10628 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10631 /* Set the shader to allow uniform loading on it */
10632 GL_EXTCALL(glUseProgram(program_id));
10633 checkGLcall("glUseProgram");
10635 entry->constant_update_mask = 0;
10636 if (vshader)
10638 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10639 if (vshader->reg_maps.integer_constants)
10640 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10641 if (vshader->reg_maps.boolean_constants)
10642 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10643 if (entry->vs.pos_fixup_location != -1)
10644 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10646 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10648 else
10650 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10651 | WINED3D_SHADER_CONST_FFP_PROJ;
10653 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10655 if (entry->vs.modelview_matrix_location[i] != -1)
10657 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10658 break;
10662 for (i = 0; i < MAX_TEXTURES; ++i)
10664 if (entry->vs.texture_matrix_location[i] != -1)
10666 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10667 break;
10670 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10671 || entry->vs.material_specular_location != -1
10672 || entry->vs.material_emissive_location != -1
10673 || entry->vs.material_shininess_location != -1)
10674 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10675 if (entry->vs.light_ambient_location != -1)
10676 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10678 if (entry->vs.clip_planes_location != -1)
10679 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10680 if (entry->vs.pointsize_min_location != -1)
10681 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10683 if (hshader)
10684 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10686 if (dshader)
10688 if (entry->ds.pos_fixup_location != -1)
10689 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10691 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10694 if (gshader)
10696 if (entry->gs.pos_fixup_location != -1)
10697 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10699 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10702 if (ps_id)
10704 if (pshader)
10706 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10707 if (pshader->reg_maps.integer_constants)
10708 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10709 if (pshader->reg_maps.boolean_constants)
10710 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10711 if (entry->ps.ycorrection_location != -1)
10712 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10714 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10715 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10717 else
10719 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10721 shader_glsl_load_samplers(context, priv, program_id, NULL);
10724 for (i = 0; i < MAX_TEXTURES; ++i)
10726 if (entry->ps.bumpenv_mat_location[i] != -1)
10728 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10729 break;
10733 if (entry->ps.fog_color_location != -1)
10734 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10735 if (entry->ps.alpha_test_ref_location != -1)
10736 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10737 if (entry->ps.np2_fixup_location != -1)
10738 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10739 if (entry->ps.color_key_location != -1)
10740 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10744 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10746 struct wined3d_device *device = shader->device;
10747 struct wined3d_context *context;
10749 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10751 context = context_acquire(device, NULL, 0);
10752 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10753 context_release(context);
10757 /* Context activation is done by the caller. */
10758 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10759 const struct wined3d_state *state)
10761 struct glsl_context_data *ctx_data = context->shader_backend_data;
10762 const struct wined3d_gl_info *gl_info = context->gl_info;
10763 struct shader_glsl_priv *priv = shader_priv;
10764 struct glsl_shader_prog_link *glsl_program;
10765 GLenum current_vertex_color_clamp;
10766 GLuint program_id, prev_id;
10768 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10769 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10771 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10772 set_glsl_shader_program(context, state, priv, ctx_data);
10773 glsl_program = ctx_data->glsl_program;
10775 if (glsl_program)
10777 program_id = glsl_program->id;
10778 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10779 if (glsl_program->shader_controlled_clip_distances)
10780 context_enable_clip_distances(context, glsl_program->clip_distance_mask);
10782 else
10784 program_id = 0;
10785 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10788 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10790 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10791 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10793 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10794 checkGLcall("glClampColorARB");
10796 else
10798 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10802 TRACE("Using GLSL program %u.\n", program_id);
10804 if (prev_id != program_id)
10806 GL_EXTCALL(glUseProgram(program_id));
10807 checkGLcall("glUseProgram");
10809 if (glsl_program)
10810 context->constant_update_mask |= glsl_program->constant_update_mask;
10813 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10816 /* Context activation is done by the caller. */
10817 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10818 const struct wined3d_state *state)
10820 struct glsl_context_data *ctx_data = context->shader_backend_data;
10821 const struct wined3d_gl_info *gl_info = context->gl_info;
10822 struct shader_glsl_priv *priv = shader_priv;
10823 GLuint program_id, prev_id;
10825 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10826 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10827 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10829 TRACE("Using GLSL program %u.\n", program_id);
10831 if (prev_id != program_id)
10833 GL_EXTCALL(glUseProgram(program_id));
10834 checkGLcall("glUseProgram");
10837 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10838 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10839 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10840 | (1u << WINED3D_SHADER_TYPE_HULL)
10841 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10844 /* "context" is not necessarily the currently active context. */
10845 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10847 struct glsl_context_data *ctx_data = context->shader_backend_data;
10849 ctx_data->glsl_program = NULL;
10850 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10851 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10852 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10853 | (1u << WINED3D_SHADER_TYPE_HULL)
10854 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10855 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10858 /* Context activation is done by the caller. */
10859 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10861 struct glsl_context_data *ctx_data = context->shader_backend_data;
10862 const struct wined3d_gl_info *gl_info = context->gl_info;
10863 struct shader_glsl_priv *priv = shader_priv;
10865 shader_glsl_invalidate_current_program(context);
10866 GL_EXTCALL(glUseProgram(0));
10867 checkGLcall("glUseProgram");
10869 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10870 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10872 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10874 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10875 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10876 checkGLcall("glClampColorARB");
10880 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10881 const struct glsl_shader_prog_link *program)
10883 const struct glsl_context_data *ctx_data;
10884 struct wined3d_context *context;
10885 unsigned int i;
10887 for (i = 0; i < device->context_count; ++i)
10889 context = device->contexts[i];
10890 ctx_data = context->shader_backend_data;
10892 if (ctx_data->glsl_program == program)
10893 shader_glsl_invalidate_current_program(context);
10897 static void shader_glsl_destroy(struct wined3d_shader *shader)
10899 struct glsl_shader_private *shader_data = shader->backend_data;
10900 struct wined3d_device *device = shader->device;
10901 struct shader_glsl_priv *priv = device->shader_priv;
10902 const struct wined3d_gl_info *gl_info;
10903 const struct list *linked_programs;
10904 struct wined3d_context *context;
10906 if (!shader_data || !shader_data->num_gl_shaders)
10908 heap_free(shader_data);
10909 shader->backend_data = NULL;
10910 return;
10913 context = context_acquire(device, NULL, 0);
10914 gl_info = context->gl_info;
10916 TRACE("Deleting linked programs.\n");
10917 linked_programs = &shader->linked_programs;
10918 if (linked_programs->next)
10920 struct glsl_shader_prog_link *entry, *entry2;
10921 UINT i;
10923 switch (shader->reg_maps.shader_version.type)
10925 case WINED3D_SHADER_TYPE_PIXEL:
10927 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10929 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10931 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10932 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10933 checkGLcall("glDeleteShader");
10935 heap_free(shader_data->gl_shaders.ps);
10937 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10938 struct glsl_shader_prog_link, ps.shader_entry)
10940 shader_glsl_invalidate_contexts_program(device, entry);
10941 delete_glsl_program_entry(priv, gl_info, entry);
10944 break;
10947 case WINED3D_SHADER_TYPE_VERTEX:
10949 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10951 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10953 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10954 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10955 checkGLcall("glDeleteShader");
10957 heap_free(shader_data->gl_shaders.vs);
10959 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10960 struct glsl_shader_prog_link, vs.shader_entry)
10962 shader_glsl_invalidate_contexts_program(device, entry);
10963 delete_glsl_program_entry(priv, gl_info, entry);
10966 break;
10969 case WINED3D_SHADER_TYPE_HULL:
10971 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10973 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10975 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10976 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10977 checkGLcall("glDeleteShader");
10979 heap_free(shader_data->gl_shaders.hs);
10981 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10982 struct glsl_shader_prog_link, hs.shader_entry)
10984 shader_glsl_invalidate_contexts_program(device, entry);
10985 delete_glsl_program_entry(priv, gl_info, entry);
10988 break;
10991 case WINED3D_SHADER_TYPE_DOMAIN:
10993 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10995 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10997 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10998 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10999 checkGLcall("glDeleteShader");
11001 heap_free(shader_data->gl_shaders.ds);
11003 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11004 struct glsl_shader_prog_link, ds.shader_entry)
11006 shader_glsl_invalidate_contexts_program(device, entry);
11007 delete_glsl_program_entry(priv, gl_info, entry);
11010 break;
11013 case WINED3D_SHADER_TYPE_GEOMETRY:
11015 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
11017 for (i = 0; i < shader_data->num_gl_shaders; ++i)
11019 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
11020 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
11021 checkGLcall("glDeleteShader");
11023 heap_free(shader_data->gl_shaders.gs);
11025 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11026 struct glsl_shader_prog_link, gs.shader_entry)
11028 shader_glsl_invalidate_contexts_program(device, entry);
11029 delete_glsl_program_entry(priv, gl_info, entry);
11032 break;
11035 case WINED3D_SHADER_TYPE_COMPUTE:
11037 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
11039 for (i = 0; i < shader_data->num_gl_shaders; ++i)
11041 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
11042 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
11043 checkGLcall("glDeleteShader");
11045 heap_free(shader_data->gl_shaders.cs);
11047 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11048 struct glsl_shader_prog_link, cs.shader_entry)
11050 shader_glsl_invalidate_contexts_program(device, entry);
11051 delete_glsl_program_entry(priv, gl_info, entry);
11054 break;
11057 default:
11058 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
11059 break;
11063 heap_free(shader->backend_data);
11064 shader->backend_data = NULL;
11066 context_release(context);
11069 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
11071 const struct glsl_program_key *k = key;
11072 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
11073 const struct glsl_shader_prog_link, program_lookup_entry);
11075 if (k->vs_id > prog->vs.id) return 1;
11076 else if (k->vs_id < prog->vs.id) return -1;
11078 if (k->gs_id > prog->gs.id) return 1;
11079 else if (k->gs_id < prog->gs.id) return -1;
11081 if (k->ps_id > prog->ps.id) return 1;
11082 else if (k->ps_id < prog->ps.id) return -1;
11084 if (k->hs_id > prog->hs.id) return 1;
11085 else if (k->hs_id < prog->hs.id) return -1;
11087 if (k->ds_id > prog->ds.id) return 1;
11088 else if (k->ds_id < prog->ds.id) return -1;
11090 if (k->cs_id > prog->cs.id) return 1;
11091 else if (k->cs_id < prog->cs.id) return -1;
11093 return 0;
11096 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
11098 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
11099 + constant_count * sizeof(*heap->contained)
11100 + constant_count * sizeof(*heap->positions);
11101 void *mem;
11103 if (!(mem = heap_alloc(size)))
11105 ERR("Failed to allocate memory\n");
11106 return FALSE;
11109 heap->entries = mem;
11110 heap->entries[1].version = 0;
11111 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11112 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11113 heap->positions = (unsigned int *)(heap->contained + constant_count);
11114 heap->size = 1;
11116 return TRUE;
11119 static void constant_heap_free(struct constant_heap *heap)
11121 heap_free(heap->entries);
11124 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11125 const struct fragment_pipeline *fragment_pipe)
11127 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11128 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
11129 struct fragment_caps fragment_caps;
11130 void *vertex_priv, *fragment_priv;
11131 struct shader_glsl_priv *priv;
11133 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11134 return E_OUTOFMEMORY;
11136 string_buffer_list_init(&priv->string_buffers);
11138 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11140 ERR("Failed to initialize vertex pipe.\n");
11141 heap_free(priv);
11142 return E_FAIL;
11145 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11147 ERR("Failed to initialize fragment pipe.\n");
11148 vertex_pipe->vp_free(device);
11149 heap_free(priv);
11150 return E_FAIL;
11153 if (!string_buffer_init(&priv->shader_buffer))
11155 ERR("Failed to initialize shader buffer.\n");
11156 goto fail;
11159 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11161 ERR("Failed to allocate memory.\n");
11162 goto fail;
11165 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11167 ERR("Failed to initialize vertex shader constant heap\n");
11168 goto fail;
11171 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11173 ERR("Failed to initialize pixel shader constant heap\n");
11174 goto fail;
11177 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11179 priv->next_constant_version = 1;
11180 priv->vertex_pipe = vertex_pipe;
11181 priv->fragment_pipe = fragment_pipe;
11182 fragment_pipe->get_caps(gl_info, &fragment_caps);
11183 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11184 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11186 device->vertex_priv = vertex_priv;
11187 device->fragment_priv = fragment_priv;
11188 device->shader_priv = priv;
11190 return WINED3D_OK;
11192 fail:
11193 constant_heap_free(&priv->pconst_heap);
11194 constant_heap_free(&priv->vconst_heap);
11195 heap_free(priv->stack);
11196 string_buffer_free(&priv->shader_buffer);
11197 fragment_pipe->free_private(device);
11198 vertex_pipe->vp_free(device);
11199 heap_free(priv);
11200 return E_OUTOFMEMORY;
11203 /* Context activation is done by the caller. */
11204 static void shader_glsl_free(struct wined3d_device *device)
11206 struct shader_glsl_priv *priv = device->shader_priv;
11208 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11209 constant_heap_free(&priv->pconst_heap);
11210 constant_heap_free(&priv->vconst_heap);
11211 heap_free(priv->stack);
11212 string_buffer_list_cleanup(&priv->string_buffers);
11213 string_buffer_free(&priv->shader_buffer);
11214 priv->fragment_pipe->free_private(device);
11215 priv->vertex_pipe->vp_free(device);
11217 heap_free(device->shader_priv);
11218 device->shader_priv = NULL;
11221 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11223 struct glsl_context_data *ctx_data;
11225 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11226 return FALSE;
11227 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11228 context->shader_backend_data = ctx_data;
11229 return TRUE;
11232 static void shader_glsl_free_context_data(struct wined3d_context *context)
11234 heap_free(context->shader_backend_data);
11237 static void shader_glsl_init_context_state(struct wined3d_context *context)
11239 const struct wined3d_gl_info *gl_info = context->gl_info;
11241 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11242 checkGLcall("GL_PROGRAM_POINT_SIZE");
11245 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11247 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11248 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11249 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11251 if (shader_model_4
11252 && gl_info->supported[ARB_COMPUTE_SHADER]
11253 && gl_info->supported[ARB_CULL_DISTANCE]
11254 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11255 && gl_info->supported[ARB_GPU_SHADER5]
11256 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11257 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11258 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11259 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11260 && gl_info->supported[ARB_TESSELLATION_SHADER]
11261 && gl_info->supported[ARB_TEXTURE_GATHER]
11262 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11263 return 5;
11265 if (shader_model_4)
11266 return 4;
11268 /* Support for texldd and texldl instructions in pixel shaders is required
11269 * for SM3. */
11270 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11271 return 3;
11273 return 2;
11276 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
11278 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11280 TRACE("Shader model %u.\n", shader_model);
11282 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11283 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11284 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11285 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11286 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11287 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11289 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11290 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11292 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11293 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11294 caps->varying_count = gl_info->limits.glsl_varyings;
11296 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11297 * Direct3D minimum requirement.
11299 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11300 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11302 * The problem is that the refrast clamps temporary results in the shader to
11303 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11304 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11305 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11306 * offer a way to query this.
11308 if (shader_model >= 4)
11309 caps->ps_1x_max_value = FLT_MAX;
11310 else
11311 caps->ps_1x_max_value = 1024.0f;
11313 /* Ideally we'd only set caps like sRGB writes here if supported by both
11314 * the shader backend and the fragment pipe, but we can get called before
11315 * shader_glsl_alloc(). */
11316 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11317 | WINED3D_SHADER_CAP_SRGB_WRITE;
11320 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11322 /* We support everything except YUV conversions. */
11323 return !is_complex_fixup(fixup);
11326 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11328 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11329 /* WINED3DSIH_ADD */ shader_glsl_binop,
11330 /* WINED3DSIH_AND */ shader_glsl_binop,
11331 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11332 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11333 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11334 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11335 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11336 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11337 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11338 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11339 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11340 /* WINED3DSIH_BEM */ shader_glsl_bem,
11341 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11342 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11343 /* WINED3DSIH_BREAK */ shader_glsl_break,
11344 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11345 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11346 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11347 /* WINED3DSIH_CALL */ shader_glsl_call,
11348 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11349 /* WINED3DSIH_CASE */ shader_glsl_case,
11350 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11351 /* WINED3DSIH_CND */ shader_glsl_cnd,
11352 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11353 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11354 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11355 /* WINED3DSIH_CRS */ shader_glsl_cross,
11356 /* WINED3DSIH_CUT */ shader_glsl_cut,
11357 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11358 /* WINED3DSIH_DCL */ shader_glsl_nop,
11359 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11360 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11361 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11362 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11363 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11364 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11365 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11366 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11367 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11368 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11369 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11370 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11371 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11372 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11373 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11374 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
11375 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
11376 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11377 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11378 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11379 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11380 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11381 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11382 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11383 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11384 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11385 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11386 /* WINED3DSIH_DCL_STREAM */ NULL,
11387 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11388 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11389 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11390 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11391 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11392 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11393 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11394 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11395 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11396 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11397 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11398 /* WINED3DSIH_DEF */ shader_glsl_nop,
11399 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11400 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11401 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11402 /* WINED3DSIH_DIV */ shader_glsl_binop,
11403 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11404 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11405 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11406 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11407 /* WINED3DSIH_DST */ shader_glsl_dst,
11408 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11409 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11410 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11411 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11412 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11413 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11414 /* WINED3DSIH_ELSE */ shader_glsl_else,
11415 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11416 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11417 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11418 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11419 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11420 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11421 /* WINED3DSIH_EQ */ shader_glsl_relop,
11422 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ NULL,
11423 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11424 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11425 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11426 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11427 /* WINED3DSIH_FCALL */ NULL,
11428 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11429 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11430 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11431 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11432 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11433 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11434 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11435 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11436 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11437 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11438 /* WINED3DSIH_GE */ shader_glsl_relop,
11439 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11440 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11441 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11442 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11443 /* WINED3DSIH_IADD */ shader_glsl_binop,
11444 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11445 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11446 /* WINED3DSIH_IF */ shader_glsl_if,
11447 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11448 /* WINED3DSIH_IGE */ shader_glsl_relop,
11449 /* WINED3DSIH_ILT */ shader_glsl_relop,
11450 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11451 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11452 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11453 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11454 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11455 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11456 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11457 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11458 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11459 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11460 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11461 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11462 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11463 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11464 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11465 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11466 /* WINED3DSIH_INE */ shader_glsl_relop,
11467 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11468 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11469 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11470 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11471 /* WINED3DSIH_LABEL */ shader_glsl_label,
11472 /* WINED3DSIH_LD */ shader_glsl_ld,
11473 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11474 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11475 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11476 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11477 /* WINED3DSIH_LIT */ shader_glsl_lit,
11478 /* WINED3DSIH_LOD */ NULL,
11479 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11480 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11481 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11482 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11483 /* WINED3DSIH_LT */ shader_glsl_relop,
11484 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11485 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11486 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11487 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11488 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11489 /* WINED3DSIH_MAD */ shader_glsl_mad,
11490 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11491 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11492 /* WINED3DSIH_MOV */ shader_glsl_mov,
11493 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11494 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11495 /* WINED3DSIH_MUL */ shader_glsl_binop,
11496 /* WINED3DSIH_NE */ shader_glsl_relop,
11497 /* WINED3DSIH_NOP */ shader_glsl_nop,
11498 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11499 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11500 /* WINED3DSIH_OR */ shader_glsl_binop,
11501 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11502 /* WINED3DSIH_POW */ shader_glsl_pow,
11503 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11504 /* WINED3DSIH_REP */ shader_glsl_rep,
11505 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11506 /* WINED3DSIH_RET */ shader_glsl_ret,
11507 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11508 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11509 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11510 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11511 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11512 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11513 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11514 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11515 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11516 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11517 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11518 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11519 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11520 /* WINED3DSIH_SAMPLE_POS */ NULL,
11521 /* WINED3DSIH_SETP */ NULL,
11522 /* WINED3DSIH_SGE */ shader_glsl_compare,
11523 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11524 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11525 /* WINED3DSIH_SLT */ shader_glsl_compare,
11526 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11527 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11528 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11529 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11530 /* WINED3DSIH_SUB */ shader_glsl_binop,
11531 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11532 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11533 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11534 /* WINED3DSIH_TEX */ shader_glsl_tex,
11535 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11536 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11537 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11538 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11539 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11540 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11541 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11542 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11543 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11544 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11545 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11546 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11547 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11548 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11549 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11550 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11551 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11552 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11553 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11554 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11555 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11556 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11557 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11558 /* WINED3DSIH_UGE */ shader_glsl_relop,
11559 /* WINED3DSIH_ULT */ shader_glsl_relop,
11560 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11561 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11562 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11563 /* WINED3DSIH_USHR */ shader_glsl_binop,
11564 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11565 /* WINED3DSIH_XOR */ shader_glsl_binop,
11568 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11569 SHADER_HANDLER hw_fct;
11571 /* Select handler */
11572 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11574 /* Unhandled opcode */
11575 if (!hw_fct)
11577 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11578 return;
11580 hw_fct(ins);
11582 shader_glsl_add_instruction_modifiers(ins);
11585 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11587 struct shader_glsl_priv *priv = shader_priv;
11589 return priv->ffp_proj_control;
11592 const struct wined3d_shader_backend_ops glsl_shader_backend =
11594 shader_glsl_handle_instruction,
11595 shader_glsl_precompile,
11596 shader_glsl_select,
11597 shader_glsl_select_compute,
11598 shader_glsl_disable,
11599 shader_glsl_update_float_vertex_constants,
11600 shader_glsl_update_float_pixel_constants,
11601 shader_glsl_load_constants,
11602 shader_glsl_destroy,
11603 shader_glsl_alloc,
11604 shader_glsl_free,
11605 shader_glsl_allocate_context_data,
11606 shader_glsl_free_context_data,
11607 shader_glsl_init_context_state,
11608 shader_glsl_get_caps,
11609 shader_glsl_color_fixup_supported,
11610 shader_glsl_has_ffp_proj_control,
11613 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11615 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11617 caps->xyzrhw = TRUE;
11618 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11619 caps->ffp_generic_attributes = TRUE;
11620 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11621 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11622 caps->max_vertex_blend_matrix_index = 0;
11623 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11624 | WINED3DVTXPCAPS_MATERIALSOURCE7
11625 | WINED3DVTXPCAPS_VERTEXFOG
11626 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11627 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11628 | WINED3DVTXPCAPS_LOCALVIEWER
11629 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11630 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11631 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11632 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11635 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11637 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11638 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11639 return 0;
11642 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11644 struct shader_glsl_priv *priv;
11646 if (shader_backend == &glsl_shader_backend)
11648 priv = shader_priv;
11649 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11650 return priv;
11653 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11655 return NULL;
11658 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11660 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11661 struct glsl_ffp_vertex_shader, desc.entry);
11662 struct glsl_shader_prog_link *program, *program2;
11663 struct glsl_ffp_destroy_ctx *ctx = context;
11665 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11666 struct glsl_shader_prog_link, vs.shader_entry)
11668 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11670 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11671 heap_free(shader);
11674 /* Context activation is done by the caller. */
11675 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11677 struct shader_glsl_priv *priv = device->vertex_priv;
11678 struct glsl_ffp_destroy_ctx ctx;
11680 ctx.priv = priv;
11681 ctx.gl_info = &device->adapter->gl_info;
11682 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11685 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11686 const struct wined3d_state *state, DWORD state_id) {}
11688 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11689 const struct wined3d_state *state, DWORD state_id)
11691 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11694 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11695 const struct wined3d_state *state, DWORD state_id)
11697 const struct wined3d_gl_info *gl_info = context->gl_info;
11698 BOOL specular = !!(context->stream_info.use_map & (1u << WINED3D_FFP_SPECULAR));
11699 BOOL diffuse = !!(context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE));
11700 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11701 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11702 BOOL transformed = context->stream_info.position_transformed;
11703 BOOL wasrhw = context->last_was_rhw;
11704 unsigned int i;
11706 context->last_was_rhw = transformed;
11708 /* If the vertex declaration contains a transformed position attribute,
11709 * the draw uses the fixed function vertex pipeline regardless of any
11710 * vertex shader set by the application. */
11711 if (transformed != wasrhw
11712 || context->stream_info.swizzle_map != context->last_swizzle_map)
11713 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11715 context->last_swizzle_map = context->stream_info.swizzle_map;
11717 if (!use_vs(state))
11719 if (context->last_was_vshader)
11721 if (legacy_clip_planes)
11722 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11723 clipplane(context, state, STATE_CLIPPLANE(i));
11724 else
11725 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11728 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11730 /* Because of settings->texcoords, we have to regenerate the vertex
11731 * shader on a vdecl change if there aren't enough varyings to just
11732 * always output all the texture coordinates.
11734 * Likewise, we have to invalidate the shader when using per-vertex
11735 * colours and diffuse/specular attribute presence changes, or when
11736 * normal presence changes. */
11737 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11738 || (state->render_states[WINED3D_RS_COLORVERTEX]
11739 && (diffuse != context->last_was_diffuse || specular != context->last_was_specular))
11740 || normal != context->last_was_normal)
11741 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11743 if (use_ps(state)
11744 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11745 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11746 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11748 else
11750 if (!context->last_was_vshader)
11752 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11753 if (legacy_clip_planes)
11754 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11755 clipplane(context, state, STATE_CLIPPLANE(i));
11756 else
11757 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11761 context->last_was_vshader = use_vs(state);
11762 context->last_was_diffuse = diffuse;
11763 context->last_was_specular = specular;
11764 context->last_was_normal = normal;
11767 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11768 const struct wined3d_state *state, DWORD state_id)
11770 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11771 /* Different vertex shaders potentially require a different vertex attributes setup. */
11772 if (!isStateDirty(context, STATE_VDECL))
11773 context_apply_state(context, state, STATE_VDECL);
11776 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11777 const struct wined3d_state *state, DWORD state_id)
11779 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11780 * winding) are defined in Hull Shaders, while in GLSL those are
11781 * specified in Tessellation Evaluation Shaders. */
11782 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11784 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11785 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11788 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11789 const struct wined3d_state *state, DWORD state_id)
11791 struct glsl_context_data *ctx_data = context->shader_backend_data;
11792 BOOL rasterization_disabled;
11794 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11795 if (ctx_data->rasterization_disabled != rasterization_disabled)
11796 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11797 ctx_data->rasterization_disabled = rasterization_disabled;
11799 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11800 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11801 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11802 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11803 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11806 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11807 const struct wined3d_state *state, DWORD state_id)
11809 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11810 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11811 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11812 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11813 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11814 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11815 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11818 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11819 const struct wined3d_state *state, DWORD state_id)
11821 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11824 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11825 const struct wined3d_state *state, DWORD state_id)
11827 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11830 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11832 const struct wined3d_gl_info *gl_info = context->gl_info;
11833 unsigned int k;
11835 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11836 | WINED3D_SHADER_CONST_FFP_LIGHTS
11837 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11839 if (needs_legacy_glsl_syntax(gl_info))
11841 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11843 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11844 clipplane(context, state, STATE_CLIPPLANE(k));
11847 else
11849 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11853 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11854 const struct wined3d_state *state, DWORD state_id)
11856 /* Table fog behavior depends on the projection matrix. */
11857 if (state->render_states[WINED3D_RS_FOGENABLE]
11858 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11859 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11860 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11863 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11864 const struct wined3d_state *state, DWORD state_id)
11866 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11867 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11868 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11869 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11870 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11871 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11874 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11875 const struct wined3d_state *state, DWORD state_id)
11877 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11880 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11881 const struct wined3d_state *state, DWORD state_id)
11883 DWORD sampler = state_id - STATE_SAMPLER(0);
11884 const struct wined3d_texture *texture = state->textures[sampler];
11885 BOOL np2;
11887 if (!texture)
11888 return;
11890 if (sampler >= MAX_TEXTURES)
11891 return;
11893 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11894 || context->lastWasPow2Texture & (1u << sampler))
11896 if (np2)
11897 context->lastWasPow2Texture |= 1u << sampler;
11898 else
11899 context->lastWasPow2Texture &= ~(1u << sampler);
11901 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11905 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11906 const struct wined3d_state *state, DWORD state_id)
11908 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11911 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11912 const struct wined3d_state *state, DWORD state_id)
11914 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11917 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11918 const struct wined3d_state *state, DWORD state_id)
11920 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11923 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11924 const struct wined3d_state *state, DWORD state_id)
11926 if (!use_vs(state))
11927 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11930 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11931 const struct wined3d_state *state, DWORD state_id)
11933 static unsigned int once;
11935 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11936 FIXME("Non-point sprite points not supported in core profile.\n");
11939 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11940 const struct wined3d_state *state, DWORD state_id)
11942 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11945 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11946 const struct wined3d_state *state, DWORD state_id)
11948 const struct wined3d_gl_info *gl_info = context->gl_info;
11949 UINT index = state_id - STATE_CLIPPLANE(0);
11951 if (index >= gl_info->limits.user_clip_distances)
11952 return;
11954 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11957 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11959 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11960 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11961 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11962 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11963 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11964 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11965 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11966 /* Clip planes */
11967 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11968 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11969 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11970 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11971 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11972 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11973 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11974 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11975 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11976 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11977 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11978 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11979 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11980 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11981 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11982 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11983 /* Lights */
11984 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11985 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11986 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11987 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11988 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11989 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11990 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11991 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11992 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11993 /* Viewport */
11994 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11995 /* Transform states */
11996 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11997 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11998 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11999 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12000 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12001 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12002 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12003 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12004 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12005 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12006 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
12007 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
12008 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
12009 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
12010 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12011 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12012 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12013 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12014 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12015 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12016 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12017 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12018 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12019 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12020 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12021 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12022 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12023 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12024 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12025 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12026 /* Fog */
12027 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12028 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12029 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12030 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12031 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
12032 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
12033 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12034 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12035 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12036 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12037 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12038 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12039 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12040 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12041 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12042 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12043 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
12044 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
12045 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
12046 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
12047 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
12048 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
12049 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12050 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12051 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12052 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
12053 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12054 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12055 /* NP2 texture matrix fixups. They are not needed if
12056 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
12057 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
12058 * matrix. */
12059 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12060 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12061 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12062 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12063 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12064 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12065 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12066 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12067 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12068 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12069 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12070 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12071 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12072 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12073 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12074 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12075 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12076 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12077 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12078 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12079 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12080 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12081 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12082 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12083 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12084 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
12085 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
12086 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
12089 /* TODO:
12090 * - Implement vertex tweening. */
12091 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12093 glsl_vertex_pipe_vp_enable,
12094 glsl_vertex_pipe_vp_get_caps,
12095 glsl_vertex_pipe_vp_get_emul_mask,
12096 glsl_vertex_pipe_vp_alloc,
12097 glsl_vertex_pipe_vp_free,
12098 glsl_vertex_pipe_vp_states,
12101 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
12103 /* Nothing to do. */
12106 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
12108 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
12109 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
12110 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
12111 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12112 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12113 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12114 | WINED3DTEXOPCAPS_SELECTARG1
12115 | WINED3DTEXOPCAPS_SELECTARG2
12116 | WINED3DTEXOPCAPS_MODULATE4X
12117 | WINED3DTEXOPCAPS_MODULATE2X
12118 | WINED3DTEXOPCAPS_MODULATE
12119 | WINED3DTEXOPCAPS_ADDSIGNED2X
12120 | WINED3DTEXOPCAPS_ADDSIGNED
12121 | WINED3DTEXOPCAPS_ADD
12122 | WINED3DTEXOPCAPS_SUBTRACT
12123 | WINED3DTEXOPCAPS_ADDSMOOTH
12124 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12125 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12126 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12127 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12128 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12129 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12130 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12131 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12132 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12133 | WINED3DTEXOPCAPS_DOTPRODUCT3
12134 | WINED3DTEXOPCAPS_MULTIPLYADD
12135 | WINED3DTEXOPCAPS_LERP
12136 | WINED3DTEXOPCAPS_BUMPENVMAP
12137 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12138 caps->MaxTextureBlendStages = MAX_TEXTURES;
12139 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
12142 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12144 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12145 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12146 return 0;
12149 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12151 struct shader_glsl_priv *priv;
12153 if (shader_backend == &glsl_shader_backend)
12155 priv = shader_priv;
12156 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12157 return priv;
12160 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12162 return NULL;
12165 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
12167 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12168 struct glsl_ffp_fragment_shader, entry.entry);
12169 struct glsl_shader_prog_link *program, *program2;
12170 struct glsl_ffp_destroy_ctx *ctx = context;
12172 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12173 struct glsl_shader_prog_link, ps.shader_entry)
12175 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
12177 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
12178 heap_free(shader);
12181 /* Context activation is done by the caller. */
12182 static void glsl_fragment_pipe_free(struct wined3d_device *device)
12184 struct shader_glsl_priv *priv = device->fragment_priv;
12185 struct glsl_ffp_destroy_ctx ctx;
12187 ctx.priv = priv;
12188 ctx.gl_info = &device->adapter->gl_info;
12189 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12192 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12193 const struct wined3d_state *state, DWORD state_id)
12195 context->last_was_pshader = use_ps(state);
12197 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12200 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12201 const struct wined3d_state *state, DWORD state_id)
12203 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12206 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12207 const struct wined3d_state *state, DWORD state_id)
12209 BOOL use_vshader = use_vs(state);
12210 enum fogsource new_source;
12211 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12212 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12214 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12216 if (!state->render_states[WINED3D_RS_FOGENABLE])
12217 return;
12219 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12221 if (use_vshader)
12222 new_source = FOGSOURCE_VS;
12223 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12224 new_source = FOGSOURCE_COORD;
12225 else
12226 new_source = FOGSOURCE_FFP;
12228 else
12230 new_source = FOGSOURCE_FFP;
12233 if (new_source != context->fog_source || fogstart == fogend)
12235 context->fog_source = new_source;
12236 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12240 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12241 const struct wined3d_state *state, DWORD state_id)
12243 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12244 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
12245 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12247 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12248 glsl_fragment_pipe_fog(context, state, state_id);
12251 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12252 const struct wined3d_state *state, DWORD state_id)
12254 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12255 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
12256 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12259 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12260 const struct wined3d_state *state, DWORD state_id)
12262 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12265 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12266 const struct wined3d_state *state, DWORD state_id)
12268 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12271 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12272 const struct wined3d_state *state, DWORD state_id)
12274 const struct wined3d_gl_info *gl_info = context->gl_info;
12275 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12276 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
12278 if (func)
12280 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12281 checkGLcall("glAlphaFunc");
12285 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12286 const struct wined3d_state *state, DWORD state_id)
12288 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12291 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12292 const struct wined3d_state *state, DWORD state_id)
12294 const struct wined3d_gl_info *gl_info = context->gl_info;
12296 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12298 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12299 checkGLcall("glEnable(GL_ALPHA_TEST)");
12301 else
12303 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12304 checkGLcall("glDisable(GL_ALPHA_TEST)");
12308 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12309 const struct wined3d_state *state, DWORD state_id)
12311 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12314 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12315 const struct wined3d_state *state, DWORD state_id)
12317 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12320 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12321 const struct wined3d_state *state, DWORD state_id)
12323 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12326 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
12328 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12329 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12330 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12331 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12332 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12333 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12334 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12335 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12336 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12337 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12338 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12339 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12340 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12341 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12342 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12343 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12344 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12345 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12346 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12347 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12348 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12349 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12350 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12351 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12352 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12353 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12354 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12355 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12356 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12357 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12358 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12359 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12360 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12361 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12362 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12363 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12364 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12365 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12366 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12367 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12368 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12369 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12370 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12371 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12372 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12373 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12374 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12375 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12376 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12377 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12378 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12379 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12380 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12381 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12382 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12383 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12384 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12385 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12386 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12387 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12388 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12389 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12390 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12391 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12392 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12393 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12394 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12395 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12396 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12397 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12398 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12399 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12400 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12401 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12402 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12403 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12404 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12405 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12406 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12407 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12408 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12409 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12410 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12411 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12412 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12413 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12414 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12415 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12416 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12417 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12418 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12419 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12420 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12421 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12422 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12423 {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 },
12424 {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 },
12425 {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 },
12426 {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 },
12427 {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 },
12428 {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 },
12429 {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 },
12430 {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 },
12431 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12432 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12433 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12434 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12435 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12436 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12437 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12438 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12439 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12440 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12441 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12442 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12443 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12446 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12448 return TRUE;
12451 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12455 const struct fragment_pipeline glsl_fragment_pipe =
12457 glsl_fragment_pipe_enable,
12458 glsl_fragment_pipe_get_caps,
12459 glsl_fragment_pipe_get_emul_mask,
12460 glsl_fragment_pipe_alloc,
12461 glsl_fragment_pipe_free,
12462 glsl_fragment_pipe_alloc_context_data,
12463 glsl_fragment_pipe_free_context_data,
12464 shader_glsl_color_fixup_supported,
12465 glsl_fragment_pipe_state_template,
12468 struct glsl_blitter_args
12470 GLenum texture_type;
12471 struct color_fixup_desc fixup;
12472 unsigned short padding;
12475 struct glsl_blitter_program
12477 struct wine_rb_entry entry;
12478 struct glsl_blitter_args args;
12479 GLuint id;
12482 struct wined3d_glsl_blitter
12484 struct wined3d_blitter blitter;
12485 struct wined3d_string_buffer_list string_buffers;
12486 struct wine_rb_tree programs;
12487 GLuint palette_texture;
12490 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12492 const struct glsl_blitter_args *a = key;
12493 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12495 return memcmp(a, b, sizeof(*a));
12498 /* Context activation is done by the caller. */
12499 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12501 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12502 struct wined3d_context *context = ctx;
12503 const struct wined3d_gl_info *gl_info = context->gl_info;
12505 GL_EXTCALL(glDeleteProgram(program->id));
12506 checkGLcall("glDeleteProgram()");
12507 heap_free(program);
12510 /* Context activation is done by the caller. */
12511 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12513 const struct wined3d_gl_info *gl_info = context->gl_info;
12514 struct wined3d_glsl_blitter *glsl_blitter;
12515 struct wined3d_blitter *next;
12517 if ((next = blitter->next))
12518 next->ops->blitter_destroy(next, context);
12520 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12522 if (glsl_blitter->palette_texture)
12523 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12525 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context);
12526 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12528 heap_free(glsl_blitter);
12531 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12532 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12533 const char *output, const char *tex_type, const char *swizzle)
12535 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12536 shader_addline(buffer, "\nvoid main()\n{\n");
12537 /* The alpha-component contains the palette index. */
12538 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12539 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12540 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12541 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12542 * the middle. */
12543 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12544 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12545 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12546 shader_addline(buffer, "}\n");
12549 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12550 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12551 const char *tex_type)
12553 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12554 char chroma, luminance;
12555 const char *tex;
12557 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12558 * macropixel, giving effectively 16 bits per pixel. The color consists of
12559 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12560 * luminance values, one for each single pixel it contains, and one U and
12561 * one V value shared between both pixels.
12563 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12564 * component contains the luminance and alpha the chroma. With UYVY it is
12565 * vice versa. Thus take the format into account when generating the read
12566 * swizzles
12568 * Reading the Y value is straightforward - just sample the texture. The
12569 * hardware takes care of filtering in the horizontal and vertical
12570 * direction.
12572 * Reading the U and V values is harder. We have to avoid filtering
12573 * horizontally, because that would mix the U and V values of one pixel or
12574 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12575 * get an unfiltered read, regardless of the filtering setting. Vertical
12576 * filtering works automatically though - the U and V values of two rows
12577 * are mixed nicely.
12579 * Apart of avoiding filtering issues, the code has to know which value it
12580 * just read, and where it can find the other one. To determine this, it
12581 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12582 * accordingly.
12584 * Handling horizontal filtering of U and V values requires reading a 2nd
12585 * pair of pixels, extracting U and V and mixing them. This is not
12586 * implemented yet.
12588 * An alternative implementation idea is to load the texture as A8R8G8B8
12589 * texture, with width / 2. This way one read gives all 3 values, finding
12590 * U and V is easy in an unfiltered situation. Finding the luminance on
12591 * the other hand requires finding out if it is an odd or even pixel. The
12592 * real drawback of this approach is filtering. This would have to be
12593 * emulated completely in the shader, reading up two 2 packed pixels in up
12594 * to 2 rows and interpolating both horizontally and vertically. Beyond
12595 * that it would require adjustments to the texture handling code to deal
12596 * with the width scaling. */
12598 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12600 chroma = 'x';
12601 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12603 else
12605 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12606 luminance = 'x';
12609 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12611 /* First we have to read the chroma values. This means we need at least
12612 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12613 * unmodified chroma, we have to rid ourselves of the filtering when we
12614 * sample the texture. */
12615 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12616 /* We must not allow filtering between pixel x and x+1, this would mix U
12617 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12618 * center is at 0.5, so add 0.5. */
12619 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12620 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12622 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12623 * and 0.75 for the even and odd pixels respectively. */
12624 /* Put the value into either of the chroma values. */
12625 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12626 shader_addline(buffer, " if (even)\n");
12627 shader_addline(buffer, " chroma.y = luminance;\n");
12628 shader_addline(buffer, " else\n");
12629 shader_addline(buffer, " chroma.x = luminance;\n");
12631 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12632 * current one. Otherwise, sample the left pixel. */
12633 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12634 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12636 /* Put the value into the other chroma. */
12637 shader_addline(buffer, " if (even)\n");
12638 shader_addline(buffer, " chroma.x = luminance;\n");
12639 shader_addline(buffer, " else\n");
12640 shader_addline(buffer, " chroma.y = luminance;\n");
12642 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12643 * the current one and lerp the two U and V values. */
12645 /* This gives the correctly filtered luminance value. */
12646 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12649 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12650 const struct wined3d_gl_info *gl_info, const char *tex_type)
12652 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12653 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12655 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12656 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12657 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12658 * have only half the pitch of the luminance plane, the packing into the
12659 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12660 * luminance data it looks approximately like this:
12662 * +----------------------------------+----
12663 * | |
12664 * | |
12665 * | |
12666 * | |
12667 * | | 2
12668 * | LUMINANCE | -
12669 * | | 3
12670 * | |
12671 * | |
12672 * | |
12673 * | |
12674 * +----------------+-----------------+----
12675 * | | |
12676 * | V even rows | V odd rows |
12677 * | | | 1
12678 * +----------------+------------------ -
12679 * | | | 3
12680 * | U even rows | U odd rows |
12681 * | | |
12682 * +----------------+-----------------+----
12683 * | | |
12684 * | 0.5 | 0.5 |
12686 * So it appears as if there are 4 chroma images, but in fact the odd rows
12687 * in the chroma images are in the same row as the even ones. So it is
12688 * kinda tricky to read. */
12690 /* First sample the chroma values. */
12691 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12692 /* The chroma planes have only half the width. */
12693 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12695 /* The first value is between 2/3 and 5/6 of the texture's height, so
12696 * scale+bias the coordinate. Also read the right side of the image when
12697 * reading odd lines.
12699 * Don't forget to clamp the y values in into the range, otherwise we'll
12700 * get filtering bleeding. */
12702 /* Read odd lines from the right side (add 0.5 to the x coordinate). */
12703 shader_addline(buffer, " if (fract(floor(texcoord.y * size.y) * 0.5 + 1.0 / 6.0) >= 0.5)\n");
12704 shader_addline(buffer, " texcoord.x += 0.5;\n");
12706 /* Clamp, keep the half pixel origin in mind. */
12707 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12708 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12710 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12712 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12713 * 6/6th No need to clamp because we're just reusing the already clamped
12714 * value from above. */
12715 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12716 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12718 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12719 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12720 * values from bleeding into the sampled luminance values due to
12721 * filtering. */
12722 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12723 /* Multiply the y coordinate by 2/3 and clamp it. */
12724 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12725 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12728 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12729 const struct wined3d_gl_info *gl_info, const char *tex_type)
12731 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12732 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12734 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12735 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12736 * effective bitdepth is 12 bits per pixel. If the whole texture is
12737 * interpreted as luminance data it looks approximately like this:
12739 * +----------------------------------+----
12740 * | |
12741 * | |
12742 * | |
12743 * | |
12744 * | | 2
12745 * | LUMINANCE | -
12746 * | | 3
12747 * | |
12748 * | |
12749 * | |
12750 * | |
12751 * +----------------------------------+----
12752 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12753 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12754 * | | 1
12755 * | | -
12756 * | | 3
12757 * | |
12758 * | |
12759 * +----------------------------------+---- */
12761 /* First sample the chroma values. */
12762 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12763 /* We only have half the number of chroma pixels. */
12764 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12765 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12767 /* We must not allow filtering horizontally, this would mix U and V.
12768 * Vertical filtering is ok. However, bear in mind that the pixel center
12769 * is at 0.5, so add 0.5. */
12771 /* Convert to non-normalised coordinates so we can find the individual
12772 * pixel. */
12773 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12774 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12775 * 0.5 to hit the center of the pixel. Then convert back to normalised
12776 * coordinates. */
12777 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12778 /* Clamp, keep the half pixel origin in mind. */
12779 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12781 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12782 /* Add 1.0 / size.x to sample the adjacent texel. */
12783 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12784 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12786 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12787 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12788 * values from bleeding into the sampled luminance values due to
12789 * filtering. */
12790 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12791 /* Multiply the y coordinate by 2/3 and clamp it. */
12792 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12793 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12796 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12797 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12798 const char *output, const char *tex_type, const char *swizzle)
12800 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12802 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12803 shader_addline(buffer, "float luminance;\n");
12804 shader_addline(buffer, "vec2 texcoord;\n");
12805 shader_addline(buffer, "vec2 chroma;\n");
12806 shader_addline(buffer, "uniform vec2 size;\n");
12808 shader_addline(buffer, "\nvoid main()\n{\n");
12810 switch (complex_fixup)
12812 case COMPLEX_FIXUP_UYVY:
12813 case COMPLEX_FIXUP_YUY2:
12814 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12815 break;
12817 case COMPLEX_FIXUP_YV12:
12818 gen_yv12_read(buffer, gl_info, tex_type);
12819 break;
12821 case COMPLEX_FIXUP_NV12:
12822 gen_nv12_read(buffer, gl_info, tex_type);
12823 break;
12825 default:
12826 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12827 string_buffer_free(buffer);
12828 return;
12831 /* Calculate the final result. Formula is taken from
12832 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12833 * ranges from -0.5 to 0.5. */
12834 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12836 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12837 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12838 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12840 shader_addline(buffer, "}\n");
12843 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12844 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12845 const char *output, const char *tex_type, const char *swizzle)
12847 shader_addline(buffer, "\nvoid main()\n{\n");
12848 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12849 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12850 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12851 shader_addline(buffer, "}\n");
12854 /* Context activation is done by the caller. */
12855 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12856 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12858 static const struct
12860 GLenum texture_target;
12861 const char texture_type[7];
12862 const char texcoord_swizzle[4];
12864 texture_data[] =
12866 {GL_TEXTURE_2D, "2D", "xy"},
12867 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12868 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12870 static const char vshader_main[] =
12871 "\n"
12872 "void main()\n"
12873 "{\n"
12874 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12875 " out_texcoord = texcoord;\n"
12876 "}\n";
12877 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12878 struct wined3d_string_buffer *buffer, *output;
12879 GLuint program, vshader_id, fshader_id;
12880 const char *tex_type, *swizzle, *ptr;
12881 unsigned int i;
12882 GLint loc;
12884 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12886 if (args->texture_type == texture_data[i].texture_target)
12888 tex_type = texture_data[i].texture_type;
12889 swizzle = texture_data[i].texcoord_swizzle;
12890 break;
12893 if (i == ARRAY_SIZE(texture_data))
12895 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12896 return 0;
12899 program = GL_EXTCALL(glCreateProgram());
12901 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12903 buffer = string_buffer_get(&blitter->string_buffers);
12904 shader_glsl_add_version_declaration(buffer, gl_info);
12905 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12906 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12907 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12908 shader_addline(buffer, vshader_main);
12910 ptr = buffer->buffer;
12911 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12912 GL_EXTCALL(glAttachShader(program, vshader_id));
12913 GL_EXTCALL(glDeleteShader(vshader_id));
12915 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12917 string_buffer_clear(buffer);
12918 shader_glsl_add_version_declaration(buffer, gl_info);
12919 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12920 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12921 /* TODO: Declare the out variable with the correct type (and put it in the
12922 * blitter args). */
12923 if (!needs_legacy_glsl_syntax(gl_info))
12924 shader_addline(buffer, "out vec4 ps_out[1];\n");
12926 output = string_buffer_get(&blitter->string_buffers);
12927 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12929 switch (complex_fixup)
12931 case COMPLEX_FIXUP_P8:
12932 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12933 break;
12934 case COMPLEX_FIXUP_YUY2:
12935 case COMPLEX_FIXUP_UYVY:
12936 case COMPLEX_FIXUP_YV12:
12937 case COMPLEX_FIXUP_NV12:
12938 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12939 break;
12940 case COMPLEX_FIXUP_NONE:
12941 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12944 string_buffer_release(&blitter->string_buffers, output);
12946 ptr = buffer->buffer;
12947 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12948 string_buffer_release(&blitter->string_buffers, buffer);
12949 GL_EXTCALL(glAttachShader(program, fshader_id));
12950 GL_EXTCALL(glDeleteShader(fshader_id));
12952 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12953 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12955 if (!needs_legacy_glsl_syntax(gl_info))
12956 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12958 GL_EXTCALL(glCompileShader(vshader_id));
12959 print_glsl_info_log(gl_info, vshader_id, FALSE);
12960 GL_EXTCALL(glCompileShader(fshader_id));
12961 print_glsl_info_log(gl_info, fshader_id, FALSE);
12962 GL_EXTCALL(glLinkProgram(program));
12963 shader_glsl_validate_link(gl_info, program);
12965 GL_EXTCALL(glUseProgram(program));
12966 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12967 GL_EXTCALL(glUniform1i(loc, 0));
12968 if (complex_fixup == COMPLEX_FIXUP_P8)
12970 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12971 GL_EXTCALL(glUniform1i(loc, 1));
12974 return program;
12977 /* Context activation is done by the caller. */
12978 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12979 struct wined3d_context *context, const struct wined3d_texture *texture)
12981 const struct wined3d_gl_info *gl_info = context->gl_info;
12982 const struct wined3d_palette *palette;
12984 palette = texture->swapchain ? texture->swapchain->palette : NULL;
12986 if (!blitter->palette_texture)
12987 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12989 context_active_texture(context, gl_info, 1);
12990 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12991 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12992 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12993 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12995 if (palette)
12997 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12998 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
13000 else
13002 static const DWORD black;
13004 FIXME("P8 texture loaded without a palette.\n");
13005 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
13006 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
13009 context_active_texture(context, gl_info, 0);
13012 /* Context activation is done by the caller. */
13013 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
13014 struct wined3d_context *context, const struct wined3d_texture *texture)
13016 const struct wined3d_gl_info *gl_info = context->gl_info;
13017 struct glsl_blitter_program *program;
13018 struct glsl_blitter_args args;
13019 struct wine_rb_entry *entry;
13021 memset(&args, 0, sizeof(args));
13022 args.texture_type = texture->target;
13023 args.fixup = texture->resource.format->color_fixup;
13025 if ((entry = wine_rb_get(&blitter->programs, &args)))
13026 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
13028 if (!(program = heap_alloc(sizeof(*program))))
13030 ERR("Failed to allocate blitter program memory.\n");
13031 return NULL;
13034 program->args = args;
13035 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
13037 WARN("Failed to generate blitter program.\n");
13038 heap_free(program);
13039 return NULL;
13042 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
13044 ERR("Failed to store blitter program.\n");
13045 GL_EXTCALL(glDeleteProgram(program->id));
13046 heap_free(program);
13047 return NULL;
13050 return program;
13053 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
13054 const struct wined3d_texture *src_texture, DWORD src_location,
13055 const struct wined3d_texture *dst_texture, DWORD dst_location)
13057 const struct wined3d_resource *src_resource = &src_texture->resource;
13058 const struct wined3d_resource *dst_resource = &dst_texture->resource;
13059 const struct wined3d_format *src_format = src_resource->format;
13060 const struct wined3d_format *dst_format = dst_resource->format;
13061 BOOL decompress;
13063 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13065 if (dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
13066 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13067 else
13068 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13071 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT)
13073 TRACE("Unsupported blit_op %#x.\n", blit_op);
13074 return FALSE;
13077 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13078 return FALSE;
13080 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13081 || dst_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13082 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY
13083 || dst_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13085 TRACE("Multi-sample textures not supported.\n");
13086 return FALSE;
13089 /* We don't necessarily want to blit from resources without
13090 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13091 * compressed textures. */
13092 decompress = src_format && (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
13093 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
13094 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13096 TRACE("Source or destination resource does not have GPU access.\n");
13097 return FALSE;
13100 if (!is_identity_fixup(dst_format->color_fixup)
13101 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13103 TRACE("Destination fixups are not supported.\n");
13104 return FALSE;
13107 TRACE("Returning supported.\n");
13108 return TRUE;
13111 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13112 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13113 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13114 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13115 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
13117 struct wined3d_device *device = dst_texture->resource.device;
13118 const struct wined3d_gl_info *gl_info = context->gl_info;
13119 struct wined3d_texture *staging_texture = NULL;
13120 struct wined3d_glsl_blitter *glsl_blitter;
13121 struct glsl_blitter_program *program;
13122 struct wined3d_blitter *next;
13123 unsigned int src_level;
13124 GLint location;
13125 RECT s, d;
13127 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
13128 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
13129 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13130 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13131 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
13133 if (!glsl_blitter_supported(op, context, src_texture, src_location, dst_texture, dst_location))
13135 if (!(next = blitter->next))
13137 ERR("No blitter to handle blit op %#x.\n", op);
13138 return dst_location;
13141 TRACE("Forwarding to blitter %p.\n", next);
13142 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13143 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
13146 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13148 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13150 struct wined3d_resource_desc desc;
13151 struct wined3d_box upload_box;
13152 HRESULT hr;
13154 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13156 src_level = src_sub_resource_idx % src_texture->level_count;
13157 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13158 desc.format = src_texture->resource.format->id;
13159 desc.multisample_type = src_texture->resource.multisample_type;
13160 desc.multisample_quality = src_texture->resource.multisample_quality;
13161 desc.usage = WINED3DUSAGE_PRIVATE;
13162 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13163 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13164 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13165 desc.depth = 1;
13166 desc.size = 0;
13168 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13169 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13171 ERR("Failed to create staging texture, hr %#x.\n", hr);
13172 return dst_location;
13175 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13176 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13177 src_texture, src_sub_resource_idx, &upload_box);
13179 src_texture = staging_texture;
13180 src_sub_resource_idx = 0;
13182 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13183 && (src_texture->sub_resources[src_sub_resource_idx].locations
13184 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13185 && !wined3d_resource_is_offscreen(&src_texture->resource))
13188 /* Without FBO blits transferring from the drawable to the texture is
13189 * expensive, because we have to flip the data in sysmem. Since we can
13190 * flip in the blitter, we don't actually need that flip anyway. So we
13191 * use the surface's texture as scratch texture, and flip the source
13192 * rectangle instead. */
13193 texture2d_load_fb_texture(src_texture, src_sub_resource_idx, FALSE, context);
13195 s = *src_rect;
13196 src_level = src_sub_resource_idx % src_texture->level_count;
13197 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13198 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13199 src_rect = &s;
13201 else
13203 wined3d_texture_load(src_texture, context, FALSE);
13206 context_apply_blit_state(context, device);
13208 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13210 d = *dst_rect;
13211 wined3d_texture_translate_drawable_coords(dst_texture, context->win_handle, &d);
13212 dst_rect = &d;
13215 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
13217 GLenum buffer;
13219 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13221 TRACE("Destination texture %p is onscreen.\n", dst_texture);
13222 buffer = wined3d_texture_get_gl_buffer(dst_texture);
13224 else
13226 TRACE("Destination texture %p is offscreen.\n", dst_texture);
13227 buffer = GL_COLOR_ATTACHMENT0;
13229 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER,
13230 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
13231 context_set_draw_buffer(context, buffer);
13232 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
13233 context_invalidate_state(context, STATE_FRAMEBUFFER);
13236 if (!(program = glsl_blitter_get_program(glsl_blitter, context, src_texture)))
13238 ERR("Failed to get blitter program.\n");
13239 return dst_location;
13241 GL_EXTCALL(glUseProgram(program->id));
13242 switch (get_complex_fixup(program->args.fixup))
13244 case COMPLEX_FIXUP_P8:
13245 glsl_blitter_upload_palette(glsl_blitter, context, src_texture);
13246 break;
13248 case COMPLEX_FIXUP_YUY2:
13249 case COMPLEX_FIXUP_UYVY:
13250 case COMPLEX_FIXUP_YV12:
13251 case COMPLEX_FIXUP_NV12:
13252 src_level = src_sub_resource_idx % src_texture->level_count;
13253 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13254 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13255 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13256 break;
13258 default:
13259 break;
13261 context_draw_shaded_quad(context, src_texture, src_sub_resource_idx, src_rect, dst_rect, filter);
13262 GL_EXTCALL(glUseProgram(0));
13264 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13265 gl_info->gl_ops.gl.p_glFlush();
13267 if (staging_texture)
13268 wined3d_texture_decref(staging_texture);
13270 return dst_location;
13273 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13274 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13275 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
13277 struct wined3d_blitter *next;
13279 if ((next = blitter->next))
13280 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13281 clear_rects, draw_rect, flags, color, depth, stencil);
13284 static const struct wined3d_blitter_ops glsl_blitter_ops =
13286 glsl_blitter_destroy,
13287 glsl_blitter_clear,
13288 glsl_blitter_blit,
13291 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13292 const struct wined3d_device *device)
13294 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13295 struct wined3d_glsl_blitter *blitter;
13297 if (device->shader_backend != &glsl_shader_backend)
13298 return NULL;
13300 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13301 return NULL;
13303 if (!(blitter = heap_alloc(sizeof(*blitter))))
13305 ERR("Failed to allocate blitter.\n");
13306 return NULL;
13309 TRACE("Created blitter %p.\n", blitter);
13311 blitter->blitter.ops = &glsl_blitter_ops;
13312 blitter->blitter.next = *next;
13313 string_buffer_list_init(&blitter->string_buffers);
13314 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13315 blitter->palette_texture = 0;
13316 *next = &blitter->blitter;
13318 return *next;