wined3d: Allow wined3d_texture_upload_data() to upload to WINED3D_LOCATION_TEXTURE_SRGB.
[wine.git] / dlls / wined3d / glsl_shader.c
blob6cc030aaa68095f8bb81d7ae383bc97695522fc0
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
32 #include "config.h"
33 #include "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
37 #ifdef HAVE_FLOAT_H
38 # include <float.h>
39 #endif
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
48 #define WINED3D_GLSL_SAMPLE_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
53 static const struct
55 unsigned int coord_size;
56 unsigned int resinfo_size;
57 const char *type_part;
59 resource_type_info[] =
61 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
62 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
63 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
64 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
66 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
67 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
68 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
69 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
70 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
71 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
74 struct glsl_dst_param
76 char reg_name[150];
77 char mask_str[6];
80 struct glsl_src_param
82 char reg_name[150];
83 char param_str[200];
86 struct glsl_sample_function
88 struct wined3d_string_buffer *name;
89 unsigned int coord_mask;
90 unsigned int deriv_mask;
91 enum wined3d_data_type data_type;
92 BOOL output_single_component;
93 unsigned int offset_size;
96 enum heap_node_op
98 HEAP_NODE_TRAVERSE_LEFT,
99 HEAP_NODE_TRAVERSE_RIGHT,
100 HEAP_NODE_POP,
103 struct constant_entry
105 unsigned int idx;
106 unsigned int version;
109 struct constant_heap
111 struct constant_entry *entries;
112 BOOL *contained;
113 unsigned int *positions;
114 unsigned int size;
117 /* GLSL shader private data */
118 struct shader_glsl_priv
120 struct wined3d_string_buffer shader_buffer;
121 struct wined3d_string_buffer_list string_buffers;
122 struct wine_rb_tree program_lookup;
123 struct constant_heap vconst_heap;
124 struct constant_heap pconst_heap;
125 unsigned char *stack;
126 UINT next_constant_version;
128 const struct wined3d_vertex_pipe_ops *vertex_pipe;
129 const struct fragment_pipeline *fragment_pipe;
130 struct wine_rb_tree ffp_vertex_shaders;
131 struct wine_rb_tree ffp_fragment_shaders;
132 BOOL ffp_proj_control;
133 BOOL legacy_lighting;
136 struct glsl_vs_program
138 struct list shader_entry;
139 GLuint id;
140 GLenum vertex_color_clamp;
141 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
142 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
143 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
144 GLint pos_fixup_location;
146 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
147 GLint projection_matrix_location;
148 GLint normal_matrix_location;
149 GLint texture_matrix_location[MAX_TEXTURES];
150 GLint material_ambient_location;
151 GLint material_diffuse_location;
152 GLint material_specular_location;
153 GLint material_emissive_location;
154 GLint material_shininess_location;
155 GLint light_ambient_location;
156 struct
158 GLint diffuse;
159 GLint specular;
160 GLint ambient;
161 GLint position;
162 GLint direction;
163 GLint range;
164 GLint falloff;
165 GLint c_att;
166 GLint l_att;
167 GLint q_att;
168 GLint cos_htheta;
169 GLint cos_hphi;
170 } light_location[MAX_ACTIVE_LIGHTS];
171 GLint pointsize_location;
172 GLint pointsize_min_location;
173 GLint pointsize_max_location;
174 GLint pointsize_c_att_location;
175 GLint pointsize_l_att_location;
176 GLint pointsize_q_att_location;
177 GLint clip_planes_location;
180 struct glsl_hs_program
182 struct list shader_entry;
183 GLuint id;
186 struct glsl_ds_program
188 struct list shader_entry;
189 GLuint id;
191 GLint pos_fixup_location;
194 struct glsl_gs_program
196 struct list shader_entry;
197 GLuint id;
199 GLint pos_fixup_location;
202 struct glsl_ps_program
204 struct list shader_entry;
205 GLuint id;
206 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
207 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
208 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
209 GLint bumpenv_mat_location[MAX_TEXTURES];
210 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
211 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
212 GLint tss_constant_location[MAX_TEXTURES];
213 GLint tex_factor_location;
214 GLint specular_enable_location;
215 GLint fog_color_location;
216 GLint fog_density_location;
217 GLint fog_end_location;
218 GLint fog_scale_location;
219 GLint alpha_test_ref_location;
220 GLint ycorrection_location;
221 GLint np2_fixup_location;
222 GLint color_key_location;
223 const struct ps_np2fixup_info *np2_fixup_info;
226 struct glsl_cs_program
228 struct list shader_entry;
229 GLuint id;
232 /* Struct to maintain data about a linked GLSL program */
233 struct glsl_shader_prog_link
235 struct wine_rb_entry program_lookup_entry;
236 struct glsl_vs_program vs;
237 struct glsl_hs_program hs;
238 struct glsl_ds_program ds;
239 struct glsl_gs_program gs;
240 struct glsl_ps_program ps;
241 struct glsl_cs_program cs;
242 GLuint id;
243 DWORD constant_update_mask;
244 unsigned int constant_version;
245 DWORD shader_controlled_clip_distances : 1;
246 DWORD clip_distance_mask : 8; /* MAX_CLIP_DISTANCES, 8 */
247 DWORD padding : 23;
250 struct glsl_program_key
252 GLuint vs_id;
253 GLuint hs_id;
254 GLuint ds_id;
255 GLuint gs_id;
256 GLuint ps_id;
257 GLuint cs_id;
260 struct shader_glsl_ctx_priv {
261 const struct vs_compile_args *cur_vs_args;
262 const struct ds_compile_args *cur_ds_args;
263 const struct ps_compile_args *cur_ps_args;
264 struct ps_np2fixup_info *cur_np2fixup_info;
265 struct wined3d_string_buffer_list *string_buffers;
268 struct glsl_context_data
270 struct glsl_shader_prog_link *glsl_program;
271 GLenum vertex_color_clamp;
272 BOOL rasterization_disabled;
275 struct glsl_ps_compiled_shader
277 struct ps_compile_args args;
278 struct ps_np2fixup_info np2fixup;
279 GLuint id;
282 struct glsl_vs_compiled_shader
284 struct vs_compile_args args;
285 GLuint id;
288 struct glsl_hs_compiled_shader
290 GLuint id;
293 struct glsl_ds_compiled_shader
295 struct ds_compile_args args;
296 GLuint id;
299 struct glsl_gs_compiled_shader
301 struct gs_compile_args args;
302 GLuint id;
305 struct glsl_cs_compiled_shader
307 GLuint id;
310 struct glsl_shader_private
312 union
314 struct glsl_vs_compiled_shader *vs;
315 struct glsl_hs_compiled_shader *hs;
316 struct glsl_ds_compiled_shader *ds;
317 struct glsl_gs_compiled_shader *gs;
318 struct glsl_ps_compiled_shader *ps;
319 struct glsl_cs_compiled_shader *cs;
320 } gl_shaders;
321 unsigned int num_gl_shaders, shader_array_size;
324 struct glsl_ffp_vertex_shader
326 struct wined3d_ffp_vs_desc desc;
327 GLuint id;
328 struct list linked_programs;
331 struct glsl_ffp_fragment_shader
333 struct ffp_frag_desc entry;
334 GLuint id;
335 struct list linked_programs;
338 struct glsl_ffp_destroy_ctx
340 struct shader_glsl_priv *priv;
341 const struct wined3d_gl_info *gl_info;
344 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
346 static const char *debug_gl_shader_type(GLenum type)
348 switch (type)
350 #define WINED3D_TO_STR(u) case u: return #u
351 WINED3D_TO_STR(GL_VERTEX_SHADER);
352 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
353 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
354 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
355 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
356 WINED3D_TO_STR(GL_COMPUTE_SHADER);
357 #undef WINED3D_TO_STR
358 default:
359 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
363 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
365 switch (type)
367 case WINED3D_SHADER_TYPE_VERTEX:
368 return "vs";
370 case WINED3D_SHADER_TYPE_HULL:
371 return "hs";
373 case WINED3D_SHADER_TYPE_DOMAIN:
374 return "ds";
376 case WINED3D_SHADER_TYPE_GEOMETRY:
377 return "gs";
379 case WINED3D_SHADER_TYPE_PIXEL:
380 return "ps";
382 case WINED3D_SHADER_TYPE_COMPUTE:
383 return "cs";
385 default:
386 FIXME("Unhandled shader type %#x.\n", type);
387 return "unknown";
391 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
393 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
394 return 440;
395 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
396 return 150;
397 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
398 return 130;
399 else
400 return 120;
403 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
404 const struct wined3d_gl_info *gl_info)
406 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
409 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
411 char str[4][17];
413 wined3d_ftoa(values[0], str[0]);
414 wined3d_ftoa(values[1], str[1]);
415 wined3d_ftoa(values[2], str[2]);
416 wined3d_ftoa(values[3], str[3]);
417 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
420 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
421 const int *values, unsigned int size)
423 int i;
425 if (!size || size > 4)
427 ERR("Invalid vector size %u.\n", size);
428 return;
431 if (size > 1)
432 shader_addline(buffer, "ivec%u(", size);
434 for (i = 0; i < size; ++i)
435 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
437 if (size > 1)
438 shader_addline(buffer, ")");
441 static const char *get_info_log_line(const char **ptr)
443 const char *p, *q;
445 p = *ptr;
446 if (!(q = strstr(p, "\n")))
448 if (!*p) return NULL;
449 *ptr += strlen(p);
450 return p;
452 *ptr = q + 1;
454 return p;
457 /* Context activation is done by the caller. */
458 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
460 int length = 0;
461 char *log;
463 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
464 return;
466 if (program)
467 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
468 else
469 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
471 /* A size of 1 is just a null-terminated string, so the log should be bigger than
472 * that if there are errors. */
473 if (length > 1)
475 const char *ptr, *line;
477 log = heap_alloc(length);
478 /* The info log is supposed to be zero-terminated, but at least some
479 * versions of fglrx don't terminate the string properly. The reported
480 * length does include the terminator, so explicitly set it to zero
481 * here. */
482 log[length - 1] = 0;
483 if (program)
484 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
485 else
486 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
488 ptr = log;
489 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
491 WARN("Info log received from GLSL shader #%u:\n", id);
492 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
494 else
496 FIXME("Info log received from GLSL shader #%u:\n", id);
497 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
499 heap_free(log);
503 /* Context activation is done by the caller. */
504 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
506 const char *ptr, *line;
508 TRACE("Compiling shader object %u.\n", shader);
510 if (TRACE_ON(d3d_shader))
512 ptr = src;
513 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
516 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
517 checkGLcall("glShaderSource");
518 GL_EXTCALL(glCompileShader(shader));
519 checkGLcall("glCompileShader");
520 print_glsl_info_log(gl_info, shader, FALSE);
523 /* Context activation is done by the caller. */
524 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
526 GLint i, shader_count, source_size = -1;
527 GLuint *shaders;
528 char *source = NULL;
530 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
531 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
533 ERR("Failed to allocate shader array memory.\n");
534 return;
537 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
538 for (i = 0; i < shader_count; ++i)
540 const char *ptr, *line;
541 GLint tmp;
543 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
545 if (source_size < tmp)
547 heap_free(source);
549 if (!(source = heap_alloc_zero(tmp)))
551 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
552 heap_free(shaders);
553 return;
555 source_size = tmp;
558 FIXME("Shader %u:\n", shaders[i]);
559 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
560 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
561 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
562 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
563 FIXME("\n");
565 ptr = source;
566 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
567 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
568 FIXME("\n");
571 heap_free(source);
572 heap_free(shaders);
575 /* Context activation is done by the caller. */
576 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
578 GLint tmp;
580 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
581 return;
583 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
584 if (!tmp)
586 FIXME("Program %u link status invalid.\n", program);
587 shader_glsl_dump_program_source(gl_info, program);
590 print_glsl_info_log(gl_info, program, TRUE);
593 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
595 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
596 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
597 * versions. */
598 return shader_glsl_get_version(gl_info) >= 140;
601 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
603 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
606 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
607 struct shader_glsl_priv *priv, GLuint program_id,
608 const struct wined3d_shader_reg_maps *reg_maps)
610 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
611 struct wined3d_string_buffer *name;
612 unsigned int i, base, count;
613 GLuint block_idx;
615 if (shader_glsl_use_layout_binding_qualifier(gl_info))
616 return;
618 name = string_buffer_get(&priv->string_buffers);
619 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
620 for (i = 0; i < count; ++i)
622 if (!reg_maps->cb_sizes[i])
623 continue;
625 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
626 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
627 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
629 checkGLcall("glUniformBlockBinding");
630 string_buffer_release(&priv->string_buffers, name);
633 /* Context activation is done by the caller. */
634 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
635 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
636 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
638 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
639 unsigned int i, mapped_unit;
640 GLint name_loc;
642 for (i = 0; i < count; ++i)
644 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
645 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
646 if (name_loc == -1)
647 continue;
649 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
650 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
652 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
653 continue;
656 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
657 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
659 checkGLcall("Load sampler bindings");
660 string_buffer_release(&priv->string_buffers, sampler_name);
663 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
664 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
666 const DWORD *tex_unit_map;
667 unsigned int base, count;
669 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
670 if (sampler_idx >= count)
671 return WINED3D_UNMAPPED_STAGE;
672 if (!tex_unit_map)
673 return base + sampler_idx;
674 return tex_unit_map[base + sampler_idx];
677 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
678 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
679 unsigned int sampler_idx)
681 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
682 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
683 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
684 else
685 ERR("Unmapped sampler %u.\n", sampler_idx);
688 /* Context activation is done by the caller. */
689 static void shader_glsl_load_samplers(const struct wined3d_context *context,
690 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
692 const struct wined3d_gl_info *gl_info = context->gl_info;
693 const struct wined3d_shader_version *shader_version;
694 const DWORD *tex_unit_map;
695 unsigned int base, count;
696 const char *prefix;
698 if (shader_glsl_use_layout_binding_qualifier(gl_info))
699 return;
701 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
702 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
703 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
704 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
707 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
708 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
710 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
712 if (icb)
714 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
715 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
716 GLint icb_location;
718 string_buffer_sprintf(icb_name, "%s_icb", prefix);
719 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
720 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
721 checkGLcall("Load immediate constant buffer");
723 string_buffer_release(&priv->string_buffers, icb_name);
727 /* Context activation is done by the caller. */
728 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
729 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
731 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
732 struct wined3d_string_buffer *name;
733 GLint location;
734 unsigned int i;
736 if (shader_glsl_use_layout_binding_qualifier(gl_info))
737 return;
739 name = string_buffer_get(&priv->string_buffers);
740 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
742 if (!reg_maps->uav_resource_info[i].type)
743 continue;
745 string_buffer_sprintf(name, "%s_image%u", prefix, i);
746 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
747 if (location == -1)
748 continue;
750 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
751 GL_EXTCALL(glUniform1i(location, i));
753 checkGLcall("Load image bindings");
754 string_buffer_release(&priv->string_buffers, name);
757 /* Context activation is done by the caller. */
758 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
759 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
761 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
763 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
764 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
765 /* Texture unit mapping is set up to be the same each time the shader
766 * program is used so we can hardcode the sampler uniform values. */
767 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
770 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
771 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
773 if (varyings && *strings)
775 char *ptr = *strings;
777 varyings[*varying_count] = ptr;
779 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
780 ptr += buffer->content_size + 1;
782 *strings = ptr;
785 *strings_length += buffer->content_size + 1;
786 ++(*varying_count);
789 static void append_transform_feedback_skip_components(const char **varyings,
790 unsigned int *varying_count, char **strings, unsigned int *strings_length,
791 struct wined3d_string_buffer *buffer, unsigned int component_count)
793 unsigned int j;
795 for (j = 0; j < component_count / 4; ++j)
797 string_buffer_sprintf(buffer, "gl_SkipComponents4");
798 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
800 if (component_count % 4)
802 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
803 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
807 static void shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
808 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
809 char *strings, unsigned int *strings_length, GLenum buffer_mode)
811 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
813 count = length = 0;
814 highest_output_slot = 0;
815 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
817 stride = 0;
819 for (i = 0; i < so_desc->element_count; ++i)
821 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
823 highest_output_slot = max(highest_output_slot, e->output_slot);
824 if (e->output_slot != buffer_idx)
825 continue;
827 if (e->stream_idx)
829 FIXME("Unhandled stream %u.\n", e->stream_idx);
830 continue;
833 stride += e->component_count;
835 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
837 append_transform_feedback_skip_components(varyings, &count,
838 &strings, &length, buffer, e->component_count);
839 continue;
842 if (e->component_idx || e->component_count != 4)
844 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
846 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
847 append_transform_feedback_skip_components(varyings, &count,
848 &strings, &length, buffer, e->component_count);
849 continue;
852 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
853 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
854 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
856 else
858 string_buffer_sprintf(buffer, "shader_in_out.reg%u", e->register_idx);
859 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
863 if (buffer_idx < so_desc->buffer_stride_count
864 && stride < so_desc->buffer_strides[buffer_idx] / 4)
866 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
867 append_transform_feedback_skip_components(varyings, &count,
868 &strings, &length, buffer, component_count);
871 if (highest_output_slot <= buffer_idx)
872 break;
874 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
876 string_buffer_sprintf(buffer, "gl_NextBuffer");
877 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
881 if (varying_count)
882 *varying_count = count;
883 if (strings_length)
884 *strings_length = length;
887 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
888 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
890 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
891 const struct wined3d_gl_info *gl_info = context->gl_info;
892 struct wined3d_string_buffer *buffer;
893 unsigned int i, count, length;
894 const char **varyings;
895 char *strings;
896 GLenum mode;
898 if (!so_desc->element_count)
899 return;
901 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
903 mode = GL_INTERLEAVED_ATTRIBS;
905 else
907 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
909 for (i = 0; i < so_desc->element_count; ++i)
911 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
913 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
914 return;
916 ++element_count[so_desc->elements[i].output_slot];
919 if (element_count[0] == so_desc->element_count)
921 mode = GL_INTERLEAVED_ATTRIBS;
923 else
925 mode = GL_SEPARATE_ATTRIBS;
926 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
928 if (element_count[i] != 1)
929 break;
931 for (; i < ARRAY_SIZE(element_count); ++i)
933 if (element_count[i])
935 FIXME("Only single element per buffer is allowed in separate mode.\n");
936 return;
942 buffer = string_buffer_get(&priv->string_buffers);
944 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode);
946 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
948 ERR("Out of memory.\n");
949 string_buffer_release(&priv->string_buffers, buffer);
950 return;
952 if (!(strings = heap_calloc(length, sizeof(*strings))))
954 ERR("Out of memory.\n");
955 heap_free(varyings);
956 string_buffer_release(&priv->string_buffers, buffer);
957 return;
960 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
961 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
962 checkGLcall("glTransformFeedbackVaryings");
964 heap_free(varyings);
965 heap_free(strings);
966 string_buffer_release(&priv->string_buffers, buffer);
969 /* Context activation is done by the caller. */
970 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
971 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
973 unsigned int start = ~0U, end = 0;
974 int stack_idx = 0;
975 unsigned int heap_idx = 1;
976 unsigned int idx;
978 if (heap->entries[heap_idx].version <= version) return;
980 idx = heap->entries[heap_idx].idx;
981 if (constant_locations[idx] != -1)
982 start = end = idx;
983 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
985 while (stack_idx >= 0)
987 /* Note that we fall through to the next case statement. */
988 switch(stack[stack_idx])
990 case HEAP_NODE_TRAVERSE_LEFT:
992 unsigned int left_idx = heap_idx << 1;
993 if (left_idx < heap->size && heap->entries[left_idx].version > version)
995 heap_idx = left_idx;
996 idx = heap->entries[heap_idx].idx;
997 if (constant_locations[idx] != -1)
999 if (start > idx)
1000 start = idx;
1001 if (end < idx)
1002 end = idx;
1005 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1006 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1007 break;
1011 case HEAP_NODE_TRAVERSE_RIGHT:
1013 unsigned int right_idx = (heap_idx << 1) + 1;
1014 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1016 heap_idx = right_idx;
1017 idx = heap->entries[heap_idx].idx;
1018 if (constant_locations[idx] != -1)
1020 if (start > idx)
1021 start = idx;
1022 if (end < idx)
1023 end = idx;
1026 stack[stack_idx++] = HEAP_NODE_POP;
1027 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1028 break;
1032 case HEAP_NODE_POP:
1033 heap_idx >>= 1;
1034 --stack_idx;
1035 break;
1038 if (start <= end)
1039 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1040 checkGLcall("walk_constant_heap()");
1043 /* Context activation is done by the caller. */
1044 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1045 GLint location, const struct wined3d_vec4 *data)
1047 GLfloat clamped_constant[4];
1049 if (location == -1) return;
1051 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1052 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1053 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1054 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1056 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1059 /* Context activation is done by the caller. */
1060 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1061 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1062 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1064 int stack_idx = 0;
1065 unsigned int heap_idx = 1;
1066 unsigned int idx;
1068 if (heap->entries[heap_idx].version <= version) return;
1070 idx = heap->entries[heap_idx].idx;
1071 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1072 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1074 while (stack_idx >= 0)
1076 /* Note that we fall through to the next case statement. */
1077 switch(stack[stack_idx])
1079 case HEAP_NODE_TRAVERSE_LEFT:
1081 unsigned int left_idx = heap_idx << 1;
1082 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1084 heap_idx = left_idx;
1085 idx = heap->entries[heap_idx].idx;
1086 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1088 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1089 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1090 break;
1094 case HEAP_NODE_TRAVERSE_RIGHT:
1096 unsigned int right_idx = (heap_idx << 1) + 1;
1097 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1099 heap_idx = right_idx;
1100 idx = heap->entries[heap_idx].idx;
1101 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1103 stack[stack_idx++] = HEAP_NODE_POP;
1104 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1105 break;
1109 case HEAP_NODE_POP:
1110 heap_idx >>= 1;
1111 --stack_idx;
1112 break;
1115 checkGLcall("walk_constant_heap_clamped()");
1118 /* Context activation is done by the caller. */
1119 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1120 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1121 unsigned char *stack, unsigned int version)
1123 const struct wined3d_shader_lconst *lconst;
1125 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1126 if (shader->reg_maps.shader_version.major == 1
1127 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1128 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1129 else
1130 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1132 if (!shader->load_local_constsF)
1134 TRACE("No need to load local float constants for this shader.\n");
1135 return;
1138 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1139 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1141 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1143 checkGLcall("glUniform4fv()");
1146 /* Context activation is done by the caller. */
1147 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1148 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1150 unsigned int i;
1151 struct list* ptr;
1153 for (i = 0; constants_set; constants_set >>= 1, ++i)
1155 if (!(constants_set & 1)) continue;
1157 /* We found this uniform name in the program - go ahead and send the data */
1158 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1161 /* Load immediate constants */
1162 ptr = list_head(&shader->constantsI);
1163 while (ptr)
1165 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1166 unsigned int idx = lconst->idx;
1167 const GLint *values = (const GLint *)lconst->value;
1169 /* We found this uniform name in the program - go ahead and send the data */
1170 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1171 ptr = list_next(&shader->constantsI, ptr);
1173 checkGLcall("glUniform4iv()");
1176 /* Context activation is done by the caller. */
1177 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1178 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1180 unsigned int i;
1181 struct list* ptr;
1183 for (i = 0; constants_set; constants_set >>= 1, ++i)
1185 if (!(constants_set & 1)) continue;
1187 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1190 /* Load immediate constants */
1191 ptr = list_head(&shader->constantsB);
1192 while (ptr)
1194 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1195 unsigned int idx = lconst->idx;
1196 const GLint *values = (const GLint *)lconst->value;
1198 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1199 ptr = list_next(&shader->constantsB, ptr);
1201 checkGLcall("glUniform1iv()");
1204 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1206 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1209 /* Context activation is done by the caller (state handler). */
1210 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1211 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1213 struct
1215 float sx, sy;
1217 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1218 UINT fixup = ps->np2_fixup_info->active;
1219 UINT i;
1221 for (i = 0; fixup; fixup >>= 1, ++i)
1223 const struct wined3d_texture *tex = state->textures[i];
1224 unsigned char idx = ps->np2_fixup_info->idx[i];
1226 if (!tex)
1228 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1229 continue;
1232 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1233 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1236 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1239 /* Taken and adapted from Mesa. */
1240 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1242 float pos, neg, t, det;
1243 struct wined3d_matrix temp;
1245 /* Calculate the determinant of upper left 3x3 submatrix and
1246 * determine if the matrix is singular. */
1247 pos = neg = 0.0f;
1248 t = in->_11 * in->_22 * in->_33;
1249 if (t >= 0.0f)
1250 pos += t;
1251 else
1252 neg += t;
1254 t = in->_21 * in->_32 * in->_13;
1255 if (t >= 0.0f)
1256 pos += t;
1257 else
1258 neg += t;
1259 t = in->_31 * in->_12 * in->_23;
1260 if (t >= 0.0f)
1261 pos += t;
1262 else
1263 neg += t;
1265 t = -in->_31 * in->_22 * in->_13;
1266 if (t >= 0.0f)
1267 pos += t;
1268 else
1269 neg += t;
1270 t = -in->_21 * in->_12 * in->_33;
1271 if (t >= 0.0f)
1272 pos += t;
1273 else
1274 neg += t;
1276 t = -in->_11 * in->_32 * in->_23;
1277 if (t >= 0.0f)
1278 pos += t;
1279 else
1280 neg += t;
1282 det = pos + neg;
1284 if (fabsf(det) < 1e-25f)
1285 return FALSE;
1287 det = 1.0f / det;
1288 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1289 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1290 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1291 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1292 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1293 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1294 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1295 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1296 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1298 *out = temp;
1299 return TRUE;
1302 static void swap_rows(float **a, float **b)
1304 float *tmp = *a;
1306 *a = *b;
1307 *b = tmp;
1310 static BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1312 float wtmp[4][8];
1313 float m0, m1, m2, m3, s;
1314 float *r0, *r1, *r2, *r3;
1316 r0 = wtmp[0];
1317 r1 = wtmp[1];
1318 r2 = wtmp[2];
1319 r3 = wtmp[3];
1321 r0[0] = m->_11;
1322 r0[1] = m->_12;
1323 r0[2] = m->_13;
1324 r0[3] = m->_14;
1325 r0[4] = 1.0f;
1326 r0[5] = r0[6] = r0[7] = 0.0f;
1328 r1[0] = m->_21;
1329 r1[1] = m->_22;
1330 r1[2] = m->_23;
1331 r1[3] = m->_24;
1332 r1[5] = 1.0f;
1333 r1[4] = r1[6] = r1[7] = 0.0f;
1335 r2[0] = m->_31;
1336 r2[1] = m->_32;
1337 r2[2] = m->_33;
1338 r2[3] = m->_34;
1339 r2[6] = 1.0f;
1340 r2[4] = r2[5] = r2[7] = 0.0f;
1342 r3[0] = m->_41;
1343 r3[1] = m->_42;
1344 r3[2] = m->_43;
1345 r3[3] = m->_44;
1346 r3[7] = 1.0f;
1347 r3[4] = r3[5] = r3[6] = 0.0f;
1349 /* Choose pivot - or die. */
1350 if (fabsf(r3[0]) > fabsf(r2[0]))
1351 swap_rows(&r3, &r2);
1352 if (fabsf(r2[0]) > fabsf(r1[0]))
1353 swap_rows(&r2, &r1);
1354 if (fabsf(r1[0]) > fabsf(r0[0]))
1355 swap_rows(&r1, &r0);
1356 if (r0[0] == 0.0f)
1357 return FALSE;
1359 /* Eliminate first variable. */
1360 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1361 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1362 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1363 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1364 s = r0[4];
1365 if (s != 0.0f)
1367 r1[4] -= m1 * s;
1368 r2[4] -= m2 * s;
1369 r3[4] -= m3 * s;
1371 s = r0[5];
1372 if (s != 0.0f)
1374 r1[5] -= m1 * s;
1375 r2[5] -= m2 * s;
1376 r3[5] -= m3 * s;
1378 s = r0[6];
1379 if (s != 0.0f)
1381 r1[6] -= m1 * s;
1382 r2[6] -= m2 * s;
1383 r3[6] -= m3 * s;
1385 s = r0[7];
1386 if (s != 0.0f)
1388 r1[7] -= m1 * s;
1389 r2[7] -= m2 * s;
1390 r3[7] -= m3 * s;
1393 /* Choose pivot - or die. */
1394 if (fabsf(r3[1]) > fabsf(r2[1]))
1395 swap_rows(&r3, &r2);
1396 if (fabsf(r2[1]) > fabsf(r1[1]))
1397 swap_rows(&r2, &r1);
1398 if (r1[1] == 0.0f)
1399 return FALSE;
1401 /* Eliminate second variable. */
1402 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1403 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1404 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1405 s = r1[4];
1406 if (s != 0.0f)
1408 r2[4] -= m2 * s;
1409 r3[4] -= m3 * s;
1411 s = r1[5];
1412 if (s != 0.0f)
1414 r2[5] -= m2 * s;
1415 r3[5] -= m3 * s;
1417 s = r1[6];
1418 if (s != 0.0f)
1420 r2[6] -= m2 * s;
1421 r3[6] -= m3 * s;
1423 s = r1[7];
1424 if (s != 0.0f)
1426 r2[7] -= m2 * s;
1427 r3[7] -= m3 * s;
1430 /* Choose pivot - or die. */
1431 if (fabsf(r3[2]) > fabsf(r2[2]))
1432 swap_rows(&r3, &r2);
1433 if (r2[2] == 0.0f)
1434 return FALSE;
1436 /* Eliminate third variable. */
1437 m3 = r3[2] / r2[2];
1438 r3[3] -= m3 * r2[3];
1439 r3[4] -= m3 * r2[4];
1440 r3[5] -= m3 * r2[5];
1441 r3[6] -= m3 * r2[6];
1442 r3[7] -= m3 * r2[7];
1444 /* Last check. */
1445 if (r3[3] == 0.0f)
1446 return FALSE;
1448 /* Back substitute row 3. */
1449 s = 1.0f / r3[3];
1450 r3[4] *= s;
1451 r3[5] *= s;
1452 r3[6] *= s;
1453 r3[7] *= s;
1455 /* Back substitute row 2. */
1456 m2 = r2[3];
1457 s = 1.0f / r2[2];
1458 r2[4] = s * (r2[4] - r3[4] * m2);
1459 r2[5] = s * (r2[5] - r3[5] * m2);
1460 r2[6] = s * (r2[6] - r3[6] * m2);
1461 r2[7] = s * (r2[7] - r3[7] * m2);
1462 m1 = r1[3];
1463 r1[4] -= r3[4] * m1;
1464 r1[5] -= r3[5] * m1;
1465 r1[6] -= r3[6] * m1;
1466 r1[7] -= r3[7] * m1;
1467 m0 = r0[3];
1468 r0[4] -= r3[4] * m0;
1469 r0[5] -= r3[5] * m0;
1470 r0[6] -= r3[6] * m0;
1471 r0[7] -= r3[7] * m0;
1473 /* Back substitute row 1. */
1474 m1 = r1[2];
1475 s = 1.0f / r1[1];
1476 r1[4] = s * (r1[4] - r2[4] * m1);
1477 r1[5] = s * (r1[5] - r2[5] * m1);
1478 r1[6] = s * (r1[6] - r2[6] * m1);
1479 r1[7] = s * (r1[7] - r2[7] * m1);
1480 m0 = r0[2];
1481 r0[4] -= r2[4] * m0;
1482 r0[5] -= r2[5] * m0;
1483 r0[6] -= r2[6] * m0;
1484 r0[7] -= r2[7] * m0;
1486 /* Back substitute row 0. */
1487 m0 = r0[1];
1488 s = 1.0f / r0[0];
1489 r0[4] = s * (r0[4] - r1[4] * m0);
1490 r0[5] = s * (r0[5] - r1[5] * m0);
1491 r0[6] = s * (r0[6] - r1[6] * m0);
1492 r0[7] = s * (r0[7] - r1[7] * m0);
1494 out->_11 = r0[4];
1495 out->_12 = r0[5];
1496 out->_13 = r0[6];
1497 out->_14 = r0[7];
1498 out->_21 = r1[4];
1499 out->_22 = r1[5];
1500 out->_23 = r1[6];
1501 out->_24 = r1[7];
1502 out->_31 = r2[4];
1503 out->_32 = r2[5];
1504 out->_33 = r2[6];
1505 out->_34 = r2[7];
1506 out->_41 = r3[4];
1507 out->_42 = r3[5];
1508 out->_43 = r3[6];
1509 out->_44 = r3[7];
1511 return TRUE;
1514 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1516 struct wined3d_matrix temp;
1517 unsigned int i, j;
1519 for (i = 0; i < 4; ++i)
1520 for (j = 0; j < 4; ++j)
1521 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1523 *out = temp;
1526 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1527 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1529 const struct wined3d_gl_info *gl_info = context->gl_info;
1530 float mat[3 * 3];
1531 struct wined3d_matrix mv;
1532 unsigned int i, j;
1534 if (prog->vs.normal_matrix_location == -1)
1535 return;
1537 get_modelview_matrix(context, state, 0, &mv);
1538 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1539 invert_matrix_3d(&mv, &mv);
1540 else
1541 invert_matrix(&mv, &mv);
1542 /* Tests show that singular modelview matrices are used unchanged as normal
1543 * matrices on D3D3 and older. There seems to be no clearly consistent
1544 * behavior on newer D3D versions so always follow older ddraw behavior. */
1545 for (i = 0; i < 3; ++i)
1546 for (j = 0; j < 3; ++j)
1547 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1549 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1550 checkGLcall("glUniformMatrix3fv");
1553 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1554 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1556 const struct wined3d_gl_info *gl_info = context->gl_info;
1557 struct wined3d_matrix mat;
1559 if (tex >= MAX_TEXTURES)
1560 return;
1561 if (prog->vs.texture_matrix_location[tex] == -1)
1562 return;
1564 get_texture_matrix(context, state, tex, &mat);
1565 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1566 checkGLcall("glUniformMatrix4fv");
1569 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1570 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1572 const struct wined3d_gl_info *gl_info = context->gl_info;
1574 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1576 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1577 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1579 else
1581 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1583 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1585 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1586 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1587 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1588 checkGLcall("setting FFP material uniforms");
1591 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1592 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1594 const struct wined3d_gl_info *gl_info = context->gl_info;
1595 struct wined3d_color color;
1597 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1598 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1599 checkGLcall("glUniform3fv");
1602 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1603 const struct wined3d_matrix *src2)
1605 struct wined3d_vec4 temp;
1607 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1608 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1609 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1610 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1612 *dest = temp;
1615 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1616 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1617 struct glsl_shader_prog_link *prog)
1619 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1620 const struct wined3d_gl_info *gl_info = context->gl_info;
1621 struct wined3d_vec4 vec4;
1623 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1624 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1625 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1627 switch (light_info->OriginalParms.type)
1629 case WINED3D_LIGHT_POINT:
1630 multiply_vector_matrix(&vec4, &light_info->position, view);
1631 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1632 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1633 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1634 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1635 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1636 break;
1638 case WINED3D_LIGHT_SPOT:
1639 multiply_vector_matrix(&vec4, &light_info->position, view);
1640 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1642 multiply_vector_matrix(&vec4, &light_info->direction, view);
1643 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1645 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1646 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1647 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1648 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1649 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1650 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1651 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1652 break;
1654 case WINED3D_LIGHT_DIRECTIONAL:
1655 multiply_vector_matrix(&vec4, &light_info->direction, view);
1656 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1657 break;
1659 case WINED3D_LIGHT_PARALLELPOINT:
1660 multiply_vector_matrix(&vec4, &light_info->position, view);
1661 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1662 break;
1664 default:
1665 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1667 checkGLcall("setting FFP lights uniforms");
1670 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1671 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1673 const struct wined3d_gl_info *gl_info = context->gl_info;
1674 float min, max;
1675 float size, att[3];
1677 get_pointsize_minmax(context, state, &min, &max);
1679 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1680 checkGLcall("glUniform1f");
1681 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1682 checkGLcall("glUniform1f");
1684 get_pointsize(context, state, &size, att);
1686 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1687 checkGLcall("glUniform1f");
1688 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1689 checkGLcall("glUniform1f");
1690 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1691 checkGLcall("glUniform1f");
1692 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1693 checkGLcall("glUniform1f");
1696 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1697 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1699 const struct wined3d_gl_info *gl_info = context->gl_info;
1700 struct wined3d_color color;
1701 float start, end, scale;
1702 union
1704 DWORD d;
1705 float f;
1706 } tmpvalue;
1708 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1709 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1710 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1711 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1712 get_fog_start_end(context, state, &start, &end);
1713 scale = 1.0f / (end - start);
1714 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1715 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1716 checkGLcall("fog emulation uniforms");
1719 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1720 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1722 const struct wined3d_gl_info *gl_info = context->gl_info;
1723 struct wined3d_matrix matrix;
1724 struct wined3d_vec4 plane;
1726 plane = state->clip_planes[index];
1728 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1729 if (!use_vs(state))
1731 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1732 transpose_matrix(&matrix, &matrix);
1733 multiply_vector_matrix(&plane, &plane, &matrix);
1736 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1739 /* Context activation is done by the caller (state handler). */
1740 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1741 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1743 struct wined3d_color float_key[2];
1744 const struct wined3d_texture *texture = state->textures[0];
1746 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1747 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1750 /* Context activation is done by the caller (state handler). */
1751 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1752 const struct wined3d_state *state)
1754 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1755 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1756 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1757 const struct wined3d_gl_info *gl_info = context->gl_info;
1758 struct shader_glsl_priv *priv = shader_priv;
1759 float position_fixup[4];
1760 DWORD update_mask;
1762 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1763 UINT constant_version;
1764 int i;
1766 if (!prog) {
1767 /* No GLSL program set - nothing to do. */
1768 return;
1770 constant_version = prog->constant_version;
1771 update_mask = context->constant_update_mask & prog->constant_update_mask;
1773 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1774 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1775 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1777 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1778 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1779 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1781 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1782 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1783 vshader->reg_maps.boolean_constants);
1785 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1787 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1788 shader_glsl_clip_plane_uniform(context, state, i, prog);
1791 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1792 shader_glsl_pointsize_uniform(context, state, prog);
1794 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1796 shader_get_position_fixup(context, state, position_fixup);
1797 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1798 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1799 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1800 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1801 else
1802 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1803 checkGLcall("glUniform4fv");
1806 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1808 struct wined3d_matrix mat;
1810 get_modelview_matrix(context, state, 0, &mat);
1811 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1812 checkGLcall("glUniformMatrix4fv");
1814 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1817 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1819 struct wined3d_matrix mat;
1821 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1823 if (prog->vs.modelview_matrix_location[i] == -1)
1824 break;
1826 get_modelview_matrix(context, state, i, &mat);
1827 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1828 checkGLcall("glUniformMatrix4fv");
1832 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1834 struct wined3d_matrix projection;
1836 get_projection_matrix(context, state, &projection);
1837 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1838 checkGLcall("glUniformMatrix4fv");
1841 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1843 for (i = 0; i < MAX_TEXTURES; ++i)
1844 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1847 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1848 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1850 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1852 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1853 DWORD point_count = 0;
1854 DWORD spot_count = 0;
1855 DWORD directional_count = 0;
1856 DWORD parallel_point_count = 0;
1858 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1860 if (!state->lights[i])
1861 continue;
1863 switch (state->lights[i]->OriginalParms.type)
1865 case WINED3D_LIGHT_POINT:
1866 ++point_count;
1867 break;
1868 case WINED3D_LIGHT_SPOT:
1869 ++spot_count;
1870 break;
1871 case WINED3D_LIGHT_DIRECTIONAL:
1872 ++directional_count;
1873 break;
1874 case WINED3D_LIGHT_PARALLELPOINT:
1875 ++parallel_point_count;
1876 break;
1877 default:
1878 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1879 break;
1882 point_idx = 0;
1883 spot_idx = point_idx + point_count;
1884 directional_idx = spot_idx + spot_count;
1885 parallel_point_idx = directional_idx + directional_count;
1887 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1888 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1890 const struct wined3d_light_info *light_info = state->lights[i];
1891 unsigned int idx;
1893 if (!light_info)
1894 continue;
1896 switch (light_info->OriginalParms.type)
1898 case WINED3D_LIGHT_POINT:
1899 idx = point_idx++;
1900 break;
1901 case WINED3D_LIGHT_SPOT:
1902 idx = spot_idx++;
1903 break;
1904 case WINED3D_LIGHT_DIRECTIONAL:
1905 idx = directional_idx++;
1906 break;
1907 case WINED3D_LIGHT_PARALLELPOINT:
1908 idx = parallel_point_idx++;
1909 break;
1910 default:
1911 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1912 continue;
1914 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1918 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1919 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1920 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1922 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1923 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1924 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1926 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1927 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1928 pshader->reg_maps.boolean_constants);
1930 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1932 for (i = 0; i < MAX_TEXTURES; ++i)
1934 if (prog->ps.bumpenv_mat_location[i] == -1)
1935 continue;
1937 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1938 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1940 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1942 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1943 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1944 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1945 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1949 checkGLcall("bump env uniforms");
1952 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1954 const struct wined3d_vec4 correction_params =
1956 /* Position is relative to the framebuffer, not the viewport. */
1957 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1958 context->render_offscreen ? 1.0f : -1.0f,
1959 0.0f,
1960 0.0f,
1963 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1966 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1967 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1968 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1969 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1971 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1973 struct wined3d_color color;
1975 if (prog->ps.tex_factor_location != -1)
1977 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1978 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1981 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1982 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1983 else
1984 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1986 for (i = 0; i < MAX_TEXTURES; ++i)
1988 if (prog->ps.tss_constant_location[i] == -1)
1989 continue;
1991 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1992 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1995 checkGLcall("fixed function uniforms");
1998 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1999 shader_glsl_load_fog_uniform(context, state, prog);
2001 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
2003 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
2005 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
2006 checkGLcall("alpha test emulation uniform");
2009 if (priv->next_constant_version == UINT_MAX)
2011 TRACE("Max constant version reached, resetting to 0.\n");
2012 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
2013 priv->next_constant_version = 1;
2015 else
2017 prog->constant_version = priv->next_constant_version++;
2021 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
2023 struct constant_entry *entries = heap->entries;
2024 unsigned int *positions = heap->positions;
2025 unsigned int heap_idx, parent_idx;
2027 if (!heap->contained[idx])
2029 heap_idx = heap->size++;
2030 heap->contained[idx] = TRUE;
2032 else
2034 heap_idx = positions[idx];
2037 while (heap_idx > 1)
2039 parent_idx = heap_idx >> 1;
2041 if (new_version <= entries[parent_idx].version) break;
2043 entries[heap_idx] = entries[parent_idx];
2044 positions[entries[parent_idx].idx] = heap_idx;
2045 heap_idx = parent_idx;
2048 entries[heap_idx].version = new_version;
2049 entries[heap_idx].idx = idx;
2050 positions[idx] = heap_idx;
2053 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2055 struct shader_glsl_priv *priv = device->shader_priv;
2056 struct constant_heap *heap = &priv->vconst_heap;
2057 UINT i;
2059 for (i = start; i < count + start; ++i)
2061 update_heap_entry(heap, i, priv->next_constant_version);
2065 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2067 struct shader_glsl_priv *priv = device->shader_priv;
2068 struct constant_heap *heap = &priv->pconst_heap;
2069 UINT i;
2071 for (i = start; i < count + start; ++i)
2073 update_heap_entry(heap, i, priv->next_constant_version);
2077 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2079 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2080 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2081 if(shader_major > 3) return ret;
2083 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2084 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2085 return ret;
2088 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2090 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2093 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2095 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2096 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2099 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2101 return shader_glsl_get_version(gl_info) >= 150;
2104 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2106 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2109 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2110 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2112 va_list args;
2113 int ret;
2115 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2116 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2117 for (;;)
2119 va_start(args, format);
2120 ret = shader_vaddline(buffer, format, args);
2121 va_end(args);
2122 if (!ret)
2123 return;
2124 if (!string_buffer_resize(buffer, ret))
2125 return;
2129 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2130 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2132 va_list args;
2133 int ret;
2135 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2136 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2137 for (;;)
2139 va_start(args, format);
2140 ret = shader_vaddline(buffer, format, args);
2141 va_end(args);
2142 if (!ret)
2143 return;
2144 if (!string_buffer_resize(buffer, ret))
2145 return;
2149 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2151 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2154 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2156 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2159 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2161 switch (mode)
2163 case WINED3DSIM_CONSTANT:
2164 return "flat";
2165 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2166 return "noperspective";
2167 default:
2168 FIXME("Unhandled interpolation mode %#x.\n", mode);
2169 case WINED3DSIM_NONE:
2170 case WINED3DSIM_LINEAR:
2171 return "";
2175 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2176 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2178 return wined3d_extract_bits(packed_interpolation_mode,
2179 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2182 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2183 struct wined3d_string_buffer *buffer, unsigned int element_count,
2184 const DWORD *interpolation_mode, BOOL unroll)
2186 enum wined3d_shader_interpolation_mode mode;
2187 unsigned int i;
2189 if (shader_glsl_use_interface_blocks(gl_info))
2191 if (unroll)
2193 shader_addline(buffer, "in shader_in_out {\n");
2194 for (i = 0; i < element_count; ++i)
2196 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2197 shader_addline(buffer, "%s vec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2199 shader_addline(buffer, "} shader_in;\n");
2201 else
2203 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2206 else
2208 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2212 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2213 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2214 const DWORD *interpolation_mode)
2216 enum wined3d_shader_interpolation_mode mode;
2217 unsigned int i;
2219 if (shader_glsl_use_interface_blocks(gl_info))
2221 if (rasterizer_setup)
2223 shader_addline(buffer, "out shader_in_out {\n");
2224 for (i = 0; i < element_count; ++i)
2226 const char *interpolation_qualifiers = "";
2227 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2229 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2230 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2232 shader_addline(buffer, "%s vec4 reg%u;\n", interpolation_qualifiers, i);
2234 shader_addline(buffer, "} shader_out;\n");
2236 else
2238 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2241 else
2243 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2247 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2249 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2252 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2254 switch (primitive_type)
2256 case WINED3D_PT_POINTLIST:
2257 return "points";
2259 case WINED3D_PT_LINELIST:
2260 return "lines";
2262 case WINED3D_PT_LINESTRIP:
2263 return "line_strip";
2265 case WINED3D_PT_TRIANGLELIST:
2266 return "triangles";
2268 case WINED3D_PT_TRIANGLESTRIP:
2269 return "triangle_strip";
2271 case WINED3D_PT_LINELIST_ADJ:
2272 return "lines_adjacency";
2274 case WINED3D_PT_TRIANGLELIST_ADJ:
2275 return "triangles_adjacency";
2277 default:
2278 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2279 return "";
2283 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2285 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2286 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2287 DWORD input_reg_used = shader->u.ps.input_reg_used;
2288 unsigned int i;
2290 if (reg_maps->shader_version.major < 3)
2291 return input_reg_used & (1u << idx);
2293 for (i = 0; i < input_signature->element_count; ++i)
2295 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2297 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2298 continue;
2300 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2301 && input->semantic_idx == idx)
2302 return input_reg_used & (1u << input->register_idx);
2304 return FALSE;
2307 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2308 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2310 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2312 if (version->major >= 4)
2313 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2314 else
2315 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2318 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2319 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2320 unsigned int index)
2322 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2323 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2324 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2325 index, scalar_type, scalar_type, index);
2328 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2329 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2331 unsigned int index = e->register_idx;
2333 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2335 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2336 index);
2337 return;
2339 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2341 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2342 index);
2343 return;
2345 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2346 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2348 if (shader_glsl_use_explicit_attrib_location(gl_info))
2349 shader_addline(buffer, "layout(location = %u) ", index);
2351 switch (e->component_type)
2353 case WINED3D_TYPE_UINT:
2354 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
2355 break;
2356 case WINED3D_TYPE_INT:
2357 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
2358 break;
2360 default:
2361 FIXME("Unhandled type %#x.\n", e->component_type);
2362 /* Fall through. */
2363 case WINED3D_TYPE_UNKNOWN:
2364 case WINED3D_TYPE_FLOAT:
2365 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2366 break;
2370 /** Generate the variable & register declarations for the GLSL output target */
2371 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2372 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2373 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2375 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2376 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2377 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2378 const struct wined3d_gl_info *gl_info = context->gl_info;
2379 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2380 unsigned int uniform_block_base, uniform_block_count;
2381 const struct wined3d_shader_lconst *lconst;
2382 const char *prefix;
2383 unsigned int i;
2384 DWORD map;
2386 prefix = shader_glsl_get_prefix(version->type);
2388 /* Prototype the subroutines */
2389 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2391 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2394 /* Declare the constants (aka uniforms) */
2395 if (shader->limits->constant_float > 0)
2397 unsigned max_constantsF;
2399 /* Unless the shader uses indirect addressing, always declare the
2400 * maximum array size and ignore that we need some uniforms privately.
2401 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2402 * and immediate values, still declare VC[256]. If the shader needs
2403 * more uniforms than we have it won't work in any case. If it uses
2404 * less, the compiler will figure out which uniforms are really used
2405 * and strip them out. This allows a shader to use c255 on a dx9 card,
2406 * as long as it doesn't also use all the other constants.
2408 * If the shader uses indirect addressing the compiler must assume
2409 * that all declared uniforms are used. In this case, declare only the
2410 * amount that we're assured to have.
2412 * Thus we run into problems in these two cases:
2413 * 1) The shader really uses more uniforms than supported.
2414 * 2) The shader uses indirect addressing, less constants than
2415 * supported, but uses a constant index > #supported consts. */
2416 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2418 /* No indirect addressing here. */
2419 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2421 else
2423 if (reg_maps->usesrelconstF)
2425 /* Subtract the other potential uniforms from the max
2426 * available (bools, ints, and 1 row of projection matrix).
2427 * Subtract another uniform for immediate values, which have
2428 * to be loaded via uniform by the driver as well. The shader
2429 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2430 * shader code, so one vec4 should be enough. (Unfortunately
2431 * the Nvidia driver doesn't store 128 and -128 in one float).
2433 * Writing gl_ClipVertex requires one uniform for each
2434 * clipplane as well. */
2435 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2436 if (vs_args->clip_enabled)
2437 max_constantsF -= gl_info->limits.user_clip_distances;
2438 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2439 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2440 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2441 * for now take this into account when calculating the number of available constants
2443 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2444 /* Set by driver quirks in directx.c */
2445 max_constantsF -= gl_info->reserved_glsl_constants;
2447 if (max_constantsF < shader->limits->constant_float)
2449 static unsigned int once;
2451 if (!once++)
2452 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2453 " it may not render correctly.\n");
2454 else
2455 WARN("The hardware does not support enough uniform components to run this shader.\n");
2458 else
2460 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2463 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2464 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2467 /* Always declare the full set of constants, the compiler can remove the
2468 * unused ones because d3d doesn't (yet) support indirect int and bool
2469 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2470 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2471 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2473 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2474 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2476 /* Declare immediate constant buffer */
2477 if (reg_maps->icb)
2478 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2480 /* Declare constant buffers */
2481 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2482 &uniform_block_base, &uniform_block_count);
2483 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2485 if (reg_maps->cb_sizes[i])
2487 shader_addline(buffer, "layout(std140");
2488 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2489 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2490 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2491 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2495 /* Declare texture samplers */
2496 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2498 struct wined3d_shader_sampler_map_entry *entry;
2499 const char *sampler_type_prefix, *sampler_type;
2500 BOOL shadow_sampler, tex_rect;
2502 entry = &reg_maps->sampler_map.entries[i];
2504 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2506 ERR("Invalid resource index %u.\n", entry->resource_idx);
2507 continue;
2510 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2512 case WINED3D_DATA_FLOAT:
2513 case WINED3D_DATA_UNORM:
2514 case WINED3D_DATA_SNORM:
2515 sampler_type_prefix = "";
2516 break;
2518 case WINED3D_DATA_INT:
2519 sampler_type_prefix = "i";
2520 break;
2522 case WINED3D_DATA_UINT:
2523 sampler_type_prefix = "u";
2524 break;
2526 default:
2527 sampler_type_prefix = "";
2528 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2529 break;
2532 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2533 switch (reg_maps->resource_info[entry->resource_idx].type)
2535 case WINED3D_SHADER_RESOURCE_BUFFER:
2536 sampler_type = "samplerBuffer";
2537 break;
2539 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2540 if (shadow_sampler)
2541 sampler_type = "sampler1DShadow";
2542 else
2543 sampler_type = "sampler1D";
2544 break;
2546 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2547 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2548 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2549 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2550 if (shadow_sampler)
2552 if (tex_rect)
2553 sampler_type = "sampler2DRectShadow";
2554 else
2555 sampler_type = "sampler2DShadow";
2557 else
2559 if (tex_rect)
2560 sampler_type = "sampler2DRect";
2561 else
2562 sampler_type = "sampler2D";
2564 break;
2566 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2567 if (shadow_sampler)
2568 FIXME("Unsupported 3D shadow sampler.\n");
2569 sampler_type = "sampler3D";
2570 break;
2572 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2573 if (shadow_sampler)
2574 sampler_type = "samplerCubeShadow";
2575 else
2576 sampler_type = "samplerCube";
2577 break;
2579 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2580 if (shadow_sampler)
2581 sampler_type = "sampler2DArrayShadow";
2582 else
2583 sampler_type = "sampler2DArray";
2584 break;
2586 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2587 if (shadow_sampler)
2588 sampler_type = "samplerCubeArrayShadow";
2589 else
2590 sampler_type = "samplerCubeArray";
2591 break;
2593 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2594 sampler_type = "sampler2DMS";
2595 break;
2597 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2598 sampler_type = "sampler2DMSArray";
2599 break;
2601 default:
2602 sampler_type = "unsupported_sampler";
2603 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2604 break;
2607 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2608 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2609 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2610 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2613 /* Declare images */
2614 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2616 const char *image_type_prefix, *image_type, *read_format;
2618 if (!reg_maps->uav_resource_info[i].type)
2619 continue;
2621 switch (reg_maps->uav_resource_info[i].data_type)
2623 case WINED3D_DATA_FLOAT:
2624 case WINED3D_DATA_UNORM:
2625 case WINED3D_DATA_SNORM:
2626 image_type_prefix = "";
2627 read_format = "r32f";
2628 break;
2630 case WINED3D_DATA_INT:
2631 image_type_prefix = "i";
2632 read_format = "r32i";
2633 break;
2635 case WINED3D_DATA_UINT:
2636 image_type_prefix = "u";
2637 read_format = "r32ui";
2638 break;
2640 default:
2641 image_type_prefix = "";
2642 read_format = "";
2643 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2644 break;
2647 switch (reg_maps->uav_resource_info[i].type)
2649 case WINED3D_SHADER_RESOURCE_BUFFER:
2650 image_type = "imageBuffer";
2651 break;
2653 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2654 image_type = "image2D";
2655 break;
2657 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2658 image_type = "image3D";
2659 break;
2661 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2662 image_type = "image2DArray";
2663 break;
2665 default:
2666 image_type = "unsupported_image";
2667 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2668 break;
2671 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2672 shader_addline(buffer, "layout(binding = %u)\n", i);
2673 if (reg_maps->uav_read_mask & (1u << i))
2674 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2675 read_format, image_type_prefix, image_type, prefix, i);
2676 else
2677 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2678 image_type_prefix, image_type, prefix, i);
2680 if (reg_maps->uav_counter_mask & (1u << i))
2681 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2682 i, prefix, i);
2685 /* Declare address variables */
2686 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2688 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2691 /* Declare output register temporaries */
2692 if (shader->limits->packed_output)
2693 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2695 /* Declare temporary variables */
2696 if (reg_maps->temporary_count)
2698 for (i = 0; i < reg_maps->temporary_count; ++i)
2699 shader_addline(buffer, "vec4 R%u;\n", i);
2701 else if (version->major < 4)
2703 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2705 if (map & 1)
2706 shader_addline(buffer, "vec4 R%u;\n", i);
2710 /* Declare indexable temporary variables */
2711 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2713 if (idx_temp_reg->component_count != 4)
2714 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2715 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2718 /* Declare loop registers aLx */
2719 if (version->major < 4)
2721 for (i = 0; i < reg_maps->loop_depth; ++i)
2723 shader_addline(buffer, "int aL%u;\n", i);
2724 shader_addline(buffer, "int tmpInt%u;\n", i);
2728 /* Temporary variables for matrix operations */
2729 shader_addline(buffer, "vec4 tmp0;\n");
2730 shader_addline(buffer, "vec4 tmp1;\n");
2732 if (!shader->load_local_constsF)
2734 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2736 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2737 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2738 shader_addline(buffer, ";\n");
2743 /* Prototypes */
2744 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2745 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2747 /** Used for opcode modifiers - They multiply the result by the specified amount */
2748 static const char * const shift_glsl_tab[] = {
2749 "", /* 0 (none) */
2750 "2.0 * ", /* 1 (x2) */
2751 "4.0 * ", /* 2 (x4) */
2752 "8.0 * ", /* 3 (x8) */
2753 "16.0 * ", /* 4 (x16) */
2754 "32.0 * ", /* 5 (x32) */
2755 "", /* 6 (x64) */
2756 "", /* 7 (x128) */
2757 "", /* 8 (d256) */
2758 "", /* 9 (d128) */
2759 "", /* 10 (d64) */
2760 "", /* 11 (d32) */
2761 "0.0625 * ", /* 12 (d16) */
2762 "0.125 * ", /* 13 (d8) */
2763 "0.25 * ", /* 14 (d4) */
2764 "0.5 * " /* 15 (d2) */
2767 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2768 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2769 const char *in_reg, const char *in_regswizzle, char *out_str)
2771 switch (src_modifier)
2773 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2774 case WINED3DSPSM_DW:
2775 case WINED3DSPSM_NONE:
2776 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2777 break;
2778 case WINED3DSPSM_NEG:
2779 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2780 break;
2781 case WINED3DSPSM_NOT:
2782 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2783 break;
2784 case WINED3DSPSM_BIAS:
2785 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2786 break;
2787 case WINED3DSPSM_BIASNEG:
2788 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2789 break;
2790 case WINED3DSPSM_SIGN:
2791 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2792 break;
2793 case WINED3DSPSM_SIGNNEG:
2794 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2795 break;
2796 case WINED3DSPSM_COMP:
2797 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2798 break;
2799 case WINED3DSPSM_X2:
2800 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2801 break;
2802 case WINED3DSPSM_X2NEG:
2803 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2804 break;
2805 case WINED3DSPSM_ABS:
2806 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2807 break;
2808 case WINED3DSPSM_ABSNEG:
2809 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2810 break;
2811 default:
2812 FIXME("Unhandled modifier %u\n", src_modifier);
2813 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2817 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2818 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2820 /* The ARB_shading_language_420pack extension allows swizzle operations on
2821 * scalars. */
2822 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2823 sprintf(register_name, "%s", glsl_variable);
2824 else
2825 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2828 /** Writes the GLSL variable name that corresponds to the register that the
2829 * DX opcode parameter is trying to access */
2830 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2831 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2832 const struct wined3d_shader_instruction *ins)
2834 /* oPos, oFog and oPts in D3D */
2835 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2837 const struct wined3d_shader *shader = ins->ctx->shader;
2838 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2839 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2840 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2841 const char *prefix = shader_glsl_get_prefix(version->type);
2842 struct glsl_src_param rel_param0, rel_param1;
2843 char imm_str[4][17];
2845 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2846 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2847 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2848 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2849 *is_color = FALSE;
2851 switch (reg->type)
2853 case WINED3DSPR_TEMP:
2854 sprintf(register_name, "R%u", reg->idx[0].offset);
2855 break;
2857 case WINED3DSPR_INPUT:
2858 case WINED3DSPR_INCONTROLPOINT:
2859 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2861 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2863 if (reg->idx[0].rel_addr)
2864 FIXME("VS3 input registers relative addressing.\n");
2865 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2866 *is_color = TRUE;
2867 if (reg->idx[0].rel_addr)
2869 sprintf(register_name, "%s_in[%s + %u]",
2870 prefix, rel_param0.param_str, reg->idx[0].offset);
2872 else
2874 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2876 break;
2879 if (version->type == WINED3D_SHADER_TYPE_HULL
2880 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2881 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2883 if (reg->idx[0].rel_addr)
2885 if (reg->idx[1].rel_addr)
2886 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2887 rel_param0.param_str, reg->idx[0].offset,
2888 rel_param1.param_str, reg->idx[1].offset);
2889 else
2890 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2891 rel_param0.param_str, reg->idx[0].offset,
2892 reg->idx[1].offset);
2894 else if (reg->idx[1].rel_addr)
2895 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2896 rel_param1.param_str, reg->idx[1].offset);
2897 else
2898 sprintf(register_name, "shader_in[%u].reg[%u]",
2899 reg->idx[0].offset, reg->idx[1].offset);
2900 break;
2903 /* pixel shaders >= 3.0 */
2904 if (version->major >= 3)
2906 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2907 unsigned int in_count = vec4_varyings(version->major, gl_info);
2909 if (reg->idx[0].rel_addr)
2911 /* Removing a + 0 would be an obvious optimization, but
2912 * OS X doesn't see the NOP operation there. */
2913 if (idx)
2915 if (needs_legacy_glsl_syntax(gl_info)
2916 && shader->u.ps.declared_in_count > in_count)
2918 sprintf(register_name,
2919 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2920 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2921 prefix, rel_param0.param_str, idx);
2923 else
2925 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2928 else
2930 if (needs_legacy_glsl_syntax(gl_info)
2931 && shader->u.ps.declared_in_count > in_count)
2933 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2934 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2935 prefix, rel_param0.param_str);
2937 else
2939 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2943 else
2945 if (idx == in_count) sprintf(register_name, "gl_Color");
2946 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2947 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2950 else
2952 if (!reg->idx[0].offset)
2953 strcpy(register_name, "ffp_varying_diffuse");
2954 else
2955 strcpy(register_name, "ffp_varying_specular");
2956 break;
2958 break;
2960 case WINED3DSPR_CONST:
2962 /* Relative addressing */
2963 if (reg->idx[0].rel_addr)
2965 if (wined3d_settings.check_float_constants)
2966 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2967 rel_param0.param_str, reg->idx[0].offset,
2968 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2969 prefix, rel_param0.param_str, reg->idx[0].offset);
2970 else if (reg->idx[0].offset)
2971 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2972 else
2973 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2975 else
2977 if (shader_constant_is_local(shader, reg->idx[0].offset))
2978 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2979 else
2980 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2983 break;
2985 case WINED3DSPR_CONSTINT:
2986 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2987 break;
2989 case WINED3DSPR_CONSTBOOL:
2990 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2991 break;
2993 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2994 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2995 sprintf(register_name, "T%u", reg->idx[0].offset);
2996 else
2997 sprintf(register_name, "A%u", reg->idx[0].offset);
2998 break;
3000 case WINED3DSPR_LOOP:
3001 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
3002 break;
3004 case WINED3DSPR_SAMPLER:
3005 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
3006 break;
3008 case WINED3DSPR_COLOROUT:
3009 if (reg->idx[0].offset >= gl_info->limits.buffers)
3010 WARN("Write to render target %u, only %d supported.\n",
3011 reg->idx[0].offset, gl_info->limits.buffers);
3013 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
3014 break;
3016 case WINED3DSPR_RASTOUT:
3017 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
3018 break;
3020 case WINED3DSPR_DEPTHOUT:
3021 case WINED3DSPR_DEPTHOUTGE:
3022 case WINED3DSPR_DEPTHOUTLE:
3023 sprintf(register_name, "gl_FragDepth");
3024 break;
3026 case WINED3DSPR_ATTROUT:
3027 if (!reg->idx[0].offset)
3028 sprintf(register_name, "%s_out[8]", prefix);
3029 else
3030 sprintf(register_name, "%s_out[9]", prefix);
3031 break;
3033 case WINED3DSPR_TEXCRDOUT:
3034 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
3035 if (reg->idx[0].rel_addr)
3036 sprintf(register_name, "%s_out[%s + %u]",
3037 prefix, rel_param0.param_str, reg->idx[0].offset);
3038 else
3039 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3040 break;
3042 case WINED3DSPR_MISCTYPE:
3043 if (!reg->idx[0].offset)
3045 /* vPos */
3046 sprintf(register_name, "vpos");
3048 else if (reg->idx[0].offset == 1)
3050 /* Note that gl_FrontFacing is a bool, while vFace is
3051 * a float for which the sign determines front/back */
3052 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3054 else
3056 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3057 sprintf(register_name, "unrecognized_register");
3059 break;
3061 case WINED3DSPR_IMMCONST:
3062 switch (reg->immconst_type)
3064 case WINED3D_IMMCONST_SCALAR:
3065 switch (data_type)
3067 case WINED3D_DATA_FLOAT:
3068 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3069 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3070 else
3071 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3072 break;
3073 case WINED3D_DATA_INT:
3074 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3075 break;
3076 case WINED3D_DATA_RESOURCE:
3077 case WINED3D_DATA_SAMPLER:
3078 case WINED3D_DATA_UINT:
3079 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3080 break;
3081 default:
3082 sprintf(register_name, "<unhandled data type %#x>", data_type);
3083 break;
3085 break;
3087 case WINED3D_IMMCONST_VEC4:
3088 switch (data_type)
3090 case WINED3D_DATA_FLOAT:
3091 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3093 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3094 reg->u.immconst_data[0], reg->u.immconst_data[1],
3095 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3097 else
3099 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3100 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3101 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3102 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3103 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3104 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3106 break;
3107 case WINED3D_DATA_INT:
3108 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3109 reg->u.immconst_data[0], reg->u.immconst_data[1],
3110 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3111 break;
3112 case WINED3D_DATA_RESOURCE:
3113 case WINED3D_DATA_SAMPLER:
3114 case WINED3D_DATA_UINT:
3115 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3116 reg->u.immconst_data[0], reg->u.immconst_data[1],
3117 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3118 break;
3119 default:
3120 sprintf(register_name, "<unhandled data type %#x>", data_type);
3121 break;
3123 break;
3125 default:
3126 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3127 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3129 break;
3131 case WINED3DSPR_CONSTBUFFER:
3132 if (reg->idx[1].rel_addr)
3133 sprintf(register_name, "%s_cb%u[%s + %u]",
3134 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3135 else
3136 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3137 break;
3139 case WINED3DSPR_IMMCONSTBUFFER:
3140 if (reg->idx[0].rel_addr)
3141 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3142 else
3143 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3144 break;
3146 case WINED3DSPR_PRIMID:
3147 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3148 sprintf(register_name, "gl_PrimitiveIDIn");
3149 else
3150 sprintf(register_name, "gl_PrimitiveID");
3151 break;
3153 case WINED3DSPR_IDXTEMP:
3154 if (reg->idx[1].rel_addr)
3155 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3156 else
3157 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3158 break;
3160 case WINED3DSPR_LOCALTHREADINDEX:
3161 shader_glsl_fixup_scalar_register_variable(register_name,
3162 "int(gl_LocalInvocationIndex)", gl_info);
3163 break;
3165 case WINED3DSPR_GSINSTID:
3166 case WINED3DSPR_OUTPOINTID:
3167 shader_glsl_fixup_scalar_register_variable(register_name,
3168 "gl_InvocationID", gl_info);
3169 break;
3171 case WINED3DSPR_THREADID:
3172 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3173 break;
3175 case WINED3DSPR_THREADGROUPID:
3176 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3177 break;
3179 case WINED3DSPR_LOCALTHREADID:
3180 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3181 break;
3183 case WINED3DSPR_FORKINSTID:
3184 case WINED3DSPR_JOININSTID:
3185 shader_glsl_fixup_scalar_register_variable(register_name,
3186 "phase_instance_id", gl_info);
3187 break;
3189 case WINED3DSPR_TESSCOORD:
3190 sprintf(register_name, "gl_TessCoord");
3191 break;
3193 case WINED3DSPR_OUTCONTROLPOINT:
3194 if (reg->idx[0].rel_addr)
3196 if (reg->idx[1].rel_addr)
3197 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3198 rel_param0.param_str, reg->idx[0].offset,
3199 rel_param1.param_str, reg->idx[1].offset);
3200 else
3201 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3202 rel_param0.param_str, reg->idx[0].offset,
3203 reg->idx[1].offset);
3205 else if (reg->idx[1].rel_addr)
3207 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3208 reg->idx[0].offset, rel_param1.param_str,
3209 reg->idx[1].offset);
3211 else
3213 sprintf(register_name, "shader_out[%u].reg[%u]",
3214 reg->idx[0].offset, reg->idx[1].offset);
3216 break;
3218 case WINED3DSPR_PATCHCONST:
3219 if (version->type == WINED3D_SHADER_TYPE_HULL)
3220 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3221 else
3222 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3223 break;
3225 default:
3226 FIXME("Unhandled register type %#x.\n", reg->type);
3227 sprintf(register_name, "unrecognized_register");
3228 break;
3232 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3234 *str++ = '.';
3235 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3236 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3237 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3238 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3239 *str = '\0';
3242 /* Get the GLSL write mask for the destination register */
3243 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3245 DWORD mask = param->write_mask;
3247 if (shader_is_scalar(&param->reg))
3249 mask = WINED3DSP_WRITEMASK_0;
3250 *write_mask = '\0';
3252 else
3254 shader_glsl_write_mask_to_str(mask, write_mask);
3257 return mask;
3260 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3262 unsigned int size = 0;
3264 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3265 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3266 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3267 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3269 return size;
3272 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3273 unsigned int component_idx)
3275 /* swizzle bits fields: wwzzyyxx */
3276 return (swizzle >> (2 * component_idx)) & 0x3;
3279 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3281 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3282 * but addressed as "rgba". To fix this we need to swap the register's x
3283 * and z components. */
3284 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3285 unsigned int i;
3287 *str++ = '.';
3288 for (i = 0; i < 4; ++i)
3290 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3291 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3293 *str = '\0';
3296 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3297 BOOL fixup, DWORD mask, char *swizzle_str)
3299 if (shader_is_scalar(&param->reg))
3300 *swizzle_str = '\0';
3301 else
3302 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3305 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3306 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3308 if (dst_data_type == src_data_type)
3310 string_buffer_sprintf(dst_param, "%s", src_param);
3311 return;
3314 if (src_data_type == WINED3D_DATA_FLOAT)
3316 switch (dst_data_type)
3318 case WINED3D_DATA_INT:
3319 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3320 return;
3321 case WINED3D_DATA_RESOURCE:
3322 case WINED3D_DATA_SAMPLER:
3323 case WINED3D_DATA_UINT:
3324 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3325 return;
3326 default:
3327 break;
3331 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3333 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3334 return;
3337 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3339 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3340 return;
3343 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3344 string_buffer_sprintf(dst_param, "%s", src_param);
3347 /* From a given parameter token, generate the corresponding GLSL string.
3348 * Also, return the actual register name and swizzle in case the
3349 * caller needs this information as well. */
3350 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3351 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3352 enum wined3d_data_type data_type)
3354 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3355 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3356 enum wined3d_data_type param_data_type;
3357 BOOL is_color = FALSE;
3358 char swizzle_str[6];
3360 glsl_src->reg_name[0] = '\0';
3361 glsl_src->param_str[0] = '\0';
3362 swizzle_str[0] = '\0';
3364 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ins);
3365 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3367 switch (wined3d_src->reg.type)
3369 case WINED3DSPR_IMMCONST:
3370 param_data_type = data_type;
3371 break;
3372 case WINED3DSPR_FORKINSTID:
3373 case WINED3DSPR_GSINSTID:
3374 case WINED3DSPR_JOININSTID:
3375 case WINED3DSPR_LOCALTHREADID:
3376 case WINED3DSPR_LOCALTHREADINDEX:
3377 case WINED3DSPR_OUTPOINTID:
3378 case WINED3DSPR_PRIMID:
3379 case WINED3DSPR_THREADGROUPID:
3380 case WINED3DSPR_THREADID:
3381 param_data_type = WINED3D_DATA_INT;
3382 break;
3383 default:
3384 param_data_type = WINED3D_DATA_FLOAT;
3385 break;
3388 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3389 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3391 string_buffer_release(priv->string_buffers, reg_name);
3394 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3395 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3397 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3400 /* From a given parameter token, generate the corresponding GLSL string.
3401 * Also, return the actual register name and swizzle in case the
3402 * caller needs this information as well. */
3403 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3404 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3406 BOOL is_color = FALSE;
3408 glsl_dst->mask_str[0] = '\0';
3409 glsl_dst->reg_name[0] = '\0';
3411 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3412 glsl_dst->reg_name, &is_color, ins);
3413 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3416 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3417 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3418 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3419 enum wined3d_data_type data_type)
3421 struct glsl_dst_param glsl_dst;
3422 DWORD mask;
3424 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3426 switch (data_type)
3428 case WINED3D_DATA_FLOAT:
3429 shader_addline(buffer, "%s%s = %s(",
3430 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3431 break;
3432 case WINED3D_DATA_INT:
3433 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3434 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3435 break;
3436 case WINED3D_DATA_RESOURCE:
3437 case WINED3D_DATA_SAMPLER:
3438 case WINED3D_DATA_UINT:
3439 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3440 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3441 break;
3442 default:
3443 FIXME("Unhandled data type %#x.\n", data_type);
3444 shader_addline(buffer, "%s%s = %s(",
3445 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3446 break;
3450 return mask;
3453 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3454 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3456 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3459 /** Process GLSL instruction modifiers */
3460 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3462 struct glsl_dst_param dst_param;
3463 DWORD modifiers;
3465 if (!ins->dst_count) return;
3467 modifiers = ins->dst[0].modifiers;
3468 if (!modifiers) return;
3470 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3472 if (modifiers & WINED3DSPDM_SATURATE)
3474 /* _SAT means to clamp the value of the register to between 0 and 1 */
3475 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3476 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3479 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3481 FIXME("_centroid modifier not handled\n");
3484 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3486 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3490 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3492 switch (op)
3494 case WINED3D_SHADER_REL_OP_GT: return ">";
3495 case WINED3D_SHADER_REL_OP_EQ: return "==";
3496 case WINED3D_SHADER_REL_OP_GE: return ">=";
3497 case WINED3D_SHADER_REL_OP_LT: return "<";
3498 case WINED3D_SHADER_REL_OP_NE: return "!=";
3499 case WINED3D_SHADER_REL_OP_LE: return "<=";
3500 default:
3501 FIXME("Unrecognized operator %#x.\n", op);
3502 return "(\?\?)";
3506 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3508 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3511 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3512 unsigned int *coord_size, unsigned int *deriv_size)
3514 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3515 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3517 *coord_size = resource_type_info[resource_type].coord_size;
3518 *deriv_size = *coord_size;
3519 if (is_array)
3520 --(*deriv_size);
3523 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3524 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3526 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3527 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3528 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3529 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3530 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3531 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3532 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3533 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3534 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3535 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3536 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3537 const char *base = "texture", *type_part = "", *suffix = "";
3538 unsigned int coord_size, deriv_size;
3540 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3542 if (resource_type >= ARRAY_SIZE(resource_type_info))
3544 ERR("Unexpected resource type %#x.\n", resource_type);
3545 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3548 /* Note that there's no such thing as a projected cube texture. */
3549 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3550 projected = FALSE;
3552 if (needs_legacy_glsl_syntax(gl_info))
3554 if (shadow)
3555 base = "shadow";
3557 type_part = resource_type_info[resource_type].type_part;
3558 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3559 type_part = "2DRect";
3560 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3561 FIXME("Unhandled resource type %#x.\n", resource_type);
3563 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3565 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3566 suffix = "ARB";
3567 else
3568 FIXME("Unsupported grad function.\n");
3572 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3574 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3575 if (flags & ~texel_fetch_flags)
3576 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3578 base = "texelFetch";
3579 type_part = "";
3582 sample_function->name = string_buffer_get(priv->string_buffers);
3583 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3584 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3586 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3587 if (shadow)
3588 ++coord_size;
3589 sample_function->offset_size = offset ? deriv_size : 0;
3590 sample_function->coord_mask = (1u << coord_size) - 1;
3591 sample_function->deriv_mask = (1u << deriv_size) - 1;
3592 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3595 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3596 struct glsl_sample_function *sample_function)
3598 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3600 string_buffer_release(priv->string_buffers, sample_function->name);
3603 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3604 BOOL sign_fixup, enum fixup_channel_source channel_source)
3606 switch(channel_source)
3608 case CHANNEL_SOURCE_ZERO:
3609 strcat(arguments, "0.0");
3610 break;
3612 case CHANNEL_SOURCE_ONE:
3613 strcat(arguments, "1.0");
3614 break;
3616 case CHANNEL_SOURCE_X:
3617 strcat(arguments, reg_name);
3618 strcat(arguments, ".x");
3619 break;
3621 case CHANNEL_SOURCE_Y:
3622 strcat(arguments, reg_name);
3623 strcat(arguments, ".y");
3624 break;
3626 case CHANNEL_SOURCE_Z:
3627 strcat(arguments, reg_name);
3628 strcat(arguments, ".z");
3629 break;
3631 case CHANNEL_SOURCE_W:
3632 strcat(arguments, reg_name);
3633 strcat(arguments, ".w");
3634 break;
3636 default:
3637 FIXME("Unhandled channel source %#x\n", channel_source);
3638 strcat(arguments, "undefined");
3639 break;
3642 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3645 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3646 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3648 unsigned int mask_size, remaining;
3649 DWORD fixup_mask = 0;
3650 char arguments[256];
3651 char mask_str[6];
3653 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3654 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3655 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3656 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3657 if (!(mask &= fixup_mask))
3658 return;
3660 if (is_complex_fixup(fixup))
3662 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3663 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3664 return;
3667 shader_glsl_write_mask_to_str(mask, mask_str);
3668 mask_size = shader_glsl_get_write_mask_size(mask);
3670 arguments[0] = '\0';
3671 remaining = mask_size;
3672 if (mask & WINED3DSP_WRITEMASK_0)
3674 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3675 if (--remaining) strcat(arguments, ", ");
3677 if (mask & WINED3DSP_WRITEMASK_1)
3679 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3680 if (--remaining) strcat(arguments, ", ");
3682 if (mask & WINED3DSP_WRITEMASK_2)
3684 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3685 if (--remaining) strcat(arguments, ", ");
3687 if (mask & WINED3DSP_WRITEMASK_3)
3689 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3690 if (--remaining) strcat(arguments, ", ");
3693 if (mask_size > 1)
3694 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3695 else
3696 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3699 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3701 char reg_name[256];
3702 BOOL is_color;
3704 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins);
3705 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3708 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3709 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3710 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3711 const char *coord_reg_fmt, ...)
3713 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3714 char dst_swizzle[6];
3715 struct color_fixup_desc fixup;
3716 BOOL np2_fixup = FALSE;
3717 va_list args;
3718 int ret;
3720 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3722 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3723 * We actually rely on it for vertex shaders and SM4+. */
3724 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3726 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3727 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3729 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3730 np2_fixup = TRUE;
3732 else
3734 fixup = COLOR_FIXUP_IDENTITY;
3737 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3739 if (sample_function->output_single_component)
3740 shader_addline(ins->ctx->buffer, "vec4(");
3742 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3743 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3745 for (;;)
3747 va_start(args, coord_reg_fmt);
3748 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3749 va_end(args);
3750 if (!ret)
3751 break;
3752 if (!string_buffer_resize(ins->ctx->buffer, ret))
3753 break;
3756 if (np2_fixup)
3758 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3759 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3761 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3763 case 1:
3764 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3765 idx >> 1, (idx % 2) ? "z" : "x");
3766 break;
3767 case 2:
3768 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3769 idx >> 1, (idx % 2) ? "zw" : "xy");
3770 break;
3771 case 3:
3772 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3773 idx >> 1, (idx % 2) ? "zw" : "xy");
3774 break;
3775 case 4:
3776 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3777 idx >> 1, (idx % 2) ? "zw" : "xy");
3778 break;
3781 if (dx && dy)
3782 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3783 else if (bias)
3784 shader_addline(ins->ctx->buffer, ", %s", bias);
3785 if (sample_function->offset_size)
3787 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3788 shader_addline(ins->ctx->buffer, ", ");
3789 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3791 shader_addline(ins->ctx->buffer, ")");
3793 if (sample_function->output_single_component)
3794 shader_addline(ins->ctx->buffer, ")");
3796 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3798 if (!is_identity_fixup(fixup))
3799 shader_glsl_color_correction(ins, fixup);
3802 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3804 /* Write the final position.
3806 * OpenGL coordinates specify the center of the pixel while D3D coords
3807 * specify the corner. The offsets are stored in z and w in
3808 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3809 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3810 * a MAD. */
3811 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3812 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3814 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3815 * in utils.c
3817 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3818 * shaders are run before the homogeneous divide, so we have to take the w
3819 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3820 * z = z * 2 - w. */
3821 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3824 /*****************************************************************************
3825 * Begin processing individual instruction opcodes
3826 ****************************************************************************/
3828 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3830 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3831 struct glsl_src_param src0_param;
3832 struct glsl_src_param src1_param;
3833 DWORD write_mask;
3834 const char *op;
3836 /* Determine the GLSL operator to use based on the opcode */
3837 switch (ins->handler_idx)
3839 case WINED3DSIH_ADD: op = "+"; break;
3840 case WINED3DSIH_AND: op = "&"; break;
3841 case WINED3DSIH_DIV: op = "/"; break;
3842 case WINED3DSIH_IADD: op = "+"; break;
3843 case WINED3DSIH_ISHL: op = "<<"; break;
3844 case WINED3DSIH_ISHR: op = ">>"; break;
3845 case WINED3DSIH_MUL: op = "*"; break;
3846 case WINED3DSIH_OR: op = "|"; break;
3847 case WINED3DSIH_SUB: op = "-"; break;
3848 case WINED3DSIH_USHR: op = ">>"; break;
3849 case WINED3DSIH_XOR: op = "^"; break;
3850 default:
3851 op = "<unhandled operator>";
3852 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3853 break;
3856 write_mask = shader_glsl_append_dst(buffer, ins);
3857 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3858 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3859 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3862 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3864 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3865 struct glsl_src_param src0_param;
3866 struct glsl_src_param src1_param;
3867 unsigned int mask_size;
3868 DWORD write_mask;
3869 const char *op;
3871 write_mask = shader_glsl_append_dst(buffer, ins);
3872 mask_size = shader_glsl_get_write_mask_size(write_mask);
3873 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3874 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3876 if (mask_size > 1)
3878 switch (ins->handler_idx)
3880 case WINED3DSIH_EQ: op = "equal"; break;
3881 case WINED3DSIH_IEQ: op = "equal"; break;
3882 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3883 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3884 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3885 case WINED3DSIH_LT: op = "lessThan"; break;
3886 case WINED3DSIH_ILT: op = "lessThan"; break;
3887 case WINED3DSIH_ULT: op = "lessThan"; break;
3888 case WINED3DSIH_NE: op = "notEqual"; break;
3889 case WINED3DSIH_INE: op = "notEqual"; break;
3890 default:
3891 op = "<unhandled operator>";
3892 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3893 break;
3896 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3897 mask_size, op, src0_param.param_str, src1_param.param_str);
3899 else
3901 switch (ins->handler_idx)
3903 case WINED3DSIH_EQ: op = "=="; break;
3904 case WINED3DSIH_IEQ: op = "=="; break;
3905 case WINED3DSIH_GE: op = ">="; break;
3906 case WINED3DSIH_IGE: op = ">="; break;
3907 case WINED3DSIH_UGE: op = ">="; break;
3908 case WINED3DSIH_LT: op = "<"; break;
3909 case WINED3DSIH_ILT: op = "<"; break;
3910 case WINED3DSIH_ULT: op = "<"; break;
3911 case WINED3DSIH_NE: op = "!="; break;
3912 case WINED3DSIH_INE: op = "!="; break;
3913 default:
3914 op = "<unhandled operator>";
3915 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3916 break;
3919 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3920 src0_param.param_str, op, src1_param.param_str);
3924 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3926 struct glsl_src_param src_param;
3927 DWORD write_mask;
3928 const char *op;
3930 switch (ins->handler_idx)
3932 case WINED3DSIH_INEG: op = "-"; break;
3933 case WINED3DSIH_NOT: op = "~"; break;
3934 default:
3935 op = "<unhandled operator>";
3936 ERR("Unhandled opcode %s.\n",
3937 debug_d3dshaderinstructionhandler(ins->handler_idx));
3938 break;
3941 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3942 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3943 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3946 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3948 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3949 struct glsl_src_param src0_param;
3950 struct glsl_src_param src1_param;
3951 DWORD write_mask;
3953 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3954 * If not, we can emulate it. */
3955 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3956 FIXME("64-bit integer multiplies not implemented.\n");
3958 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3960 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3961 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3962 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3964 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3965 src0_param.param_str, src1_param.param_str);
3969 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3971 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3972 struct glsl_src_param src0_param, src1_param;
3973 DWORD write_mask;
3975 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3977 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3979 char dst_mask[6];
3981 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3982 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3983 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3984 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3985 dst_mask, src0_param.param_str, src1_param.param_str);
3987 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3988 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3989 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3990 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3992 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3993 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3995 else
3997 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3998 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3999 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4000 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
4003 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4005 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4006 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4007 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4008 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4012 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
4013 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
4015 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4016 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4017 struct glsl_src_param src0_param;
4018 DWORD write_mask;
4020 write_mask = shader_glsl_append_dst(buffer, ins);
4021 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4023 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
4024 * shader versions WINED3DSIO_MOVA is used for this. */
4025 if (ins->ctx->reg_maps->shader_version.major == 1
4026 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
4027 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
4029 /* This is a simple floor() */
4030 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4031 if (mask_size > 1) {
4032 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4033 } else {
4034 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4037 else if (ins->handler_idx == WINED3DSIH_MOVA)
4039 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4041 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4043 if (mask_size > 1)
4044 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4045 else
4046 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4048 else
4050 if (mask_size > 1)
4051 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4052 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4053 else
4054 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4055 src0_param.param_str, src0_param.param_str);
4058 else
4060 shader_addline(buffer, "%s);\n", src0_param.param_str);
4064 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4065 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4067 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4068 struct glsl_src_param src0_param;
4069 struct glsl_src_param src1_param;
4070 DWORD dst_write_mask, src_write_mask;
4071 unsigned int dst_size;
4073 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4074 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4076 /* dp4 works on vec4, dp3 on vec3, etc. */
4077 if (ins->handler_idx == WINED3DSIH_DP4)
4078 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4079 else if (ins->handler_idx == WINED3DSIH_DP3)
4080 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4081 else
4082 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4084 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4085 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4087 if (dst_size > 1) {
4088 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4089 } else {
4090 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4094 /* Note that this instruction has some restrictions. The destination write mask
4095 * can't contain the w component, and the source swizzles have to be .xyzw */
4096 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4098 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4099 struct glsl_src_param src0_param;
4100 struct glsl_src_param src1_param;
4101 char dst_mask[6];
4103 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4104 shader_glsl_append_dst(ins->ctx->buffer, ins);
4105 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4106 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4107 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4110 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4112 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4114 if (!stream)
4115 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4116 else
4117 FIXME("Unhandled primitive stream %u.\n", stream);
4120 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4121 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4122 * GLSL uses the value as-is. */
4123 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4125 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4126 struct glsl_src_param src0_param;
4127 struct glsl_src_param src1_param;
4128 DWORD dst_write_mask;
4129 unsigned int dst_size;
4131 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4132 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4134 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4135 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4137 if (dst_size > 1)
4139 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4140 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4142 else
4144 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4145 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4149 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4150 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4152 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4153 struct glsl_src_param src_param;
4154 const char *instruction;
4155 DWORD write_mask;
4156 unsigned i;
4158 /* Determine the GLSL function to use based on the opcode */
4159 /* TODO: Possibly make this a table for faster lookups */
4160 switch (ins->handler_idx)
4162 case WINED3DSIH_ABS: instruction = "abs"; break;
4163 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4164 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4165 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4166 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4167 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4168 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4169 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4170 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4171 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4172 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4173 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4174 case WINED3DSIH_FRC: instruction = "fract"; break;
4175 case WINED3DSIH_IMAX: instruction = "max"; break;
4176 case WINED3DSIH_IMIN: instruction = "min"; break;
4177 case WINED3DSIH_MAX: instruction = "max"; break;
4178 case WINED3DSIH_MIN: instruction = "min"; break;
4179 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4180 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4181 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4182 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4183 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4184 case WINED3DSIH_UMAX: instruction = "max"; break;
4185 case WINED3DSIH_UMIN: instruction = "min"; break;
4186 default: instruction = "";
4187 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4188 break;
4191 write_mask = shader_glsl_append_dst(buffer, ins);
4193 /* In D3D bits are numbered from the most significant bit. */
4194 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4195 shader_addline(buffer, "31 - ");
4196 shader_addline(buffer, "%s(", instruction);
4198 if (ins->src_count)
4200 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4201 shader_addline(buffer, "%s", src_param.param_str);
4202 for (i = 1; i < ins->src_count; ++i)
4204 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4205 shader_addline(buffer, ", %s", src_param.param_str);
4209 shader_addline(buffer, "));\n");
4212 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4214 struct wined3d_shader_dst_param dst;
4215 struct glsl_src_param src;
4216 DWORD write_mask;
4217 const char *fmt;
4218 unsigned int i;
4220 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4221 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4223 dst = ins->dst[0];
4224 for (i = 0; i < 4; ++i)
4226 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4227 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4228 &dst, dst.reg.data_type)))
4229 continue;
4231 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4232 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4236 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4238 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4239 struct wined3d_shader_dst_param dst;
4240 struct glsl_src_param src[4];
4241 const char *instruction;
4242 BOOL tmp_dst = FALSE;
4243 char mask_char[6];
4244 unsigned int i, j;
4245 DWORD write_mask;
4247 switch (ins->handler_idx)
4249 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4250 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4251 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4252 default:
4253 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4254 return;
4257 for (i = 0; i < ins->src_count; ++i)
4259 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4260 && ins->dst[0].reg.type == ins->src[i].reg.type)
4261 tmp_dst = TRUE;
4264 dst = ins->dst[0];
4265 for (i = 0; i < 4; ++i)
4267 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4268 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4269 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4270 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4271 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4272 continue;
4274 for (j = 0; j < ins->src_count; ++j)
4275 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4276 shader_addline(buffer, "%s(", instruction);
4277 for (j = 0; j < ins->src_count - 2; ++j)
4278 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4279 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4282 if (tmp_dst)
4284 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4285 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4286 shader_addline(buffer, "tmp0%s);\n", mask_char);
4290 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4292 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4294 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4295 struct glsl_src_param src_param;
4296 unsigned int mask_size;
4297 DWORD write_mask;
4298 char dst_mask[6];
4300 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4301 mask_size = shader_glsl_get_write_mask_size(write_mask);
4302 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4304 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4305 src_param.param_str, src_param.param_str);
4306 shader_glsl_append_dst(buffer, ins);
4308 if (mask_size > 1)
4310 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4311 mask_size, src_param.param_str);
4313 else
4315 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4316 src_param.param_str);
4320 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4322 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4323 ins->ctx->reg_maps->shader_version.minor);
4324 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4325 struct glsl_src_param src0_param;
4326 const char *prefix, *suffix;
4327 unsigned int dst_size;
4328 DWORD dst_write_mask;
4330 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4331 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4333 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4334 dst_write_mask = WINED3DSP_WRITEMASK_3;
4336 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4338 switch (ins->handler_idx)
4340 case WINED3DSIH_EXP:
4341 case WINED3DSIH_EXPP:
4342 prefix = "exp2(";
4343 suffix = ")";
4344 break;
4346 case WINED3DSIH_LOG:
4347 case WINED3DSIH_LOGP:
4348 prefix = "log2(abs(";
4349 suffix = "))";
4350 break;
4352 case WINED3DSIH_RCP:
4353 prefix = "1.0 / ";
4354 suffix = "";
4355 break;
4357 case WINED3DSIH_RSQ:
4358 prefix = "inversesqrt(abs(";
4359 suffix = "))";
4360 break;
4362 default:
4363 prefix = "";
4364 suffix = "";
4365 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4366 break;
4369 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4370 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4371 else
4372 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4375 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4376 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4377 * dst.x = 2^(floor(src))
4378 * dst.y = src - floor(src)
4379 * dst.z = 2^src (partial precision is allowed, but optional)
4380 * dst.w = 1.0;
4381 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4382 * dst = 2^src; (partial precision is allowed, but optional)
4384 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4386 if (ins->ctx->reg_maps->shader_version.major < 2)
4388 struct glsl_src_param src_param;
4389 char dst_mask[6];
4391 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4393 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4394 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4395 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4396 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4398 shader_glsl_append_dst(ins->ctx->buffer, ins);
4399 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4400 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4401 return;
4404 shader_glsl_scalar_op(ins);
4407 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4408 const char *vector_constructor, const char *scalar_constructor)
4410 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4411 struct glsl_src_param src_param;
4412 unsigned int mask_size;
4413 DWORD write_mask;
4415 write_mask = shader_glsl_append_dst(buffer, ins);
4416 mask_size = shader_glsl_get_write_mask_size(write_mask);
4417 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4419 if (mask_size > 1)
4420 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4421 else
4422 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4425 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4427 shader_glsl_cast(ins, "ivec", "int");
4430 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4432 shader_glsl_cast(ins, "uvec", "uint");
4435 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4437 shader_glsl_cast(ins, "vec", "float");
4440 /** Process signed comparison opcodes in GLSL. */
4441 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4443 struct glsl_src_param src0_param;
4444 struct glsl_src_param src1_param;
4445 DWORD write_mask;
4446 unsigned int mask_size;
4448 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4449 mask_size = shader_glsl_get_write_mask_size(write_mask);
4450 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4451 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4453 if (mask_size > 1) {
4454 const char *compare;
4456 switch(ins->handler_idx)
4458 case WINED3DSIH_SLT: compare = "lessThan"; break;
4459 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4460 default: compare = "";
4461 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4464 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4465 src0_param.param_str, src1_param.param_str);
4466 } else {
4467 switch(ins->handler_idx)
4469 case WINED3DSIH_SLT:
4470 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4471 * to return 0.0 but step returns 1.0 because step is not < x
4472 * An alternative is a bvec compare padded with an unused second component.
4473 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4474 * issue. Playing with not() is not possible either because not() does not accept
4475 * a scalar.
4477 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4478 src0_param.param_str, src1_param.param_str);
4479 break;
4480 case WINED3DSIH_SGE:
4481 /* Here we can use the step() function and safe a conditional */
4482 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4483 break;
4484 default:
4485 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4491 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4493 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4494 struct wined3d_shader_dst_param dst[2];
4495 struct glsl_src_param src[3];
4496 unsigned int i, j, k;
4497 char mask_char[6];
4498 DWORD write_mask;
4499 BOOL tmp_dst[2];
4501 for (i = 0; i < ins->dst_count; ++i)
4503 tmp_dst[i] = FALSE;
4504 for (j = 0; j < ins->src_count; ++j)
4506 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4507 && ins->dst[i].reg.type == ins->src[j].reg.type)
4508 tmp_dst[i] = TRUE;
4512 dst[0] = ins->dst[0];
4513 dst[1] = ins->dst[1];
4514 for (i = 0; i < 4; ++i)
4516 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4518 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4519 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4520 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4521 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4522 continue;
4524 for (k = 0; k < ARRAY_SIZE(src); ++k)
4525 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4527 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4528 src[0].param_str, src[1].param_str, src[2].param_str);
4532 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4534 if (tmp_dst[i])
4536 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4537 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4538 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4543 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4545 const char *condition_prefix, *condition_suffix;
4546 struct wined3d_shader_dst_param dst;
4547 struct glsl_src_param src0_param;
4548 struct glsl_src_param src1_param;
4549 struct glsl_src_param src2_param;
4550 BOOL temp_destination = FALSE;
4551 DWORD cmp_channel = 0;
4552 unsigned int i, j;
4553 char mask_char[6];
4554 DWORD write_mask;
4556 switch (ins->handler_idx)
4558 case WINED3DSIH_CMP:
4559 condition_prefix = "";
4560 condition_suffix = " >= 0.0";
4561 break;
4563 case WINED3DSIH_CND:
4564 condition_prefix = "";
4565 condition_suffix = " > 0.5";
4566 break;
4568 case WINED3DSIH_MOVC:
4569 condition_prefix = "bool(";
4570 condition_suffix = ")";
4571 break;
4573 default:
4574 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4575 condition_prefix = "<unhandled prefix>";
4576 condition_suffix = "<unhandled suffix>";
4577 break;
4580 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4582 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4583 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4584 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4585 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4587 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4588 condition_prefix, src0_param.param_str, condition_suffix,
4589 src1_param.param_str, src2_param.param_str);
4590 return;
4593 dst = ins->dst[0];
4595 /* Splitting the instruction up in multiple lines imposes a problem:
4596 * The first lines may overwrite source parameters of the following lines.
4597 * Deal with that by using a temporary destination register if needed. */
4598 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4599 && ins->src[0].reg.type == dst.reg.type)
4600 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4601 && ins->src[1].reg.type == dst.reg.type)
4602 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4603 && ins->src[2].reg.type == dst.reg.type))
4604 temp_destination = TRUE;
4606 /* Cycle through all source0 channels. */
4607 for (i = 0; i < 4; ++i)
4609 write_mask = 0;
4610 /* Find the destination channels which use the current source0 channel. */
4611 for (j = 0; j < 4; ++j)
4613 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4615 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4616 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4619 dst.write_mask = ins->dst[0].write_mask & write_mask;
4621 if (temp_destination)
4623 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4624 continue;
4625 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4627 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4628 continue;
4630 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4631 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4632 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4634 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4635 condition_prefix, src0_param.param_str, condition_suffix,
4636 src1_param.param_str, src2_param.param_str);
4639 if (temp_destination)
4641 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4642 shader_glsl_append_dst(ins->ctx->buffer, ins);
4643 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4647 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4648 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4649 * the compare is done per component of src0. */
4650 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4652 struct glsl_src_param src0_param;
4653 struct glsl_src_param src1_param;
4654 struct glsl_src_param src2_param;
4655 DWORD write_mask;
4656 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4657 ins->ctx->reg_maps->shader_version.minor);
4659 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4661 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4662 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4663 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4664 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4666 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4667 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4668 else
4669 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4670 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4671 return;
4674 shader_glsl_conditional_move(ins);
4677 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4678 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4680 struct glsl_src_param src0_param;
4681 struct glsl_src_param src1_param;
4682 struct glsl_src_param src2_param;
4683 DWORD write_mask;
4685 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4686 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4687 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4688 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4689 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4690 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4693 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4694 Vertex shaders to GLSL codes */
4695 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4697 int i;
4698 int nComponents = 0;
4699 struct wined3d_shader_dst_param tmp_dst = {{0}};
4700 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4701 struct wined3d_shader_instruction tmp_ins;
4703 memset(&tmp_ins, 0, sizeof(tmp_ins));
4705 /* Set constants for the temporary argument */
4706 tmp_ins.ctx = ins->ctx;
4707 tmp_ins.dst_count = 1;
4708 tmp_ins.dst = &tmp_dst;
4709 tmp_ins.src_count = 2;
4710 tmp_ins.src = tmp_src;
4712 switch(ins->handler_idx)
4714 case WINED3DSIH_M4x4:
4715 nComponents = 4;
4716 tmp_ins.handler_idx = WINED3DSIH_DP4;
4717 break;
4718 case WINED3DSIH_M4x3:
4719 nComponents = 3;
4720 tmp_ins.handler_idx = WINED3DSIH_DP4;
4721 break;
4722 case WINED3DSIH_M3x4:
4723 nComponents = 4;
4724 tmp_ins.handler_idx = WINED3DSIH_DP3;
4725 break;
4726 case WINED3DSIH_M3x3:
4727 nComponents = 3;
4728 tmp_ins.handler_idx = WINED3DSIH_DP3;
4729 break;
4730 case WINED3DSIH_M3x2:
4731 nComponents = 2;
4732 tmp_ins.handler_idx = WINED3DSIH_DP3;
4733 break;
4734 default:
4735 break;
4738 tmp_dst = ins->dst[0];
4739 tmp_src[0] = ins->src[0];
4740 tmp_src[1] = ins->src[1];
4741 for (i = 0; i < nComponents; ++i)
4743 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4744 shader_glsl_dot(&tmp_ins);
4745 ++tmp_src[1].reg.idx[0].offset;
4750 The LRP instruction performs a component-wise linear interpolation
4751 between the second and third operands using the first operand as the
4752 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4753 This is equivalent to mix(src2, src1, src0);
4755 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4757 struct glsl_src_param src0_param;
4758 struct glsl_src_param src1_param;
4759 struct glsl_src_param src2_param;
4760 DWORD write_mask;
4762 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4764 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4765 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4766 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4768 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4769 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4772 /** Process the WINED3DSIO_LIT instruction in GLSL:
4773 * dst.x = dst.w = 1.0
4774 * dst.y = (src0.x > 0) ? src0.x
4775 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4776 * where src.w is clamped at +- 128
4778 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4780 struct glsl_src_param src0_param;
4781 struct glsl_src_param src1_param;
4782 struct glsl_src_param src3_param;
4783 char dst_mask[6];
4785 shader_glsl_append_dst(ins->ctx->buffer, ins);
4786 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4788 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4789 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4790 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4792 /* The sdk specifies the instruction like this
4793 * dst.x = 1.0;
4794 * if(src.x > 0.0) dst.y = src.x
4795 * else dst.y = 0.0.
4796 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4797 * else dst.z = 0.0;
4798 * dst.w = 1.0;
4799 * (where power = src.w clamped between -128 and 128)
4801 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4802 * dst.x = 1.0 ... No further explanation needed
4803 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4804 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4805 * dst.w = 1.0. ... Nothing fancy.
4807 * So we still have one conditional in there. So do this:
4808 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4810 * 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),
4811 * 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.
4812 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4814 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4815 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4816 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4818 shader_addline(ins->ctx->buffer,
4819 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4820 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4821 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4822 src0_param.param_str, src3_param.param_str, dst_mask);
4825 /** Process the WINED3DSIO_DST instruction in GLSL:
4826 * dst.x = 1.0
4827 * dst.y = src0.x * src0.y
4828 * dst.z = src0.z
4829 * dst.w = src1.w
4831 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4833 struct glsl_src_param src0y_param;
4834 struct glsl_src_param src0z_param;
4835 struct glsl_src_param src1y_param;
4836 struct glsl_src_param src1w_param;
4837 char dst_mask[6];
4839 shader_glsl_append_dst(ins->ctx->buffer, ins);
4840 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4842 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4843 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4844 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4845 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4847 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4848 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4851 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4852 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4853 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4855 * dst.x = cos(src0.?)
4856 * dst.y = sin(src0.?)
4857 * dst.z = dst.z
4858 * dst.w = dst.w
4860 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4862 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4863 struct glsl_src_param src0_param;
4864 DWORD write_mask;
4866 if (ins->ctx->reg_maps->shader_version.major < 4)
4868 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4870 write_mask = shader_glsl_append_dst(buffer, ins);
4871 switch (write_mask)
4873 case WINED3DSP_WRITEMASK_0:
4874 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4875 break;
4877 case WINED3DSP_WRITEMASK_1:
4878 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4879 break;
4881 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4882 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4883 src0_param.param_str, src0_param.param_str);
4884 break;
4886 default:
4887 ERR("Write mask should be .x, .y or .xy\n");
4888 break;
4891 return;
4894 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4897 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4899 char dst_mask[6];
4901 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4902 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4903 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4905 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4906 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4907 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4909 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4910 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4912 else
4914 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4915 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4916 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4919 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4921 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4922 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4923 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4927 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4928 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4929 * generate invalid code
4931 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4933 struct glsl_src_param src0_param;
4934 DWORD write_mask;
4936 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4937 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4939 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4942 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4943 * Start a for() loop where src1.y is the initial value of aL,
4944 * increment aL by src1.z for a total of src1.x iterations.
4945 * Need to use a temporary variable for this operation.
4947 /* FIXME: I don't think nested loops will work correctly this way. */
4948 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4950 struct wined3d_shader_parser_state *state = ins->ctx->state;
4951 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4952 const struct wined3d_shader *shader = ins->ctx->shader;
4953 const struct wined3d_shader_lconst *constant;
4954 struct glsl_src_param src1_param;
4955 const DWORD *control_values = NULL;
4957 if (ins->ctx->reg_maps->shader_version.major < 4)
4959 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4961 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4962 * class hardware doesn't support real varying indexing, but Microsoft
4963 * designed this feature for Shader model 2.x+. If the loop control is
4964 * known at compile time, the GLSL compiler can unroll the loop, and
4965 * replace indirect addressing with direct addressing. */
4966 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4968 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4970 if (constant->idx == ins->src[1].reg.idx[0].offset)
4972 control_values = constant->value;
4973 break;
4978 if (control_values)
4980 struct wined3d_shader_loop_control loop_control;
4981 loop_control.count = control_values[0];
4982 loop_control.start = control_values[1];
4983 loop_control.step = (int)control_values[2];
4985 if (loop_control.step > 0)
4987 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4988 state->current_loop_depth, loop_control.start,
4989 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4990 state->current_loop_depth, loop_control.step);
4992 else if (loop_control.step < 0)
4994 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4995 state->current_loop_depth, loop_control.start,
4996 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4997 state->current_loop_depth, loop_control.step);
4999 else
5001 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
5002 state->current_loop_depth, loop_control.start, state->current_loop_depth,
5003 state->current_loop_depth, loop_control.count,
5004 state->current_loop_depth);
5007 else
5009 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
5010 state->current_loop_depth, state->current_loop_reg,
5011 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
5012 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
5015 ++state->current_loop_reg;
5017 else
5019 shader_addline(buffer, "for (;;)\n{\n");
5022 ++state->current_loop_depth;
5025 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
5027 struct wined3d_shader_parser_state *state = ins->ctx->state;
5029 shader_addline(ins->ctx->buffer, "}\n");
5031 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5033 --state->current_loop_depth;
5034 --state->current_loop_reg;
5037 if (ins->handler_idx == WINED3DSIH_ENDREP)
5039 --state->current_loop_depth;
5043 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5045 struct wined3d_shader_parser_state *state = ins->ctx->state;
5046 const struct wined3d_shader *shader = ins->ctx->shader;
5047 const struct wined3d_shader_lconst *constant;
5048 struct glsl_src_param src0_param;
5049 const DWORD *control_values = NULL;
5051 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5052 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5054 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5056 if (constant->idx == ins->src[0].reg.idx[0].offset)
5058 control_values = constant->value;
5059 break;
5064 if (control_values)
5066 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5067 state->current_loop_depth, state->current_loop_depth,
5068 control_values[0], state->current_loop_depth);
5070 else
5072 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5073 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5074 state->current_loop_depth, state->current_loop_depth,
5075 src0_param.param_str, state->current_loop_depth);
5078 ++state->current_loop_depth;
5081 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5083 struct glsl_src_param src0_param;
5085 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5086 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5089 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5091 struct glsl_src_param src0_param;
5093 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5094 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5097 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5099 shader_addline(ins->ctx->buffer, "default:\n");
5102 static void shader_glsl_generate_conditional_op(const struct wined3d_shader_instruction *ins,
5103 const char *op)
5105 struct glsl_src_param src_param;
5106 const char *condition;
5108 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5109 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5110 shader_addline(ins->ctx->buffer, "if (%s(%s)) %s\n", condition, src_param.param_str, op);
5113 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5115 shader_glsl_generate_conditional_op(ins, "{");
5118 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5120 struct glsl_src_param src0_param;
5121 struct glsl_src_param src1_param;
5123 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5124 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5126 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5127 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5130 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5132 shader_addline(ins->ctx->buffer, "} else {\n");
5135 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5137 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5139 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5140 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5141 shader_glsl_fixup_position(ins->ctx->buffer);
5143 if (!stream)
5144 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5145 else
5146 FIXME("Unhandled primitive stream %u.\n", stream);
5149 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5151 shader_addline(ins->ctx->buffer, "break;\n");
5154 /* FIXME: According to MSDN the compare is done per component. */
5155 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5157 struct glsl_src_param src0_param;
5158 struct glsl_src_param src1_param;
5160 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5161 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5163 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5164 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5167 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5169 const char *op;
5171 switch (ins->handler_idx)
5173 case WINED3DSIH_BREAKP: op = "break;"; break;
5174 case WINED3DSIH_CONTINUEP: op = "continue;"; break;
5175 case WINED3DSIH_RETP: op = "return;"; break;
5176 default:
5177 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5178 return;
5181 shader_glsl_generate_conditional_op(ins, op);
5184 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5186 shader_addline(ins->ctx->buffer, "continue;\n");
5189 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5191 shader_addline(ins->ctx->buffer, "}\n");
5192 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5194 /* Subroutines appear at the end of the shader. */
5195 ins->ctx->state->in_subroutine = TRUE;
5198 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5200 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5203 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5205 struct glsl_src_param src1_param;
5207 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5208 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5209 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5212 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5214 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5216 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5218 shader_glsl_generate_shader_epilogue(ins->ctx);
5219 shader_addline(ins->ctx->buffer, "return;\n");
5223 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5225 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5226 ins->ctx->reg_maps->shader_version.minor);
5227 struct glsl_sample_function sample_function;
5228 DWORD sample_flags = 0;
5229 DWORD resource_idx;
5230 DWORD mask = 0, swizzle;
5231 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5233 /* 1.0-1.4: Use destination register as sampler source.
5234 * 2.0+: Use provided sampler source. */
5235 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5236 resource_idx = ins->dst[0].reg.idx[0].offset;
5237 else
5238 resource_idx = ins->src[1].reg.idx[0].offset;
5240 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5242 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5243 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5244 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5246 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5247 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5249 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5250 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5252 case WINED3D_TTFF_COUNT1:
5253 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5254 break;
5255 case WINED3D_TTFF_COUNT2:
5256 mask = WINED3DSP_WRITEMASK_1;
5257 break;
5258 case WINED3D_TTFF_COUNT3:
5259 mask = WINED3DSP_WRITEMASK_2;
5260 break;
5261 case WINED3D_TTFF_COUNT4:
5262 case WINED3D_TTFF_DISABLE:
5263 mask = WINED3DSP_WRITEMASK_3;
5264 break;
5268 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5270 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5272 if (src_mod == WINED3DSPSM_DZ) {
5273 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5274 mask = WINED3DSP_WRITEMASK_2;
5275 } else if (src_mod == WINED3DSPSM_DW) {
5276 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5277 mask = WINED3DSP_WRITEMASK_3;
5280 else
5282 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5283 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5285 /* ps 2.0 texldp instruction always divides by the fourth component. */
5286 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5287 mask = WINED3DSP_WRITEMASK_3;
5291 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5292 mask |= sample_function.coord_mask;
5293 sample_function.coord_mask = mask;
5295 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5296 else swizzle = ins->src[1].swizzle;
5298 /* 1.0-1.3: Use destination register as coordinate source.
5299 1.4+: Use provided coordinate source register. */
5300 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5302 char coord_mask[6];
5303 shader_glsl_write_mask_to_str(mask, coord_mask);
5304 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5305 "T%u%s", resource_idx, coord_mask);
5307 else
5309 struct glsl_src_param coord_param;
5310 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5311 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5313 struct glsl_src_param bias;
5314 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5315 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5316 NULL, "%s", coord_param.param_str);
5317 } else {
5318 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5319 "%s", coord_param.param_str);
5322 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5325 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5327 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5328 struct glsl_src_param coord_param, dx_param, dy_param;
5329 struct glsl_sample_function sample_function;
5330 DWORD sampler_idx;
5331 DWORD swizzle = ins->src[1].swizzle;
5333 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5335 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5336 shader_glsl_tex(ins);
5337 return;
5340 sampler_idx = ins->src[1].reg.idx[0].offset;
5342 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5343 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5344 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5345 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5347 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5348 NULL, NULL, "%s", coord_param.param_str);
5349 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5352 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5354 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5355 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5356 struct glsl_src_param coord_param, lod_param;
5357 struct glsl_sample_function sample_function;
5358 DWORD swizzle = ins->src[1].swizzle;
5359 DWORD sampler_idx;
5361 sampler_idx = ins->src[1].reg.idx[0].offset;
5363 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5364 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5366 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5368 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5369 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5371 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5372 * However, the NVIDIA drivers allow them in fragment shaders as well,
5373 * even without the appropriate extension. */
5374 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5376 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5377 "%s", coord_param.param_str);
5378 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5381 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5382 unsigned int resource_idx, unsigned int sampler_idx)
5384 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5385 unsigned int i;
5387 for (i = 0; i < sampler_map->count; ++i)
5389 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5390 return entries[i].bind_idx;
5393 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5395 return ~0u;
5398 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5400 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5401 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5402 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5403 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5404 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5405 struct glsl_src_param structure_idx, offset, data, data2;
5406 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5407 enum wined3d_shader_resource_type resource_type;
5408 struct wined3d_string_buffer *address;
5409 enum wined3d_data_type data_type;
5410 unsigned int resource_idx, stride;
5411 const char *op, *resource;
5412 DWORD coord_mask;
5413 BOOL is_tgsm;
5415 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5416 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5417 if (is_tgsm)
5419 if (resource_idx >= reg_maps->tgsm_count)
5421 ERR("Invalid TGSM index %u.\n", resource_idx);
5422 return;
5424 resource = "g";
5425 data_type = WINED3D_DATA_UINT;
5426 coord_mask = 1;
5427 stride = reg_maps->tgsm[resource_idx].stride;
5429 else
5431 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5433 ERR("Invalid UAV index %u.\n", resource_idx);
5434 return;
5436 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5437 if (resource_type >= ARRAY_SIZE(resource_type_info))
5439 ERR("Unexpected resource type %#x.\n", resource_type);
5440 return;
5442 resource = "image";
5443 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5444 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5445 stride = reg_maps->uav_resource_info[resource_idx].stride;
5448 switch (ins->handler_idx)
5450 case WINED3DSIH_ATOMIC_AND:
5451 case WINED3DSIH_IMM_ATOMIC_AND:
5452 if (is_tgsm)
5453 op = "atomicAnd";
5454 else
5455 op = "imageAtomicAnd";
5456 break;
5457 case WINED3DSIH_ATOMIC_CMP_STORE:
5458 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5459 if (is_tgsm)
5460 op = "atomicCompSwap";
5461 else
5462 op = "imageAtomicCompSwap";
5463 break;
5464 case WINED3DSIH_ATOMIC_IADD:
5465 case WINED3DSIH_IMM_ATOMIC_IADD:
5466 if (is_tgsm)
5467 op = "atomicAdd";
5468 else
5469 op = "imageAtomicAdd";
5470 break;
5471 case WINED3DSIH_ATOMIC_IMAX:
5472 case WINED3DSIH_IMM_ATOMIC_IMAX:
5473 if (is_tgsm)
5474 op = "atomicMax";
5475 else
5476 op = "imageAtomicMax";
5477 if (data_type != WINED3D_DATA_INT)
5479 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5480 return;
5482 break;
5483 case WINED3DSIH_ATOMIC_IMIN:
5484 case WINED3DSIH_IMM_ATOMIC_IMIN:
5485 if (is_tgsm)
5486 op = "atomicMin";
5487 else
5488 op = "imageAtomicMin";
5489 if (data_type != WINED3D_DATA_INT)
5491 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5492 return;
5494 break;
5495 case WINED3DSIH_ATOMIC_OR:
5496 case WINED3DSIH_IMM_ATOMIC_OR:
5497 if (is_tgsm)
5498 op = "atomicOr";
5499 else
5500 op = "imageAtomicOr";
5501 break;
5502 case WINED3DSIH_ATOMIC_UMAX:
5503 case WINED3DSIH_IMM_ATOMIC_UMAX:
5504 if (is_tgsm)
5505 op = "atomicMax";
5506 else
5507 op = "imageAtomicMax";
5508 if (data_type != WINED3D_DATA_UINT)
5510 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5511 return;
5513 break;
5514 case WINED3DSIH_ATOMIC_UMIN:
5515 case WINED3DSIH_IMM_ATOMIC_UMIN:
5516 if (is_tgsm)
5517 op = "atomicMin";
5518 else
5519 op = "imageAtomicMin";
5520 if (data_type != WINED3D_DATA_UINT)
5522 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5523 return;
5525 break;
5526 case WINED3DSIH_ATOMIC_XOR:
5527 case WINED3DSIH_IMM_ATOMIC_XOR:
5528 if (is_tgsm)
5529 op = "atomicXor";
5530 else
5531 op = "imageAtomicXor";
5532 break;
5533 case WINED3DSIH_IMM_ATOMIC_EXCH:
5534 if (is_tgsm)
5535 op = "atomicExchange";
5536 else
5537 op = "imageAtomicExchange";
5538 break;
5539 default:
5540 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5541 return;
5544 address = string_buffer_get(priv->string_buffers);
5545 if (stride)
5547 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5548 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5549 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5551 else
5553 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5554 string_buffer_sprintf(address, "%s", offset.param_str);
5555 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5556 shader_addline(address, "/ 4");
5559 if (is_imm_instruction)
5560 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5562 if (is_tgsm)
5563 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5564 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5565 else
5566 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5567 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5569 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5570 shader_addline(buffer, "%s", data.param_str);
5571 if (ins->src_count >= 3)
5573 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5574 shader_addline(buffer, ", %s", data2.param_str);
5577 if (is_imm_instruction)
5578 shader_addline(buffer, ")");
5579 shader_addline(buffer, ");\n");
5581 string_buffer_release(priv->string_buffers, address);
5584 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5586 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5587 const char *op;
5589 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5590 op = "atomicCounterIncrement";
5591 else
5592 op = "atomicCounterDecrement";
5594 shader_glsl_append_dst(ins->ctx->buffer, ins);
5595 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5598 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5600 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5601 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5602 enum wined3d_shader_resource_type resource_type;
5603 struct glsl_src_param image_coord_param;
5604 enum wined3d_data_type data_type;
5605 DWORD coord_mask, write_mask;
5606 unsigned int uav_idx;
5607 char dst_swizzle[6];
5609 uav_idx = ins->src[1].reg.idx[0].offset;
5610 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5612 ERR("Invalid UAV index %u.\n", uav_idx);
5613 return;
5615 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5616 if (resource_type >= ARRAY_SIZE(resource_type_info))
5618 ERR("Unexpected resource type %#x.\n", resource_type);
5619 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5621 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5622 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5624 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5625 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5627 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5628 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5629 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5632 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5634 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5635 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5636 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5637 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5638 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5639 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5640 struct glsl_src_param structure_idx, offset;
5641 struct wined3d_string_buffer *address;
5642 struct wined3d_shader_dst_param dst;
5643 const char *function, *resource;
5645 resource_idx = src->reg.idx[0].offset;
5646 if (src->reg.type == WINED3DSPR_RESOURCE)
5648 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5650 ERR("Invalid resource index %u.\n", resource_idx);
5651 return;
5653 stride = reg_maps->resource_info[resource_idx].stride;
5654 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5655 function = "texelFetch";
5656 resource = "sampler";
5658 else if (src->reg.type == WINED3DSPR_UAV)
5660 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5662 ERR("Invalid UAV index %u.\n", resource_idx);
5663 return;
5665 stride = reg_maps->uav_resource_info[resource_idx].stride;
5666 bind_idx = resource_idx;
5667 function = "imageLoad";
5668 resource = "image";
5670 else
5672 if (resource_idx >= reg_maps->tgsm_count)
5674 ERR("Invalid TGSM index %u.\n", resource_idx);
5675 return;
5677 stride = reg_maps->tgsm[resource_idx].stride;
5678 bind_idx = resource_idx;
5679 function = NULL;
5680 resource = "g";
5683 address = string_buffer_get(priv->string_buffers);
5684 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5686 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5687 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5689 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5690 shader_addline(address, "%s / 4", offset.param_str);
5692 dst = ins->dst[0];
5693 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5695 /* The instruction is split into multiple lines. The first lines may
5696 * overwrite source parameters of the following lines. */
5697 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5698 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5701 for (i = 0; i < 4; ++i)
5703 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5704 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5705 continue;
5707 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5708 if (function)
5709 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5710 function, prefix, resource, bind_idx, address->buffer, swizzle);
5711 else
5712 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5713 prefix, resource, bind_idx, address->buffer, swizzle);
5716 string_buffer_release(priv->string_buffers, address);
5719 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5721 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5722 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5723 struct glsl_src_param image_coord_param, image_data_param;
5724 enum wined3d_shader_resource_type resource_type;
5725 enum wined3d_data_type data_type;
5726 unsigned int uav_idx;
5727 DWORD coord_mask;
5729 uav_idx = ins->dst[0].reg.idx[0].offset;
5730 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5732 ERR("Invalid UAV index %u.\n", uav_idx);
5733 return;
5735 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5736 if (resource_type >= ARRAY_SIZE(resource_type_info))
5738 ERR("Unexpected resource type %#x.\n", resource_type);
5739 return;
5741 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5742 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5744 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5745 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5746 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5747 shader_glsl_get_prefix(version->type), uav_idx,
5748 image_coord_param.param_str, image_data_param.param_str);
5751 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5753 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5754 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5755 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5756 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5757 struct glsl_src_param structure_idx, offset, data;
5758 unsigned int i, resource_idx, stride, src_idx = 0;
5759 struct wined3d_string_buffer *address;
5760 DWORD write_mask;
5761 BOOL is_tgsm;
5763 resource_idx = ins->dst[0].reg.idx[0].offset;
5764 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5765 if (is_tgsm)
5767 if (resource_idx >= reg_maps->tgsm_count)
5769 ERR("Invalid TGSM index %u.\n", resource_idx);
5770 return;
5772 stride = reg_maps->tgsm[resource_idx].stride;
5774 else
5776 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5778 ERR("Invalid UAV index %u.\n", resource_idx);
5779 return;
5781 stride = reg_maps->uav_resource_info[resource_idx].stride;
5784 address = string_buffer_get(priv->string_buffers);
5785 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5787 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5788 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5790 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5791 shader_addline(address, "%s / 4", offset.param_str);
5793 for (i = 0; i < 4; ++i)
5795 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5796 continue;
5798 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5800 if (is_tgsm)
5801 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5802 prefix, resource_idx, address->buffer, i, data.param_str);
5803 else
5804 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5805 prefix, resource_idx, address->buffer, i, data.param_str);
5808 string_buffer_release(priv->string_buffers, address);
5811 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5813 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5814 unsigned int sync_flags = ins->flags;
5816 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5818 shader_addline(buffer, "barrier();\n");
5819 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5822 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5824 shader_addline(buffer, "memoryBarrierShared();\n");
5825 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5828 if (sync_flags)
5829 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5832 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5833 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5835 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5836 unsigned int idx = reg->idx[0].offset;
5838 if (reg->type == WINED3DSPR_RESOURCE)
5840 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5842 ERR("Invalid resource index %u.\n", idx);
5843 return NULL;
5845 return &reg_maps->resource_info[idx];
5848 if (reg->type == WINED3DSPR_UAV)
5850 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5852 ERR("Invalid UAV index %u.\n", idx);
5853 return NULL;
5855 return &reg_maps->uav_resource_info[idx];
5858 FIXME("Unhandled register type %#x.\n", reg->type);
5859 return NULL;
5862 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5864 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5865 const struct wined3d_shader_resource_info *resource_info;
5866 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5867 unsigned int resource_idx;
5868 char dst_swizzle[6];
5869 DWORD write_mask;
5871 write_mask = shader_glsl_append_dst(buffer, ins);
5872 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5874 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5875 return;
5876 resource_idx = ins->src[0].reg.idx[0].offset;
5878 shader_addline(buffer, "ivec2(");
5879 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5881 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5882 resource_idx, WINED3D_SAMPLER_DEFAULT);
5883 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5885 else
5887 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5889 if (resource_info->stride)
5890 shader_addline(buffer, " / %u", resource_info->stride);
5891 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5892 shader_addline(buffer, " * 4");
5893 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5896 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5898 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5899 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5902 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5904 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5907 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5909 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5910 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5911 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5912 enum wined3d_shader_resource_type resource_type;
5913 enum wined3d_shader_register_type reg_type;
5914 unsigned int resource_idx, bind_idx, i;
5915 enum wined3d_data_type dst_data_type;
5916 struct glsl_src_param lod_param;
5917 BOOL supports_mipmaps;
5918 char dst_swizzle[6];
5919 DWORD write_mask;
5921 dst_data_type = ins->dst[0].reg.data_type;
5922 if (ins->flags == WINED3DSI_RESINFO_UINT)
5923 dst_data_type = WINED3D_DATA_UINT;
5924 else if (ins->flags)
5925 FIXME("Unhandled flags %#x.\n", ins->flags);
5927 reg_type = ins->src[1].reg.type;
5928 resource_idx = ins->src[1].reg.idx[0].offset;
5929 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5930 if (reg_type == WINED3DSPR_RESOURCE)
5932 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5933 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5934 resource_idx, WINED3D_SAMPLER_DEFAULT);
5936 else
5938 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5939 bind_idx = resource_idx;
5942 if (resource_type >= ARRAY_SIZE(resource_type_info))
5944 ERR("Unexpected resource type %#x.\n", resource_type);
5945 return;
5948 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], dst_data_type);
5949 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5951 if (dst_data_type == WINED3D_DATA_UINT)
5952 shader_addline(buffer, "uvec4(");
5953 else
5954 shader_addline(buffer, "vec4(");
5956 if (reg_type == WINED3DSPR_RESOURCE)
5958 shader_addline(buffer, "textureSize(%s_sampler%u",
5959 shader_glsl_get_prefix(version->type), bind_idx);
5961 else
5963 shader_addline(buffer, "imageSize(%s_image%u",
5964 shader_glsl_get_prefix(version->type), bind_idx);
5967 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5968 if (supports_mipmaps)
5969 shader_addline(buffer, ", %s", lod_param.param_str);
5970 shader_addline(buffer, "), ");
5972 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5973 shader_addline(buffer, "0, ");
5975 if (supports_mipmaps)
5977 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5979 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5980 shader_glsl_get_prefix(version->type), bind_idx);
5982 else
5984 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5985 shader_addline(buffer, "1");
5988 else
5990 shader_addline(buffer, "1");
5993 shader_addline(buffer, ")%s);\n", dst_swizzle);
5996 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5998 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5999 struct glsl_src_param coord_param, lod_param, sample_param;
6000 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6001 struct glsl_sample_function sample_function;
6002 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6003 BOOL has_lod_param;
6005 if (wined3d_shader_instruction_has_texel_offset(ins))
6006 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6008 resource_idx = ins->src[1].reg.idx[0].offset;
6009 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6011 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6013 ERR("Invalid resource index %u.\n", resource_idx);
6014 return;
6016 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6018 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6019 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6020 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6021 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6022 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6024 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6025 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6026 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6028 else
6030 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6031 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6032 "%s", coord_param.param_str);
6034 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6037 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6039 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6040 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6041 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6042 struct glsl_sample_function sample_function;
6043 DWORD flags = 0;
6045 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6046 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6047 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6048 flags |= WINED3D_GLSL_SAMPLE_LOD;
6049 if (wined3d_shader_instruction_has_texel_offset(ins))
6050 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6052 resource_idx = ins->src[1].reg.idx[0].offset;
6053 sampler_idx = ins->src[2].reg.idx[0].offset;
6055 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6056 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6058 switch (ins->handler_idx)
6060 case WINED3DSIH_SAMPLE:
6061 break;
6062 case WINED3DSIH_SAMPLE_B:
6063 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6064 lod_param_str = lod_param.param_str;
6065 break;
6066 case WINED3DSIH_SAMPLE_GRAD:
6067 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6068 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6069 dx_param_str = dx_param.param_str;
6070 dy_param_str = dy_param.param_str;
6071 break;
6072 case WINED3DSIH_SAMPLE_LOD:
6073 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6074 lod_param_str = lod_param.param_str;
6075 break;
6076 default:
6077 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6078 break;
6081 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6082 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6083 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6084 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6087 /* GLSL doesn't provide a function to sample from level zero with depth
6088 * comparison for array textures and cube textures. We use textureGrad*()
6089 * to implement sample_c_lz.
6091 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6092 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6093 unsigned int coord_size, const char *coord_param, const char *ref_param)
6095 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6096 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6097 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6098 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6099 char dst_swizzle[6];
6101 WARN("Emitting textureGrad() for sample_c_lz.\n");
6103 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6104 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6105 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6106 sample_function->offset_size ? "Offset" : "",
6107 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6108 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6109 if (sample_function->offset_size)
6111 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6112 shader_addline(buffer, ", ");
6113 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6115 shader_addline(buffer, "))%s);\n", dst_swizzle);
6118 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6120 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6121 const struct wined3d_shader_resource_info *resource_info;
6122 struct glsl_src_param coord_param, compare_param;
6123 struct glsl_sample_function sample_function;
6124 const char *lod_param = NULL;
6125 unsigned int coord_size;
6126 DWORD flags = 0;
6128 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6130 lod_param = "0";
6131 flags |= WINED3D_GLSL_SAMPLE_LOD;
6134 if (wined3d_shader_instruction_has_texel_offset(ins))
6135 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6137 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6138 return;
6139 resource_idx = ins->src[1].reg.idx[0].offset;
6140 sampler_idx = ins->src[2].reg.idx[0].offset;
6142 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6143 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6144 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6145 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6146 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6147 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6148 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6149 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6151 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6152 coord_size, coord_param.param_str, compare_param.param_str);
6154 else
6156 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6157 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6158 coord_size, coord_param.param_str, compare_param.param_str);
6160 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6163 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6165 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6166 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6167 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6168 struct glsl_src_param coord_param, compare_param, offset_param;
6169 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6170 const struct wined3d_shader_resource_info *resource_info;
6171 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6172 unsigned int coord_size, offset_size;
6173 char dst_swizzle[6];
6174 BOOL has_offset;
6176 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6178 FIXME("OpenGL implementation does not support textureGather.\n");
6179 return;
6182 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6183 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6184 || wined3d_shader_instruction_has_texel_offset(ins);
6186 resource_param_idx =
6187 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6188 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6189 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6190 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6191 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6193 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6194 return;
6196 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6198 ERR("Unexpected resource type %#x.\n", resource_info->type);
6199 return;
6201 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6203 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6204 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6206 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6208 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6209 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6210 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6212 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6213 shader_addline(buffer, ", %s", compare_param.param_str);
6215 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6217 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6218 shader_addline(buffer, ", %s", offset_param.param_str);
6220 else if (has_offset)
6222 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6223 shader_addline(buffer, ", ");
6224 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6226 if (component_idx)
6227 shader_addline(buffer, ", %u", component_idx);
6229 shader_addline(buffer, ")%s);\n", dst_swizzle);
6232 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6234 /* FIXME: Make this work for more than just 2D textures */
6235 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6236 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6238 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6240 char dst_mask[6];
6242 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6243 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6244 ins->dst[0].reg.idx[0].offset, dst_mask);
6246 else
6248 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6249 DWORD reg = ins->src[0].reg.idx[0].offset;
6250 char dst_swizzle[6];
6252 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6254 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6256 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6257 struct glsl_src_param div_param;
6258 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6260 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6262 if (mask_size > 1)
6263 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6264 else
6265 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6267 else
6269 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6274 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6275 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6276 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6277 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6279 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6280 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6281 struct glsl_sample_function sample_function;
6282 struct glsl_src_param src0_param;
6283 UINT mask_size;
6285 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6287 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6288 * scalar, and projected sampling would require 4.
6290 * It is a dependent read - not valid with conditional NP2 textures
6292 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6293 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6295 switch(mask_size)
6297 case 1:
6298 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6299 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6300 break;
6302 case 2:
6303 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6304 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6305 break;
6307 case 3:
6308 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6309 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6310 break;
6312 default:
6313 FIXME("Unexpected mask size %u\n", mask_size);
6314 break;
6316 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6319 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6320 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6321 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6323 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6324 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6325 struct glsl_src_param src0_param;
6326 DWORD dst_mask;
6327 unsigned int mask_size;
6329 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6330 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6331 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6333 if (mask_size > 1) {
6334 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6335 } else {
6336 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6340 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6341 * Calculate the depth as dst.x / dst.y */
6342 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6344 struct glsl_dst_param dst_param;
6346 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6348 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6349 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6350 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6351 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6352 * >= 1.0 or < 0.0
6354 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6355 dst_param.reg_name, dst_param.reg_name);
6358 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6359 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6360 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6361 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6363 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6365 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6366 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6367 struct glsl_src_param src0_param;
6369 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6371 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6372 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6375 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6376 * Calculate the 1st of a 2-row matrix multiplication. */
6377 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6379 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6380 DWORD reg = ins->dst[0].reg.idx[0].offset;
6381 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6382 struct glsl_src_param src0_param;
6384 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6385 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6388 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6389 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6390 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6392 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6393 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6394 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6395 DWORD reg = ins->dst[0].reg.idx[0].offset;
6396 struct glsl_src_param src0_param;
6398 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6399 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6400 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6403 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6405 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6406 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6407 struct glsl_sample_function sample_function;
6408 DWORD reg = ins->dst[0].reg.idx[0].offset;
6409 struct glsl_src_param src0_param;
6411 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6412 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6414 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6416 /* Sample the texture using the calculated coordinates */
6417 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6418 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6421 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6422 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6423 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6425 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6426 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6427 struct glsl_sample_function sample_function;
6428 DWORD reg = ins->dst[0].reg.idx[0].offset;
6429 struct glsl_src_param src0_param;
6431 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6432 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6434 /* Dependent read, not valid with conditional NP2 */
6435 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6437 /* Sample the texture using the calculated coordinates */
6438 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6439 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6441 tex_mx->current_row = 0;
6444 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6445 * Perform the 3rd row of a 3x3 matrix multiply */
6446 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6448 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6449 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6450 DWORD reg = ins->dst[0].reg.idx[0].offset;
6451 struct glsl_src_param src0_param;
6452 char dst_mask[6];
6454 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6456 shader_glsl_append_dst(ins->ctx->buffer, ins);
6457 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6458 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6460 tex_mx->current_row = 0;
6463 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6464 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6465 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6467 struct glsl_src_param src0_param;
6468 struct glsl_src_param src1_param;
6469 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6470 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6471 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6472 struct glsl_sample_function sample_function;
6473 DWORD reg = ins->dst[0].reg.idx[0].offset;
6474 char coord_mask[6];
6476 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6477 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6479 /* Perform the last matrix multiply operation */
6480 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6481 /* Reflection calculation */
6482 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6484 /* Dependent read, not valid with conditional NP2 */
6485 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6486 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6488 /* Sample the texture */
6489 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6490 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6491 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6493 tex_mx->current_row = 0;
6496 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6497 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6498 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6500 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6501 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6502 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6503 struct glsl_sample_function sample_function;
6504 DWORD reg = ins->dst[0].reg.idx[0].offset;
6505 struct glsl_src_param src0_param;
6506 char coord_mask[6];
6508 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6510 /* Perform the last matrix multiply operation */
6511 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6513 /* Construct the eye-ray vector from w coordinates */
6514 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6515 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6516 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6518 /* Dependent read, not valid with conditional NP2 */
6519 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6520 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6522 /* Sample the texture using the calculated coordinates */
6523 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6524 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6525 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6527 tex_mx->current_row = 0;
6530 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6531 * Apply a fake bump map transform.
6532 * texbem is pshader <= 1.3 only, this saves a few version checks
6534 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6536 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6537 struct glsl_sample_function sample_function;
6538 struct glsl_src_param coord_param;
6539 DWORD sampler_idx;
6540 DWORD mask;
6541 DWORD flags;
6542 char coord_mask[6];
6544 sampler_idx = ins->dst[0].reg.idx[0].offset;
6545 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6546 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6548 /* Dependent read, not valid with conditional NP2 */
6549 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6550 mask = sample_function.coord_mask;
6552 shader_glsl_write_mask_to_str(mask, coord_mask);
6554 /* With projected textures, texbem only divides the static texture coord,
6555 * not the displacement, so we can't let GL handle this. */
6556 if (flags & WINED3D_PSARGS_PROJECTED)
6558 DWORD div_mask=0;
6559 char coord_div_mask[3];
6560 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6562 case WINED3D_TTFF_COUNT1:
6563 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6564 break;
6565 case WINED3D_TTFF_COUNT2:
6566 div_mask = WINED3DSP_WRITEMASK_1;
6567 break;
6568 case WINED3D_TTFF_COUNT3:
6569 div_mask = WINED3DSP_WRITEMASK_2;
6570 break;
6571 case WINED3D_TTFF_COUNT4:
6572 case WINED3D_TTFF_DISABLE:
6573 div_mask = WINED3DSP_WRITEMASK_3;
6574 break;
6576 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6577 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6580 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6582 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6583 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6584 coord_param.param_str, coord_mask);
6586 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6588 struct glsl_src_param luminance_param;
6589 struct glsl_dst_param dst_param;
6591 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6592 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6594 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6595 dst_param.reg_name, dst_param.mask_str,
6596 luminance_param.param_str, sampler_idx, sampler_idx);
6598 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6601 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6603 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6604 struct glsl_src_param src0_param, src1_param;
6606 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6607 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6609 shader_glsl_append_dst(ins->ctx->buffer, ins);
6610 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6611 src0_param.param_str, sampler_idx, src1_param.param_str);
6614 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6615 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6616 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6618 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6619 struct glsl_sample_function sample_function;
6620 struct glsl_src_param src0_param;
6622 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6624 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6625 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6626 "%s.wx", src0_param.reg_name);
6627 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6630 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6631 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6632 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6634 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6635 struct glsl_sample_function sample_function;
6636 struct glsl_src_param src0_param;
6638 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6640 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6641 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6642 "%s.yz", src0_param.reg_name);
6643 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6646 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6647 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6648 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6650 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6651 struct glsl_sample_function sample_function;
6652 struct glsl_src_param src0_param;
6654 /* Dependent read, not valid with conditional NP2 */
6655 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6656 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6658 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6659 "%s", src0_param.param_str);
6660 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6663 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6664 * If any of the first 3 components are < 0, discard this pixel */
6665 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6667 if (ins->ctx->reg_maps->shader_version.major >= 4)
6669 shader_glsl_generate_conditional_op(ins, "discard;");
6671 else
6673 struct glsl_dst_param dst_param;
6675 /* The argument is a destination parameter, and no writemasks are allowed */
6676 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6678 /* 2.0 shaders compare all 4 components in texkill. */
6679 if (ins->ctx->reg_maps->shader_version.major >= 2)
6680 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6681 /* 1.x shaders only compare the first 3 components, probably due to
6682 * the nature of the texkill instruction as a tex* instruction, and
6683 * phase, which kills all .w components. Even if all 4 components are
6684 * defined, only the first 3 are used. */
6685 else
6686 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6690 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6691 * dst = dot2(src0, src1) + src2 */
6692 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6694 struct glsl_src_param src0_param;
6695 struct glsl_src_param src1_param;
6696 struct glsl_src_param src2_param;
6697 DWORD write_mask;
6698 unsigned int mask_size;
6700 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6701 mask_size = shader_glsl_get_write_mask_size(write_mask);
6703 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6704 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6705 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6707 if (mask_size > 1) {
6708 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6709 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6710 } else {
6711 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6712 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6716 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6717 const struct wined3d_shader_signature *input_signature,
6718 const struct wined3d_shader_reg_maps *reg_maps,
6719 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6721 unsigned int i;
6723 for (i = 0; i < input_signature->element_count; ++i)
6725 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6726 const char *semantic_name;
6727 UINT semantic_idx;
6728 char reg_mask[6];
6730 /* Unused */
6731 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6732 continue;
6734 semantic_name = input->semantic_name;
6735 semantic_idx = input->semantic_idx;
6736 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6738 if (args->vp_mode == vertexshader)
6740 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6742 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6743 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6745 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6747 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6749 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6751 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6752 input->register_idx, reg_mask);
6754 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6756 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6757 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6758 input->register_idx, reg_mask);
6759 else
6760 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6762 else
6764 if (input->sysval_semantic)
6765 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6766 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6767 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6768 shader_glsl_shader_input_name(gl_info),
6769 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6772 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6774 if (args->pointsprite)
6775 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6776 shader->u.ps.input_reg_map[input->register_idx]);
6777 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6778 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6779 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6780 needs_legacy_glsl_syntax(gl_info)
6781 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6782 else
6783 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6784 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6786 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6788 if (!semantic_idx)
6789 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6790 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6791 else if (semantic_idx == 1)
6792 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6793 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6794 else
6795 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6796 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6798 else
6800 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6801 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6806 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6808 struct glsl_program_key key;
6810 key.vs_id = entry->vs.id;
6811 key.hs_id = entry->hs.id;
6812 key.ds_id = entry->ds.id;
6813 key.gs_id = entry->gs.id;
6814 key.ps_id = entry->ps.id;
6815 key.cs_id = entry->cs.id;
6817 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6819 ERR("Failed to insert program entry.\n");
6823 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6824 const struct glsl_program_key *key)
6826 struct wine_rb_entry *entry;
6828 entry = wine_rb_get(&priv->program_lookup, key);
6829 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6832 /* Context activation is done by the caller. */
6833 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6834 struct glsl_shader_prog_link *entry)
6836 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6838 GL_EXTCALL(glDeleteProgram(entry->id));
6839 if (entry->vs.id)
6840 list_remove(&entry->vs.shader_entry);
6841 if (entry->hs.id)
6842 list_remove(&entry->hs.shader_entry);
6843 if (entry->ds.id)
6844 list_remove(&entry->ds.shader_entry);
6845 if (entry->gs.id)
6846 list_remove(&entry->gs.shader_entry);
6847 if (entry->ps.id)
6848 list_remove(&entry->ps.shader_entry);
6849 if (entry->cs.id)
6850 list_remove(&entry->cs.shader_entry);
6851 heap_free(entry);
6854 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6855 const struct wined3d_gl_info *gl_info, const DWORD *map,
6856 const struct wined3d_shader_signature *input_signature,
6857 const struct wined3d_shader_reg_maps *reg_maps_in,
6858 const struct wined3d_shader_signature *output_signature,
6859 const struct wined3d_shader_reg_maps *reg_maps_out)
6861 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6862 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6863 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6864 unsigned int in_count = vec4_varyings(3, gl_info);
6865 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6866 DWORD in_idx, *set = NULL;
6867 unsigned int i, j;
6868 char reg_mask[6];
6870 set = heap_calloc(max_varyings, sizeof(*set));
6872 for (i = 0; i < input_signature->element_count; ++i)
6874 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6876 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6877 continue;
6879 in_idx = map[input->register_idx];
6880 /* Declared, but not read register */
6881 if (in_idx == ~0u)
6882 continue;
6883 if (in_idx >= max_varyings)
6885 FIXME("More input varyings declared than supported, expect issues.\n");
6886 continue;
6889 if (in_idx == in_count)
6890 string_buffer_sprintf(destination, "gl_FrontColor");
6891 else if (in_idx == in_count + 1)
6892 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6893 else
6894 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6896 if (!set[in_idx])
6897 set[in_idx] = ~0u;
6899 for (j = 0; j < output_signature->element_count; ++j)
6901 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6902 DWORD mask;
6904 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6905 || input->semantic_idx != output->semantic_idx
6906 || strcmp(input->semantic_name, output->semantic_name)
6907 || !(mask = input->mask & output->mask))
6908 continue;
6910 if (set[in_idx] == ~0u)
6911 set[in_idx] = 0;
6912 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6913 shader_glsl_write_mask_to_str(mask, reg_mask);
6915 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6916 destination->buffer, reg_mask, output->register_idx, reg_mask);
6920 for (i = 0; i < max_varyings; ++i)
6922 unsigned int size;
6924 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6925 continue;
6927 if (set[i] == ~0u)
6928 set[i] = 0;
6930 size = 0;
6931 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6932 reg_mask[size++] = 'x';
6933 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6934 reg_mask[size++] = 'y';
6935 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6936 reg_mask[size++] = 'z';
6937 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6938 reg_mask[size++] = 'w';
6939 reg_mask[size] = '\0';
6941 if (i == in_count)
6942 string_buffer_sprintf(destination, "gl_FrontColor");
6943 else if (i == in_count + 1)
6944 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6945 else
6946 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6948 if (size == 1)
6949 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6950 else
6951 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6954 heap_free(set);
6955 string_buffer_release(&priv->string_buffers, destination);
6958 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6959 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6960 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6961 BOOL rasterizer_setup)
6963 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6964 char reg_mask[6];
6965 unsigned int i;
6967 for (i = 0; i < output_signature->element_count; ++i)
6969 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6971 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6972 continue;
6974 if (output->stream_idx)
6975 continue;
6977 if (output->register_idx >= input_count)
6978 continue;
6980 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6982 shader_addline(buffer,
6983 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
6984 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
6988 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
6989 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
6991 unsigned int i, clip_or_cull_index;
6992 const char *name;
6993 char reg_mask[6];
6995 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
6996 /* Assign consecutive indices starting from 0. */
6997 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
6998 for (i = 0; i < 4; ++i)
7000 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7001 continue;
7003 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7004 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7005 name, clip_or_cull_index, element->register_idx, reg_mask);
7006 ++clip_or_cull_index;
7010 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7011 const struct wined3d_gl_info *gl_info, const DWORD *map,
7012 const struct wined3d_shader_signature *input_signature,
7013 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7014 const struct wined3d_shader_signature *output_signature,
7015 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7017 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7018 const char *semantic_name;
7019 unsigned int semantic_idx;
7020 char reg_mask[6];
7021 unsigned int i;
7023 /* First, sort out position and point size system values. */
7024 for (i = 0; i < output_signature->element_count; ++i)
7026 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7028 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7029 continue;
7031 if (output->stream_idx)
7032 continue;
7034 semantic_name = output->semantic_name;
7035 semantic_idx = output->semantic_idx;
7036 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7038 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7040 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7041 reg_mask, output->register_idx, reg_mask);
7043 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7045 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7046 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7048 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7050 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7051 output->register_idx, reg_mask);
7053 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7055 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7057 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7059 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7061 else if (output->sysval_semantic)
7063 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7067 /* Then, setup the pixel shader input. */
7068 if (reg_maps_out->shader_version.major < 4)
7069 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7070 output_signature, reg_maps_out);
7071 else
7072 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7075 /* Context activation is done by the caller. */
7076 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7077 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7078 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7080 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7081 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7082 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7083 const char *semantic_name;
7084 UINT semantic_idx;
7085 char reg_mask[6];
7086 unsigned int i;
7087 GLuint ret;
7089 string_buffer_clear(buffer);
7091 shader_glsl_add_version_declaration(buffer, gl_info);
7093 if (per_vertex_point_size)
7095 shader_addline(buffer, "uniform struct\n{\n");
7096 shader_addline(buffer, " float size_min;\n");
7097 shader_addline(buffer, " float size_max;\n");
7098 shader_addline(buffer, "} ffp_point;\n");
7101 if (ps_major < 3)
7103 DWORD colors_written_mask[2] = {0};
7104 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7106 if (!legacy_syntax)
7108 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7109 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7110 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7111 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7114 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7116 for (i = 0; i < vs->output_signature.element_count; ++i)
7118 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7119 DWORD write_mask;
7121 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7122 continue;
7124 semantic_name = output->semantic_name;
7125 semantic_idx = output->semantic_idx;
7126 write_mask = output->mask;
7127 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7129 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7131 if (legacy_syntax)
7132 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7133 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7134 else
7135 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7136 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7138 colors_written_mask[semantic_idx] = write_mask;
7140 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7142 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7143 reg_mask, output->register_idx, reg_mask);
7145 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7147 if (semantic_idx < MAX_TEXTURES)
7149 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7150 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7151 semantic_idx, reg_mask, output->register_idx, reg_mask);
7152 texcoords_written_mask[semantic_idx] = write_mask;
7155 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7157 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7158 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7160 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7162 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7163 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7164 output->register_idx, reg_mask[1]);
7168 for (i = 0; i < 2; ++i)
7170 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7172 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7173 if (!i)
7174 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7175 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7176 reg_mask, reg_mask);
7177 else
7178 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7179 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7180 reg_mask, reg_mask);
7183 for (i = 0; i < MAX_TEXTURES; ++i)
7185 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7186 continue;
7188 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7190 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7191 && !texcoords_written_mask[i])
7192 continue;
7194 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7195 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7196 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7200 else
7202 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7204 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7205 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7206 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7207 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7210 shader_addline(buffer, "}\n");
7212 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7213 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7214 shader_glsl_compile(gl_info, ret, buffer->buffer);
7216 return ret;
7219 static void shader_glsl_generate_stream_output_setup(struct shader_glsl_priv *priv,
7220 const struct wined3d_shader *shader, const struct wined3d_stream_output_desc *so_desc)
7222 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7223 unsigned int i;
7225 shader_addline(buffer, "out shader_in_out\n{\n");
7226 for (i = 0; i < so_desc->element_count; ++i)
7228 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7230 if (e->stream_idx)
7232 FIXME("Unhandled stream %u.\n", e->stream_idx);
7233 continue;
7235 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7236 continue;
7238 if (e->component_idx || e->component_count != 4)
7240 if (e->component_count == 1)
7241 shader_addline(buffer, "float");
7242 else
7243 shader_addline(buffer, "vec%u", e->component_count);
7244 shader_addline(buffer, " reg%u_%u_%u;\n",
7245 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
7247 else
7249 shader_addline(buffer, "vec4 reg%u;\n", e->register_idx);
7252 shader_addline(buffer, "} shader_out;\n");
7254 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7255 shader->limits->packed_output);
7256 for (i = 0; i < so_desc->element_count; ++i)
7258 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7260 if (e->stream_idx)
7262 FIXME("Unhandled stream %u.\n", e->stream_idx);
7263 continue;
7265 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7266 continue;
7268 if (e->component_idx || e->component_count != 4)
7270 DWORD write_mask;
7271 char str_mask[6];
7273 write_mask = ((1u << e->component_count) - 1) << e->component_idx;
7274 shader_glsl_write_mask_to_str(write_mask, str_mask);
7275 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7276 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1,
7277 e->register_idx, str_mask);
7279 else
7281 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n",
7282 e->register_idx, e->register_idx);
7285 shader_addline(buffer, "}\n");
7288 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7289 const struct wined3d_shader *shader, unsigned int input_count,
7290 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7292 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7293 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7295 if (rasterizer_setup)
7296 input_count = min(vec4_varyings(4, gl_info), input_count);
7298 if (input_count)
7299 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7301 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7302 prefix, shader->limits->packed_output);
7304 if (rasterizer_setup)
7305 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7306 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7307 else
7308 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7309 &shader->reg_maps, "shader_out", rasterizer_setup);
7311 shader_addline(buffer, "}\n");
7314 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7315 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7316 const char *reg_mask)
7318 if (!constant->sysval_semantic)
7320 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7321 return;
7324 switch (constant->sysval_semantic)
7326 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7327 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7328 case WINED3D_SV_TESS_FACTOR_LINEDET:
7329 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7330 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7331 break;
7332 case WINED3D_SV_TESS_FACTOR_QUADINT:
7333 case WINED3D_SV_TESS_FACTOR_TRIINT:
7334 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7335 break;
7336 default:
7337 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7338 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7342 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7343 const struct wined3d_shader_signature *signature, BOOL input_setup)
7345 unsigned int i, register_count, user_constant_index, user_constant_count;
7347 register_count = user_constant_count = 0;
7348 for (i = 0; i < signature->element_count; ++i)
7350 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7351 register_count = max(constant->register_idx + 1, register_count);
7352 if (!constant->sysval_semantic)
7353 ++user_constant_count;
7356 if (user_constant_count)
7357 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7358 input_setup ? "in" : "out", user_constant_count);
7359 if (input_setup)
7360 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7362 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7363 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7365 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7366 char reg_mask[6];
7368 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7370 if (input_setup)
7371 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7372 else
7373 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7375 shader_addline(buffer, " = ");
7377 if (input_setup)
7378 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7379 else
7380 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7382 shader_addline(buffer, ";\n");
7384 shader_addline(buffer, "}\n");
7387 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7388 const struct wined3d_gl_info *gl_info)
7390 const char *output = get_fragment_output(gl_info);
7392 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7393 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7394 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7395 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7396 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7397 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7400 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7401 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7403 const char *output = get_fragment_output(gl_info);
7405 switch (mode)
7407 case WINED3D_FFP_PS_FOG_OFF:
7408 return;
7410 case WINED3D_FFP_PS_FOG_LINEAR:
7411 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7412 break;
7414 case WINED3D_FFP_PS_FOG_EXP:
7415 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7416 break;
7418 case WINED3D_FFP_PS_FOG_EXP2:
7419 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7420 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7421 break;
7423 default:
7424 ERR("Invalid fog mode %#x.\n", mode);
7425 return;
7428 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7429 output, output);
7432 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7433 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7435 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7436 * flipping all the operators here, just negate the comparison below. */
7437 static const char * const comparison_operator[] =
7439 "", /* WINED3D_CMP_NEVER */
7440 "<", /* WINED3D_CMP_LESS */
7441 "==", /* WINED3D_CMP_EQUAL */
7442 "<=", /* WINED3D_CMP_LESSEQUAL */
7443 ">", /* WINED3D_CMP_GREATER */
7444 "!=", /* WINED3D_CMP_NOTEQUAL */
7445 ">=", /* WINED3D_CMP_GREATEREQUAL */
7446 "" /* WINED3D_CMP_ALWAYS */
7449 if (alpha_func == WINED3D_CMP_ALWAYS)
7450 return;
7452 if (alpha_func != WINED3D_CMP_NEVER)
7453 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7454 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7455 shader_addline(buffer, " discard;\n");
7458 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7459 const struct wined3d_gl_info *gl_info)
7461 if (gl_info->supported[ARB_CULL_DISTANCE])
7462 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7463 if (gl_info->supported[ARB_GPU_SHADER5])
7464 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7465 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7466 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7467 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7468 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7469 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7470 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7471 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7472 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7473 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7474 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7475 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7476 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7477 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7478 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7479 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7480 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7481 if (gl_info->supported[ARB_TEXTURE_GATHER])
7482 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7483 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7484 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7485 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7486 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7487 if (gl_info->supported[EXT_GPU_SHADER4])
7488 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7489 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7490 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7493 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7494 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7495 const struct ps_compile_args *args)
7497 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7499 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7500 if (reg_maps->shader_version.major < 2)
7501 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7503 if (args->srgb_correction)
7504 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7506 /* SM < 3 does not replace the fog stage. */
7507 if (reg_maps->shader_version.major < 3)
7508 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7510 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7513 /* Context activation is done by the caller. */
7514 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7515 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7516 const struct wined3d_shader *shader,
7517 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7519 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7520 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7521 const char *prefix = shader_glsl_get_prefix(version->type);
7522 const struct wined3d_gl_info *gl_info = context->gl_info;
7523 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7524 unsigned int i, extra_constants_needed = 0;
7525 struct shader_glsl_ctx_priv priv_ctx;
7526 GLuint shader_id;
7527 DWORD map;
7529 memset(&priv_ctx, 0, sizeof(priv_ctx));
7530 priv_ctx.cur_ps_args = args;
7531 priv_ctx.cur_np2fixup_info = np2fixup_info;
7532 priv_ctx.string_buffers = string_buffers;
7534 shader_glsl_add_version_declaration(buffer, gl_info);
7536 shader_glsl_enable_extensions(buffer, gl_info);
7537 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7538 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7539 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7540 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7541 if (shader_glsl_use_explicit_attrib_location(gl_info))
7542 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7543 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7544 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7545 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7546 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7547 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7548 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7549 /* The spec says that it doesn't have to be explicitly enabled, but the
7550 * nvidia drivers write a warning if we don't do so. */
7551 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7552 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7554 /* Base Declarations */
7555 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7557 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7559 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7560 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7561 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7562 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7565 /* Declare uniforms for NP2 texcoord fixup:
7566 * This is NOT done inside the loop that declares the texture samplers
7567 * since the NP2 fixup code is currently only used for the GeforceFX
7568 * series and when forcing the ARB_npot extension off. Modern cards just
7569 * skip the code anyway, so put it inside a separate loop. */
7570 if (args->np2_fixup)
7572 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7573 unsigned int cur = 0;
7575 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7576 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7577 * samplerNP2Fixup stores texture dimensions and is updated through
7578 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7580 for (i = 0; i < shader->limits->sampler; ++i)
7582 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7583 continue;
7585 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7587 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7588 continue;
7591 fixup->idx[i] = cur++;
7594 fixup->num_consts = (cur + 1) >> 1;
7595 fixup->active = args->np2_fixup;
7596 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7599 if (version->major < 3 || args->vp_mode != vertexshader)
7601 shader_addline(buffer, "uniform struct\n{\n");
7602 shader_addline(buffer, " vec4 color;\n");
7603 shader_addline(buffer, " float density;\n");
7604 shader_addline(buffer, " float end;\n");
7605 shader_addline(buffer, " float scale;\n");
7606 shader_addline(buffer, "} ffp_fog;\n");
7608 if (needs_legacy_glsl_syntax(gl_info))
7610 if (glsl_is_color_reg_read(shader, 0))
7611 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7612 if (glsl_is_color_reg_read(shader, 1))
7613 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7614 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7615 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7617 else
7619 if (glsl_is_color_reg_read(shader, 0))
7620 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7621 if (glsl_is_color_reg_read(shader, 1))
7622 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7623 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7624 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7625 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7629 if (version->major >= 3)
7631 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7633 if (args->vp_mode == vertexshader && reg_maps->input_registers)
7634 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7635 shader->u.ps.interpolation_mode, version->major >= 4);
7636 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7639 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7641 if (!(map & 1))
7642 continue;
7644 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7646 if (reg_maps->luminanceparams & (1u << i))
7648 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7649 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7650 extra_constants_needed++;
7653 extra_constants_needed++;
7656 if (args->srgb_correction)
7658 shader_addline(buffer, "const vec4 srgb_const0 = ");
7659 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7660 shader_addline(buffer, ";\n");
7661 shader_addline(buffer, "const vec4 srgb_const1 = ");
7662 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7663 shader_addline(buffer, ";\n");
7665 if (reg_maps->vpos || reg_maps->usesdsy)
7667 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7669 ++extra_constants_needed;
7670 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7672 if (reg_maps->vpos)
7674 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7676 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7677 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7678 args->render_offscreen ? "" : "origin_upper_left, ");
7679 else if (!args->render_offscreen)
7680 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7682 shader_addline(buffer, "vec4 vpos;\n");
7686 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7687 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7689 if (!needs_legacy_glsl_syntax(gl_info))
7691 if (shader_glsl_use_explicit_attrib_location(gl_info))
7692 shader_addline(buffer, "layout(location = 0) ");
7693 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
7696 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7697 FIXME("Insufficient uniforms to run this shader.\n");
7699 if (shader->u.ps.force_early_depth_stencil)
7700 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7702 shader_addline(buffer, "void main()\n{\n");
7704 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7705 * add approximately 0.5. This causes off-by-one problems as spotted by
7706 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7707 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7708 * causes precision troubles when we just subtract 0.5.
7710 * To deal with that, just floor() the position. This will eliminate the
7711 * fraction on all cards.
7713 * TODO: Test how this behaves with multisampling.
7715 * An advantage of floor is that it works even if the driver doesn't add
7716 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7717 * to return in gl_FragCoord, even though coordinates specify the pixel
7718 * centers instead of the pixel corners. This code will behave correctly
7719 * on drivers that returns integer values. */
7720 if (reg_maps->vpos)
7722 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7723 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7724 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7725 shader_addline(buffer,
7726 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7727 else
7728 shader_addline(buffer,
7729 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7732 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
7734 unsigned int i;
7735 WORD map = reg_maps->texcoord;
7737 if (legacy_syntax)
7739 if (glsl_is_color_reg_read(shader, 0))
7740 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7741 if (glsl_is_color_reg_read(shader, 1))
7742 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7745 for (i = 0; map; map >>= 1, ++i)
7747 if (map & 1)
7749 if (args->pointsprite)
7750 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7751 else if (args->texcoords_initialized & (1u << i))
7752 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7753 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7754 else
7755 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7756 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7760 if (legacy_syntax)
7761 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7764 /* Pack 3.0 inputs */
7765 if (reg_maps->shader_version.major >= 3)
7766 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7767 reg_maps->shader_version.major >= 4);
7769 /* Base Shader Body */
7770 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7771 return 0;
7773 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7774 if (reg_maps->shader_version.major < 4)
7775 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
7777 shader_addline(buffer, "}\n");
7779 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7780 TRACE("Compiling shader object %u.\n", shader_id);
7781 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7783 return shader_id;
7786 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7787 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7788 const struct vs_compile_args *args)
7790 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7791 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7792 unsigned int i;
7794 /* Unpack outputs. */
7795 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7797 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7798 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7799 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7800 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7802 if (reg_maps->shader_version.major < 3)
7804 if (args->fog_src == VS_FOG_Z)
7805 shader_addline(buffer, "%s = gl_Position.z;\n",
7806 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7807 else if (!reg_maps->fog)
7808 shader_addline(buffer, "%s = 0.0;\n",
7809 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7812 /* We always store the clipplanes without y inversion. */
7813 if (args->clip_enabled)
7815 if (legacy_syntax)
7816 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7817 else
7818 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7819 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7822 if (args->point_size && !args->per_vertex_point_size)
7823 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7825 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7826 shader_glsl_fixup_position(buffer);
7829 /* Context activation is done by the caller. */
7830 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
7831 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7833 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7834 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7835 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7836 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7837 const struct wined3d_gl_info *gl_info = context->gl_info;
7838 struct shader_glsl_ctx_priv priv_ctx;
7839 GLuint shader_id;
7840 unsigned int i;
7842 memset(&priv_ctx, 0, sizeof(priv_ctx));
7843 priv_ctx.cur_vs_args = args;
7844 priv_ctx.string_buffers = string_buffers;
7846 shader_glsl_add_version_declaration(buffer, gl_info);
7848 shader_glsl_enable_extensions(buffer, gl_info);
7849 if (gl_info->supported[ARB_DRAW_INSTANCED])
7850 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7851 if (shader_glsl_use_explicit_attrib_location(gl_info))
7852 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7854 /* Base Declarations */
7855 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7857 for (i = 0; i < shader->input_signature.element_count; ++i)
7858 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7860 if (args->point_size && !args->per_vertex_point_size)
7862 shader_addline(buffer, "uniform struct\n{\n");
7863 shader_addline(buffer, " float size;\n");
7864 shader_addline(buffer, " float size_min;\n");
7865 shader_addline(buffer, " float size_max;\n");
7866 shader_addline(buffer, "} ffp_point;\n");
7869 if (!needs_legacy_glsl_syntax(gl_info))
7871 if (args->clip_enabled)
7872 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7874 if (version->major < 3)
7876 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7877 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7878 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7879 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7883 if (version->major < 4)
7884 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
7886 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7887 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7889 if (reg_maps->shader_version.major >= 4)
7890 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
7891 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
7893 shader_addline(buffer, "void main()\n{\n");
7895 if (reg_maps->input_rel_addressing)
7897 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
7898 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
7899 for (i = 0; i < shader->input_signature.element_count; ++i)
7901 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
7902 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
7906 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7907 return 0;
7909 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7910 if (reg_maps->shader_version.major < 4)
7911 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7913 shader_addline(buffer, "}\n");
7915 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7916 TRACE("Compiling shader object %u.\n", shader_id);
7917 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7919 return shader_id;
7922 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
7923 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
7925 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7926 char reg_mask[6];
7927 unsigned int i;
7929 for (i = 0; i < output_signature->element_count; ++i)
7931 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7933 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7934 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
7935 output->register_idx, reg_mask, output->register_idx, reg_mask);
7939 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
7940 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
7941 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
7942 const char *phase_name, unsigned phase_idx)
7944 unsigned int i;
7945 HRESULT hr;
7947 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
7948 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
7949 for (i = 0; i < phase->temporary_count; ++i)
7950 shader_addline(buffer, "vec4 R%u;\n", i);
7951 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
7952 shader_addline(buffer, "}\n");
7953 return hr;
7956 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
7957 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
7959 if (phase->instance_count)
7961 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
7962 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
7963 shader_addline(buffer, "}\n");
7965 else
7967 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
7971 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
7972 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
7974 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7975 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7976 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7977 const struct wined3d_gl_info *gl_info = context->gl_info;
7978 const struct wined3d_hull_shader *hs = &shader->u.hs;
7979 const struct wined3d_shader_phase *phase;
7980 struct shader_glsl_ctx_priv priv_ctx;
7981 GLuint shader_id;
7982 unsigned int i;
7984 memset(&priv_ctx, 0, sizeof(priv_ctx));
7985 priv_ctx.string_buffers = string_buffers;
7987 shader_glsl_add_version_declaration(buffer, gl_info);
7989 shader_glsl_enable_extensions(buffer, gl_info);
7990 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7992 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7994 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
7996 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7997 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
7999 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8001 if (hs->phases.control_point)
8003 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8004 shader->limits->packed_output);
8005 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8006 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8007 shader_addline(buffer, "}\n");
8010 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8011 if ((phase = hs->phases.control_point))
8013 for (i = 0; i < phase->temporary_count; ++i)
8014 shader_addline(buffer, "vec4 R%u;\n", i);
8015 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8016 return 0;
8017 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8019 else
8021 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8023 shader_addline(buffer, "}\n");
8025 for (i = 0; i < hs->phases.fork_count; ++i)
8027 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8028 &hs->phases.fork[i], "fork", i)))
8029 return 0;
8032 for (i = 0; i < hs->phases.join_count; ++i)
8034 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8035 &hs->phases.join[i], "join", i)))
8036 return 0;
8039 shader_addline(buffer, "void main()\n{\n");
8040 shader_addline(buffer, "hs_control_point_phase();\n");
8041 if (reg_maps->vocp)
8042 shader_addline(buffer, "barrier();\n");
8043 for (i = 0; i < hs->phases.fork_count; ++i)
8044 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8045 for (i = 0; i < hs->phases.join_count; ++i)
8046 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8047 shader_addline(buffer, "setup_patch_constant_output();\n");
8048 shader_addline(buffer, "}\n");
8050 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8051 TRACE("Compiling shader object %u.\n", shader_id);
8052 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8054 return shader_id;
8057 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8058 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8059 const struct ds_compile_args *args)
8061 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8063 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8064 shader_glsl_fixup_position(buffer);
8067 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
8068 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8070 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8071 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8072 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8073 const struct wined3d_gl_info *gl_info = context->gl_info;
8074 struct shader_glsl_ctx_priv priv_ctx;
8075 GLuint shader_id;
8077 memset(&priv_ctx, 0, sizeof(priv_ctx));
8078 priv_ctx.cur_ds_args = args;
8079 priv_ctx.string_buffers = string_buffers;
8081 shader_glsl_add_version_declaration(buffer, gl_info);
8083 shader_glsl_enable_extensions(buffer, gl_info);
8084 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8086 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8088 shader_addline(buffer, "layout(");
8089 switch (shader->u.ds.tessellator_domain)
8091 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8092 shader_addline(buffer, "isolines");
8093 break;
8094 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8095 shader_addline(buffer, "quads");
8096 break;
8097 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8098 shader_addline(buffer, "triangles");
8099 break;
8101 switch (args->tessellator_output_primitive)
8103 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8104 if (args->render_offscreen)
8105 shader_addline(buffer, ", ccw");
8106 else
8107 shader_addline(buffer, ", cw");
8108 break;
8109 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8110 if (args->render_offscreen)
8111 shader_addline(buffer, ", cw");
8112 else
8113 shader_addline(buffer, ", ccw");
8114 break;
8115 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8116 shader_addline(buffer, ", point_mode");
8117 break;
8118 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8119 break;
8121 switch (args->tessellator_partitioning)
8123 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8124 shader_addline(buffer, ", fractional_odd_spacing");
8125 break;
8126 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8127 shader_addline(buffer, ", fractional_even_spacing");
8128 break;
8129 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8130 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8131 shader_addline(buffer, ", equal_spacing");
8132 break;
8134 shader_addline(buffer, ") in;\n");
8136 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8138 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8139 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8141 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8142 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8143 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8145 shader_addline(buffer, "void main()\n{\n");
8146 shader_addline(buffer, "setup_patch_constant_input();\n");
8148 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8149 return 0;
8151 shader_addline(buffer, "}\n");
8153 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8154 TRACE("Compiling shader object %u.\n", shader_id);
8155 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8157 return shader_id;
8160 /* Context activation is done by the caller. */
8161 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
8162 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8164 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8165 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8166 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8167 const struct wined3d_gl_info *gl_info = context->gl_info;
8168 struct shader_glsl_ctx_priv priv_ctx;
8169 GLuint shader_id;
8171 memset(&priv_ctx, 0, sizeof(priv_ctx));
8172 priv_ctx.string_buffers = string_buffers;
8174 shader_glsl_add_version_declaration(buffer, gl_info);
8176 shader_glsl_enable_extensions(buffer, gl_info);
8178 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8180 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
8181 if (shader->u.gs.instance_count > 1)
8182 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8183 shader_addline(buffer, ") in;\n");
8184 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8185 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
8186 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8188 if (!gl_info->supported[ARB_CLIP_CONTROL])
8189 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8191 if (is_rasterization_disabled(shader))
8193 shader_glsl_generate_stream_output_setup(priv, shader, &shader->u.gs.so_desc);
8195 else
8197 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8198 gl_info, TRUE, args->interpolation_mode);
8200 shader_addline(buffer, "void main()\n{\n");
8201 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8202 return 0;
8203 shader_addline(buffer, "}\n");
8205 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8206 TRACE("Compiling shader object %u.\n", shader_id);
8207 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8209 return shader_id;
8212 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8214 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8215 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8216 struct wined3d_string_buffer *buffer = ctx->buffer;
8217 const struct wined3d_shader *shader = ctx->shader;
8219 switch (shader->reg_maps.shader_version.type)
8221 case WINED3D_SHADER_TYPE_PIXEL:
8222 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args);
8223 break;
8224 case WINED3D_SHADER_TYPE_VERTEX:
8225 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8226 break;
8227 case WINED3D_SHADER_TYPE_DOMAIN:
8228 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8229 break;
8230 case WINED3D_SHADER_TYPE_GEOMETRY:
8231 case WINED3D_SHADER_TYPE_COMPUTE:
8232 break;
8233 default:
8234 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8235 break;
8239 /* Context activation is done by the caller. */
8240 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8241 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8242 const struct wined3d_shader *shader)
8244 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8245 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8246 const struct wined3d_gl_info *gl_info = context->gl_info;
8247 struct shader_glsl_ctx_priv priv_ctx;
8248 GLuint shader_id;
8249 unsigned int i;
8251 memset(&priv_ctx, 0, sizeof(priv_ctx));
8252 priv_ctx.string_buffers = string_buffers;
8254 shader_glsl_add_version_declaration(buffer, gl_info);
8256 shader_glsl_enable_extensions(buffer, gl_info);
8257 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8259 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8261 for (i = 0; i < reg_maps->tgsm_count; ++i)
8263 if (reg_maps->tgsm[i].size)
8264 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8267 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8268 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8270 shader_addline(buffer, "void main()\n{\n");
8271 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8272 shader_addline(buffer, "}\n");
8274 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8275 TRACE("Compiling shader object %u.\n", shader_id);
8276 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8278 return shader_id;
8281 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8282 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8283 struct wined3d_shader *shader,
8284 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8286 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8287 struct glsl_shader_private *shader_data;
8288 struct ps_np2fixup_info *np2fixup;
8289 UINT i;
8290 DWORD new_size;
8291 GLuint ret;
8293 if (!shader->backend_data)
8295 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8297 ERR("Failed to allocate backend data.\n");
8298 return 0;
8301 shader_data = shader->backend_data;
8302 gl_shaders = shader_data->gl_shaders.ps;
8304 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8305 * so a linear search is more performant than a hashmap or a binary search
8306 * (cache coherency etc)
8308 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8310 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8312 if (args->np2_fixup)
8313 *np2fixup_info = &gl_shaders[i].np2fixup;
8314 return gl_shaders[i].id;
8318 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8319 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8321 if (shader_data->num_gl_shaders)
8323 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8324 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8326 else
8328 new_array = heap_alloc(sizeof(*gl_shaders));
8329 new_size = 1;
8332 if(!new_array) {
8333 ERR("Out of memory\n");
8334 return 0;
8336 shader_data->gl_shaders.ps = new_array;
8337 shader_data->shader_array_size = new_size;
8338 gl_shaders = new_array;
8341 gl_shaders[shader_data->num_gl_shaders].args = *args;
8343 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8344 memset(np2fixup, 0, sizeof(*np2fixup));
8345 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8347 pixelshader_update_resource_types(shader, args->tex_types);
8349 string_buffer_clear(buffer);
8350 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8351 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8353 return ret;
8356 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8357 const DWORD use_map)
8359 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
8360 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
8361 if (stored->point_size != new->point_size)
8362 return FALSE;
8363 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8364 return FALSE;
8365 if (stored->flatshading != new->flatshading)
8366 return FALSE;
8367 if (stored->next_shader_type != new->next_shader_type)
8368 return FALSE;
8369 if (stored->next_shader_input_count != new->next_shader_input_count)
8370 return FALSE;
8371 return stored->fog_src == new->fog_src;
8374 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8375 struct wined3d_shader *shader, const struct vs_compile_args *args)
8377 UINT i;
8378 DWORD new_size;
8379 DWORD use_map = context->stream_info.use_map;
8380 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8381 struct glsl_shader_private *shader_data;
8382 GLuint ret;
8384 if (!shader->backend_data)
8386 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8388 ERR("Failed to allocate backend data.\n");
8389 return 0;
8392 shader_data = shader->backend_data;
8393 gl_shaders = shader_data->gl_shaders.vs;
8395 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8396 * so a linear search is more performant than a hashmap or a binary search
8397 * (cache coherency etc)
8399 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8401 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8402 return gl_shaders[i].id;
8405 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8407 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8409 if (shader_data->num_gl_shaders)
8411 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8412 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8414 else
8416 new_array = heap_alloc(sizeof(*gl_shaders));
8417 new_size = 1;
8420 if(!new_array) {
8421 ERR("Out of memory\n");
8422 return 0;
8424 shader_data->gl_shaders.vs = new_array;
8425 shader_data->shader_array_size = new_size;
8426 gl_shaders = new_array;
8429 gl_shaders[shader_data->num_gl_shaders].args = *args;
8431 string_buffer_clear(&priv->shader_buffer);
8432 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8433 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8435 return ret;
8438 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8439 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8441 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8442 struct glsl_shader_private *shader_data;
8443 unsigned int new_size;
8444 GLuint ret;
8446 if (!shader->backend_data)
8448 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8450 ERR("Failed to allocate backend data.\n");
8451 return 0;
8454 shader_data = shader->backend_data;
8455 gl_shaders = shader_data->gl_shaders.hs;
8457 if (shader_data->num_gl_shaders > 0)
8459 assert(shader_data->num_gl_shaders == 1);
8460 return gl_shaders[0].id;
8463 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8465 assert(!shader_data->gl_shaders.hs);
8466 new_size = 1;
8467 if (!(new_array = heap_alloc(sizeof(*new_array))))
8469 ERR("Failed to allocate GL shaders array.\n");
8470 return 0;
8472 shader_data->gl_shaders.hs = new_array;
8473 shader_data->shader_array_size = new_size;
8474 gl_shaders = new_array;
8476 string_buffer_clear(&priv->shader_buffer);
8477 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8478 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8480 return ret;
8483 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8484 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8486 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8487 struct glsl_shader_private *shader_data;
8488 unsigned int i, new_size;
8489 GLuint ret;
8491 if (!shader->backend_data)
8493 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8495 ERR("Failed to allocate backend data.\n");
8496 return 0;
8499 shader_data = shader->backend_data;
8500 gl_shaders = shader_data->gl_shaders.ds;
8502 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8504 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8505 return gl_shaders[i].id;
8508 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8510 if (shader_data->num_gl_shaders)
8512 new_size = shader_data->shader_array_size + 1;
8513 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8515 else
8517 new_array = heap_alloc(sizeof(*new_array));
8518 new_size = 1;
8521 if (!new_array)
8523 ERR("Failed to allocate GL shaders array.\n");
8524 return 0;
8526 shader_data->gl_shaders.ds = new_array;
8527 shader_data->shader_array_size = new_size;
8528 gl_shaders = new_array;
8530 string_buffer_clear(&priv->shader_buffer);
8531 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8532 gl_shaders[shader_data->num_gl_shaders].args = *args;
8533 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8535 return ret;
8538 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8539 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8541 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8542 struct glsl_shader_private *shader_data;
8543 unsigned int i, new_size;
8544 GLuint ret;
8546 if (!shader->backend_data)
8548 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8550 ERR("Failed to allocate backend data.\n");
8551 return 0;
8554 shader_data = shader->backend_data;
8555 gl_shaders = shader_data->gl_shaders.gs;
8557 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8559 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8560 return gl_shaders[i].id;
8563 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8565 if (shader_data->num_gl_shaders)
8567 new_size = shader_data->shader_array_size + 1;
8568 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8570 else
8572 new_array = heap_alloc(sizeof(*new_array));
8573 new_size = 1;
8576 if (!new_array)
8578 ERR("Failed to allocate GL shaders array.\n");
8579 return 0;
8581 shader_data->gl_shaders.gs = new_array;
8582 shader_data->shader_array_size = new_size;
8583 gl_shaders = new_array;
8585 string_buffer_clear(&priv->shader_buffer);
8586 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8587 gl_shaders[shader_data->num_gl_shaders].args = *args;
8588 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8590 return ret;
8593 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8595 switch (mcs)
8597 case WINED3D_MCS_MATERIAL:
8598 return material;
8599 case WINED3D_MCS_COLOR1:
8600 return "ffp_attrib_diffuse";
8601 case WINED3D_MCS_COLOR2:
8602 return "ffp_attrib_specular";
8603 default:
8604 ERR("Invalid material color source %#x.\n", mcs);
8605 return "<invalid>";
8609 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8610 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8612 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8613 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8614 if (settings->localviewer)
8615 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8616 else
8617 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8618 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8619 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8622 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8623 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8625 const char *diffuse, *specular, *emissive, *ambient;
8626 unsigned int i, idx;
8628 if (!settings->lighting)
8630 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8631 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8632 return;
8635 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8636 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8637 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8638 shader_addline(buffer, "vec3 dir, dst;\n");
8639 shader_addline(buffer, "float att, t;\n");
8641 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8642 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8643 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8644 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8646 idx = 0;
8647 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8649 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8650 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8651 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8652 shader_addline(buffer, "dst.x = 1.0;\n");
8653 if (legacy_lighting)
8655 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8656 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8657 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8659 else
8661 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8663 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8664 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8665 if (!legacy_lighting)
8666 shader_addline(buffer, "att = 1.0 / att;\n");
8667 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8668 if (!settings->normal)
8670 shader_addline(buffer, "}\n");
8671 continue;
8673 shader_addline(buffer, "dir = normalize(dir);\n");
8674 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8675 shader_addline(buffer, "}\n");
8678 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8680 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8681 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8682 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8683 shader_addline(buffer, "dst.x = 1.0;\n");
8684 if (legacy_lighting)
8686 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8687 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8688 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8690 else
8692 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8694 shader_addline(buffer, "dir = normalize(dir);\n");
8695 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8696 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8697 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8698 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8699 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8700 idx, idx, idx, idx);
8701 if (legacy_lighting)
8702 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8703 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8704 idx, idx, idx);
8705 else
8706 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8707 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8708 idx, idx, idx);
8709 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8710 if (!settings->normal)
8712 shader_addline(buffer, "}\n");
8713 continue;
8715 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8716 shader_addline(buffer, "}\n");
8719 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8721 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8722 if (!settings->normal)
8723 continue;
8724 shader_addline(buffer, "att = 1.0;\n");
8725 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8726 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8729 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8731 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8732 if (!settings->normal)
8733 continue;
8734 shader_addline(buffer, "att = 1.0;\n");
8735 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8736 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8739 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8740 ambient, diffuse, emissive);
8741 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8742 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8745 /* Context activation is done by the caller. */
8746 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8747 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8749 static const struct attrib_info
8751 const char type[6];
8752 const char name[24];
8754 attrib_info[] =
8756 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8757 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8758 /* TODO: Indexed vertex blending */
8759 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8760 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8761 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8762 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8763 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8765 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8766 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8767 BOOL output_legacy_fogcoord = legacy_syntax;
8768 BOOL legacy_lighting = priv->legacy_lighting;
8769 GLuint shader_obj;
8770 unsigned int i;
8772 string_buffer_clear(buffer);
8774 shader_glsl_add_version_declaration(buffer, gl_info);
8776 if (shader_glsl_use_explicit_attrib_location(gl_info))
8777 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8779 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8781 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8783 if (shader_glsl_use_explicit_attrib_location(gl_info))
8784 shader_addline(buffer, "layout(location = %u) ", i);
8785 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8787 shader_addline(buffer, "\n");
8789 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8790 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8791 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8792 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
8794 shader_addline(buffer, "uniform struct\n{\n");
8795 shader_addline(buffer, " vec4 emissive;\n");
8796 shader_addline(buffer, " vec4 ambient;\n");
8797 shader_addline(buffer, " vec4 diffuse;\n");
8798 shader_addline(buffer, " vec4 specular;\n");
8799 shader_addline(buffer, " float shininess;\n");
8800 shader_addline(buffer, "} ffp_material;\n");
8802 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8803 shader_addline(buffer, "uniform struct\n{\n");
8804 shader_addline(buffer, " vec4 diffuse;\n");
8805 shader_addline(buffer, " vec4 specular;\n");
8806 shader_addline(buffer, " vec4 ambient;\n");
8807 shader_addline(buffer, " vec4 position;\n");
8808 shader_addline(buffer, " vec3 direction;\n");
8809 shader_addline(buffer, " float range;\n");
8810 shader_addline(buffer, " float falloff;\n");
8811 shader_addline(buffer, " float c_att;\n");
8812 shader_addline(buffer, " float l_att;\n");
8813 shader_addline(buffer, " float q_att;\n");
8814 shader_addline(buffer, " float cos_htheta;\n");
8815 shader_addline(buffer, " float cos_hphi;\n");
8816 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
8818 if (settings->point_size)
8820 shader_addline(buffer, "uniform struct\n{\n");
8821 shader_addline(buffer, " float size;\n");
8822 shader_addline(buffer, " float size_min;\n");
8823 shader_addline(buffer, " float size_max;\n");
8824 shader_addline(buffer, " float c_att;\n");
8825 shader_addline(buffer, " float l_att;\n");
8826 shader_addline(buffer, " float q_att;\n");
8827 shader_addline(buffer, "} ffp_point;\n");
8830 if (legacy_syntax)
8832 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8833 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8834 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8835 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8837 else
8839 if (settings->clipping)
8840 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8842 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8843 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8844 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8845 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8848 shader_addline(buffer, "\nvoid main()\n{\n");
8849 shader_addline(buffer, "float m;\n");
8850 shader_addline(buffer, "vec3 r;\n");
8852 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
8854 if (attrib_info[i].name[0])
8855 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
8856 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
8858 for (i = 0; i < MAX_TEXTURES; ++i)
8860 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
8861 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
8862 && settings->texcoords & (1u << i))
8863 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
8866 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
8868 if (settings->transformed)
8870 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
8871 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8872 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
8874 else
8876 for (i = 0; i < settings->vertexblends; ++i)
8877 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
8879 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
8880 for (i = 0; i < settings->vertexblends + 1; ++i)
8881 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
8883 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8884 if (settings->clipping)
8886 if (legacy_syntax)
8887 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
8888 else
8889 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8890 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
8892 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
8895 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
8896 if (settings->normal)
8898 if (!settings->vertexblends)
8900 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
8902 else
8904 for (i = 0; i < settings->vertexblends + 1; ++i)
8905 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
8908 if (settings->normalize)
8909 shader_addline(buffer, "normal = normalize(normal);\n");
8912 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
8913 if (legacy_syntax)
8915 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
8916 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
8918 else
8920 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
8921 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
8924 for (i = 0; i < MAX_TEXTURES; ++i)
8926 BOOL output_legacy_texcoord = legacy_syntax;
8928 switch (settings->texgen[i] & 0xffff0000)
8930 case WINED3DTSS_TCI_PASSTHRU:
8931 if (settings->texcoords & (1u << i))
8932 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
8933 i, i, i);
8934 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
8935 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
8936 else
8937 output_legacy_texcoord = FALSE;
8938 break;
8940 case WINED3DTSS_TCI_CAMERASPACENORMAL:
8941 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
8942 break;
8944 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
8945 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
8946 break;
8948 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
8949 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8950 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
8951 break;
8953 case WINED3DTSS_TCI_SPHEREMAP:
8954 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
8955 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
8956 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8957 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
8958 break;
8960 default:
8961 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
8962 break;
8964 if (output_legacy_texcoord)
8965 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
8968 switch (settings->fog_mode)
8970 case WINED3D_FFP_VS_FOG_OFF:
8971 output_legacy_fogcoord = FALSE;
8972 break;
8974 case WINED3D_FFP_VS_FOG_FOGCOORD:
8975 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
8976 break;
8978 case WINED3D_FFP_VS_FOG_RANGE:
8979 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
8980 break;
8982 case WINED3D_FFP_VS_FOG_DEPTH:
8983 if (settings->ortho_fog)
8985 if (gl_info->supported[ARB_CLIP_CONTROL])
8986 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
8987 else
8988 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
8989 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
8991 else if (settings->transformed)
8993 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
8995 else
8997 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
8999 break;
9001 default:
9002 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9003 break;
9005 if (output_legacy_fogcoord)
9006 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9008 if (settings->point_size)
9010 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9011 " + ffp_point.l_att * length(ec_pos.xyz)"
9012 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9013 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9014 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9017 shader_addline(buffer, "}\n");
9019 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9020 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9022 return shader_obj;
9025 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9026 DWORD argnum, unsigned int stage, DWORD arg)
9028 const char *ret;
9030 if (arg == ARG_UNUSED)
9031 return "<unused arg>";
9033 switch (arg & WINED3DTA_SELECTMASK)
9035 case WINED3DTA_DIFFUSE:
9036 ret = "ffp_varying_diffuse";
9037 break;
9039 case WINED3DTA_CURRENT:
9040 ret = "ret";
9041 break;
9043 case WINED3DTA_TEXTURE:
9044 switch (stage)
9046 case 0: ret = "tex0"; break;
9047 case 1: ret = "tex1"; break;
9048 case 2: ret = "tex2"; break;
9049 case 3: ret = "tex3"; break;
9050 case 4: ret = "tex4"; break;
9051 case 5: ret = "tex5"; break;
9052 case 6: ret = "tex6"; break;
9053 case 7: ret = "tex7"; break;
9054 default:
9055 ret = "<invalid texture>";
9056 break;
9058 break;
9060 case WINED3DTA_TFACTOR:
9061 ret = "tex_factor";
9062 break;
9064 case WINED3DTA_SPECULAR:
9065 ret = "ffp_varying_specular";
9066 break;
9068 case WINED3DTA_TEMP:
9069 ret = "temp_reg";
9070 break;
9072 case WINED3DTA_CONSTANT:
9073 switch (stage)
9075 case 0: ret = "tss_const0"; break;
9076 case 1: ret = "tss_const1"; break;
9077 case 2: ret = "tss_const2"; break;
9078 case 3: ret = "tss_const3"; break;
9079 case 4: ret = "tss_const4"; break;
9080 case 5: ret = "tss_const5"; break;
9081 case 6: ret = "tss_const6"; break;
9082 case 7: ret = "tss_const7"; break;
9083 default:
9084 ret = "<invalid constant>";
9085 break;
9087 break;
9089 default:
9090 return "<unhandled arg>";
9093 if (arg & WINED3DTA_COMPLEMENT)
9095 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9096 if (argnum == 0)
9097 ret = "arg0";
9098 else if (argnum == 1)
9099 ret = "arg1";
9100 else if (argnum == 2)
9101 ret = "arg2";
9104 if (arg & WINED3DTA_ALPHAREPLICATE)
9106 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9107 if (argnum == 0)
9108 ret = "arg0";
9109 else if (argnum == 1)
9110 ret = "arg1";
9111 else if (argnum == 2)
9112 ret = "arg2";
9115 return ret;
9118 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9119 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9121 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9123 if (color && alpha)
9124 dstmask = "";
9125 else if (color)
9126 dstmask = ".xyz";
9127 else
9128 dstmask = ".w";
9130 if (dst == tempreg)
9131 dstreg = "temp_reg";
9132 else
9133 dstreg = "ret";
9135 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9136 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9137 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9139 switch (op)
9141 case WINED3D_TOP_DISABLE:
9142 break;
9144 case WINED3D_TOP_SELECT_ARG1:
9145 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9146 break;
9148 case WINED3D_TOP_SELECT_ARG2:
9149 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9150 break;
9152 case WINED3D_TOP_MODULATE:
9153 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9154 break;
9156 case WINED3D_TOP_MODULATE_4X:
9157 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9158 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9159 break;
9161 case WINED3D_TOP_MODULATE_2X:
9162 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9163 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9164 break;
9166 case WINED3D_TOP_ADD:
9167 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9168 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9169 break;
9171 case WINED3D_TOP_ADD_SIGNED:
9172 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9173 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9174 break;
9176 case WINED3D_TOP_ADD_SIGNED_2X:
9177 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9178 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9179 break;
9181 case WINED3D_TOP_SUBTRACT:
9182 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9183 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9184 break;
9186 case WINED3D_TOP_ADD_SMOOTH:
9187 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9188 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9189 break;
9191 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9192 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9193 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9194 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9195 break;
9197 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9198 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9199 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9200 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9201 break;
9203 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9204 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9205 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9206 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9207 break;
9209 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9210 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9211 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9212 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9213 break;
9215 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9216 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9217 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9218 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9219 break;
9221 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9222 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9223 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9224 break;
9226 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9227 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9228 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9229 break;
9231 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9232 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9233 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9234 break;
9235 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9236 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9237 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9238 break;
9240 case WINED3D_TOP_BUMPENVMAP:
9241 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9242 /* These are handled in the first pass, nothing to do. */
9243 break;
9245 case WINED3D_TOP_DOTPRODUCT3:
9246 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9247 dstreg, dstmask, arg1, arg2, dstmask);
9248 break;
9250 case WINED3D_TOP_MULTIPLY_ADD:
9251 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9252 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9253 break;
9255 case WINED3D_TOP_LERP:
9256 /* MSDN isn't quite right here. */
9257 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9258 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9259 break;
9261 default:
9262 FIXME("Unhandled operation %#x.\n", op);
9263 break;
9267 /* Context activation is done by the caller. */
9268 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9269 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9271 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9272 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9273 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9274 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9275 const struct wined3d_gl_info *gl_info = context->gl_info;
9276 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9277 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9278 UINT lowest_disabled_stage;
9279 GLuint shader_id;
9280 DWORD arg0, arg1, arg2;
9281 unsigned int stage;
9283 string_buffer_clear(buffer);
9285 /* Find out which textures are read */
9286 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9288 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9289 break;
9291 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9292 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9293 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9295 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9296 || (stage == 0 && settings->color_key_enabled))
9297 tex_map |= 1u << stage;
9298 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9299 tfactor_used = TRUE;
9300 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9301 tempreg_used = TRUE;
9302 if (settings->op[stage].dst == tempreg)
9303 tempreg_used = TRUE;
9304 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9305 tss_const_map |= 1u << stage;
9307 switch (settings->op[stage].cop)
9309 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9310 lum_map |= 1u << stage;
9311 /* fall through */
9312 case WINED3D_TOP_BUMPENVMAP:
9313 bump_map |= 1u << stage;
9314 /* fall through */
9315 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9316 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9317 tex_map |= 1u << stage;
9318 break;
9320 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9321 tfactor_used = TRUE;
9322 break;
9324 default:
9325 break;
9328 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9329 continue;
9331 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9332 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9333 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9335 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9336 tex_map |= 1u << stage;
9337 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9338 tfactor_used = TRUE;
9339 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9340 tempreg_used = TRUE;
9341 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9342 tss_const_map |= 1u << stage;
9344 lowest_disabled_stage = stage;
9346 shader_glsl_add_version_declaration(buffer, gl_info);
9348 if (shader_glsl_use_explicit_attrib_location(gl_info))
9349 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9350 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9351 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9352 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9353 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9355 if (!needs_legacy_glsl_syntax(gl_info))
9357 if (shader_glsl_use_explicit_attrib_location(gl_info))
9358 shader_addline(buffer, "layout(location = 0) ");
9359 shader_addline(buffer, "out vec4 ps_out[1];\n");
9362 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9363 shader_addline(buffer, "vec4 ret;\n");
9364 if (tempreg_used || settings->sRGB_write)
9365 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9366 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9368 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9370 const char *sampler_type;
9372 if (tss_const_map & (1u << stage))
9373 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9375 if (!(tex_map & (1u << stage)))
9376 continue;
9378 switch (settings->op[stage].tex_type)
9380 case WINED3D_GL_RES_TYPE_TEX_1D:
9381 sampler_type = "1D";
9382 break;
9383 case WINED3D_GL_RES_TYPE_TEX_2D:
9384 sampler_type = "2D";
9385 break;
9386 case WINED3D_GL_RES_TYPE_TEX_3D:
9387 sampler_type = "3D";
9388 break;
9389 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9390 sampler_type = "Cube";
9391 break;
9392 case WINED3D_GL_RES_TYPE_TEX_RECT:
9393 sampler_type = "2DRect";
9394 break;
9395 default:
9396 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9397 sampler_type = NULL;
9398 break;
9400 if (sampler_type)
9402 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9403 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9404 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9407 shader_addline(buffer, "vec4 tex%u;\n", stage);
9409 if (!(bump_map & (1u << stage)))
9410 continue;
9411 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9413 if (!(lum_map & (1u << stage)))
9414 continue;
9415 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9416 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9418 if (tfactor_used)
9419 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9420 if (settings->color_key_enabled)
9421 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9422 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9424 if (settings->sRGB_write)
9426 shader_addline(buffer, "const vec4 srgb_const0 = ");
9427 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9428 shader_addline(buffer, ";\n");
9429 shader_addline(buffer, "const vec4 srgb_const1 = ");
9430 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9431 shader_addline(buffer, ";\n");
9434 shader_addline(buffer, "uniform struct\n{\n");
9435 shader_addline(buffer, " vec4 color;\n");
9436 shader_addline(buffer, " float density;\n");
9437 shader_addline(buffer, " float end;\n");
9438 shader_addline(buffer, " float scale;\n");
9439 shader_addline(buffer, "} ffp_fog;\n");
9441 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9442 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9444 if (legacy_syntax)
9446 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9447 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9448 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9449 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9450 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9452 else
9454 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9455 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9456 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9457 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9458 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9461 shader_addline(buffer, "void main()\n{\n");
9463 if (legacy_syntax)
9465 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9466 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9469 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9471 if (tex_map & (1u << stage))
9473 if (settings->pointsprite)
9474 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9475 else if (settings->texcoords_initialized & (1u << stage))
9476 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9477 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9478 else
9479 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9483 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9484 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9486 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9487 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9489 /* Generate texture sampling instructions */
9490 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9492 const char *texture_function, *coord_mask;
9493 BOOL proj;
9495 if (!(tex_map & (1u << stage)))
9496 continue;
9498 if (settings->op[stage].projected == proj_none)
9500 proj = FALSE;
9502 else if (settings->op[stage].projected == proj_count4
9503 || settings->op[stage].projected == proj_count3)
9505 proj = TRUE;
9507 else
9509 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9510 proj = TRUE;
9513 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9514 proj = FALSE;
9516 switch (settings->op[stage].tex_type)
9518 case WINED3D_GL_RES_TYPE_TEX_1D:
9519 if (proj)
9521 texture_function = "texture1DProj";
9522 coord_mask = "xw";
9524 else
9526 texture_function = "texture1D";
9527 coord_mask = "x";
9529 break;
9530 case WINED3D_GL_RES_TYPE_TEX_2D:
9531 if (proj)
9533 texture_function = "texture2DProj";
9534 coord_mask = "xyw";
9536 else
9538 texture_function = "texture2D";
9539 coord_mask = "xy";
9541 break;
9542 case WINED3D_GL_RES_TYPE_TEX_3D:
9543 if (proj)
9545 texture_function = "texture3DProj";
9546 coord_mask = "xyzw";
9548 else
9550 texture_function = "texture3D";
9551 coord_mask = "xyz";
9553 break;
9554 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9555 texture_function = "textureCube";
9556 coord_mask = "xyz";
9557 break;
9558 case WINED3D_GL_RES_TYPE_TEX_RECT:
9559 if (proj)
9561 texture_function = "texture2DRectProj";
9562 coord_mask = "xyw";
9564 else
9566 texture_function = "texture2DRect";
9567 coord_mask = "xy";
9569 break;
9570 default:
9571 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9572 texture_function = "";
9573 coord_mask = "xyzw";
9574 break;
9576 if (!legacy_syntax)
9577 texture_function = proj ? "textureProj" : "texture";
9579 if (stage > 0
9580 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9581 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9583 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9585 /* With projective textures, texbem only divides the static
9586 * texture coord, not the displacement, so multiply the
9587 * displacement with the dividing parameter before passing it to
9588 * TXP. */
9589 if (settings->op[stage].projected != proj_none)
9591 if (settings->op[stage].projected == proj_count4)
9593 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9594 stage, stage);
9595 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9597 else
9599 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9600 stage, stage);
9601 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9604 else
9606 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9609 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9610 stage, texture_function, stage, coord_mask);
9612 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9613 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9614 stage, stage - 1, stage - 1, stage - 1);
9616 else if (settings->op[stage].projected == proj_count3)
9618 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9619 stage, texture_function, stage, stage);
9621 else
9623 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9624 stage, texture_function, stage, stage, coord_mask);
9627 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9628 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9629 settings->op[stage].color_fixup);
9632 if (settings->color_key_enabled)
9634 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9635 shader_addline(buffer, " discard;\n");
9638 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9640 /* Generate the main shader */
9641 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9643 BOOL op_equal;
9645 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9646 break;
9648 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9649 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9650 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9651 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9652 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9653 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9654 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9655 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9656 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9657 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9658 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9659 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9660 else
9661 op_equal = settings->op[stage].aop == settings->op[stage].cop
9662 && settings->op[stage].carg0 == settings->op[stage].aarg0
9663 && settings->op[stage].carg1 == settings->op[stage].aarg1
9664 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9666 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9668 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9669 settings->op[stage].cop, settings->op[stage].carg0,
9670 settings->op[stage].carg1, settings->op[stage].carg2);
9672 else if (op_equal)
9674 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
9675 settings->op[stage].cop, settings->op[stage].carg0,
9676 settings->op[stage].carg1, settings->op[stage].carg2);
9678 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9679 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9681 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9682 settings->op[stage].cop, settings->op[stage].carg0,
9683 settings->op[stage].carg1, settings->op[stage].carg2);
9684 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
9685 settings->op[stage].aop, settings->op[stage].aarg0,
9686 settings->op[stage].aarg1, settings->op[stage].aarg2);
9690 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9691 get_fragment_output(gl_info));
9693 if (settings->sRGB_write)
9694 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9696 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9698 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9700 shader_addline(buffer, "}\n");
9702 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9703 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9705 string_buffer_release(&priv->string_buffers, tex_reg_name);
9706 return shader_id;
9709 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9710 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9712 struct glsl_ffp_vertex_shader *shader;
9713 const struct wine_rb_entry *entry;
9715 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9716 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9718 if (!(shader = heap_alloc(sizeof(*shader))))
9719 return NULL;
9721 shader->desc.settings = *settings;
9722 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9723 list_init(&shader->linked_programs);
9724 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9725 ERR("Failed to insert ffp vertex shader.\n");
9727 return shader;
9730 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9731 const struct ffp_frag_settings *args, const struct wined3d_context *context)
9733 struct glsl_ffp_fragment_shader *glsl_desc;
9734 const struct ffp_frag_desc *desc;
9736 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9737 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9739 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9740 return NULL;
9742 glsl_desc->entry.settings = *args;
9743 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
9744 list_init(&glsl_desc->linked_programs);
9745 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9747 return glsl_desc;
9751 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9752 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9754 unsigned int i;
9755 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9757 for (i = 0; i < vs_c_count; ++i)
9759 string_buffer_sprintf(name, "vs_c[%u]", i);
9760 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9762 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9764 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9766 string_buffer_sprintf(name, "vs_i[%u]", i);
9767 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9770 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9772 string_buffer_sprintf(name, "vs_b[%u]", i);
9773 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9776 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9778 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9780 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9781 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9783 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9784 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9785 for (i = 0; i < MAX_TEXTURES; ++i)
9787 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9788 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9790 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9791 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9792 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9793 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9794 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9795 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9796 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
9798 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9799 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9800 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9801 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9802 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9803 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9804 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9805 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9806 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9807 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9808 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9809 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9810 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9811 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9812 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9813 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9814 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9815 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9816 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9817 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9818 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9819 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9820 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9821 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9823 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
9824 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
9825 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
9826 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
9827 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
9828 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
9829 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
9831 string_buffer_release(&priv->string_buffers, name);
9834 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
9835 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
9837 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9840 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
9841 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
9843 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9846 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
9847 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
9849 unsigned int i;
9850 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9852 for (i = 0; i < ps_c_count; ++i)
9854 string_buffer_sprintf(name, "ps_c[%u]", i);
9855 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9857 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
9859 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9861 string_buffer_sprintf(name, "ps_i[%u]", i);
9862 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9865 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9867 string_buffer_sprintf(name, "ps_b[%u]", i);
9868 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9871 for (i = 0; i < MAX_TEXTURES; ++i)
9873 string_buffer_sprintf(name, "bumpenv_mat%u", i);
9874 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9875 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
9876 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9877 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
9878 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9879 string_buffer_sprintf(name, "tss_const%u", i);
9880 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9883 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
9884 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
9886 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
9887 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
9888 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
9889 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
9891 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
9893 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
9894 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
9895 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
9897 string_buffer_release(&priv->string_buffers, name);
9900 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
9901 const struct wined3d_context *context, struct wined3d_shader *shader)
9903 struct glsl_context_data *ctx_data = context->shader_backend_data;
9904 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9905 const struct wined3d_gl_info *gl_info = context->gl_info;
9906 struct glsl_cs_compiled_shader *gl_shaders;
9907 struct glsl_shader_private *shader_data;
9908 struct glsl_shader_prog_link *entry;
9909 GLuint shader_id, program_id;
9911 if (!(entry = heap_alloc(sizeof(*entry))))
9913 ERR("Out of memory.\n");
9914 return E_OUTOFMEMORY;
9917 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
9919 ERR("Failed to allocate backend data.\n");
9920 heap_free(entry);
9921 return E_OUTOFMEMORY;
9923 shader_data = shader->backend_data;
9924 gl_shaders = shader_data->gl_shaders.cs;
9926 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
9928 ERR("Failed to allocate GL shader array.\n");
9929 heap_free(entry);
9930 heap_free(shader->backend_data);
9931 shader->backend_data = NULL;
9932 return E_OUTOFMEMORY;
9934 shader_data->shader_array_size = 1;
9935 gl_shaders = shader_data->gl_shaders.cs;
9937 TRACE("Compiling compute shader %p.\n", shader);
9939 string_buffer_clear(buffer);
9940 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
9941 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
9943 program_id = GL_EXTCALL(glCreateProgram());
9944 TRACE("Created new GLSL shader program %u.\n", program_id);
9946 entry->id = program_id;
9947 entry->vs.id = 0;
9948 entry->hs.id = 0;
9949 entry->ds.id = 0;
9950 entry->gs.id = 0;
9951 entry->ps.id = 0;
9952 entry->cs.id = shader_id;
9953 entry->constant_version = 0;
9954 entry->shader_controlled_clip_distances = 0;
9955 entry->ps.np2_fixup_info = NULL;
9956 add_glsl_program_entry(priv, entry);
9958 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
9959 GL_EXTCALL(glAttachShader(program_id, shader_id));
9960 checkGLcall("glAttachShader");
9962 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
9964 TRACE("Linking GLSL shader program %u.\n", program_id);
9965 GL_EXTCALL(glLinkProgram(program_id));
9966 shader_glsl_validate_link(gl_info, program_id);
9968 GL_EXTCALL(glUseProgram(program_id));
9969 checkGLcall("glUseProgram");
9970 shader_glsl_load_program_resources(context, priv, program_id, shader);
9971 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
9973 entry->constant_update_mask = 0;
9975 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
9976 checkGLcall("glUseProgram");
9977 return WINED3D_OK;
9980 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
9981 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
9983 struct glsl_shader_private *shader_data;
9985 if (!shader->backend_data)
9987 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
9988 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
9990 ERR("Failed to compile compute shader %p.\n", shader);
9991 return 0;
9994 shader_data = shader->backend_data;
9995 return shader_data->gl_shaders.cs[0].id;
9998 /* Context activation is done by the caller. */
9999 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
10000 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10002 struct glsl_shader_prog_link *entry;
10003 struct wined3d_shader *shader;
10004 struct glsl_program_key key;
10005 GLuint cs_id;
10007 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10008 return;
10010 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10012 WARN("Compute shader is NULL.\n");
10013 ctx_data->glsl_program = NULL;
10014 return;
10017 cs_id = find_glsl_compute_shader(context, priv, shader);
10018 memset(&key, 0, sizeof(key));
10019 key.cs_id = cs_id;
10020 if (!(entry = get_glsl_program_entry(priv, &key)))
10021 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10022 ctx_data->glsl_program = entry;
10025 /* Context activation is done by the caller. */
10026 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
10027 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10029 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
10030 const struct wined3d_gl_info *gl_info = context->gl_info;
10031 const struct wined3d_shader *pre_rasterization_shader;
10032 const struct ps_np2fixup_info *np2fixup_info = NULL;
10033 struct wined3d_shader *hshader, *dshader, *gshader;
10034 struct glsl_shader_prog_link *entry = NULL;
10035 struct wined3d_shader *vshader = NULL;
10036 struct wined3d_shader *pshader = NULL;
10037 GLuint reorder_shader_id = 0;
10038 struct glsl_program_key key;
10039 GLuint program_id;
10040 unsigned int i;
10041 GLuint vs_id = 0;
10042 GLuint hs_id = 0;
10043 GLuint ds_id = 0;
10044 GLuint gs_id = 0;
10045 GLuint ps_id = 0;
10046 struct list *ps_list, *vs_list;
10047 WORD attribs_map;
10048 struct wined3d_string_buffer *tmp_name;
10050 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10052 vs_id = ctx_data->glsl_program->vs.id;
10053 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10055 if (use_vs(state))
10056 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10058 else if (use_vs(state))
10060 struct vs_compile_args vs_compile_args;
10062 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10064 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
10065 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
10066 vs_list = &vshader->linked_programs;
10068 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10070 struct glsl_ffp_vertex_shader *ffp_shader;
10071 struct wined3d_ffp_vs_settings settings;
10073 wined3d_ffp_get_vs_settings(context, state, &settings);
10074 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10075 vs_id = ffp_shader->id;
10076 vs_list = &ffp_shader->linked_programs;
10079 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10080 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10081 hs_id = ctx_data->glsl_program->hs.id;
10082 else if (hshader)
10083 hs_id = find_glsl_hull_shader(context, priv, hshader);
10085 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10086 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10088 ds_id = ctx_data->glsl_program->ds.id;
10090 else if (dshader)
10092 struct ds_compile_args args;
10094 find_ds_compile_args(state, dshader, &args, context);
10095 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
10098 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10099 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10101 gs_id = ctx_data->glsl_program->gs.id;
10103 else if (gshader)
10105 struct gs_compile_args args;
10107 find_gs_compile_args(state, gshader, &args, context);
10108 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
10111 /* A pixel shader is not used when rasterization is disabled. */
10112 if (is_rasterization_disabled(gshader))
10114 ps_id = 0;
10115 ps_list = NULL;
10117 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10119 ps_id = ctx_data->glsl_program->ps.id;
10120 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10122 if (use_ps(state))
10123 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10125 else if (use_ps(state))
10127 struct ps_compile_args ps_compile_args;
10128 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10129 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
10130 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
10131 pshader, &ps_compile_args, &np2fixup_info);
10132 ps_list = &pshader->linked_programs;
10134 else if (priv->fragment_pipe == &glsl_fragment_pipe
10135 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10137 struct glsl_ffp_fragment_shader *ffp_shader;
10138 struct ffp_frag_settings settings;
10140 gen_ffp_frag_op(context, state, &settings, FALSE);
10141 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
10142 ps_id = ffp_shader->id;
10143 ps_list = &ffp_shader->linked_programs;
10146 key.vs_id = vs_id;
10147 key.hs_id = hs_id;
10148 key.ds_id = ds_id;
10149 key.gs_id = gs_id;
10150 key.ps_id = ps_id;
10151 key.cs_id = 0;
10152 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10154 ctx_data->glsl_program = entry;
10155 return;
10158 /* If we get to this point, then no matching program exists, so we create one */
10159 program_id = GL_EXTCALL(glCreateProgram());
10160 TRACE("Created new GLSL shader program %u.\n", program_id);
10162 /* Create the entry */
10163 entry = heap_alloc(sizeof(*entry));
10164 entry->id = program_id;
10165 entry->vs.id = vs_id;
10166 entry->hs.id = hs_id;
10167 entry->ds.id = ds_id;
10168 entry->gs.id = gs_id;
10169 entry->ps.id = ps_id;
10170 entry->cs.id = 0;
10171 entry->constant_version = 0;
10172 entry->shader_controlled_clip_distances = 0;
10173 entry->ps.np2_fixup_info = np2fixup_info;
10174 /* Add the hash table entry */
10175 add_glsl_program_entry(priv, entry);
10177 /* Set the current program */
10178 ctx_data->glsl_program = entry;
10180 /* Attach GLSL vshader */
10181 if (vs_id)
10183 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10184 GL_EXTCALL(glAttachShader(program_id, vs_id));
10185 checkGLcall("glAttachShader");
10187 list_add_head(vs_list, &entry->vs.shader_entry);
10190 if (vshader)
10192 attribs_map = vshader->reg_maps.input_registers;
10193 if (vshader->reg_maps.shader_version.major < 4)
10195 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10196 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10197 d3d_info->emulated_flatshading
10198 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10199 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10200 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10201 checkGLcall("glAttachShader");
10202 /* Flag the reorder function for deletion, it will be freed
10203 * automatically when the program is destroyed. */
10204 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10207 else
10209 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10212 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10214 /* Bind vertex attributes to a corresponding index number to match
10215 * the same index numbers as ARB_vertex_programs (makes loading
10216 * vertex attributes simpler). With this method, we can use the
10217 * exact same code to load the attributes later for both ARB and
10218 * GLSL shaders.
10220 * We have to do this here because we need to know the Program ID
10221 * in order to make the bindings work, and it has to be done prior
10222 * to linking the GLSL program. */
10223 tmp_name = string_buffer_get(&priv->string_buffers);
10224 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10226 if (!(attribs_map & 1))
10227 continue;
10229 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10230 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10231 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10233 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10234 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10235 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10236 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10239 checkGLcall("glBindAttribLocation");
10240 string_buffer_release(&priv->string_buffers, tmp_name);
10242 if (!needs_legacy_glsl_syntax(gl_info))
10244 GL_EXTCALL(glBindFragDataLocation(program_id, 0, "ps_out"));
10245 checkGLcall("glBindFragDataLocation");
10249 if (hshader)
10251 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10252 GL_EXTCALL(glAttachShader(program_id, hs_id));
10253 checkGLcall("glAttachShader");
10255 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10258 if (dshader)
10260 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10261 GL_EXTCALL(glAttachShader(program_id, ds_id));
10262 checkGLcall("glAttachShader");
10264 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10267 if (gshader)
10269 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10270 GL_EXTCALL(glAttachShader(program_id, gs_id));
10271 checkGLcall("glAttachShader");
10273 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10275 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10278 /* Attach GLSL pshader */
10279 if (ps_id)
10281 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10282 GL_EXTCALL(glAttachShader(program_id, ps_id));
10283 checkGLcall("glAttachShader");
10285 list_add_head(ps_list, &entry->ps.shader_entry);
10288 /* Link the program */
10289 TRACE("Linking GLSL shader program %u.\n", program_id);
10290 GL_EXTCALL(glLinkProgram(program_id));
10291 shader_glsl_validate_link(gl_info, program_id);
10293 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10294 vshader ? vshader->limits->constant_float : 0);
10295 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10296 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10297 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10298 pshader ? pshader->limits->constant_float : 0);
10299 checkGLcall("find glsl program uniform locations");
10301 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10302 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10304 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10305 entry->shader_controlled_clip_distances = 1;
10306 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10309 if (needs_legacy_glsl_syntax(gl_info))
10311 if (pshader && pshader->reg_maps.shader_version.major >= 3
10312 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10314 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10315 entry->vs.vertex_color_clamp = GL_FALSE;
10317 else
10319 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10322 else
10324 /* With core profile we never change vertex_color_clamp from
10325 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10326 * glClampColorARB(). */
10327 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10330 /* Set the shader to allow uniform loading on it */
10331 GL_EXTCALL(glUseProgram(program_id));
10332 checkGLcall("glUseProgram");
10334 entry->constant_update_mask = 0;
10335 if (vshader)
10337 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10338 if (vshader->reg_maps.integer_constants)
10339 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10340 if (vshader->reg_maps.boolean_constants)
10341 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10342 if (entry->vs.pos_fixup_location != -1)
10343 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10345 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10347 else
10349 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10350 | WINED3D_SHADER_CONST_FFP_PROJ;
10352 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10354 if (entry->vs.modelview_matrix_location[i] != -1)
10356 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10357 break;
10361 for (i = 0; i < MAX_TEXTURES; ++i)
10363 if (entry->vs.texture_matrix_location[i] != -1)
10365 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10366 break;
10369 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10370 || entry->vs.material_specular_location != -1
10371 || entry->vs.material_emissive_location != -1
10372 || entry->vs.material_shininess_location != -1)
10373 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10374 if (entry->vs.light_ambient_location != -1)
10375 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10377 if (entry->vs.clip_planes_location != -1)
10378 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10379 if (entry->vs.pointsize_min_location != -1)
10380 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10382 if (hshader)
10383 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10385 if (dshader)
10387 if (entry->ds.pos_fixup_location != -1)
10388 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10390 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10393 if (gshader)
10395 if (entry->gs.pos_fixup_location != -1)
10396 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10398 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10401 if (ps_id)
10403 if (pshader)
10405 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10406 if (pshader->reg_maps.integer_constants)
10407 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10408 if (pshader->reg_maps.boolean_constants)
10409 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10410 if (entry->ps.ycorrection_location != -1)
10411 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10413 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10414 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10416 else
10418 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10420 shader_glsl_load_samplers(context, priv, program_id, NULL);
10423 for (i = 0; i < MAX_TEXTURES; ++i)
10425 if (entry->ps.bumpenv_mat_location[i] != -1)
10427 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10428 break;
10432 if (entry->ps.fog_color_location != -1)
10433 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10434 if (entry->ps.alpha_test_ref_location != -1)
10435 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10436 if (entry->ps.np2_fixup_location != -1)
10437 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10438 if (entry->ps.color_key_location != -1)
10439 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10443 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10445 struct wined3d_device *device = shader->device;
10446 struct wined3d_context *context;
10448 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10450 context = context_acquire(device, NULL, 0);
10451 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10452 context_release(context);
10456 /* Context activation is done by the caller. */
10457 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10458 const struct wined3d_state *state)
10460 struct glsl_context_data *ctx_data = context->shader_backend_data;
10461 const struct wined3d_gl_info *gl_info = context->gl_info;
10462 struct shader_glsl_priv *priv = shader_priv;
10463 struct glsl_shader_prog_link *glsl_program;
10464 GLenum current_vertex_color_clamp;
10465 GLuint program_id, prev_id;
10467 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10468 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10470 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10471 set_glsl_shader_program(context, state, priv, ctx_data);
10472 glsl_program = ctx_data->glsl_program;
10474 if (glsl_program)
10476 program_id = glsl_program->id;
10477 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10478 if (glsl_program->shader_controlled_clip_distances)
10479 context_enable_clip_distances(context, glsl_program->clip_distance_mask);
10481 else
10483 program_id = 0;
10484 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10487 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10489 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10490 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10492 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10493 checkGLcall("glClampColorARB");
10495 else
10497 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10501 TRACE("Using GLSL program %u.\n", program_id);
10503 if (prev_id != program_id)
10505 GL_EXTCALL(glUseProgram(program_id));
10506 checkGLcall("glUseProgram");
10508 if (glsl_program)
10509 context->constant_update_mask |= glsl_program->constant_update_mask;
10512 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10515 /* Context activation is done by the caller. */
10516 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10517 const struct wined3d_state *state)
10519 struct glsl_context_data *ctx_data = context->shader_backend_data;
10520 const struct wined3d_gl_info *gl_info = context->gl_info;
10521 struct shader_glsl_priv *priv = shader_priv;
10522 GLuint program_id, prev_id;
10524 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10525 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10526 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10528 TRACE("Using GLSL program %u.\n", program_id);
10530 if (prev_id != program_id)
10532 GL_EXTCALL(glUseProgram(program_id));
10533 checkGLcall("glUseProgram");
10536 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10537 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10538 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10539 | (1u << WINED3D_SHADER_TYPE_HULL)
10540 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10543 /* "context" is not necessarily the currently active context. */
10544 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10546 struct glsl_context_data *ctx_data = context->shader_backend_data;
10548 ctx_data->glsl_program = NULL;
10549 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10550 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10551 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10552 | (1u << WINED3D_SHADER_TYPE_HULL)
10553 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10554 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10557 /* Context activation is done by the caller. */
10558 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10560 struct glsl_context_data *ctx_data = context->shader_backend_data;
10561 const struct wined3d_gl_info *gl_info = context->gl_info;
10562 struct shader_glsl_priv *priv = shader_priv;
10564 shader_glsl_invalidate_current_program(context);
10565 GL_EXTCALL(glUseProgram(0));
10566 checkGLcall("glUseProgram");
10568 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10569 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10571 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10573 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10574 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10575 checkGLcall("glClampColorARB");
10579 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10580 const struct glsl_shader_prog_link *program)
10582 const struct glsl_context_data *ctx_data;
10583 struct wined3d_context *context;
10584 unsigned int i;
10586 for (i = 0; i < device->context_count; ++i)
10588 context = device->contexts[i];
10589 ctx_data = context->shader_backend_data;
10591 if (ctx_data->glsl_program == program)
10592 shader_glsl_invalidate_current_program(context);
10596 static void shader_glsl_destroy(struct wined3d_shader *shader)
10598 struct glsl_shader_private *shader_data = shader->backend_data;
10599 struct wined3d_device *device = shader->device;
10600 struct shader_glsl_priv *priv = device->shader_priv;
10601 const struct wined3d_gl_info *gl_info;
10602 const struct list *linked_programs;
10603 struct wined3d_context *context;
10605 if (!shader_data || !shader_data->num_gl_shaders)
10607 heap_free(shader_data);
10608 shader->backend_data = NULL;
10609 return;
10612 context = context_acquire(device, NULL, 0);
10613 gl_info = context->gl_info;
10615 TRACE("Deleting linked programs.\n");
10616 linked_programs = &shader->linked_programs;
10617 if (linked_programs->next)
10619 struct glsl_shader_prog_link *entry, *entry2;
10620 UINT i;
10622 switch (shader->reg_maps.shader_version.type)
10624 case WINED3D_SHADER_TYPE_PIXEL:
10626 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10628 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10630 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10631 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10632 checkGLcall("glDeleteShader");
10634 heap_free(shader_data->gl_shaders.ps);
10636 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10637 struct glsl_shader_prog_link, ps.shader_entry)
10639 shader_glsl_invalidate_contexts_program(device, entry);
10640 delete_glsl_program_entry(priv, gl_info, entry);
10643 break;
10646 case WINED3D_SHADER_TYPE_VERTEX:
10648 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10650 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10652 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10653 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10654 checkGLcall("glDeleteShader");
10656 heap_free(shader_data->gl_shaders.vs);
10658 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10659 struct glsl_shader_prog_link, vs.shader_entry)
10661 shader_glsl_invalidate_contexts_program(device, entry);
10662 delete_glsl_program_entry(priv, gl_info, entry);
10665 break;
10668 case WINED3D_SHADER_TYPE_HULL:
10670 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10672 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10674 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10675 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10676 checkGLcall("glDeleteShader");
10678 heap_free(shader_data->gl_shaders.hs);
10680 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10681 struct glsl_shader_prog_link, hs.shader_entry)
10683 shader_glsl_invalidate_contexts_program(device, entry);
10684 delete_glsl_program_entry(priv, gl_info, entry);
10687 break;
10690 case WINED3D_SHADER_TYPE_DOMAIN:
10692 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10694 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10696 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10697 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10698 checkGLcall("glDeleteShader");
10700 heap_free(shader_data->gl_shaders.ds);
10702 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10703 struct glsl_shader_prog_link, ds.shader_entry)
10705 shader_glsl_invalidate_contexts_program(device, entry);
10706 delete_glsl_program_entry(priv, gl_info, entry);
10709 break;
10712 case WINED3D_SHADER_TYPE_GEOMETRY:
10714 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10716 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10718 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10719 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10720 checkGLcall("glDeleteShader");
10722 heap_free(shader_data->gl_shaders.gs);
10724 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10725 struct glsl_shader_prog_link, gs.shader_entry)
10727 shader_glsl_invalidate_contexts_program(device, entry);
10728 delete_glsl_program_entry(priv, gl_info, entry);
10731 break;
10734 case WINED3D_SHADER_TYPE_COMPUTE:
10736 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10738 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10740 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10741 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10742 checkGLcall("glDeleteShader");
10744 heap_free(shader_data->gl_shaders.cs);
10746 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10747 struct glsl_shader_prog_link, cs.shader_entry)
10749 shader_glsl_invalidate_contexts_program(device, entry);
10750 delete_glsl_program_entry(priv, gl_info, entry);
10753 break;
10756 default:
10757 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10758 break;
10762 heap_free(shader->backend_data);
10763 shader->backend_data = NULL;
10765 context_release(context);
10768 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10770 const struct glsl_program_key *k = key;
10771 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10772 const struct glsl_shader_prog_link, program_lookup_entry);
10774 if (k->vs_id > prog->vs.id) return 1;
10775 else if (k->vs_id < prog->vs.id) return -1;
10777 if (k->gs_id > prog->gs.id) return 1;
10778 else if (k->gs_id < prog->gs.id) return -1;
10780 if (k->ps_id > prog->ps.id) return 1;
10781 else if (k->ps_id < prog->ps.id) return -1;
10783 if (k->hs_id > prog->hs.id) return 1;
10784 else if (k->hs_id < prog->hs.id) return -1;
10786 if (k->ds_id > prog->ds.id) return 1;
10787 else if (k->ds_id < prog->ds.id) return -1;
10789 if (k->cs_id > prog->cs.id) return 1;
10790 else if (k->cs_id < prog->cs.id) return -1;
10792 return 0;
10795 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10797 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10798 + constant_count * sizeof(*heap->contained)
10799 + constant_count * sizeof(*heap->positions);
10800 void *mem;
10802 if (!(mem = heap_alloc(size)))
10804 ERR("Failed to allocate memory\n");
10805 return FALSE;
10808 heap->entries = mem;
10809 heap->entries[1].version = 0;
10810 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10811 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10812 heap->positions = (unsigned int *)(heap->contained + constant_count);
10813 heap->size = 1;
10815 return TRUE;
10818 static void constant_heap_free(struct constant_heap *heap)
10820 heap_free(heap->entries);
10823 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
10824 const struct fragment_pipeline *fragment_pipe)
10826 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
10827 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
10828 struct fragment_caps fragment_caps;
10829 void *vertex_priv, *fragment_priv;
10830 struct shader_glsl_priv *priv;
10832 if (!(priv = heap_alloc_zero(sizeof(*priv))))
10833 return E_OUTOFMEMORY;
10835 string_buffer_list_init(&priv->string_buffers);
10837 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
10839 ERR("Failed to initialize vertex pipe.\n");
10840 heap_free(priv);
10841 return E_FAIL;
10844 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
10846 ERR("Failed to initialize fragment pipe.\n");
10847 vertex_pipe->vp_free(device);
10848 heap_free(priv);
10849 return E_FAIL;
10852 if (!string_buffer_init(&priv->shader_buffer))
10854 ERR("Failed to initialize shader buffer.\n");
10855 goto fail;
10858 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
10860 ERR("Failed to allocate memory.\n");
10861 goto fail;
10864 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
10866 ERR("Failed to initialize vertex shader constant heap\n");
10867 goto fail;
10870 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
10872 ERR("Failed to initialize pixel shader constant heap\n");
10873 goto fail;
10876 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
10878 priv->next_constant_version = 1;
10879 priv->vertex_pipe = vertex_pipe;
10880 priv->fragment_pipe = fragment_pipe;
10881 fragment_pipe->get_caps(gl_info, &fragment_caps);
10882 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
10883 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
10885 device->vertex_priv = vertex_priv;
10886 device->fragment_priv = fragment_priv;
10887 device->shader_priv = priv;
10889 return WINED3D_OK;
10891 fail:
10892 constant_heap_free(&priv->pconst_heap);
10893 constant_heap_free(&priv->vconst_heap);
10894 heap_free(priv->stack);
10895 string_buffer_free(&priv->shader_buffer);
10896 fragment_pipe->free_private(device);
10897 vertex_pipe->vp_free(device);
10898 heap_free(priv);
10899 return E_OUTOFMEMORY;
10902 /* Context activation is done by the caller. */
10903 static void shader_glsl_free(struct wined3d_device *device)
10905 struct shader_glsl_priv *priv = device->shader_priv;
10907 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
10908 constant_heap_free(&priv->pconst_heap);
10909 constant_heap_free(&priv->vconst_heap);
10910 heap_free(priv->stack);
10911 string_buffer_list_cleanup(&priv->string_buffers);
10912 string_buffer_free(&priv->shader_buffer);
10913 priv->fragment_pipe->free_private(device);
10914 priv->vertex_pipe->vp_free(device);
10916 heap_free(device->shader_priv);
10917 device->shader_priv = NULL;
10920 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
10922 struct glsl_context_data *ctx_data;
10924 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
10925 return FALSE;
10926 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10927 context->shader_backend_data = ctx_data;
10928 return TRUE;
10931 static void shader_glsl_free_context_data(struct wined3d_context *context)
10933 heap_free(context->shader_backend_data);
10936 static void shader_glsl_init_context_state(struct wined3d_context *context)
10938 const struct wined3d_gl_info *gl_info = context->gl_info;
10940 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
10941 checkGLcall("GL_PROGRAM_POINT_SIZE");
10944 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
10946 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
10947 && gl_info->supported[WINED3D_GL_VERSION_3_2]
10948 && gl_info->supported[ARB_SAMPLER_OBJECTS]
10949 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
10950 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
10952 if (shader_model_4
10953 && gl_info->supported[ARB_COMPUTE_SHADER]
10954 && gl_info->supported[ARB_CULL_DISTANCE]
10955 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
10956 && gl_info->supported[ARB_DRAW_INDIRECT]
10957 && gl_info->supported[ARB_GPU_SHADER5]
10958 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
10959 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
10960 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
10961 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
10962 && gl_info->supported[ARB_TESSELLATION_SHADER]
10963 && gl_info->supported[ARB_TEXTURE_GATHER]
10964 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
10965 return 5;
10967 if (shader_model_4)
10968 return 4;
10970 /* Support for texldd and texldl instructions in pixel shaders is required
10971 * for SM3. */
10972 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
10973 return 3;
10975 return 2;
10978 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
10980 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
10982 TRACE("Shader model %u.\n", shader_model);
10984 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
10985 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
10986 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
10987 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
10988 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
10989 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
10991 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
10992 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
10994 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
10995 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
10996 caps->varying_count = gl_info->limits.glsl_varyings;
10998 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
10999 * Direct3D minimum requirement.
11001 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11002 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11004 * The problem is that the refrast clamps temporary results in the shader to
11005 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11006 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11007 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11008 * offer a way to query this.
11010 if (shader_model >= 4)
11011 caps->ps_1x_max_value = FLT_MAX;
11012 else
11013 caps->ps_1x_max_value = 1024.0f;
11015 /* Ideally we'd only set caps like sRGB writes here if supported by both
11016 * the shader backend and the fragment pipe, but we can get called before
11017 * shader_glsl_alloc(). */
11018 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11019 | WINED3D_SHADER_CAP_SRGB_WRITE;
11022 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11024 /* We support everything except YUV conversions. */
11025 return !is_complex_fixup(fixup);
11028 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11030 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11031 /* WINED3DSIH_ADD */ shader_glsl_binop,
11032 /* WINED3DSIH_AND */ shader_glsl_binop,
11033 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11034 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11035 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11036 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11037 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11038 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11039 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11040 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11041 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11042 /* WINED3DSIH_BEM */ shader_glsl_bem,
11043 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11044 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11045 /* WINED3DSIH_BREAK */ shader_glsl_break,
11046 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11047 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11048 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11049 /* WINED3DSIH_CALL */ shader_glsl_call,
11050 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11051 /* WINED3DSIH_CASE */ shader_glsl_case,
11052 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11053 /* WINED3DSIH_CND */ shader_glsl_cnd,
11054 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11055 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11056 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11057 /* WINED3DSIH_CRS */ shader_glsl_cross,
11058 /* WINED3DSIH_CUT */ shader_glsl_cut,
11059 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11060 /* WINED3DSIH_DCL */ shader_glsl_nop,
11061 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11062 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11063 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11064 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11065 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11066 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11067 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11068 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11069 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11070 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11071 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11072 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11073 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11074 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11075 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11076 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
11077 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
11078 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11079 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11080 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11081 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11082 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11083 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11084 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11085 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11086 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11087 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11088 /* WINED3DSIH_DCL_STREAM */ NULL,
11089 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11090 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11091 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11092 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11093 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11094 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11095 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11096 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11097 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11098 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11099 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11100 /* WINED3DSIH_DEF */ shader_glsl_nop,
11101 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11102 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11103 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11104 /* WINED3DSIH_DIV */ shader_glsl_binop,
11105 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11106 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11107 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11108 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11109 /* WINED3DSIH_DST */ shader_glsl_dst,
11110 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11111 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11112 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11113 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11114 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11115 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11116 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ NULL,
11117 /* WINED3DSIH_ELSE */ shader_glsl_else,
11118 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11119 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11120 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11121 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11122 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11123 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11124 /* WINED3DSIH_EQ */ shader_glsl_relop,
11125 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11126 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11127 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11128 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11129 /* WINED3DSIH_FCALL */ NULL,
11130 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11131 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11132 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11133 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11134 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11135 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11136 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11137 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11138 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11139 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11140 /* WINED3DSIH_GE */ shader_glsl_relop,
11141 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11142 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11143 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11144 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11145 /* WINED3DSIH_IADD */ shader_glsl_binop,
11146 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11147 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11148 /* WINED3DSIH_IF */ shader_glsl_if,
11149 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11150 /* WINED3DSIH_IGE */ shader_glsl_relop,
11151 /* WINED3DSIH_ILT */ shader_glsl_relop,
11152 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11153 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11154 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11155 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11156 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11157 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11158 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11159 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11160 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11161 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11162 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11163 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11164 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11165 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11166 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11167 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11168 /* WINED3DSIH_INE */ shader_glsl_relop,
11169 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11170 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11171 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11172 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11173 /* WINED3DSIH_LABEL */ shader_glsl_label,
11174 /* WINED3DSIH_LD */ shader_glsl_ld,
11175 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11176 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11177 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11178 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11179 /* WINED3DSIH_LIT */ shader_glsl_lit,
11180 /* WINED3DSIH_LOD */ NULL,
11181 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11182 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11183 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11184 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11185 /* WINED3DSIH_LT */ shader_glsl_relop,
11186 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11187 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11188 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11189 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11190 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11191 /* WINED3DSIH_MAD */ shader_glsl_mad,
11192 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11193 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11194 /* WINED3DSIH_MOV */ shader_glsl_mov,
11195 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11196 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11197 /* WINED3DSIH_MUL */ shader_glsl_binop,
11198 /* WINED3DSIH_NE */ shader_glsl_relop,
11199 /* WINED3DSIH_NOP */ shader_glsl_nop,
11200 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11201 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11202 /* WINED3DSIH_OR */ shader_glsl_binop,
11203 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11204 /* WINED3DSIH_POW */ shader_glsl_pow,
11205 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11206 /* WINED3DSIH_REP */ shader_glsl_rep,
11207 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11208 /* WINED3DSIH_RET */ shader_glsl_ret,
11209 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11210 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11211 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11212 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11213 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11214 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11215 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11216 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11217 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11218 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11219 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11220 /* WINED3DSIH_SAMPLE_INFO */ NULL,
11221 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11222 /* WINED3DSIH_SAMPLE_POS */ NULL,
11223 /* WINED3DSIH_SETP */ NULL,
11224 /* WINED3DSIH_SGE */ shader_glsl_compare,
11225 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11226 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11227 /* WINED3DSIH_SLT */ shader_glsl_compare,
11228 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11229 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11230 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11231 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11232 /* WINED3DSIH_SUB */ shader_glsl_binop,
11233 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11234 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11235 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11236 /* WINED3DSIH_TEX */ shader_glsl_tex,
11237 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11238 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11239 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11240 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11241 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11242 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11243 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11244 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11245 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11246 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11247 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11248 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11249 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11250 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11251 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11252 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11253 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11254 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11255 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11256 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11257 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11258 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11259 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11260 /* WINED3DSIH_UGE */ shader_glsl_relop,
11261 /* WINED3DSIH_ULT */ shader_glsl_relop,
11262 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11263 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11264 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11265 /* WINED3DSIH_USHR */ shader_glsl_binop,
11266 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11267 /* WINED3DSIH_XOR */ shader_glsl_binop,
11270 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11271 SHADER_HANDLER hw_fct;
11273 /* Select handler */
11274 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11276 /* Unhandled opcode */
11277 if (!hw_fct)
11279 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11280 return;
11282 hw_fct(ins);
11284 shader_glsl_add_instruction_modifiers(ins);
11287 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11289 struct shader_glsl_priv *priv = shader_priv;
11291 return priv->ffp_proj_control;
11294 const struct wined3d_shader_backend_ops glsl_shader_backend =
11296 shader_glsl_handle_instruction,
11297 shader_glsl_precompile,
11298 shader_glsl_select,
11299 shader_glsl_select_compute,
11300 shader_glsl_disable,
11301 shader_glsl_update_float_vertex_constants,
11302 shader_glsl_update_float_pixel_constants,
11303 shader_glsl_load_constants,
11304 shader_glsl_destroy,
11305 shader_glsl_alloc,
11306 shader_glsl_free,
11307 shader_glsl_allocate_context_data,
11308 shader_glsl_free_context_data,
11309 shader_glsl_init_context_state,
11310 shader_glsl_get_caps,
11311 shader_glsl_color_fixup_supported,
11312 shader_glsl_has_ffp_proj_control,
11315 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11317 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11319 caps->xyzrhw = TRUE;
11320 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11321 caps->ffp_generic_attributes = TRUE;
11322 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11323 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11324 caps->max_vertex_blend_matrix_index = 0;
11325 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11326 | WINED3DVTXPCAPS_MATERIALSOURCE7
11327 | WINED3DVTXPCAPS_VERTEXFOG
11328 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11329 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11330 | WINED3DVTXPCAPS_LOCALVIEWER
11331 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11332 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11333 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11334 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11337 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11339 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11340 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11341 return 0;
11344 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11346 struct shader_glsl_priv *priv;
11348 if (shader_backend == &glsl_shader_backend)
11350 priv = shader_priv;
11351 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11352 return priv;
11355 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11357 return NULL;
11360 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11362 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11363 struct glsl_ffp_vertex_shader, desc.entry);
11364 struct glsl_shader_prog_link *program, *program2;
11365 struct glsl_ffp_destroy_ctx *ctx = context;
11367 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11368 struct glsl_shader_prog_link, vs.shader_entry)
11370 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11372 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11373 heap_free(shader);
11376 /* Context activation is done by the caller. */
11377 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11379 struct shader_glsl_priv *priv = device->vertex_priv;
11380 struct glsl_ffp_destroy_ctx ctx;
11382 ctx.priv = priv;
11383 ctx.gl_info = &device->adapter->gl_info;
11384 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11387 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11388 const struct wined3d_state *state, DWORD state_id) {}
11390 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11391 const struct wined3d_state *state, DWORD state_id)
11393 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11396 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11397 const struct wined3d_state *state, DWORD state_id)
11399 const struct wined3d_gl_info *gl_info = context->gl_info;
11400 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11401 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11402 BOOL transformed = context->stream_info.position_transformed;
11403 BOOL wasrhw = context->last_was_rhw;
11404 unsigned int i;
11406 context->last_was_rhw = transformed;
11408 /* If the vertex declaration contains a transformed position attribute,
11409 * the draw uses the fixed function vertex pipeline regardless of any
11410 * vertex shader set by the application. */
11411 if (transformed != wasrhw
11412 || context->stream_info.swizzle_map != context->last_swizzle_map)
11413 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11415 context->last_swizzle_map = context->stream_info.swizzle_map;
11417 if (!use_vs(state))
11419 if (context->last_was_vshader)
11421 if (legacy_clip_planes)
11422 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11423 clipplane(context, state, STATE_CLIPPLANE(i));
11424 else
11425 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11428 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11430 /* Because of settings->texcoords, we have to regenerate the vertex
11431 * shader on a vdecl change if there aren't enough varyings to just
11432 * always output all the texture coordinates. */
11433 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11434 || normal != context->last_was_normal)
11435 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11437 if (use_ps(state)
11438 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11439 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11440 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11442 else
11444 if (!context->last_was_vshader)
11446 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11447 if (legacy_clip_planes)
11448 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11449 clipplane(context, state, STATE_CLIPPLANE(i));
11450 else
11451 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11455 context->last_was_vshader = use_vs(state);
11456 context->last_was_normal = normal;
11459 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11460 const struct wined3d_state *state, DWORD state_id)
11462 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11463 /* Different vertex shaders potentially require a different vertex attributes setup. */
11464 if (!isStateDirty(context, STATE_VDECL))
11465 context_apply_state(context, state, STATE_VDECL);
11468 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11469 const struct wined3d_state *state, DWORD state_id)
11471 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11472 * winding) are defined in Hull Shaders, while in GLSL those are
11473 * specified in Tessellation Evaluation Shaders. */
11474 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11476 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11477 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11480 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11481 const struct wined3d_state *state, DWORD state_id)
11483 struct glsl_context_data *ctx_data = context->shader_backend_data;
11484 BOOL rasterization_disabled;
11486 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11487 if (ctx_data->rasterization_disabled != rasterization_disabled)
11488 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11489 ctx_data->rasterization_disabled = rasterization_disabled;
11491 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11492 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11493 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11494 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11495 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11498 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11499 const struct wined3d_state *state, DWORD state_id)
11501 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11502 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11503 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11504 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11505 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11506 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11507 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11510 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11511 const struct wined3d_state *state, DWORD state_id)
11513 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11516 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11517 const struct wined3d_state *state, DWORD state_id)
11519 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11522 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11524 const struct wined3d_gl_info *gl_info = context->gl_info;
11525 unsigned int k;
11527 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11528 | WINED3D_SHADER_CONST_FFP_LIGHTS
11529 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11531 if (needs_legacy_glsl_syntax(gl_info))
11533 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11535 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11536 clipplane(context, state, STATE_CLIPPLANE(k));
11539 else
11541 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11545 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11546 const struct wined3d_state *state, DWORD state_id)
11548 /* Table fog behavior depends on the projection matrix. */
11549 if (state->render_states[WINED3D_RS_FOGENABLE]
11550 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11551 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11552 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11555 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11556 const struct wined3d_state *state, DWORD state_id)
11558 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11559 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11560 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11561 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11562 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11563 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11566 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11567 const struct wined3d_state *state, DWORD state_id)
11569 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11572 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11573 const struct wined3d_state *state, DWORD state_id)
11575 DWORD sampler = state_id - STATE_SAMPLER(0);
11576 const struct wined3d_texture *texture = state->textures[sampler];
11577 BOOL np2;
11579 if (!texture)
11580 return;
11582 if (sampler >= MAX_TEXTURES)
11583 return;
11585 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11586 || context->lastWasPow2Texture & (1u << sampler))
11588 if (np2)
11589 context->lastWasPow2Texture |= 1u << sampler;
11590 else
11591 context->lastWasPow2Texture &= ~(1u << sampler);
11593 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11597 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11598 const struct wined3d_state *state, DWORD state_id)
11600 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11603 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11604 const struct wined3d_state *state, DWORD state_id)
11606 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11609 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11610 const struct wined3d_state *state, DWORD state_id)
11612 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11615 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11616 const struct wined3d_state *state, DWORD state_id)
11618 if (!use_vs(state))
11619 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11622 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11623 const struct wined3d_state *state, DWORD state_id)
11625 static unsigned int once;
11627 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11628 FIXME("Non-point sprite points not supported in core profile.\n");
11631 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11632 const struct wined3d_state *state, DWORD state_id)
11634 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11637 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11638 const struct wined3d_state *state, DWORD state_id)
11640 const struct wined3d_gl_info *gl_info = context->gl_info;
11641 UINT index = state_id - STATE_CLIPPLANE(0);
11643 if (index >= gl_info->limits.user_clip_distances)
11644 return;
11646 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11649 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11651 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11652 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11653 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11654 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11655 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11656 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11657 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11658 /* Clip planes */
11659 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11660 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11661 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11662 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11663 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11664 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11665 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11666 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11667 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11668 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11669 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11670 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11671 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11672 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11673 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11674 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11675 /* Lights */
11676 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11677 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11678 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11679 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11680 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11681 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11682 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11683 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11684 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11685 /* Viewport */
11686 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11687 /* Transform states */
11688 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11689 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11690 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11691 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11692 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11693 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11694 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11695 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11696 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11697 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11698 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11699 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11700 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11701 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11702 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11703 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11704 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11705 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11706 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11707 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11708 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11709 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11710 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11711 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11712 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11713 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11714 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11715 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11716 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11717 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11718 /* Fog */
11719 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11720 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11721 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11722 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11723 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11724 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11725 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11726 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11727 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11728 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11729 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11730 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11731 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11732 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11733 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11734 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11735 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11736 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11737 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11738 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11739 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11740 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11741 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11742 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11743 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11744 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11745 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11746 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11747 /* NP2 texture matrix fixups. They are not needed if
11748 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11749 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11750 * matrix. */
11751 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11752 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11753 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11754 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11755 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11756 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11757 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11758 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11759 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11760 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11761 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11762 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11763 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11764 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11765 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11766 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11767 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11768 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11769 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11770 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11771 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11772 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11773 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11774 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11775 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11776 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11777 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11778 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11781 /* TODO:
11782 * - Implement vertex tweening. */
11783 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11785 glsl_vertex_pipe_vp_enable,
11786 glsl_vertex_pipe_vp_get_caps,
11787 glsl_vertex_pipe_vp_get_emul_mask,
11788 glsl_vertex_pipe_vp_alloc,
11789 glsl_vertex_pipe_vp_free,
11790 glsl_vertex_pipe_vp_states,
11793 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
11795 /* Nothing to do. */
11798 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
11800 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11801 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11802 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11803 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11804 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11805 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11806 | WINED3DTEXOPCAPS_SELECTARG1
11807 | WINED3DTEXOPCAPS_SELECTARG2
11808 | WINED3DTEXOPCAPS_MODULATE4X
11809 | WINED3DTEXOPCAPS_MODULATE2X
11810 | WINED3DTEXOPCAPS_MODULATE
11811 | WINED3DTEXOPCAPS_ADDSIGNED2X
11812 | WINED3DTEXOPCAPS_ADDSIGNED
11813 | WINED3DTEXOPCAPS_ADD
11814 | WINED3DTEXOPCAPS_SUBTRACT
11815 | WINED3DTEXOPCAPS_ADDSMOOTH
11816 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11817 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11818 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11819 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11820 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
11821 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
11822 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
11823 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
11824 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
11825 | WINED3DTEXOPCAPS_DOTPRODUCT3
11826 | WINED3DTEXOPCAPS_MULTIPLYADD
11827 | WINED3DTEXOPCAPS_LERP
11828 | WINED3DTEXOPCAPS_BUMPENVMAP
11829 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
11830 caps->MaxTextureBlendStages = MAX_TEXTURES;
11831 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
11834 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
11836 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11837 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11838 return 0;
11841 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11843 struct shader_glsl_priv *priv;
11845 if (shader_backend == &glsl_shader_backend)
11847 priv = shader_priv;
11848 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
11849 return priv;
11852 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
11854 return NULL;
11857 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
11859 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11860 struct glsl_ffp_fragment_shader, entry.entry);
11861 struct glsl_shader_prog_link *program, *program2;
11862 struct glsl_ffp_destroy_ctx *ctx = context;
11864 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11865 struct glsl_shader_prog_link, ps.shader_entry)
11867 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11869 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11870 heap_free(shader);
11873 /* Context activation is done by the caller. */
11874 static void glsl_fragment_pipe_free(struct wined3d_device *device)
11876 struct shader_glsl_priv *priv = device->fragment_priv;
11877 struct glsl_ffp_destroy_ctx ctx;
11879 ctx.priv = priv;
11880 ctx.gl_info = &device->adapter->gl_info;
11881 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
11884 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
11885 const struct wined3d_state *state, DWORD state_id)
11887 context->last_was_pshader = use_ps(state);
11889 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11892 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
11893 const struct wined3d_state *state, DWORD state_id)
11895 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11898 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
11899 const struct wined3d_state *state, DWORD state_id)
11901 BOOL use_vshader = use_vs(state);
11902 enum fogsource new_source;
11903 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
11904 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
11906 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11908 if (!state->render_states[WINED3D_RS_FOGENABLE])
11909 return;
11911 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
11913 if (use_vshader)
11914 new_source = FOGSOURCE_VS;
11915 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
11916 new_source = FOGSOURCE_COORD;
11917 else
11918 new_source = FOGSOURCE_FFP;
11920 else
11922 new_source = FOGSOURCE_FFP;
11925 if (new_source != context->fog_source || fogstart == fogend)
11927 context->fog_source = new_source;
11928 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11932 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
11933 const struct wined3d_state *state, DWORD state_id)
11935 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11936 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11937 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11939 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
11940 glsl_fragment_pipe_fog(context, state, state_id);
11943 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
11944 const struct wined3d_state *state, DWORD state_id)
11946 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11947 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11948 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11951 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
11952 const struct wined3d_state *state, DWORD state_id)
11954 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11957 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
11958 const struct wined3d_state *state, DWORD state_id)
11960 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
11963 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
11964 const struct wined3d_state *state, DWORD state_id)
11966 const struct wined3d_gl_info *gl_info = context->gl_info;
11967 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
11968 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
11970 if (func)
11972 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
11973 checkGLcall("glAlphaFunc");
11977 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
11978 const struct wined3d_state *state, DWORD state_id)
11980 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11983 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
11984 const struct wined3d_state *state, DWORD state_id)
11986 const struct wined3d_gl_info *gl_info = context->gl_info;
11988 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
11990 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
11991 checkGLcall("glEnable(GL_ALPHA_TEST)");
11993 else
11995 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
11996 checkGLcall("glDisable(GL_ALPHA_TEST)");
12000 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12001 const struct wined3d_state *state, DWORD state_id)
12003 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12006 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12007 const struct wined3d_state *state, DWORD state_id)
12009 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12012 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12013 const struct wined3d_state *state, DWORD state_id)
12015 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12018 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
12020 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12021 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12022 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12023 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12024 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12025 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12026 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12027 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12028 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12029 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12030 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12031 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12032 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12033 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12034 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12035 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12036 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12037 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12038 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12039 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12040 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12041 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12042 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12043 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12044 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12045 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12046 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12047 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12048 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12049 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12050 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12051 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12052 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12053 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12054 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12055 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12056 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12057 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12058 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12059 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12060 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12061 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12062 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12063 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12064 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12065 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12066 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12067 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12068 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12069 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12070 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12071 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12072 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12073 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12074 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12075 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12076 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12077 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12078 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12079 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12080 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12081 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12082 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12083 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12084 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12085 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12086 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12087 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12088 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12089 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12090 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12091 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12092 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12093 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12094 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12095 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12096 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12097 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12098 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12099 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12100 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12101 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12102 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12103 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12104 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12105 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12106 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12107 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12108 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12109 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12110 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12111 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12112 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12113 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12114 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12115 {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 },
12116 {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 },
12117 {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 },
12118 {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 },
12119 {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 },
12120 {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 },
12121 {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 },
12122 {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 },
12123 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12124 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12125 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12126 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12127 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12128 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12129 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12130 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12131 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12132 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12133 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12134 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12135 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12138 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12140 return TRUE;
12143 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12147 const struct fragment_pipeline glsl_fragment_pipe =
12149 glsl_fragment_pipe_enable,
12150 glsl_fragment_pipe_get_caps,
12151 glsl_fragment_pipe_get_emul_mask,
12152 glsl_fragment_pipe_alloc,
12153 glsl_fragment_pipe_free,
12154 glsl_fragment_pipe_alloc_context_data,
12155 glsl_fragment_pipe_free_context_data,
12156 shader_glsl_color_fixup_supported,
12157 glsl_fragment_pipe_state_template,