wined3d: Fix relative addressing for SM4+ vertex shader inputs.
[wine.git] / dlls / wined3d / glsl_shader.c
blob32a75bf5628811e0742f5f1398d01f4004d07d25
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 = HeapAlloc(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, 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 = wined3d_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 HeapFree(GetProcessHeap(), 0, source);
549 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
550 if (!source)
552 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
553 HeapFree(GetProcessHeap(), 0, shaders);
554 return;
556 source_size = tmp;
559 FIXME("Shader %u:\n", shaders[i]);
560 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
561 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
562 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
563 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
564 FIXME("\n");
566 ptr = source;
567 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
568 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
569 FIXME("\n");
572 HeapFree(GetProcessHeap(), 0, source);
573 HeapFree(GetProcessHeap(), 0, shaders);
576 /* Context activation is done by the caller. */
577 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
579 GLint tmp;
581 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
582 return;
584 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
585 if (!tmp)
587 FIXME("Program %u link status invalid.\n", program);
588 shader_glsl_dump_program_source(gl_info, program);
591 print_glsl_info_log(gl_info, program, TRUE);
594 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
596 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
597 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
598 * versions. */
599 return shader_glsl_get_version(gl_info) >= 140;
602 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
604 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
607 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
608 struct shader_glsl_priv *priv, GLuint program_id,
609 const struct wined3d_shader_reg_maps *reg_maps)
611 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
612 struct wined3d_string_buffer *name;
613 unsigned int i, base, count;
614 GLuint block_idx;
616 if (shader_glsl_use_layout_binding_qualifier(gl_info))
617 return;
619 name = string_buffer_get(&priv->string_buffers);
620 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
621 for (i = 0; i < count; ++i)
623 if (!reg_maps->cb_sizes[i])
624 continue;
626 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
627 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
628 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
630 checkGLcall("glUniformBlockBinding");
631 string_buffer_release(&priv->string_buffers, name);
634 /* Context activation is done by the caller. */
635 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
636 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
637 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
639 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
640 unsigned int i, mapped_unit;
641 GLint name_loc;
643 for (i = 0; i < count; ++i)
645 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
646 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
647 if (name_loc == -1)
648 continue;
650 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
651 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
653 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
654 continue;
657 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
658 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
660 checkGLcall("Load sampler bindings");
661 string_buffer_release(&priv->string_buffers, sampler_name);
664 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
665 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
667 const DWORD *tex_unit_map;
668 unsigned int base, count;
670 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
671 if (sampler_idx >= count)
672 return WINED3D_UNMAPPED_STAGE;
673 if (!tex_unit_map)
674 return base + sampler_idx;
675 return tex_unit_map[base + sampler_idx];
678 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
679 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
680 unsigned int sampler_idx)
682 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
683 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
684 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
685 else
686 ERR("Unmapped sampler %u.\n", sampler_idx);
689 /* Context activation is done by the caller. */
690 static void shader_glsl_load_samplers(const struct wined3d_context *context,
691 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
693 const struct wined3d_gl_info *gl_info = context->gl_info;
694 const struct wined3d_shader_version *shader_version;
695 const DWORD *tex_unit_map;
696 unsigned int base, count;
697 const char *prefix;
699 if (shader_glsl_use_layout_binding_qualifier(gl_info))
700 return;
702 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
703 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
704 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
705 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
708 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
709 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
711 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
713 if (icb)
715 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
716 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
717 GLint icb_location;
719 string_buffer_sprintf(icb_name, "%s_icb", prefix);
720 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
721 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
722 checkGLcall("Load immediate constant buffer");
724 string_buffer_release(&priv->string_buffers, icb_name);
728 /* Context activation is done by the caller. */
729 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
730 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
732 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
733 struct wined3d_string_buffer *name;
734 GLint location;
735 unsigned int i;
737 if (shader_glsl_use_layout_binding_qualifier(gl_info))
738 return;
740 name = string_buffer_get(&priv->string_buffers);
741 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
743 if (!reg_maps->uav_resource_info[i].type)
744 continue;
746 string_buffer_sprintf(name, "%s_image%u", prefix, i);
747 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
748 if (location == -1)
749 continue;
751 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
752 GL_EXTCALL(glUniform1i(location, i));
754 checkGLcall("Load image bindings");
755 string_buffer_release(&priv->string_buffers, name);
758 /* Context activation is done by the caller. */
759 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
760 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
762 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
764 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
765 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
766 /* Texture unit mapping is set up to be the same each time the shader
767 * program is used so we can hardcode the sampler uniform values. */
768 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
771 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
772 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
774 if (varyings && *strings)
776 char *ptr = *strings;
778 varyings[*varying_count] = ptr;
780 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
781 ptr += buffer->content_size + 1;
783 *strings = ptr;
786 *strings_length += buffer->content_size + 1;
787 ++(*varying_count);
790 static void append_transform_feedback_skip_components(const char **varyings,
791 unsigned int *varying_count, char **strings, unsigned int *strings_length,
792 struct wined3d_string_buffer *buffer, unsigned int component_count)
794 unsigned int j;
796 for (j = 0; j < component_count / 4; ++j)
798 string_buffer_sprintf(buffer, "gl_SkipComponents4");
799 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
801 if (component_count % 4)
803 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
804 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
808 static void shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
809 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
810 char *strings, unsigned int *strings_length, GLenum buffer_mode)
812 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
814 count = length = 0;
815 highest_output_slot = 0;
816 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
818 stride = 0;
820 for (i = 0; i < so_desc->element_count; ++i)
822 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
824 highest_output_slot = max(highest_output_slot, e->output_slot);
825 if (e->output_slot != buffer_idx)
826 continue;
828 if (e->stream_idx)
830 FIXME("Unhandled stream %u.\n", e->stream_idx);
831 continue;
834 stride += e->component_count;
836 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
838 append_transform_feedback_skip_components(varyings, &count,
839 &strings, &length, buffer, e->component_count);
840 continue;
843 if (e->component_idx || e->component_count != 4)
845 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
847 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
848 append_transform_feedback_skip_components(varyings, &count,
849 &strings, &length, buffer, e->component_count);
850 continue;
853 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
854 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
855 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
857 else
859 string_buffer_sprintf(buffer, "shader_in_out.reg%u", e->register_idx);
860 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
864 if (buffer_idx < so_desc->buffer_stride_count
865 && stride < so_desc->buffer_strides[buffer_idx] / 4)
867 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
868 append_transform_feedback_skip_components(varyings, &count,
869 &strings, &length, buffer, component_count);
872 if (highest_output_slot <= buffer_idx)
873 break;
875 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
877 string_buffer_sprintf(buffer, "gl_NextBuffer");
878 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
882 if (varying_count)
883 *varying_count = count;
884 if (strings_length)
885 *strings_length = length;
888 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
889 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
891 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
892 const struct wined3d_gl_info *gl_info = context->gl_info;
893 struct wined3d_string_buffer *buffer;
894 unsigned int i, count, length;
895 const char **varyings;
896 char *strings;
897 GLenum mode;
899 if (!so_desc->element_count)
900 return;
902 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
904 mode = GL_INTERLEAVED_ATTRIBS;
906 else
908 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
910 for (i = 0; i < so_desc->element_count; ++i)
912 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
914 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
915 return;
917 ++element_count[so_desc->elements[i].output_slot];
920 if (element_count[0] == so_desc->element_count)
922 mode = GL_INTERLEAVED_ATTRIBS;
924 else
926 mode = GL_SEPARATE_ATTRIBS;
927 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
929 if (element_count[i] != 1)
930 break;
932 for (; i < ARRAY_SIZE(element_count); ++i)
934 if (element_count[i])
936 FIXME("Only single element per buffer is allowed in separate mode.\n");
937 return;
943 buffer = string_buffer_get(&priv->string_buffers);
945 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode);
947 if (!(varyings = wined3d_calloc(count, sizeof(*varyings))))
949 ERR("Out of memory.\n");
950 string_buffer_release(&priv->string_buffers, buffer);
951 return;
953 if (!(strings = wined3d_calloc(length, sizeof(*strings))))
955 ERR("Out of memory.\n");
956 HeapFree(GetProcessHeap(), 0, varyings);
957 string_buffer_release(&priv->string_buffers, buffer);
958 return;
961 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
962 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
963 checkGLcall("glTransformFeedbackVaryings");
965 HeapFree(GetProcessHeap(), 0, varyings);
966 HeapFree(GetProcessHeap(), 0, strings);
967 string_buffer_release(&priv->string_buffers, buffer);
970 /* Context activation is done by the caller. */
971 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
972 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
974 unsigned int start = ~0U, end = 0;
975 int stack_idx = 0;
976 unsigned int heap_idx = 1;
977 unsigned int idx;
979 if (heap->entries[heap_idx].version <= version) return;
981 idx = heap->entries[heap_idx].idx;
982 if (constant_locations[idx] != -1)
983 start = end = idx;
984 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
986 while (stack_idx >= 0)
988 /* Note that we fall through to the next case statement. */
989 switch(stack[stack_idx])
991 case HEAP_NODE_TRAVERSE_LEFT:
993 unsigned int left_idx = heap_idx << 1;
994 if (left_idx < heap->size && heap->entries[left_idx].version > version)
996 heap_idx = left_idx;
997 idx = heap->entries[heap_idx].idx;
998 if (constant_locations[idx] != -1)
1000 if (start > idx)
1001 start = idx;
1002 if (end < idx)
1003 end = idx;
1006 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1007 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1008 break;
1012 case HEAP_NODE_TRAVERSE_RIGHT:
1014 unsigned int right_idx = (heap_idx << 1) + 1;
1015 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1017 heap_idx = right_idx;
1018 idx = heap->entries[heap_idx].idx;
1019 if (constant_locations[idx] != -1)
1021 if (start > idx)
1022 start = idx;
1023 if (end < idx)
1024 end = idx;
1027 stack[stack_idx++] = HEAP_NODE_POP;
1028 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1029 break;
1033 case HEAP_NODE_POP:
1034 heap_idx >>= 1;
1035 --stack_idx;
1036 break;
1039 if (start <= end)
1040 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1041 checkGLcall("walk_constant_heap()");
1044 /* Context activation is done by the caller. */
1045 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1046 GLint location, const struct wined3d_vec4 *data)
1048 GLfloat clamped_constant[4];
1050 if (location == -1) return;
1052 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1053 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1054 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1055 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1057 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1060 /* Context activation is done by the caller. */
1061 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1062 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1063 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1065 int stack_idx = 0;
1066 unsigned int heap_idx = 1;
1067 unsigned int idx;
1069 if (heap->entries[heap_idx].version <= version) return;
1071 idx = heap->entries[heap_idx].idx;
1072 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1073 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1075 while (stack_idx >= 0)
1077 /* Note that we fall through to the next case statement. */
1078 switch(stack[stack_idx])
1080 case HEAP_NODE_TRAVERSE_LEFT:
1082 unsigned int left_idx = heap_idx << 1;
1083 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1085 heap_idx = left_idx;
1086 idx = heap->entries[heap_idx].idx;
1087 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1089 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1090 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1091 break;
1095 case HEAP_NODE_TRAVERSE_RIGHT:
1097 unsigned int right_idx = (heap_idx << 1) + 1;
1098 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1100 heap_idx = right_idx;
1101 idx = heap->entries[heap_idx].idx;
1102 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1104 stack[stack_idx++] = HEAP_NODE_POP;
1105 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1106 break;
1110 case HEAP_NODE_POP:
1111 heap_idx >>= 1;
1112 --stack_idx;
1113 break;
1116 checkGLcall("walk_constant_heap_clamped()");
1119 /* Context activation is done by the caller. */
1120 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1121 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1122 unsigned char *stack, unsigned int version)
1124 const struct wined3d_shader_lconst *lconst;
1126 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1127 if (shader->reg_maps.shader_version.major == 1
1128 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1129 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1130 else
1131 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1133 if (!shader->load_local_constsF)
1135 TRACE("No need to load local float constants for this shader.\n");
1136 return;
1139 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1140 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1142 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1144 checkGLcall("glUniform4fv()");
1147 /* Context activation is done by the caller. */
1148 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1149 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1151 unsigned int i;
1152 struct list* ptr;
1154 for (i = 0; constants_set; constants_set >>= 1, ++i)
1156 if (!(constants_set & 1)) continue;
1158 /* We found this uniform name in the program - go ahead and send the data */
1159 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1162 /* Load immediate constants */
1163 ptr = list_head(&shader->constantsI);
1164 while (ptr)
1166 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1167 unsigned int idx = lconst->idx;
1168 const GLint *values = (const GLint *)lconst->value;
1170 /* We found this uniform name in the program - go ahead and send the data */
1171 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1172 ptr = list_next(&shader->constantsI, ptr);
1174 checkGLcall("glUniform4iv()");
1177 /* Context activation is done by the caller. */
1178 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1179 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1181 unsigned int i;
1182 struct list* ptr;
1184 for (i = 0; constants_set; constants_set >>= 1, ++i)
1186 if (!(constants_set & 1)) continue;
1188 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1191 /* Load immediate constants */
1192 ptr = list_head(&shader->constantsB);
1193 while (ptr)
1195 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1196 unsigned int idx = lconst->idx;
1197 const GLint *values = (const GLint *)lconst->value;
1199 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1200 ptr = list_next(&shader->constantsB, ptr);
1202 checkGLcall("glUniform1iv()");
1205 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1207 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1210 /* Context activation is done by the caller (state handler). */
1211 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1212 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1214 struct
1216 float sx, sy;
1218 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1219 UINT fixup = ps->np2_fixup_info->active;
1220 UINT i;
1222 for (i = 0; fixup; fixup >>= 1, ++i)
1224 const struct wined3d_texture *tex = state->textures[i];
1225 unsigned char idx = ps->np2_fixup_info->idx[i];
1227 if (!tex)
1229 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1230 continue;
1233 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1234 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1237 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1240 /* Taken and adapted from Mesa. */
1241 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1243 float pos, neg, t, det;
1244 struct wined3d_matrix temp;
1246 /* Calculate the determinant of upper left 3x3 submatrix and
1247 * determine if the matrix is singular. */
1248 pos = neg = 0.0f;
1249 t = in->_11 * in->_22 * in->_33;
1250 if (t >= 0.0f)
1251 pos += t;
1252 else
1253 neg += t;
1255 t = in->_21 * in->_32 * in->_13;
1256 if (t >= 0.0f)
1257 pos += t;
1258 else
1259 neg += t;
1260 t = in->_31 * in->_12 * in->_23;
1261 if (t >= 0.0f)
1262 pos += t;
1263 else
1264 neg += t;
1266 t = -in->_31 * in->_22 * in->_13;
1267 if (t >= 0.0f)
1268 pos += t;
1269 else
1270 neg += t;
1271 t = -in->_21 * in->_12 * in->_33;
1272 if (t >= 0.0f)
1273 pos += t;
1274 else
1275 neg += t;
1277 t = -in->_11 * in->_32 * in->_23;
1278 if (t >= 0.0f)
1279 pos += t;
1280 else
1281 neg += t;
1283 det = pos + neg;
1285 if (fabsf(det) < 1e-25f)
1286 return FALSE;
1288 det = 1.0f / det;
1289 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1290 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1291 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1292 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1293 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1294 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1295 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1296 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1297 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1299 *out = temp;
1300 return TRUE;
1303 static void swap_rows(float **a, float **b)
1305 float *tmp = *a;
1307 *a = *b;
1308 *b = tmp;
1311 static BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1313 float wtmp[4][8];
1314 float m0, m1, m2, m3, s;
1315 float *r0, *r1, *r2, *r3;
1317 r0 = wtmp[0];
1318 r1 = wtmp[1];
1319 r2 = wtmp[2];
1320 r3 = wtmp[3];
1322 r0[0] = m->_11;
1323 r0[1] = m->_12;
1324 r0[2] = m->_13;
1325 r0[3] = m->_14;
1326 r0[4] = 1.0f;
1327 r0[5] = r0[6] = r0[7] = 0.0f;
1329 r1[0] = m->_21;
1330 r1[1] = m->_22;
1331 r1[2] = m->_23;
1332 r1[3] = m->_24;
1333 r1[5] = 1.0f;
1334 r1[4] = r1[6] = r1[7] = 0.0f;
1336 r2[0] = m->_31;
1337 r2[1] = m->_32;
1338 r2[2] = m->_33;
1339 r2[3] = m->_34;
1340 r2[6] = 1.0f;
1341 r2[4] = r2[5] = r2[7] = 0.0f;
1343 r3[0] = m->_41;
1344 r3[1] = m->_42;
1345 r3[2] = m->_43;
1346 r3[3] = m->_44;
1347 r3[7] = 1.0f;
1348 r3[4] = r3[5] = r3[6] = 0.0f;
1350 /* Choose pivot - or die. */
1351 if (fabsf(r3[0]) > fabsf(r2[0]))
1352 swap_rows(&r3, &r2);
1353 if (fabsf(r2[0]) > fabsf(r1[0]))
1354 swap_rows(&r2, &r1);
1355 if (fabsf(r1[0]) > fabsf(r0[0]))
1356 swap_rows(&r1, &r0);
1357 if (r0[0] == 0.0f)
1358 return FALSE;
1360 /* Eliminate first variable. */
1361 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1362 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1363 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1364 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1365 s = r0[4];
1366 if (s != 0.0f)
1368 r1[4] -= m1 * s;
1369 r2[4] -= m2 * s;
1370 r3[4] -= m3 * s;
1372 s = r0[5];
1373 if (s != 0.0f)
1375 r1[5] -= m1 * s;
1376 r2[5] -= m2 * s;
1377 r3[5] -= m3 * s;
1379 s = r0[6];
1380 if (s != 0.0f)
1382 r1[6] -= m1 * s;
1383 r2[6] -= m2 * s;
1384 r3[6] -= m3 * s;
1386 s = r0[7];
1387 if (s != 0.0f)
1389 r1[7] -= m1 * s;
1390 r2[7] -= m2 * s;
1391 r3[7] -= m3 * s;
1394 /* Choose pivot - or die. */
1395 if (fabsf(r3[1]) > fabsf(r2[1]))
1396 swap_rows(&r3, &r2);
1397 if (fabsf(r2[1]) > fabsf(r1[1]))
1398 swap_rows(&r2, &r1);
1399 if (r1[1] == 0.0f)
1400 return FALSE;
1402 /* Eliminate second variable. */
1403 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1404 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1405 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1406 s = r1[4];
1407 if (s != 0.0f)
1409 r2[4] -= m2 * s;
1410 r3[4] -= m3 * s;
1412 s = r1[5];
1413 if (s != 0.0f)
1415 r2[5] -= m2 * s;
1416 r3[5] -= m3 * s;
1418 s = r1[6];
1419 if (s != 0.0f)
1421 r2[6] -= m2 * s;
1422 r3[6] -= m3 * s;
1424 s = r1[7];
1425 if (s != 0.0f)
1427 r2[7] -= m2 * s;
1428 r3[7] -= m3 * s;
1431 /* Choose pivot - or die. */
1432 if (fabsf(r3[2]) > fabsf(r2[2]))
1433 swap_rows(&r3, &r2);
1434 if (r2[2] == 0.0f)
1435 return FALSE;
1437 /* Eliminate third variable. */
1438 m3 = r3[2] / r2[2];
1439 r3[3] -= m3 * r2[3];
1440 r3[4] -= m3 * r2[4];
1441 r3[5] -= m3 * r2[5];
1442 r3[6] -= m3 * r2[6];
1443 r3[7] -= m3 * r2[7];
1445 /* Last check. */
1446 if (r3[3] == 0.0f)
1447 return FALSE;
1449 /* Back substitute row 3. */
1450 s = 1.0f / r3[3];
1451 r3[4] *= s;
1452 r3[5] *= s;
1453 r3[6] *= s;
1454 r3[7] *= s;
1456 /* Back substitute row 2. */
1457 m2 = r2[3];
1458 s = 1.0f / r2[2];
1459 r2[4] = s * (r2[4] - r3[4] * m2);
1460 r2[5] = s * (r2[5] - r3[5] * m2);
1461 r2[6] = s * (r2[6] - r3[6] * m2);
1462 r2[7] = s * (r2[7] - r3[7] * m2);
1463 m1 = r1[3];
1464 r1[4] -= r3[4] * m1;
1465 r1[5] -= r3[5] * m1;
1466 r1[6] -= r3[6] * m1;
1467 r1[7] -= r3[7] * m1;
1468 m0 = r0[3];
1469 r0[4] -= r3[4] * m0;
1470 r0[5] -= r3[5] * m0;
1471 r0[6] -= r3[6] * m0;
1472 r0[7] -= r3[7] * m0;
1474 /* Back substitute row 1. */
1475 m1 = r1[2];
1476 s = 1.0f / r1[1];
1477 r1[4] = s * (r1[4] - r2[4] * m1);
1478 r1[5] = s * (r1[5] - r2[5] * m1);
1479 r1[6] = s * (r1[6] - r2[6] * m1);
1480 r1[7] = s * (r1[7] - r2[7] * m1);
1481 m0 = r0[2];
1482 r0[4] -= r2[4] * m0;
1483 r0[5] -= r2[5] * m0;
1484 r0[6] -= r2[6] * m0;
1485 r0[7] -= r2[7] * m0;
1487 /* Back substitute row 0. */
1488 m0 = r0[1];
1489 s = 1.0f / r0[0];
1490 r0[4] = s * (r0[4] - r1[4] * m0);
1491 r0[5] = s * (r0[5] - r1[5] * m0);
1492 r0[6] = s * (r0[6] - r1[6] * m0);
1493 r0[7] = s * (r0[7] - r1[7] * m0);
1495 out->_11 = r0[4];
1496 out->_12 = r0[5];
1497 out->_13 = r0[6];
1498 out->_14 = r0[7];
1499 out->_21 = r1[4];
1500 out->_22 = r1[5];
1501 out->_23 = r1[6];
1502 out->_24 = r1[7];
1503 out->_31 = r2[4];
1504 out->_32 = r2[5];
1505 out->_33 = r2[6];
1506 out->_34 = r2[7];
1507 out->_41 = r3[4];
1508 out->_42 = r3[5];
1509 out->_43 = r3[6];
1510 out->_44 = r3[7];
1512 return TRUE;
1515 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1517 struct wined3d_matrix temp;
1518 unsigned int i, j;
1520 for (i = 0; i < 4; ++i)
1521 for (j = 0; j < 4; ++j)
1522 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1524 *out = temp;
1527 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1528 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1530 const struct wined3d_gl_info *gl_info = context->gl_info;
1531 float mat[3 * 3];
1532 struct wined3d_matrix mv;
1533 unsigned int i, j;
1535 if (prog->vs.normal_matrix_location == -1)
1536 return;
1538 get_modelview_matrix(context, state, 0, &mv);
1539 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1540 invert_matrix_3d(&mv, &mv);
1541 else
1542 invert_matrix(&mv, &mv);
1543 /* Tests show that singular modelview matrices are used unchanged as normal
1544 * matrices on D3D3 and older. There seems to be no clearly consistent
1545 * behavior on newer D3D versions so always follow older ddraw behavior. */
1546 for (i = 0; i < 3; ++i)
1547 for (j = 0; j < 3; ++j)
1548 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1550 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1551 checkGLcall("glUniformMatrix3fv");
1554 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1555 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1557 const struct wined3d_gl_info *gl_info = context->gl_info;
1558 struct wined3d_matrix mat;
1560 if (tex >= MAX_TEXTURES)
1561 return;
1562 if (prog->vs.texture_matrix_location[tex] == -1)
1563 return;
1565 get_texture_matrix(context, state, tex, &mat);
1566 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1567 checkGLcall("glUniformMatrix4fv");
1570 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1571 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1573 const struct wined3d_gl_info *gl_info = context->gl_info;
1575 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1577 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1578 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1580 else
1582 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1584 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1586 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1587 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1588 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1589 checkGLcall("setting FFP material uniforms");
1592 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1593 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1595 const struct wined3d_gl_info *gl_info = context->gl_info;
1596 struct wined3d_color color;
1598 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1599 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1600 checkGLcall("glUniform3fv");
1603 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1604 const struct wined3d_matrix *src2)
1606 struct wined3d_vec4 temp;
1608 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1609 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1610 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1611 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1613 *dest = temp;
1616 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1617 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1618 struct glsl_shader_prog_link *prog)
1620 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1621 const struct wined3d_gl_info *gl_info = context->gl_info;
1622 struct wined3d_vec4 vec4;
1624 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1625 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1626 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1628 switch (light_info->OriginalParms.type)
1630 case WINED3D_LIGHT_POINT:
1631 multiply_vector_matrix(&vec4, &light_info->position, view);
1632 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1633 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1634 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1635 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1636 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1637 break;
1639 case WINED3D_LIGHT_SPOT:
1640 multiply_vector_matrix(&vec4, &light_info->position, view);
1641 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1643 multiply_vector_matrix(&vec4, &light_info->direction, view);
1644 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1646 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1647 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1648 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1649 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1650 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1651 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1652 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1653 break;
1655 case WINED3D_LIGHT_DIRECTIONAL:
1656 multiply_vector_matrix(&vec4, &light_info->direction, view);
1657 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1658 break;
1660 case WINED3D_LIGHT_PARALLELPOINT:
1661 multiply_vector_matrix(&vec4, &light_info->position, view);
1662 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1663 break;
1665 default:
1666 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1668 checkGLcall("setting FFP lights uniforms");
1671 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1672 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1674 const struct wined3d_gl_info *gl_info = context->gl_info;
1675 float min, max;
1676 float size, att[3];
1678 get_pointsize_minmax(context, state, &min, &max);
1680 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1681 checkGLcall("glUniform1f");
1682 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1683 checkGLcall("glUniform1f");
1685 get_pointsize(context, state, &size, att);
1687 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1688 checkGLcall("glUniform1f");
1689 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1690 checkGLcall("glUniform1f");
1691 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1692 checkGLcall("glUniform1f");
1693 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1694 checkGLcall("glUniform1f");
1697 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1698 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1700 const struct wined3d_gl_info *gl_info = context->gl_info;
1701 struct wined3d_color color;
1702 float start, end, scale;
1703 union
1705 DWORD d;
1706 float f;
1707 } tmpvalue;
1709 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1710 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1711 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1712 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1713 get_fog_start_end(context, state, &start, &end);
1714 scale = 1.0f / (end - start);
1715 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1716 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1717 checkGLcall("fog emulation uniforms");
1720 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1721 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1723 const struct wined3d_gl_info *gl_info = context->gl_info;
1724 struct wined3d_matrix matrix;
1725 struct wined3d_vec4 plane;
1727 plane = state->clip_planes[index];
1729 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1730 if (!use_vs(state))
1732 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1733 transpose_matrix(&matrix, &matrix);
1734 multiply_vector_matrix(&plane, &plane, &matrix);
1737 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1740 /* Context activation is done by the caller (state handler). */
1741 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1742 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1744 struct wined3d_color float_key[2];
1745 const struct wined3d_texture *texture = state->textures[0];
1747 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1748 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1751 /* Context activation is done by the caller (state handler). */
1752 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1753 const struct wined3d_state *state)
1755 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1756 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1757 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1758 const struct wined3d_gl_info *gl_info = context->gl_info;
1759 struct shader_glsl_priv *priv = shader_priv;
1760 float position_fixup[4];
1761 DWORD update_mask;
1763 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1764 UINT constant_version;
1765 int i;
1767 if (!prog) {
1768 /* No GLSL program set - nothing to do. */
1769 return;
1771 constant_version = prog->constant_version;
1772 update_mask = context->constant_update_mask & prog->constant_update_mask;
1774 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1775 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1776 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1778 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1779 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1780 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1782 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1783 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1784 vshader->reg_maps.boolean_constants);
1786 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1788 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1789 shader_glsl_clip_plane_uniform(context, state, i, prog);
1792 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1793 shader_glsl_pointsize_uniform(context, state, prog);
1795 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1797 shader_get_position_fixup(context, state, position_fixup);
1798 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1799 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1800 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1801 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1802 else
1803 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1804 checkGLcall("glUniform4fv");
1807 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1809 struct wined3d_matrix mat;
1811 get_modelview_matrix(context, state, 0, &mat);
1812 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1813 checkGLcall("glUniformMatrix4fv");
1815 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1818 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1820 struct wined3d_matrix mat;
1822 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1824 if (prog->vs.modelview_matrix_location[i] == -1)
1825 break;
1827 get_modelview_matrix(context, state, i, &mat);
1828 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1829 checkGLcall("glUniformMatrix4fv");
1833 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1835 struct wined3d_matrix projection;
1837 get_projection_matrix(context, state, &projection);
1838 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1839 checkGLcall("glUniformMatrix4fv");
1842 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1844 for (i = 0; i < MAX_TEXTURES; ++i)
1845 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1848 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1849 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1851 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1853 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1854 DWORD point_count = 0;
1855 DWORD spot_count = 0;
1856 DWORD directional_count = 0;
1857 DWORD parallel_point_count = 0;
1859 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1861 if (!state->lights[i])
1862 continue;
1864 switch (state->lights[i]->OriginalParms.type)
1866 case WINED3D_LIGHT_POINT:
1867 ++point_count;
1868 break;
1869 case WINED3D_LIGHT_SPOT:
1870 ++spot_count;
1871 break;
1872 case WINED3D_LIGHT_DIRECTIONAL:
1873 ++directional_count;
1874 break;
1875 case WINED3D_LIGHT_PARALLELPOINT:
1876 ++parallel_point_count;
1877 break;
1878 default:
1879 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1880 break;
1883 point_idx = 0;
1884 spot_idx = point_idx + point_count;
1885 directional_idx = spot_idx + spot_count;
1886 parallel_point_idx = directional_idx + directional_count;
1888 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1889 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1891 const struct wined3d_light_info *light_info = state->lights[i];
1892 unsigned int idx;
1894 if (!light_info)
1895 continue;
1897 switch (light_info->OriginalParms.type)
1899 case WINED3D_LIGHT_POINT:
1900 idx = point_idx++;
1901 break;
1902 case WINED3D_LIGHT_SPOT:
1903 idx = spot_idx++;
1904 break;
1905 case WINED3D_LIGHT_DIRECTIONAL:
1906 idx = directional_idx++;
1907 break;
1908 case WINED3D_LIGHT_PARALLELPOINT:
1909 idx = parallel_point_idx++;
1910 break;
1911 default:
1912 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1913 continue;
1915 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1919 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1920 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1921 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1923 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1924 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1925 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1927 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1928 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1929 pshader->reg_maps.boolean_constants);
1931 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1933 for (i = 0; i < MAX_TEXTURES; ++i)
1935 if (prog->ps.bumpenv_mat_location[i] == -1)
1936 continue;
1938 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1939 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1941 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1943 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1944 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1945 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1946 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1950 checkGLcall("bump env uniforms");
1953 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1955 const struct wined3d_vec4 correction_params =
1957 /* Position is relative to the framebuffer, not the viewport. */
1958 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1959 context->render_offscreen ? 1.0f : -1.0f,
1960 0.0f,
1961 0.0f,
1964 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1967 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1968 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1969 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1970 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1972 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1974 struct wined3d_color color;
1976 if (prog->ps.tex_factor_location != -1)
1978 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1979 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1982 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1983 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1984 else
1985 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1987 for (i = 0; i < MAX_TEXTURES; ++i)
1989 if (prog->ps.tss_constant_location[i] == -1)
1990 continue;
1992 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1993 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1996 checkGLcall("fixed function uniforms");
1999 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
2000 shader_glsl_load_fog_uniform(context, state, prog);
2002 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
2004 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
2006 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
2007 checkGLcall("alpha test emulation uniform");
2010 if (priv->next_constant_version == UINT_MAX)
2012 TRACE("Max constant version reached, resetting to 0.\n");
2013 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
2014 priv->next_constant_version = 1;
2016 else
2018 prog->constant_version = priv->next_constant_version++;
2022 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
2024 struct constant_entry *entries = heap->entries;
2025 unsigned int *positions = heap->positions;
2026 unsigned int heap_idx, parent_idx;
2028 if (!heap->contained[idx])
2030 heap_idx = heap->size++;
2031 heap->contained[idx] = TRUE;
2033 else
2035 heap_idx = positions[idx];
2038 while (heap_idx > 1)
2040 parent_idx = heap_idx >> 1;
2042 if (new_version <= entries[parent_idx].version) break;
2044 entries[heap_idx] = entries[parent_idx];
2045 positions[entries[parent_idx].idx] = heap_idx;
2046 heap_idx = parent_idx;
2049 entries[heap_idx].version = new_version;
2050 entries[heap_idx].idx = idx;
2051 positions[idx] = heap_idx;
2054 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2056 struct shader_glsl_priv *priv = device->shader_priv;
2057 struct constant_heap *heap = &priv->vconst_heap;
2058 UINT i;
2060 for (i = start; i < count + start; ++i)
2062 update_heap_entry(heap, i, priv->next_constant_version);
2066 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2068 struct shader_glsl_priv *priv = device->shader_priv;
2069 struct constant_heap *heap = &priv->pconst_heap;
2070 UINT i;
2072 for (i = start; i < count + start; ++i)
2074 update_heap_entry(heap, i, priv->next_constant_version);
2078 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2080 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2081 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2082 if(shader_major > 3) return ret;
2084 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2085 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2086 return ret;
2089 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2091 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2094 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2096 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2097 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2100 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2102 return shader_glsl_get_version(gl_info) >= 150;
2105 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2107 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2110 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2111 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2113 va_list args;
2114 int ret;
2116 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2117 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2118 for (;;)
2120 va_start(args, format);
2121 ret = shader_vaddline(buffer, format, args);
2122 va_end(args);
2123 if (!ret)
2124 return;
2125 if (!string_buffer_resize(buffer, ret))
2126 return;
2130 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2131 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2133 va_list args;
2134 int ret;
2136 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2137 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2138 for (;;)
2140 va_start(args, format);
2141 ret = shader_vaddline(buffer, format, args);
2142 va_end(args);
2143 if (!ret)
2144 return;
2145 if (!string_buffer_resize(buffer, ret))
2146 return;
2150 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2152 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2155 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2157 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2160 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2162 switch (mode)
2164 case WINED3DSIM_CONSTANT:
2165 return "flat";
2166 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2167 return "noperspective";
2168 default:
2169 FIXME("Unhandled interpolation mode %#x.\n", mode);
2170 case WINED3DSIM_NONE:
2171 case WINED3DSIM_LINEAR:
2172 return "";
2176 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2177 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2179 return wined3d_extract_bits(packed_interpolation_mode,
2180 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2183 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2184 struct wined3d_string_buffer *buffer, unsigned int element_count,
2185 const DWORD *interpolation_mode, BOOL unroll)
2187 enum wined3d_shader_interpolation_mode mode;
2188 unsigned int i;
2190 if (shader_glsl_use_interface_blocks(gl_info))
2192 if (unroll)
2194 shader_addline(buffer, "in shader_in_out {\n");
2195 for (i = 0; i < element_count; ++i)
2197 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2198 shader_addline(buffer, "%s vec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2200 shader_addline(buffer, "} shader_in;\n");
2202 else
2204 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2207 else
2209 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2213 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2214 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2215 const DWORD *interpolation_mode)
2217 enum wined3d_shader_interpolation_mode mode;
2218 unsigned int i;
2220 if (shader_glsl_use_interface_blocks(gl_info))
2222 if (rasterizer_setup)
2224 shader_addline(buffer, "out shader_in_out {\n");
2225 for (i = 0; i < element_count; ++i)
2227 const char *interpolation_qualifiers = "";
2228 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2230 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2231 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2233 shader_addline(buffer, "%s vec4 reg%u;\n", interpolation_qualifiers, i);
2235 shader_addline(buffer, "} shader_out;\n");
2237 else
2239 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2242 else
2244 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2248 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2250 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2253 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2255 switch (primitive_type)
2257 case WINED3D_PT_POINTLIST:
2258 return "points";
2260 case WINED3D_PT_LINELIST:
2261 return "lines";
2263 case WINED3D_PT_LINESTRIP:
2264 return "line_strip";
2266 case WINED3D_PT_TRIANGLELIST:
2267 return "triangles";
2269 case WINED3D_PT_TRIANGLESTRIP:
2270 return "triangle_strip";
2272 case WINED3D_PT_LINELIST_ADJ:
2273 return "lines_adjacency";
2275 case WINED3D_PT_TRIANGLELIST_ADJ:
2276 return "triangles_adjacency";
2278 default:
2279 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2280 return "";
2284 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2286 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2287 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2288 DWORD input_reg_used = shader->u.ps.input_reg_used;
2289 unsigned int i;
2291 if (reg_maps->shader_version.major < 3)
2292 return input_reg_used & (1u << idx);
2294 for (i = 0; i < input_signature->element_count; ++i)
2296 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2298 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2299 continue;
2301 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2302 && input->semantic_idx == idx)
2303 return input_reg_used & (1u << input->register_idx);
2305 return FALSE;
2308 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2309 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2311 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2313 if (version->major >= 4)
2314 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2315 else
2316 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2319 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2320 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2321 unsigned int index)
2323 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2324 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2325 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2326 index, scalar_type, scalar_type, index);
2329 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2330 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2332 unsigned int index = e->register_idx;
2334 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2336 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2337 index);
2338 return;
2340 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2342 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2343 index);
2344 return;
2346 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2347 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2349 if (shader_glsl_use_explicit_attrib_location(gl_info))
2350 shader_addline(buffer, "layout(location = %u) ", index);
2352 switch (e->component_type)
2354 case WINED3D_TYPE_UINT:
2355 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
2356 break;
2357 case WINED3D_TYPE_INT:
2358 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
2359 break;
2361 default:
2362 FIXME("Unhandled type %#x.\n", e->component_type);
2363 /* Fall through. */
2364 case WINED3D_TYPE_UNKNOWN:
2365 case WINED3D_TYPE_FLOAT:
2366 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2367 break;
2371 /** Generate the variable & register declarations for the GLSL output target */
2372 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2373 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2374 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2376 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2377 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2378 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2379 const struct wined3d_gl_info *gl_info = context->gl_info;
2380 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2381 unsigned int uniform_block_base, uniform_block_count;
2382 const struct wined3d_shader_lconst *lconst;
2383 const char *prefix;
2384 unsigned int i;
2385 DWORD map;
2387 prefix = shader_glsl_get_prefix(version->type);
2389 /* Prototype the subroutines */
2390 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2392 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2395 /* Declare the constants (aka uniforms) */
2396 if (shader->limits->constant_float > 0)
2398 unsigned max_constantsF;
2400 /* Unless the shader uses indirect addressing, always declare the
2401 * maximum array size and ignore that we need some uniforms privately.
2402 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2403 * and immediate values, still declare VC[256]. If the shader needs
2404 * more uniforms than we have it won't work in any case. If it uses
2405 * less, the compiler will figure out which uniforms are really used
2406 * and strip them out. This allows a shader to use c255 on a dx9 card,
2407 * as long as it doesn't also use all the other constants.
2409 * If the shader uses indirect addressing the compiler must assume
2410 * that all declared uniforms are used. In this case, declare only the
2411 * amount that we're assured to have.
2413 * Thus we run into problems in these two cases:
2414 * 1) The shader really uses more uniforms than supported.
2415 * 2) The shader uses indirect addressing, less constants than
2416 * supported, but uses a constant index > #supported consts. */
2417 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2419 /* No indirect addressing here. */
2420 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2422 else
2424 if (reg_maps->usesrelconstF)
2426 /* Subtract the other potential uniforms from the max
2427 * available (bools, ints, and 1 row of projection matrix).
2428 * Subtract another uniform for immediate values, which have
2429 * to be loaded via uniform by the driver as well. The shader
2430 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2431 * shader code, so one vec4 should be enough. (Unfortunately
2432 * the Nvidia driver doesn't store 128 and -128 in one float).
2434 * Writing gl_ClipVertex requires one uniform for each
2435 * clipplane as well. */
2436 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2437 if (vs_args->clip_enabled)
2438 max_constantsF -= gl_info->limits.user_clip_distances;
2439 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2440 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2441 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2442 * for now take this into account when calculating the number of available constants
2444 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2445 /* Set by driver quirks in directx.c */
2446 max_constantsF -= gl_info->reserved_glsl_constants;
2448 if (max_constantsF < shader->limits->constant_float)
2450 static unsigned int once;
2452 if (!once++)
2453 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2454 " it may not render correctly.\n");
2455 else
2456 WARN("The hardware does not support enough uniform components to run this shader.\n");
2459 else
2461 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2464 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2465 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2468 /* Always declare the full set of constants, the compiler can remove the
2469 * unused ones because d3d doesn't (yet) support indirect int and bool
2470 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2471 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2472 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2474 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2475 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2477 /* Declare immediate constant buffer */
2478 if (reg_maps->icb)
2479 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2481 /* Declare constant buffers */
2482 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2483 &uniform_block_base, &uniform_block_count);
2484 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2486 if (reg_maps->cb_sizes[i])
2488 shader_addline(buffer, "layout(std140");
2489 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2490 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2491 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2492 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2496 /* Declare texture samplers */
2497 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2499 struct wined3d_shader_sampler_map_entry *entry;
2500 const char *sampler_type_prefix, *sampler_type;
2501 BOOL shadow_sampler, tex_rect;
2503 entry = &reg_maps->sampler_map.entries[i];
2505 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2507 ERR("Invalid resource index %u.\n", entry->resource_idx);
2508 continue;
2511 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2513 case WINED3D_DATA_FLOAT:
2514 case WINED3D_DATA_UNORM:
2515 case WINED3D_DATA_SNORM:
2516 sampler_type_prefix = "";
2517 break;
2519 case WINED3D_DATA_INT:
2520 sampler_type_prefix = "i";
2521 break;
2523 case WINED3D_DATA_UINT:
2524 sampler_type_prefix = "u";
2525 break;
2527 default:
2528 sampler_type_prefix = "";
2529 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2530 break;
2533 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2534 switch (reg_maps->resource_info[entry->resource_idx].type)
2536 case WINED3D_SHADER_RESOURCE_BUFFER:
2537 sampler_type = "samplerBuffer";
2538 break;
2540 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2541 if (shadow_sampler)
2542 sampler_type = "sampler1DShadow";
2543 else
2544 sampler_type = "sampler1D";
2545 break;
2547 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2548 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2549 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2550 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2551 if (shadow_sampler)
2553 if (tex_rect)
2554 sampler_type = "sampler2DRectShadow";
2555 else
2556 sampler_type = "sampler2DShadow";
2558 else
2560 if (tex_rect)
2561 sampler_type = "sampler2DRect";
2562 else
2563 sampler_type = "sampler2D";
2565 break;
2567 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2568 if (shadow_sampler)
2569 FIXME("Unsupported 3D shadow sampler.\n");
2570 sampler_type = "sampler3D";
2571 break;
2573 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2574 if (shadow_sampler)
2575 sampler_type = "samplerCubeShadow";
2576 else
2577 sampler_type = "samplerCube";
2578 break;
2580 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2581 if (shadow_sampler)
2582 sampler_type = "sampler2DArrayShadow";
2583 else
2584 sampler_type = "sampler2DArray";
2585 break;
2587 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2588 if (shadow_sampler)
2589 sampler_type = "samplerCubeArrayShadow";
2590 else
2591 sampler_type = "samplerCubeArray";
2592 break;
2594 default:
2595 sampler_type = "unsupported_sampler";
2596 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2597 break;
2600 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2601 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2602 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2603 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2606 /* Declare images */
2607 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2609 const char *image_type_prefix, *image_type, *read_format;
2611 if (!reg_maps->uav_resource_info[i].type)
2612 continue;
2614 switch (reg_maps->uav_resource_info[i].data_type)
2616 case WINED3D_DATA_FLOAT:
2617 case WINED3D_DATA_UNORM:
2618 case WINED3D_DATA_SNORM:
2619 image_type_prefix = "";
2620 read_format = "r32f";
2621 break;
2623 case WINED3D_DATA_INT:
2624 image_type_prefix = "i";
2625 read_format = "r32i";
2626 break;
2628 case WINED3D_DATA_UINT:
2629 image_type_prefix = "u";
2630 read_format = "r32ui";
2631 break;
2633 default:
2634 image_type_prefix = "";
2635 read_format = "";
2636 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2637 break;
2640 switch (reg_maps->uav_resource_info[i].type)
2642 case WINED3D_SHADER_RESOURCE_BUFFER:
2643 image_type = "imageBuffer";
2644 break;
2646 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2647 image_type = "image2D";
2648 break;
2650 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2651 image_type = "image3D";
2652 break;
2654 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2655 image_type = "image2DArray";
2656 break;
2658 default:
2659 image_type = "unsupported_image";
2660 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2661 break;
2664 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2665 shader_addline(buffer, "layout(binding = %u)\n", i);
2666 if (reg_maps->uav_read_mask & (1u << i))
2667 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2668 read_format, image_type_prefix, image_type, prefix, i);
2669 else
2670 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2671 image_type_prefix, image_type, prefix, i);
2673 if (reg_maps->uav_counter_mask & (1u << i))
2674 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2675 i, prefix, i);
2678 /* Declare address variables */
2679 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2681 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2684 /* Declare output register temporaries */
2685 if (shader->limits->packed_output)
2686 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2688 /* Declare temporary variables */
2689 if (reg_maps->temporary_count)
2691 for (i = 0; i < reg_maps->temporary_count; ++i)
2692 shader_addline(buffer, "vec4 R%u;\n", i);
2694 else if (version->major < 4)
2696 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2698 if (map & 1)
2699 shader_addline(buffer, "vec4 R%u;\n", i);
2703 /* Declare indexable temporary variables */
2704 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2706 if (idx_temp_reg->component_count != 4)
2707 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2708 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2711 /* Declare loop registers aLx */
2712 if (version->major < 4)
2714 for (i = 0; i < reg_maps->loop_depth; ++i)
2716 shader_addline(buffer, "int aL%u;\n", i);
2717 shader_addline(buffer, "int tmpInt%u;\n", i);
2721 /* Temporary variables for matrix operations */
2722 shader_addline(buffer, "vec4 tmp0;\n");
2723 shader_addline(buffer, "vec4 tmp1;\n");
2725 if (!shader->load_local_constsF)
2727 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2729 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2730 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2731 shader_addline(buffer, ";\n");
2736 /* Prototypes */
2737 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2738 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2740 /** Used for opcode modifiers - They multiply the result by the specified amount */
2741 static const char * const shift_glsl_tab[] = {
2742 "", /* 0 (none) */
2743 "2.0 * ", /* 1 (x2) */
2744 "4.0 * ", /* 2 (x4) */
2745 "8.0 * ", /* 3 (x8) */
2746 "16.0 * ", /* 4 (x16) */
2747 "32.0 * ", /* 5 (x32) */
2748 "", /* 6 (x64) */
2749 "", /* 7 (x128) */
2750 "", /* 8 (d256) */
2751 "", /* 9 (d128) */
2752 "", /* 10 (d64) */
2753 "", /* 11 (d32) */
2754 "0.0625 * ", /* 12 (d16) */
2755 "0.125 * ", /* 13 (d8) */
2756 "0.25 * ", /* 14 (d4) */
2757 "0.5 * " /* 15 (d2) */
2760 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2761 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2762 const char *in_reg, const char *in_regswizzle, char *out_str)
2764 switch (src_modifier)
2766 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2767 case WINED3DSPSM_DW:
2768 case WINED3DSPSM_NONE:
2769 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2770 break;
2771 case WINED3DSPSM_NEG:
2772 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2773 break;
2774 case WINED3DSPSM_NOT:
2775 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2776 break;
2777 case WINED3DSPSM_BIAS:
2778 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2779 break;
2780 case WINED3DSPSM_BIASNEG:
2781 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2782 break;
2783 case WINED3DSPSM_SIGN:
2784 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2785 break;
2786 case WINED3DSPSM_SIGNNEG:
2787 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2788 break;
2789 case WINED3DSPSM_COMP:
2790 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2791 break;
2792 case WINED3DSPSM_X2:
2793 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2794 break;
2795 case WINED3DSPSM_X2NEG:
2796 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2797 break;
2798 case WINED3DSPSM_ABS:
2799 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2800 break;
2801 case WINED3DSPSM_ABSNEG:
2802 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2803 break;
2804 default:
2805 FIXME("Unhandled modifier %u\n", src_modifier);
2806 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2810 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2811 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2813 /* The ARB_shading_language_420pack extension allows swizzle operations on
2814 * scalars. */
2815 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2816 sprintf(register_name, "%s", glsl_variable);
2817 else
2818 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2821 /** Writes the GLSL variable name that corresponds to the register that the
2822 * DX opcode parameter is trying to access */
2823 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2824 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2825 const struct wined3d_shader_instruction *ins)
2827 /* oPos, oFog and oPts in D3D */
2828 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2830 const struct wined3d_shader *shader = ins->ctx->shader;
2831 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2832 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2833 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2834 const char *prefix = shader_glsl_get_prefix(version->type);
2835 struct glsl_src_param rel_param0, rel_param1;
2836 char imm_str[4][17];
2838 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2839 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2840 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2841 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2842 *is_color = FALSE;
2844 switch (reg->type)
2846 case WINED3DSPR_TEMP:
2847 sprintf(register_name, "R%u", reg->idx[0].offset);
2848 break;
2850 case WINED3DSPR_INPUT:
2851 case WINED3DSPR_INCONTROLPOINT:
2852 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2854 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2856 if (reg->idx[0].rel_addr)
2857 FIXME("VS3 input registers relative addressing.\n");
2858 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2859 *is_color = TRUE;
2860 if (reg->idx[0].rel_addr)
2862 sprintf(register_name, "%s_in[%s + %u]",
2863 prefix, rel_param0.param_str, reg->idx[0].offset);
2865 else
2867 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2869 break;
2872 if (version->type == WINED3D_SHADER_TYPE_HULL
2873 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2874 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2876 if (reg->idx[0].rel_addr)
2878 if (reg->idx[1].rel_addr)
2879 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2880 rel_param0.param_str, reg->idx[0].offset,
2881 rel_param1.param_str, reg->idx[1].offset);
2882 else
2883 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2884 rel_param0.param_str, reg->idx[0].offset,
2885 reg->idx[1].offset);
2887 else if (reg->idx[1].rel_addr)
2888 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2889 rel_param1.param_str, reg->idx[1].offset);
2890 else
2891 sprintf(register_name, "shader_in[%u].reg[%u]",
2892 reg->idx[0].offset, reg->idx[1].offset);
2893 break;
2896 /* pixel shaders >= 3.0 */
2897 if (version->major >= 3)
2899 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2900 unsigned int in_count = vec4_varyings(version->major, gl_info);
2902 if (reg->idx[0].rel_addr)
2904 /* Removing a + 0 would be an obvious optimization, but
2905 * OS X doesn't see the NOP operation there. */
2906 if (idx)
2908 if (needs_legacy_glsl_syntax(gl_info)
2909 && shader->u.ps.declared_in_count > in_count)
2911 sprintf(register_name,
2912 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2913 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2914 prefix, rel_param0.param_str, idx);
2916 else
2918 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2921 else
2923 if (needs_legacy_glsl_syntax(gl_info)
2924 && shader->u.ps.declared_in_count > in_count)
2926 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2927 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2928 prefix, rel_param0.param_str);
2930 else
2932 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2936 else
2938 if (idx == in_count) sprintf(register_name, "gl_Color");
2939 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2940 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2943 else
2945 if (!reg->idx[0].offset)
2946 strcpy(register_name, "ffp_varying_diffuse");
2947 else
2948 strcpy(register_name, "ffp_varying_specular");
2949 break;
2951 break;
2953 case WINED3DSPR_CONST:
2955 /* Relative addressing */
2956 if (reg->idx[0].rel_addr)
2958 if (wined3d_settings.check_float_constants)
2959 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2960 rel_param0.param_str, reg->idx[0].offset,
2961 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2962 prefix, rel_param0.param_str, reg->idx[0].offset);
2963 else if (reg->idx[0].offset)
2964 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2965 else
2966 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2968 else
2970 if (shader_constant_is_local(shader, reg->idx[0].offset))
2971 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2972 else
2973 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2976 break;
2978 case WINED3DSPR_CONSTINT:
2979 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2980 break;
2982 case WINED3DSPR_CONSTBOOL:
2983 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2984 break;
2986 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2987 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2988 sprintf(register_name, "T%u", reg->idx[0].offset);
2989 else
2990 sprintf(register_name, "A%u", reg->idx[0].offset);
2991 break;
2993 case WINED3DSPR_LOOP:
2994 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2995 break;
2997 case WINED3DSPR_SAMPLER:
2998 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2999 break;
3001 case WINED3DSPR_COLOROUT:
3002 if (reg->idx[0].offset >= gl_info->limits.buffers)
3003 WARN("Write to render target %u, only %d supported.\n",
3004 reg->idx[0].offset, gl_info->limits.buffers);
3006 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
3007 break;
3009 case WINED3DSPR_RASTOUT:
3010 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
3011 break;
3013 case WINED3DSPR_DEPTHOUT:
3014 case WINED3DSPR_DEPTHOUTGE:
3015 case WINED3DSPR_DEPTHOUTLE:
3016 sprintf(register_name, "gl_FragDepth");
3017 break;
3019 case WINED3DSPR_ATTROUT:
3020 if (!reg->idx[0].offset)
3021 sprintf(register_name, "%s_out[8]", prefix);
3022 else
3023 sprintf(register_name, "%s_out[9]", prefix);
3024 break;
3026 case WINED3DSPR_TEXCRDOUT:
3027 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
3028 if (reg->idx[0].rel_addr)
3029 sprintf(register_name, "%s_out[%s + %u]",
3030 prefix, rel_param0.param_str, reg->idx[0].offset);
3031 else
3032 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3033 break;
3035 case WINED3DSPR_MISCTYPE:
3036 if (!reg->idx[0].offset)
3038 /* vPos */
3039 sprintf(register_name, "vpos");
3041 else if (reg->idx[0].offset == 1)
3043 /* Note that gl_FrontFacing is a bool, while vFace is
3044 * a float for which the sign determines front/back */
3045 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3047 else
3049 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3050 sprintf(register_name, "unrecognized_register");
3052 break;
3054 case WINED3DSPR_IMMCONST:
3055 switch (reg->immconst_type)
3057 case WINED3D_IMMCONST_SCALAR:
3058 switch (data_type)
3060 case WINED3D_DATA_FLOAT:
3061 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3062 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3063 else
3064 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3065 break;
3066 case WINED3D_DATA_INT:
3067 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3068 break;
3069 case WINED3D_DATA_RESOURCE:
3070 case WINED3D_DATA_SAMPLER:
3071 case WINED3D_DATA_UINT:
3072 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3073 break;
3074 default:
3075 sprintf(register_name, "<unhandled data type %#x>", data_type);
3076 break;
3078 break;
3080 case WINED3D_IMMCONST_VEC4:
3081 switch (data_type)
3083 case WINED3D_DATA_FLOAT:
3084 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3086 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3087 reg->u.immconst_data[0], reg->u.immconst_data[1],
3088 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3090 else
3092 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3093 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3094 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3095 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3096 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3097 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3099 break;
3100 case WINED3D_DATA_INT:
3101 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3102 reg->u.immconst_data[0], reg->u.immconst_data[1],
3103 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3104 break;
3105 case WINED3D_DATA_RESOURCE:
3106 case WINED3D_DATA_SAMPLER:
3107 case WINED3D_DATA_UINT:
3108 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
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 default:
3113 sprintf(register_name, "<unhandled data type %#x>", data_type);
3114 break;
3116 break;
3118 default:
3119 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3120 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3122 break;
3124 case WINED3DSPR_CONSTBUFFER:
3125 if (reg->idx[1].rel_addr)
3126 sprintf(register_name, "%s_cb%u[%s + %u]",
3127 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3128 else
3129 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3130 break;
3132 case WINED3DSPR_IMMCONSTBUFFER:
3133 if (reg->idx[0].rel_addr)
3134 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3135 else
3136 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3137 break;
3139 case WINED3DSPR_PRIMID:
3140 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3141 sprintf(register_name, "gl_PrimitiveIDIn");
3142 else
3143 sprintf(register_name, "gl_PrimitiveID");
3144 break;
3146 case WINED3DSPR_IDXTEMP:
3147 if (reg->idx[1].rel_addr)
3148 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3149 else
3150 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3151 break;
3153 case WINED3DSPR_LOCALTHREADINDEX:
3154 shader_glsl_fixup_scalar_register_variable(register_name,
3155 "int(gl_LocalInvocationIndex)", gl_info);
3156 break;
3158 case WINED3DSPR_GSINSTID:
3159 case WINED3DSPR_OUTPOINTID:
3160 shader_glsl_fixup_scalar_register_variable(register_name,
3161 "gl_InvocationID", gl_info);
3162 break;
3164 case WINED3DSPR_THREADID:
3165 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3166 break;
3168 case WINED3DSPR_THREADGROUPID:
3169 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3170 break;
3172 case WINED3DSPR_LOCALTHREADID:
3173 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3174 break;
3176 case WINED3DSPR_FORKINSTID:
3177 case WINED3DSPR_JOININSTID:
3178 shader_glsl_fixup_scalar_register_variable(register_name,
3179 "phase_instance_id", gl_info);
3180 break;
3182 case WINED3DSPR_TESSCOORD:
3183 sprintf(register_name, "gl_TessCoord");
3184 break;
3186 case WINED3DSPR_OUTCONTROLPOINT:
3187 if (reg->idx[0].rel_addr)
3189 if (reg->idx[1].rel_addr)
3190 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3191 rel_param0.param_str, reg->idx[0].offset,
3192 rel_param1.param_str, reg->idx[1].offset);
3193 else
3194 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3195 rel_param0.param_str, reg->idx[0].offset,
3196 reg->idx[1].offset);
3198 else if (reg->idx[1].rel_addr)
3200 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3201 reg->idx[0].offset, rel_param1.param_str,
3202 reg->idx[1].offset);
3204 else
3206 sprintf(register_name, "shader_out[%u].reg[%u]",
3207 reg->idx[0].offset, reg->idx[1].offset);
3209 break;
3211 case WINED3DSPR_PATCHCONST:
3212 if (version->type == WINED3D_SHADER_TYPE_HULL)
3213 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3214 else
3215 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3216 break;
3218 default:
3219 FIXME("Unhandled register type %#x.\n", reg->type);
3220 sprintf(register_name, "unrecognized_register");
3221 break;
3225 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3227 *str++ = '.';
3228 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3229 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3230 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3231 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3232 *str = '\0';
3235 /* Get the GLSL write mask for the destination register */
3236 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3238 DWORD mask = param->write_mask;
3240 if (shader_is_scalar(&param->reg))
3242 mask = WINED3DSP_WRITEMASK_0;
3243 *write_mask = '\0';
3245 else
3247 shader_glsl_write_mask_to_str(mask, write_mask);
3250 return mask;
3253 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3255 unsigned int size = 0;
3257 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3258 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3259 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3260 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3262 return size;
3265 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3266 unsigned int component_idx)
3268 /* swizzle bits fields: wwzzyyxx */
3269 return (swizzle >> (2 * component_idx)) & 0x3;
3272 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3274 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3275 * but addressed as "rgba". To fix this we need to swap the register's x
3276 * and z components. */
3277 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3278 unsigned int i;
3280 *str++ = '.';
3281 for (i = 0; i < 4; ++i)
3283 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3284 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3286 *str = '\0';
3289 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3290 BOOL fixup, DWORD mask, char *swizzle_str)
3292 if (shader_is_scalar(&param->reg))
3293 *swizzle_str = '\0';
3294 else
3295 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3298 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3299 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3301 if (dst_data_type == src_data_type)
3303 string_buffer_sprintf(dst_param, "%s", src_param);
3304 return;
3307 if (src_data_type == WINED3D_DATA_FLOAT)
3309 switch (dst_data_type)
3311 case WINED3D_DATA_INT:
3312 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3313 return;
3314 case WINED3D_DATA_RESOURCE:
3315 case WINED3D_DATA_SAMPLER:
3316 case WINED3D_DATA_UINT:
3317 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3318 return;
3319 default:
3320 break;
3324 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3326 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3327 return;
3330 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3332 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3333 return;
3336 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3337 string_buffer_sprintf(dst_param, "%s", src_param);
3340 /* From a given parameter token, generate the corresponding GLSL string.
3341 * Also, return the actual register name and swizzle in case the
3342 * caller needs this information as well. */
3343 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3344 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3345 enum wined3d_data_type data_type)
3347 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3348 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3349 enum wined3d_data_type param_data_type;
3350 BOOL is_color = FALSE;
3351 char swizzle_str[6];
3353 glsl_src->reg_name[0] = '\0';
3354 glsl_src->param_str[0] = '\0';
3355 swizzle_str[0] = '\0';
3357 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ins);
3358 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3360 switch (wined3d_src->reg.type)
3362 case WINED3DSPR_IMMCONST:
3363 param_data_type = data_type;
3364 break;
3365 case WINED3DSPR_FORKINSTID:
3366 case WINED3DSPR_GSINSTID:
3367 case WINED3DSPR_JOININSTID:
3368 case WINED3DSPR_LOCALTHREADID:
3369 case WINED3DSPR_LOCALTHREADINDEX:
3370 case WINED3DSPR_OUTPOINTID:
3371 case WINED3DSPR_PRIMID:
3372 case WINED3DSPR_THREADGROUPID:
3373 case WINED3DSPR_THREADID:
3374 param_data_type = WINED3D_DATA_INT;
3375 break;
3376 default:
3377 param_data_type = WINED3D_DATA_FLOAT;
3378 break;
3381 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3382 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3384 string_buffer_release(priv->string_buffers, reg_name);
3387 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3388 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3390 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3393 /* From a given parameter token, generate the corresponding GLSL string.
3394 * Also, return the actual register name and swizzle in case the
3395 * caller needs this information as well. */
3396 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3397 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3399 BOOL is_color = FALSE;
3401 glsl_dst->mask_str[0] = '\0';
3402 glsl_dst->reg_name[0] = '\0';
3404 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3405 glsl_dst->reg_name, &is_color, ins);
3406 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3409 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3410 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3411 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3412 enum wined3d_data_type data_type)
3414 struct glsl_dst_param glsl_dst;
3415 DWORD mask;
3417 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3419 switch (data_type)
3421 case WINED3D_DATA_FLOAT:
3422 shader_addline(buffer, "%s%s = %s(",
3423 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3424 break;
3425 case WINED3D_DATA_INT:
3426 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3427 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3428 break;
3429 case WINED3D_DATA_RESOURCE:
3430 case WINED3D_DATA_SAMPLER:
3431 case WINED3D_DATA_UINT:
3432 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3433 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3434 break;
3435 default:
3436 FIXME("Unhandled data type %#x.\n", data_type);
3437 shader_addline(buffer, "%s%s = %s(",
3438 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3439 break;
3443 return mask;
3446 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3447 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3449 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3452 /** Process GLSL instruction modifiers */
3453 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3455 struct glsl_dst_param dst_param;
3456 DWORD modifiers;
3458 if (!ins->dst_count) return;
3460 modifiers = ins->dst[0].modifiers;
3461 if (!modifiers) return;
3463 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3465 if (modifiers & WINED3DSPDM_SATURATE)
3467 /* _SAT means to clamp the value of the register to between 0 and 1 */
3468 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3469 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3472 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3474 FIXME("_centroid modifier not handled\n");
3477 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3479 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3483 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3485 switch (op)
3487 case WINED3D_SHADER_REL_OP_GT: return ">";
3488 case WINED3D_SHADER_REL_OP_EQ: return "==";
3489 case WINED3D_SHADER_REL_OP_GE: return ">=";
3490 case WINED3D_SHADER_REL_OP_LT: return "<";
3491 case WINED3D_SHADER_REL_OP_NE: return "!=";
3492 case WINED3D_SHADER_REL_OP_LE: return "<=";
3493 default:
3494 FIXME("Unrecognized operator %#x.\n", op);
3495 return "(\?\?)";
3499 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3501 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3504 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3505 unsigned int *coord_size, unsigned int *deriv_size)
3507 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3508 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3510 *coord_size = resource_type_info[resource_type].coord_size;
3511 *deriv_size = *coord_size;
3512 if (is_array)
3513 --(*deriv_size);
3516 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3517 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3519 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3520 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3521 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3522 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3523 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3524 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3525 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3526 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3527 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3528 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3529 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3530 const char *base = "texture", *type_part = "", *suffix = "";
3531 unsigned int coord_size, deriv_size;
3533 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3535 if (resource_type >= ARRAY_SIZE(resource_type_info))
3537 ERR("Unexpected resource type %#x.\n", resource_type);
3538 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3541 /* Note that there's no such thing as a projected cube texture. */
3542 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3543 projected = FALSE;
3545 if (needs_legacy_glsl_syntax(gl_info))
3547 if (shadow)
3548 base = "shadow";
3550 type_part = resource_type_info[resource_type].type_part;
3551 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3552 type_part = "2DRect";
3553 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3554 FIXME("Unhandled resource type %#x.\n", resource_type);
3556 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3558 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3559 suffix = "ARB";
3560 else
3561 FIXME("Unsupported grad function.\n");
3565 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3567 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3568 if (flags & ~texel_fetch_flags)
3569 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3571 base = "texelFetch";
3572 type_part = "";
3575 sample_function->name = string_buffer_get(priv->string_buffers);
3576 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3577 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3579 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3580 if (shadow)
3581 ++coord_size;
3582 sample_function->offset_size = offset ? deriv_size : 0;
3583 sample_function->coord_mask = (1u << coord_size) - 1;
3584 sample_function->deriv_mask = (1u << deriv_size) - 1;
3585 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3588 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3589 struct glsl_sample_function *sample_function)
3591 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3593 string_buffer_release(priv->string_buffers, sample_function->name);
3596 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3597 BOOL sign_fixup, enum fixup_channel_source channel_source)
3599 switch(channel_source)
3601 case CHANNEL_SOURCE_ZERO:
3602 strcat(arguments, "0.0");
3603 break;
3605 case CHANNEL_SOURCE_ONE:
3606 strcat(arguments, "1.0");
3607 break;
3609 case CHANNEL_SOURCE_X:
3610 strcat(arguments, reg_name);
3611 strcat(arguments, ".x");
3612 break;
3614 case CHANNEL_SOURCE_Y:
3615 strcat(arguments, reg_name);
3616 strcat(arguments, ".y");
3617 break;
3619 case CHANNEL_SOURCE_Z:
3620 strcat(arguments, reg_name);
3621 strcat(arguments, ".z");
3622 break;
3624 case CHANNEL_SOURCE_W:
3625 strcat(arguments, reg_name);
3626 strcat(arguments, ".w");
3627 break;
3629 default:
3630 FIXME("Unhandled channel source %#x\n", channel_source);
3631 strcat(arguments, "undefined");
3632 break;
3635 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3638 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3639 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3641 unsigned int mask_size, remaining;
3642 DWORD fixup_mask = 0;
3643 char arguments[256];
3644 char mask_str[6];
3646 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3647 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3648 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3649 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3650 if (!(mask &= fixup_mask))
3651 return;
3653 if (is_complex_fixup(fixup))
3655 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3656 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3657 return;
3660 shader_glsl_write_mask_to_str(mask, mask_str);
3661 mask_size = shader_glsl_get_write_mask_size(mask);
3663 arguments[0] = '\0';
3664 remaining = mask_size;
3665 if (mask & WINED3DSP_WRITEMASK_0)
3667 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3668 if (--remaining) strcat(arguments, ", ");
3670 if (mask & WINED3DSP_WRITEMASK_1)
3672 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3673 if (--remaining) strcat(arguments, ", ");
3675 if (mask & WINED3DSP_WRITEMASK_2)
3677 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3678 if (--remaining) strcat(arguments, ", ");
3680 if (mask & WINED3DSP_WRITEMASK_3)
3682 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3683 if (--remaining) strcat(arguments, ", ");
3686 if (mask_size > 1)
3687 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3688 else
3689 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3692 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3694 char reg_name[256];
3695 BOOL is_color;
3697 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins);
3698 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3701 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3702 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3703 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3704 const char *coord_reg_fmt, ...)
3706 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3707 char dst_swizzle[6];
3708 struct color_fixup_desc fixup;
3709 BOOL np2_fixup = FALSE;
3710 va_list args;
3711 int ret;
3713 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3715 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3716 * We actually rely on it for vertex shaders and SM4+. */
3717 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3719 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3720 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3722 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3723 np2_fixup = TRUE;
3725 else
3727 fixup = COLOR_FIXUP_IDENTITY;
3730 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3732 if (sample_function->output_single_component)
3733 shader_addline(ins->ctx->buffer, "vec4(");
3735 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3736 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3738 for (;;)
3740 va_start(args, coord_reg_fmt);
3741 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3742 va_end(args);
3743 if (!ret)
3744 break;
3745 if (!string_buffer_resize(ins->ctx->buffer, ret))
3746 break;
3749 if (np2_fixup)
3751 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3752 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3754 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3756 case 1:
3757 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3758 idx >> 1, (idx % 2) ? "z" : "x");
3759 break;
3760 case 2:
3761 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3762 idx >> 1, (idx % 2) ? "zw" : "xy");
3763 break;
3764 case 3:
3765 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3766 idx >> 1, (idx % 2) ? "zw" : "xy");
3767 break;
3768 case 4:
3769 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3770 idx >> 1, (idx % 2) ? "zw" : "xy");
3771 break;
3774 if (dx && dy)
3775 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3776 else if (bias)
3777 shader_addline(ins->ctx->buffer, ", %s", bias);
3778 if (sample_function->offset_size)
3780 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3781 shader_addline(ins->ctx->buffer, ", ");
3782 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3784 shader_addline(ins->ctx->buffer, ")");
3786 if (sample_function->output_single_component)
3787 shader_addline(ins->ctx->buffer, ")");
3789 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3791 if (!is_identity_fixup(fixup))
3792 shader_glsl_color_correction(ins, fixup);
3795 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3797 /* Write the final position.
3799 * OpenGL coordinates specify the center of the pixel while D3D coords
3800 * specify the corner. The offsets are stored in z and w in
3801 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3802 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3803 * a MAD. */
3804 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3805 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3807 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3808 * in utils.c
3810 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3811 * shaders are run before the homogeneous divide, so we have to take the w
3812 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3813 * z = z * 2 - w. */
3814 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3817 /*****************************************************************************
3818 * Begin processing individual instruction opcodes
3819 ****************************************************************************/
3821 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3823 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3824 struct glsl_src_param src0_param;
3825 struct glsl_src_param src1_param;
3826 DWORD write_mask;
3827 const char *op;
3829 /* Determine the GLSL operator to use based on the opcode */
3830 switch (ins->handler_idx)
3832 case WINED3DSIH_ADD: op = "+"; break;
3833 case WINED3DSIH_AND: op = "&"; break;
3834 case WINED3DSIH_DIV: op = "/"; break;
3835 case WINED3DSIH_IADD: op = "+"; break;
3836 case WINED3DSIH_ISHL: op = "<<"; break;
3837 case WINED3DSIH_ISHR: op = ">>"; break;
3838 case WINED3DSIH_MUL: op = "*"; break;
3839 case WINED3DSIH_OR: op = "|"; break;
3840 case WINED3DSIH_SUB: op = "-"; break;
3841 case WINED3DSIH_USHR: op = ">>"; break;
3842 case WINED3DSIH_XOR: op = "^"; break;
3843 default:
3844 op = "<unhandled operator>";
3845 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3846 break;
3849 write_mask = shader_glsl_append_dst(buffer, ins);
3850 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3851 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3852 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3855 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3857 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3858 struct glsl_src_param src0_param;
3859 struct glsl_src_param src1_param;
3860 unsigned int mask_size;
3861 DWORD write_mask;
3862 const char *op;
3864 write_mask = shader_glsl_append_dst(buffer, ins);
3865 mask_size = shader_glsl_get_write_mask_size(write_mask);
3866 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3867 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3869 if (mask_size > 1)
3871 switch (ins->handler_idx)
3873 case WINED3DSIH_EQ: op = "equal"; break;
3874 case WINED3DSIH_IEQ: op = "equal"; break;
3875 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3876 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3877 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3878 case WINED3DSIH_LT: op = "lessThan"; break;
3879 case WINED3DSIH_ILT: op = "lessThan"; break;
3880 case WINED3DSIH_ULT: op = "lessThan"; break;
3881 case WINED3DSIH_NE: op = "notEqual"; break;
3882 case WINED3DSIH_INE: op = "notEqual"; break;
3883 default:
3884 op = "<unhandled operator>";
3885 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3886 break;
3889 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3890 mask_size, op, src0_param.param_str, src1_param.param_str);
3892 else
3894 switch (ins->handler_idx)
3896 case WINED3DSIH_EQ: op = "=="; break;
3897 case WINED3DSIH_IEQ: op = "=="; break;
3898 case WINED3DSIH_GE: op = ">="; break;
3899 case WINED3DSIH_IGE: op = ">="; break;
3900 case WINED3DSIH_UGE: op = ">="; break;
3901 case WINED3DSIH_LT: op = "<"; break;
3902 case WINED3DSIH_ILT: op = "<"; break;
3903 case WINED3DSIH_ULT: op = "<"; break;
3904 case WINED3DSIH_NE: op = "!="; break;
3905 case WINED3DSIH_INE: op = "!="; break;
3906 default:
3907 op = "<unhandled operator>";
3908 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3909 break;
3912 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3913 src0_param.param_str, op, src1_param.param_str);
3917 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3919 struct glsl_src_param src_param;
3920 DWORD write_mask;
3921 const char *op;
3923 switch (ins->handler_idx)
3925 case WINED3DSIH_INEG: op = "-"; break;
3926 case WINED3DSIH_NOT: op = "~"; break;
3927 default:
3928 op = "<unhandled operator>";
3929 ERR("Unhandled opcode %s.\n",
3930 debug_d3dshaderinstructionhandler(ins->handler_idx));
3931 break;
3934 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3935 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3936 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3939 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3941 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3942 struct glsl_src_param src0_param;
3943 struct glsl_src_param src1_param;
3944 DWORD write_mask;
3946 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3947 * If not, we can emulate it. */
3948 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3949 FIXME("64-bit integer multiplies not implemented.\n");
3951 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3953 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3954 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3955 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3957 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3958 src0_param.param_str, src1_param.param_str);
3962 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3964 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3965 struct glsl_src_param src0_param, src1_param;
3966 DWORD write_mask;
3968 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3970 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3972 char dst_mask[6];
3974 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3975 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3976 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3977 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3978 dst_mask, src0_param.param_str, src1_param.param_str);
3980 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3981 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3982 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3983 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3985 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3986 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3988 else
3990 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3991 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3992 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3993 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3996 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3998 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3999 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4000 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4001 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4005 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
4006 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
4008 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4009 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4010 struct glsl_src_param src0_param;
4011 DWORD write_mask;
4013 write_mask = shader_glsl_append_dst(buffer, ins);
4014 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4016 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
4017 * shader versions WINED3DSIO_MOVA is used for this. */
4018 if (ins->ctx->reg_maps->shader_version.major == 1
4019 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
4020 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
4022 /* This is a simple floor() */
4023 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4024 if (mask_size > 1) {
4025 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4026 } else {
4027 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4030 else if (ins->handler_idx == WINED3DSIH_MOVA)
4032 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4034 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4036 if (mask_size > 1)
4037 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4038 else
4039 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4041 else
4043 if (mask_size > 1)
4044 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4045 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4046 else
4047 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4048 src0_param.param_str, src0_param.param_str);
4051 else
4053 shader_addline(buffer, "%s);\n", src0_param.param_str);
4057 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4058 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4060 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4061 struct glsl_src_param src0_param;
4062 struct glsl_src_param src1_param;
4063 DWORD dst_write_mask, src_write_mask;
4064 unsigned int dst_size;
4066 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4067 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4069 /* dp4 works on vec4, dp3 on vec3, etc. */
4070 if (ins->handler_idx == WINED3DSIH_DP4)
4071 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4072 else if (ins->handler_idx == WINED3DSIH_DP3)
4073 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4074 else
4075 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4077 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4078 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4080 if (dst_size > 1) {
4081 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4082 } else {
4083 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4087 /* Note that this instruction has some restrictions. The destination write mask
4088 * can't contain the w component, and the source swizzles have to be .xyzw */
4089 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4091 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4092 struct glsl_src_param src0_param;
4093 struct glsl_src_param src1_param;
4094 char dst_mask[6];
4096 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4097 shader_glsl_append_dst(ins->ctx->buffer, ins);
4098 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4099 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4100 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4103 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4105 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4107 if (!stream)
4108 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4109 else
4110 FIXME("Unhandled primitive stream %u.\n", stream);
4113 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4114 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4115 * GLSL uses the value as-is. */
4116 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4118 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4119 struct glsl_src_param src0_param;
4120 struct glsl_src_param src1_param;
4121 DWORD dst_write_mask;
4122 unsigned int dst_size;
4124 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4125 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4127 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4128 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4130 if (dst_size > 1)
4132 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4133 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4135 else
4137 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4138 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4142 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4143 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4145 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4146 struct glsl_src_param src_param;
4147 const char *instruction;
4148 DWORD write_mask;
4149 unsigned i;
4151 /* Determine the GLSL function to use based on the opcode */
4152 /* TODO: Possibly make this a table for faster lookups */
4153 switch (ins->handler_idx)
4155 case WINED3DSIH_ABS: instruction = "abs"; break;
4156 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4157 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4158 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4159 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4160 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4161 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4162 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4163 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4164 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4165 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4166 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4167 case WINED3DSIH_FRC: instruction = "fract"; break;
4168 case WINED3DSIH_IMAX: instruction = "max"; break;
4169 case WINED3DSIH_IMIN: instruction = "min"; break;
4170 case WINED3DSIH_MAX: instruction = "max"; break;
4171 case WINED3DSIH_MIN: instruction = "min"; break;
4172 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4173 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4174 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4175 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4176 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4177 case WINED3DSIH_UMAX: instruction = "max"; break;
4178 case WINED3DSIH_UMIN: instruction = "min"; break;
4179 default: instruction = "";
4180 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4181 break;
4184 write_mask = shader_glsl_append_dst(buffer, ins);
4186 /* In D3D bits are numbered from the most significant bit. */
4187 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4188 shader_addline(buffer, "31 - ");
4189 shader_addline(buffer, "%s(", instruction);
4191 if (ins->src_count)
4193 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4194 shader_addline(buffer, "%s", src_param.param_str);
4195 for (i = 1; i < ins->src_count; ++i)
4197 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4198 shader_addline(buffer, ", %s", src_param.param_str);
4202 shader_addline(buffer, "));\n");
4205 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4207 struct wined3d_shader_dst_param dst;
4208 struct glsl_src_param src;
4209 DWORD write_mask;
4210 const char *fmt;
4211 unsigned int i;
4213 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4214 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4216 dst = ins->dst[0];
4217 for (i = 0; i < 4; ++i)
4219 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4220 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4221 &dst, dst.reg.data_type)))
4222 continue;
4224 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4225 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4229 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4231 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4232 struct wined3d_shader_dst_param dst;
4233 struct glsl_src_param src[4];
4234 const char *instruction;
4235 BOOL tmp_dst = FALSE;
4236 char mask_char[6];
4237 unsigned int i, j;
4238 DWORD write_mask;
4240 switch (ins->handler_idx)
4242 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4243 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4244 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4245 default:
4246 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4247 return;
4250 for (i = 0; i < ins->src_count; ++i)
4252 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4253 && ins->dst[0].reg.type == ins->src[i].reg.type)
4254 tmp_dst = TRUE;
4257 dst = ins->dst[0];
4258 for (i = 0; i < 4; ++i)
4260 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4261 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4262 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4263 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4264 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4265 continue;
4267 for (j = 0; j < ins->src_count; ++j)
4268 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4269 shader_addline(buffer, "%s(", instruction);
4270 for (j = 0; j < ins->src_count - 2; ++j)
4271 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4272 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4275 if (tmp_dst)
4277 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4278 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4279 shader_addline(buffer, "tmp0%s);\n", mask_char);
4283 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4285 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4287 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4288 struct glsl_src_param src_param;
4289 unsigned int mask_size;
4290 DWORD write_mask;
4291 char dst_mask[6];
4293 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4294 mask_size = shader_glsl_get_write_mask_size(write_mask);
4295 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4297 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4298 src_param.param_str, src_param.param_str);
4299 shader_glsl_append_dst(buffer, ins);
4301 if (mask_size > 1)
4303 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4304 mask_size, src_param.param_str);
4306 else
4308 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4309 src_param.param_str);
4313 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4315 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4316 ins->ctx->reg_maps->shader_version.minor);
4317 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4318 struct glsl_src_param src0_param;
4319 const char *prefix, *suffix;
4320 unsigned int dst_size;
4321 DWORD dst_write_mask;
4323 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4324 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4326 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4327 dst_write_mask = WINED3DSP_WRITEMASK_3;
4329 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4331 switch (ins->handler_idx)
4333 case WINED3DSIH_EXP:
4334 case WINED3DSIH_EXPP:
4335 prefix = "exp2(";
4336 suffix = ")";
4337 break;
4339 case WINED3DSIH_LOG:
4340 case WINED3DSIH_LOGP:
4341 prefix = "log2(abs(";
4342 suffix = "))";
4343 break;
4345 case WINED3DSIH_RCP:
4346 prefix = "1.0 / ";
4347 suffix = "";
4348 break;
4350 case WINED3DSIH_RSQ:
4351 prefix = "inversesqrt(abs(";
4352 suffix = "))";
4353 break;
4355 default:
4356 prefix = "";
4357 suffix = "";
4358 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4359 break;
4362 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4363 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4364 else
4365 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4368 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4369 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4370 * dst.x = 2^(floor(src))
4371 * dst.y = src - floor(src)
4372 * dst.z = 2^src (partial precision is allowed, but optional)
4373 * dst.w = 1.0;
4374 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4375 * dst = 2^src; (partial precision is allowed, but optional)
4377 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4379 if (ins->ctx->reg_maps->shader_version.major < 2)
4381 struct glsl_src_param src_param;
4382 char dst_mask[6];
4384 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4386 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4387 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4388 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4389 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4391 shader_glsl_append_dst(ins->ctx->buffer, ins);
4392 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4393 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4394 return;
4397 shader_glsl_scalar_op(ins);
4400 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4401 const char *vector_constructor, const char *scalar_constructor)
4403 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4404 struct glsl_src_param src_param;
4405 unsigned int mask_size;
4406 DWORD write_mask;
4408 write_mask = shader_glsl_append_dst(buffer, ins);
4409 mask_size = shader_glsl_get_write_mask_size(write_mask);
4410 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4412 if (mask_size > 1)
4413 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4414 else
4415 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4418 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4420 shader_glsl_cast(ins, "ivec", "int");
4423 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4425 shader_glsl_cast(ins, "uvec", "uint");
4428 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4430 shader_glsl_cast(ins, "vec", "float");
4433 /** Process signed comparison opcodes in GLSL. */
4434 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4436 struct glsl_src_param src0_param;
4437 struct glsl_src_param src1_param;
4438 DWORD write_mask;
4439 unsigned int mask_size;
4441 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4442 mask_size = shader_glsl_get_write_mask_size(write_mask);
4443 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4444 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4446 if (mask_size > 1) {
4447 const char *compare;
4449 switch(ins->handler_idx)
4451 case WINED3DSIH_SLT: compare = "lessThan"; break;
4452 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4453 default: compare = "";
4454 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4457 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4458 src0_param.param_str, src1_param.param_str);
4459 } else {
4460 switch(ins->handler_idx)
4462 case WINED3DSIH_SLT:
4463 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4464 * to return 0.0 but step returns 1.0 because step is not < x
4465 * An alternative is a bvec compare padded with an unused second component.
4466 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4467 * issue. Playing with not() is not possible either because not() does not accept
4468 * a scalar.
4470 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4471 src0_param.param_str, src1_param.param_str);
4472 break;
4473 case WINED3DSIH_SGE:
4474 /* Here we can use the step() function and safe a conditional */
4475 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4476 break;
4477 default:
4478 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4484 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4486 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4487 struct wined3d_shader_dst_param dst[2];
4488 struct glsl_src_param src[3];
4489 unsigned int i, j, k;
4490 char mask_char[6];
4491 DWORD write_mask;
4492 BOOL tmp_dst[2];
4494 for (i = 0; i < ins->dst_count; ++i)
4496 tmp_dst[i] = FALSE;
4497 for (j = 0; j < ins->src_count; ++j)
4499 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4500 && ins->dst[i].reg.type == ins->src[j].reg.type)
4501 tmp_dst[i] = TRUE;
4505 dst[0] = ins->dst[0];
4506 dst[1] = ins->dst[1];
4507 for (i = 0; i < 4; ++i)
4509 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4511 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4512 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4513 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4514 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4515 continue;
4517 for (k = 0; k < ARRAY_SIZE(src); ++k)
4518 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4520 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4521 src[0].param_str, src[1].param_str, src[2].param_str);
4525 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4527 if (tmp_dst[i])
4529 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4530 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4531 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4536 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4538 const char *condition_prefix, *condition_suffix;
4539 struct wined3d_shader_dst_param dst;
4540 struct glsl_src_param src0_param;
4541 struct glsl_src_param src1_param;
4542 struct glsl_src_param src2_param;
4543 BOOL temp_destination = FALSE;
4544 DWORD cmp_channel = 0;
4545 unsigned int i, j;
4546 char mask_char[6];
4547 DWORD write_mask;
4549 switch (ins->handler_idx)
4551 case WINED3DSIH_CMP:
4552 condition_prefix = "";
4553 condition_suffix = " >= 0.0";
4554 break;
4556 case WINED3DSIH_CND:
4557 condition_prefix = "";
4558 condition_suffix = " > 0.5";
4559 break;
4561 case WINED3DSIH_MOVC:
4562 condition_prefix = "bool(";
4563 condition_suffix = ")";
4564 break;
4566 default:
4567 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4568 condition_prefix = "<unhandled prefix>";
4569 condition_suffix = "<unhandled suffix>";
4570 break;
4573 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4575 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4576 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4577 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4578 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4580 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4581 condition_prefix, src0_param.param_str, condition_suffix,
4582 src1_param.param_str, src2_param.param_str);
4583 return;
4586 dst = ins->dst[0];
4588 /* Splitting the instruction up in multiple lines imposes a problem:
4589 * The first lines may overwrite source parameters of the following lines.
4590 * Deal with that by using a temporary destination register if needed. */
4591 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4592 && ins->src[0].reg.type == dst.reg.type)
4593 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4594 && ins->src[1].reg.type == dst.reg.type)
4595 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4596 && ins->src[2].reg.type == dst.reg.type))
4597 temp_destination = TRUE;
4599 /* Cycle through all source0 channels. */
4600 for (i = 0; i < 4; ++i)
4602 write_mask = 0;
4603 /* Find the destination channels which use the current source0 channel. */
4604 for (j = 0; j < 4; ++j)
4606 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4608 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4609 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4612 dst.write_mask = ins->dst[0].write_mask & write_mask;
4614 if (temp_destination)
4616 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4617 continue;
4618 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4620 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4621 continue;
4623 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4624 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4625 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4627 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4628 condition_prefix, src0_param.param_str, condition_suffix,
4629 src1_param.param_str, src2_param.param_str);
4632 if (temp_destination)
4634 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4635 shader_glsl_append_dst(ins->ctx->buffer, ins);
4636 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4640 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4641 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4642 * the compare is done per component of src0. */
4643 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4645 struct glsl_src_param src0_param;
4646 struct glsl_src_param src1_param;
4647 struct glsl_src_param src2_param;
4648 DWORD write_mask;
4649 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4650 ins->ctx->reg_maps->shader_version.minor);
4652 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4654 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4655 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4656 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4657 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4659 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4660 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4661 else
4662 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4663 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4664 return;
4667 shader_glsl_conditional_move(ins);
4670 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4671 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4673 struct glsl_src_param src0_param;
4674 struct glsl_src_param src1_param;
4675 struct glsl_src_param src2_param;
4676 DWORD write_mask;
4678 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4679 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4680 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4681 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4682 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4683 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4686 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4687 Vertex shaders to GLSL codes */
4688 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4690 int i;
4691 int nComponents = 0;
4692 struct wined3d_shader_dst_param tmp_dst = {{0}};
4693 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4694 struct wined3d_shader_instruction tmp_ins;
4696 memset(&tmp_ins, 0, sizeof(tmp_ins));
4698 /* Set constants for the temporary argument */
4699 tmp_ins.ctx = ins->ctx;
4700 tmp_ins.dst_count = 1;
4701 tmp_ins.dst = &tmp_dst;
4702 tmp_ins.src_count = 2;
4703 tmp_ins.src = tmp_src;
4705 switch(ins->handler_idx)
4707 case WINED3DSIH_M4x4:
4708 nComponents = 4;
4709 tmp_ins.handler_idx = WINED3DSIH_DP4;
4710 break;
4711 case WINED3DSIH_M4x3:
4712 nComponents = 3;
4713 tmp_ins.handler_idx = WINED3DSIH_DP4;
4714 break;
4715 case WINED3DSIH_M3x4:
4716 nComponents = 4;
4717 tmp_ins.handler_idx = WINED3DSIH_DP3;
4718 break;
4719 case WINED3DSIH_M3x3:
4720 nComponents = 3;
4721 tmp_ins.handler_idx = WINED3DSIH_DP3;
4722 break;
4723 case WINED3DSIH_M3x2:
4724 nComponents = 2;
4725 tmp_ins.handler_idx = WINED3DSIH_DP3;
4726 break;
4727 default:
4728 break;
4731 tmp_dst = ins->dst[0];
4732 tmp_src[0] = ins->src[0];
4733 tmp_src[1] = ins->src[1];
4734 for (i = 0; i < nComponents; ++i)
4736 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4737 shader_glsl_dot(&tmp_ins);
4738 ++tmp_src[1].reg.idx[0].offset;
4743 The LRP instruction performs a component-wise linear interpolation
4744 between the second and third operands using the first operand as the
4745 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4746 This is equivalent to mix(src2, src1, src0);
4748 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4750 struct glsl_src_param src0_param;
4751 struct glsl_src_param src1_param;
4752 struct glsl_src_param src2_param;
4753 DWORD write_mask;
4755 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4757 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4758 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4759 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4761 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4762 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4765 /** Process the WINED3DSIO_LIT instruction in GLSL:
4766 * dst.x = dst.w = 1.0
4767 * dst.y = (src0.x > 0) ? src0.x
4768 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4769 * where src.w is clamped at +- 128
4771 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4773 struct glsl_src_param src0_param;
4774 struct glsl_src_param src1_param;
4775 struct glsl_src_param src3_param;
4776 char dst_mask[6];
4778 shader_glsl_append_dst(ins->ctx->buffer, ins);
4779 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4781 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4782 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4783 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4785 /* The sdk specifies the instruction like this
4786 * dst.x = 1.0;
4787 * if(src.x > 0.0) dst.y = src.x
4788 * else dst.y = 0.0.
4789 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4790 * else dst.z = 0.0;
4791 * dst.w = 1.0;
4792 * (where power = src.w clamped between -128 and 128)
4794 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4795 * dst.x = 1.0 ... No further explanation needed
4796 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4797 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4798 * dst.w = 1.0. ... Nothing fancy.
4800 * So we still have one conditional in there. So do this:
4801 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4803 * 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),
4804 * 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.
4805 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4807 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4808 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4809 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4811 shader_addline(ins->ctx->buffer,
4812 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4813 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4814 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4815 src0_param.param_str, src3_param.param_str, dst_mask);
4818 /** Process the WINED3DSIO_DST instruction in GLSL:
4819 * dst.x = 1.0
4820 * dst.y = src0.x * src0.y
4821 * dst.z = src0.z
4822 * dst.w = src1.w
4824 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4826 struct glsl_src_param src0y_param;
4827 struct glsl_src_param src0z_param;
4828 struct glsl_src_param src1y_param;
4829 struct glsl_src_param src1w_param;
4830 char dst_mask[6];
4832 shader_glsl_append_dst(ins->ctx->buffer, ins);
4833 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4835 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4836 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4837 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4838 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4840 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4841 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4844 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4845 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4846 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4848 * dst.x = cos(src0.?)
4849 * dst.y = sin(src0.?)
4850 * dst.z = dst.z
4851 * dst.w = dst.w
4853 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4855 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4856 struct glsl_src_param src0_param;
4857 DWORD write_mask;
4859 if (ins->ctx->reg_maps->shader_version.major < 4)
4861 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4863 write_mask = shader_glsl_append_dst(buffer, ins);
4864 switch (write_mask)
4866 case WINED3DSP_WRITEMASK_0:
4867 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4868 break;
4870 case WINED3DSP_WRITEMASK_1:
4871 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4872 break;
4874 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4875 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4876 src0_param.param_str, src0_param.param_str);
4877 break;
4879 default:
4880 ERR("Write mask should be .x, .y or .xy\n");
4881 break;
4884 return;
4887 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4890 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4892 char dst_mask[6];
4894 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4895 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4896 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4898 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4899 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4900 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4902 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4903 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4905 else
4907 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4908 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4909 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4912 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4914 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4915 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4916 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4920 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4921 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4922 * generate invalid code
4924 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4926 struct glsl_src_param src0_param;
4927 DWORD write_mask;
4929 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4930 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4932 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4935 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4936 * Start a for() loop where src1.y is the initial value of aL,
4937 * increment aL by src1.z for a total of src1.x iterations.
4938 * Need to use a temporary variable for this operation.
4940 /* FIXME: I don't think nested loops will work correctly this way. */
4941 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4943 struct wined3d_shader_parser_state *state = ins->ctx->state;
4944 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4945 const struct wined3d_shader *shader = ins->ctx->shader;
4946 const struct wined3d_shader_lconst *constant;
4947 struct glsl_src_param src1_param;
4948 const DWORD *control_values = NULL;
4950 if (ins->ctx->reg_maps->shader_version.major < 4)
4952 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4954 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4955 * class hardware doesn't support real varying indexing, but Microsoft
4956 * designed this feature for Shader model 2.x+. If the loop control is
4957 * known at compile time, the GLSL compiler can unroll the loop, and
4958 * replace indirect addressing with direct addressing. */
4959 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4961 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4963 if (constant->idx == ins->src[1].reg.idx[0].offset)
4965 control_values = constant->value;
4966 break;
4971 if (control_values)
4973 struct wined3d_shader_loop_control loop_control;
4974 loop_control.count = control_values[0];
4975 loop_control.start = control_values[1];
4976 loop_control.step = (int)control_values[2];
4978 if (loop_control.step > 0)
4980 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4981 state->current_loop_depth, loop_control.start,
4982 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4983 state->current_loop_depth, loop_control.step);
4985 else 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
4994 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4995 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4996 state->current_loop_depth, loop_control.count,
4997 state->current_loop_depth);
5000 else
5002 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
5003 state->current_loop_depth, state->current_loop_reg,
5004 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
5005 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
5008 ++state->current_loop_reg;
5010 else
5012 shader_addline(buffer, "for (;;)\n{\n");
5015 ++state->current_loop_depth;
5018 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
5020 struct wined3d_shader_parser_state *state = ins->ctx->state;
5022 shader_addline(ins->ctx->buffer, "}\n");
5024 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5026 --state->current_loop_depth;
5027 --state->current_loop_reg;
5030 if (ins->handler_idx == WINED3DSIH_ENDREP)
5032 --state->current_loop_depth;
5036 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5038 struct wined3d_shader_parser_state *state = ins->ctx->state;
5039 const struct wined3d_shader *shader = ins->ctx->shader;
5040 const struct wined3d_shader_lconst *constant;
5041 struct glsl_src_param src0_param;
5042 const DWORD *control_values = NULL;
5044 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5045 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5047 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5049 if (constant->idx == ins->src[0].reg.idx[0].offset)
5051 control_values = constant->value;
5052 break;
5057 if (control_values)
5059 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5060 state->current_loop_depth, state->current_loop_depth,
5061 control_values[0], state->current_loop_depth);
5063 else
5065 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5066 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5067 state->current_loop_depth, state->current_loop_depth,
5068 src0_param.param_str, state->current_loop_depth);
5071 ++state->current_loop_depth;
5074 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5076 struct glsl_src_param src0_param;
5078 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5079 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5082 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5084 struct glsl_src_param src0_param;
5086 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5087 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5090 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5092 shader_addline(ins->ctx->buffer, "default:\n");
5095 static void shader_glsl_generate_conditional_op(const struct wined3d_shader_instruction *ins,
5096 const char *op)
5098 struct glsl_src_param src_param;
5099 const char *condition;
5101 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5102 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5103 shader_addline(ins->ctx->buffer, "if (%s(%s)) %s\n", condition, src_param.param_str, op);
5106 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5108 shader_glsl_generate_conditional_op(ins, "{");
5111 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5113 struct glsl_src_param src0_param;
5114 struct glsl_src_param src1_param;
5116 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5117 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5119 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5120 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5123 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5125 shader_addline(ins->ctx->buffer, "} else {\n");
5128 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5130 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5132 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5133 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5134 shader_glsl_fixup_position(ins->ctx->buffer);
5136 if (!stream)
5137 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5138 else
5139 FIXME("Unhandled primitive stream %u.\n", stream);
5142 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5144 shader_addline(ins->ctx->buffer, "break;\n");
5147 /* FIXME: According to MSDN the compare is done per component. */
5148 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5150 struct glsl_src_param src0_param;
5151 struct glsl_src_param src1_param;
5153 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5154 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5156 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5157 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5160 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5162 const char *op;
5164 switch (ins->handler_idx)
5166 case WINED3DSIH_BREAKP: op = "break;"; break;
5167 case WINED3DSIH_CONTINUEP: op = "continue;"; break;
5168 case WINED3DSIH_RETP: op = "return;"; break;
5169 default:
5170 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5171 return;
5174 shader_glsl_generate_conditional_op(ins, op);
5177 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5179 shader_addline(ins->ctx->buffer, "continue;\n");
5182 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5184 shader_addline(ins->ctx->buffer, "}\n");
5185 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5187 /* Subroutines appear at the end of the shader. */
5188 ins->ctx->state->in_subroutine = TRUE;
5191 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5193 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5196 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5198 struct glsl_src_param src1_param;
5200 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5201 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5202 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5205 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5207 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5209 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5211 shader_glsl_generate_shader_epilogue(ins->ctx);
5212 shader_addline(ins->ctx->buffer, "return;\n");
5216 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5218 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5219 ins->ctx->reg_maps->shader_version.minor);
5220 struct glsl_sample_function sample_function;
5221 DWORD sample_flags = 0;
5222 DWORD resource_idx;
5223 DWORD mask = 0, swizzle;
5224 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5226 /* 1.0-1.4: Use destination register as sampler source.
5227 * 2.0+: Use provided sampler source. */
5228 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5229 resource_idx = ins->dst[0].reg.idx[0].offset;
5230 else
5231 resource_idx = ins->src[1].reg.idx[0].offset;
5233 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5235 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5236 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5237 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5239 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5240 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5242 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5243 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5245 case WINED3D_TTFF_COUNT1:
5246 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5247 break;
5248 case WINED3D_TTFF_COUNT2:
5249 mask = WINED3DSP_WRITEMASK_1;
5250 break;
5251 case WINED3D_TTFF_COUNT3:
5252 mask = WINED3DSP_WRITEMASK_2;
5253 break;
5254 case WINED3D_TTFF_COUNT4:
5255 case WINED3D_TTFF_DISABLE:
5256 mask = WINED3DSP_WRITEMASK_3;
5257 break;
5261 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5263 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5265 if (src_mod == WINED3DSPSM_DZ) {
5266 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5267 mask = WINED3DSP_WRITEMASK_2;
5268 } else if (src_mod == WINED3DSPSM_DW) {
5269 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5270 mask = WINED3DSP_WRITEMASK_3;
5273 else
5275 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5276 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5278 /* ps 2.0 texldp instruction always divides by the fourth component. */
5279 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5280 mask = WINED3DSP_WRITEMASK_3;
5284 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5285 mask |= sample_function.coord_mask;
5286 sample_function.coord_mask = mask;
5288 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5289 else swizzle = ins->src[1].swizzle;
5291 /* 1.0-1.3: Use destination register as coordinate source.
5292 1.4+: Use provided coordinate source register. */
5293 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5295 char coord_mask[6];
5296 shader_glsl_write_mask_to_str(mask, coord_mask);
5297 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5298 "T%u%s", resource_idx, coord_mask);
5300 else
5302 struct glsl_src_param coord_param;
5303 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5304 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5306 struct glsl_src_param bias;
5307 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5308 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5309 NULL, "%s", coord_param.param_str);
5310 } else {
5311 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5312 "%s", coord_param.param_str);
5315 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5318 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5320 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5321 struct glsl_src_param coord_param, dx_param, dy_param;
5322 struct glsl_sample_function sample_function;
5323 DWORD sampler_idx;
5324 DWORD swizzle = ins->src[1].swizzle;
5326 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5328 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5329 shader_glsl_tex(ins);
5330 return;
5333 sampler_idx = ins->src[1].reg.idx[0].offset;
5335 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5336 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5337 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5338 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5340 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5341 NULL, NULL, "%s", coord_param.param_str);
5342 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5345 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5347 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5348 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5349 struct glsl_src_param coord_param, lod_param;
5350 struct glsl_sample_function sample_function;
5351 DWORD swizzle = ins->src[1].swizzle;
5352 DWORD sampler_idx;
5354 sampler_idx = ins->src[1].reg.idx[0].offset;
5356 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5357 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5359 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5361 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5362 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5364 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5365 * However, the NVIDIA drivers allow them in fragment shaders as well,
5366 * even without the appropriate extension. */
5367 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5369 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5370 "%s", coord_param.param_str);
5371 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5374 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5375 unsigned int resource_idx, unsigned int sampler_idx)
5377 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5378 unsigned int i;
5380 for (i = 0; i < sampler_map->count; ++i)
5382 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5383 return entries[i].bind_idx;
5386 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5388 return ~0u;
5391 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5393 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5394 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5395 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5396 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5397 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5398 struct glsl_src_param structure_idx, offset, data, data2;
5399 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5400 enum wined3d_shader_resource_type resource_type;
5401 struct wined3d_string_buffer *address;
5402 enum wined3d_data_type data_type;
5403 unsigned int resource_idx, stride;
5404 const char *op, *resource;
5405 DWORD coord_mask;
5406 BOOL is_tgsm;
5408 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5409 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5410 if (is_tgsm)
5412 if (resource_idx >= reg_maps->tgsm_count)
5414 ERR("Invalid TGSM index %u.\n", resource_idx);
5415 return;
5417 resource = "g";
5418 data_type = WINED3D_DATA_UINT;
5419 coord_mask = 1;
5420 stride = reg_maps->tgsm[resource_idx].stride;
5422 else
5424 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5426 ERR("Invalid UAV index %u.\n", resource_idx);
5427 return;
5429 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5430 if (resource_type >= ARRAY_SIZE(resource_type_info))
5432 ERR("Unexpected resource type %#x.\n", resource_type);
5433 return;
5435 resource = "image";
5436 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5437 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5438 stride = reg_maps->uav_resource_info[resource_idx].stride;
5441 switch (ins->handler_idx)
5443 case WINED3DSIH_ATOMIC_AND:
5444 case WINED3DSIH_IMM_ATOMIC_AND:
5445 if (is_tgsm)
5446 op = "atomicAnd";
5447 else
5448 op = "imageAtomicAnd";
5449 break;
5450 case WINED3DSIH_ATOMIC_CMP_STORE:
5451 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5452 if (is_tgsm)
5453 op = "atomicCompSwap";
5454 else
5455 op = "imageAtomicCompSwap";
5456 break;
5457 case WINED3DSIH_ATOMIC_IADD:
5458 case WINED3DSIH_IMM_ATOMIC_IADD:
5459 if (is_tgsm)
5460 op = "atomicAdd";
5461 else
5462 op = "imageAtomicAdd";
5463 break;
5464 case WINED3DSIH_ATOMIC_IMAX:
5465 case WINED3DSIH_IMM_ATOMIC_IMAX:
5466 if (is_tgsm)
5467 op = "atomicMax";
5468 else
5469 op = "imageAtomicMax";
5470 if (data_type != WINED3D_DATA_INT)
5472 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5473 return;
5475 break;
5476 case WINED3DSIH_ATOMIC_IMIN:
5477 case WINED3DSIH_IMM_ATOMIC_IMIN:
5478 if (is_tgsm)
5479 op = "atomicMin";
5480 else
5481 op = "imageAtomicMin";
5482 if (data_type != WINED3D_DATA_INT)
5484 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5485 return;
5487 break;
5488 case WINED3DSIH_ATOMIC_OR:
5489 case WINED3DSIH_IMM_ATOMIC_OR:
5490 if (is_tgsm)
5491 op = "atomicOr";
5492 else
5493 op = "imageAtomicOr";
5494 break;
5495 case WINED3DSIH_ATOMIC_UMAX:
5496 case WINED3DSIH_IMM_ATOMIC_UMAX:
5497 if (is_tgsm)
5498 op = "atomicMax";
5499 else
5500 op = "imageAtomicMax";
5501 if (data_type != WINED3D_DATA_UINT)
5503 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5504 return;
5506 break;
5507 case WINED3DSIH_ATOMIC_UMIN:
5508 case WINED3DSIH_IMM_ATOMIC_UMIN:
5509 if (is_tgsm)
5510 op = "atomicMin";
5511 else
5512 op = "imageAtomicMin";
5513 if (data_type != WINED3D_DATA_UINT)
5515 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5516 return;
5518 break;
5519 case WINED3DSIH_ATOMIC_XOR:
5520 case WINED3DSIH_IMM_ATOMIC_XOR:
5521 if (is_tgsm)
5522 op = "atomicXor";
5523 else
5524 op = "imageAtomicXor";
5525 break;
5526 case WINED3DSIH_IMM_ATOMIC_EXCH:
5527 if (is_tgsm)
5528 op = "atomicExchange";
5529 else
5530 op = "imageAtomicExchange";
5531 break;
5532 default:
5533 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5534 return;
5537 address = string_buffer_get(priv->string_buffers);
5538 if (stride)
5540 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5541 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5542 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5544 else
5546 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5547 string_buffer_sprintf(address, "%s", offset.param_str);
5548 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5549 shader_addline(address, "/ 4");
5552 if (is_imm_instruction)
5553 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5555 if (is_tgsm)
5556 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5557 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5558 else
5559 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5560 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5562 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5563 shader_addline(buffer, "%s", data.param_str);
5564 if (ins->src_count >= 3)
5566 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5567 shader_addline(buffer, ", %s", data2.param_str);
5570 if (is_imm_instruction)
5571 shader_addline(buffer, ")");
5572 shader_addline(buffer, ");\n");
5574 string_buffer_release(priv->string_buffers, address);
5577 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5579 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5580 const char *op;
5582 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5583 op = "atomicCounterIncrement";
5584 else
5585 op = "atomicCounterDecrement";
5587 shader_glsl_append_dst(ins->ctx->buffer, ins);
5588 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5591 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5593 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5594 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5595 enum wined3d_shader_resource_type resource_type;
5596 struct glsl_src_param image_coord_param;
5597 enum wined3d_data_type data_type;
5598 DWORD coord_mask, write_mask;
5599 unsigned int uav_idx;
5600 char dst_swizzle[6];
5602 uav_idx = ins->src[1].reg.idx[0].offset;
5603 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5605 ERR("Invalid UAV index %u.\n", uav_idx);
5606 return;
5608 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5609 if (resource_type >= ARRAY_SIZE(resource_type_info))
5611 ERR("Unexpected resource type %#x.\n", resource_type);
5612 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5614 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5615 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5617 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5618 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5620 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5621 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5622 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5625 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5627 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5628 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5629 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5630 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5631 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5632 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5633 struct glsl_src_param structure_idx, offset;
5634 struct wined3d_string_buffer *address;
5635 struct wined3d_shader_dst_param dst;
5636 const char *function, *resource;
5638 resource_idx = src->reg.idx[0].offset;
5639 if (src->reg.type == WINED3DSPR_RESOURCE)
5641 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5643 ERR("Invalid resource index %u.\n", resource_idx);
5644 return;
5646 stride = reg_maps->resource_info[resource_idx].stride;
5647 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5648 function = "texelFetch";
5649 resource = "sampler";
5651 else if (src->reg.type == WINED3DSPR_UAV)
5653 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5655 ERR("Invalid UAV index %u.\n", resource_idx);
5656 return;
5658 stride = reg_maps->uav_resource_info[resource_idx].stride;
5659 bind_idx = resource_idx;
5660 function = "imageLoad";
5661 resource = "image";
5663 else
5665 if (resource_idx >= reg_maps->tgsm_count)
5667 ERR("Invalid TGSM index %u.\n", resource_idx);
5668 return;
5670 stride = reg_maps->tgsm[resource_idx].stride;
5671 bind_idx = resource_idx;
5672 function = NULL;
5673 resource = "g";
5676 address = string_buffer_get(priv->string_buffers);
5677 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5679 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5680 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5682 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5683 shader_addline(address, "%s / 4", offset.param_str);
5685 dst = ins->dst[0];
5686 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5688 /* The instruction is split into multiple lines. The first lines may
5689 * overwrite source parameters of the following lines. */
5690 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5691 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5694 for (i = 0; i < 4; ++i)
5696 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5697 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5698 continue;
5700 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5701 if (function)
5702 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5703 function, prefix, resource, bind_idx, address->buffer, swizzle);
5704 else
5705 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5706 prefix, resource, bind_idx, address->buffer, swizzle);
5709 string_buffer_release(priv->string_buffers, address);
5712 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5714 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5715 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5716 struct glsl_src_param image_coord_param, image_data_param;
5717 enum wined3d_shader_resource_type resource_type;
5718 enum wined3d_data_type data_type;
5719 unsigned int uav_idx;
5720 DWORD coord_mask;
5722 uav_idx = ins->dst[0].reg.idx[0].offset;
5723 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5725 ERR("Invalid UAV index %u.\n", uav_idx);
5726 return;
5728 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5729 if (resource_type >= ARRAY_SIZE(resource_type_info))
5731 ERR("Unexpected resource type %#x.\n", resource_type);
5732 return;
5734 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5735 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5737 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5738 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5739 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5740 shader_glsl_get_prefix(version->type), uav_idx,
5741 image_coord_param.param_str, image_data_param.param_str);
5744 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5746 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5747 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5748 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5749 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5750 struct glsl_src_param structure_idx, offset, data;
5751 unsigned int i, resource_idx, stride, src_idx = 0;
5752 struct wined3d_string_buffer *address;
5753 DWORD write_mask;
5754 BOOL is_tgsm;
5756 resource_idx = ins->dst[0].reg.idx[0].offset;
5757 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5758 if (is_tgsm)
5760 if (resource_idx >= reg_maps->tgsm_count)
5762 ERR("Invalid TGSM index %u.\n", resource_idx);
5763 return;
5765 stride = reg_maps->tgsm[resource_idx].stride;
5767 else
5769 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5771 ERR("Invalid UAV index %u.\n", resource_idx);
5772 return;
5774 stride = reg_maps->uav_resource_info[resource_idx].stride;
5777 address = string_buffer_get(priv->string_buffers);
5778 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5780 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5781 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5783 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5784 shader_addline(address, "%s / 4", offset.param_str);
5786 for (i = 0; i < 4; ++i)
5788 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5789 continue;
5791 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5793 if (is_tgsm)
5794 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5795 prefix, resource_idx, address->buffer, i, data.param_str);
5796 else
5797 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5798 prefix, resource_idx, address->buffer, i, data.param_str);
5801 string_buffer_release(priv->string_buffers, address);
5804 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5806 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5807 unsigned int sync_flags = ins->flags;
5809 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5811 shader_addline(buffer, "barrier();\n");
5812 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5815 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5817 shader_addline(buffer, "memoryBarrierShared();\n");
5818 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5821 if (sync_flags)
5822 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5825 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5826 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5828 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5829 unsigned int idx = reg->idx[0].offset;
5831 if (reg->type == WINED3DSPR_RESOURCE)
5833 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5835 ERR("Invalid resource index %u.\n", idx);
5836 return NULL;
5838 return &reg_maps->resource_info[idx];
5841 if (reg->type == WINED3DSPR_UAV)
5843 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5845 ERR("Invalid UAV index %u.\n", idx);
5846 return NULL;
5848 return &reg_maps->uav_resource_info[idx];
5851 FIXME("Unhandled register type %#x.\n", reg->type);
5852 return NULL;
5855 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5857 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5858 const struct wined3d_shader_resource_info *resource_info;
5859 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5860 unsigned int resource_idx;
5861 char dst_swizzle[6];
5862 DWORD write_mask;
5864 write_mask = shader_glsl_append_dst(buffer, ins);
5865 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5867 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5868 return;
5869 resource_idx = ins->src[0].reg.idx[0].offset;
5871 shader_addline(buffer, "ivec2(");
5872 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5874 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5875 resource_idx, WINED3D_SAMPLER_DEFAULT);
5876 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5878 else
5880 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5882 if (resource_info->stride)
5883 shader_addline(buffer, " / %u", resource_info->stride);
5884 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5885 shader_addline(buffer, " * 4");
5886 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5889 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5891 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5892 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5893 enum wined3d_shader_resource_type resource_type;
5894 enum wined3d_shader_register_type reg_type;
5895 unsigned int resource_idx, bind_idx, i;
5896 enum wined3d_data_type dst_data_type;
5897 struct glsl_src_param lod_param;
5898 char dst_swizzle[6];
5899 DWORD write_mask;
5901 dst_data_type = ins->dst[0].reg.data_type;
5902 if (ins->flags == WINED3DSI_RESINFO_UINT)
5903 dst_data_type = WINED3D_DATA_UINT;
5904 else if (ins->flags)
5905 FIXME("Unhandled flags %#x.\n", ins->flags);
5907 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5908 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5910 reg_type = ins->src[1].reg.type;
5911 resource_idx = ins->src[1].reg.idx[0].offset;
5912 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5913 if (reg_type == WINED3DSPR_RESOURCE)
5915 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5916 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5917 resource_idx, WINED3D_SAMPLER_DEFAULT);
5919 else
5921 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5922 bind_idx = resource_idx;
5925 if (resource_type >= ARRAY_SIZE(resource_type_info))
5927 ERR("Unexpected resource type %#x.\n", resource_type);
5928 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5931 if (dst_data_type == WINED3D_DATA_UINT)
5932 shader_addline(ins->ctx->buffer, "uvec4(");
5933 else
5934 shader_addline(ins->ctx->buffer, "vec4(");
5936 if (reg_type == WINED3DSPR_RESOURCE)
5938 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5939 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5941 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5942 shader_addline(ins->ctx->buffer, "0, ");
5944 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5946 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5947 shader_glsl_get_prefix(version->type), bind_idx);
5949 else
5951 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5952 shader_addline(ins->ctx->buffer, "1");
5955 else
5957 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5958 shader_glsl_get_prefix(version->type), bind_idx);
5960 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5961 shader_addline(ins->ctx->buffer, "0, ");
5963 /* For UAVs the returned miplevel count is always 1. */
5964 shader_addline(ins->ctx->buffer, "1");
5967 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5970 /* FIXME: The current implementation does not handle multisample textures correctly. */
5971 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5973 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5974 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5975 struct glsl_src_param coord_param, lod_param;
5976 struct glsl_sample_function sample_function;
5977 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5978 BOOL has_lod_param;
5980 if (wined3d_shader_instruction_has_texel_offset(ins))
5981 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5983 resource_idx = ins->src[1].reg.idx[0].offset;
5984 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5986 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5988 ERR("Invalid resource index %u.\n", resource_idx);
5989 return;
5991 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5993 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5994 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5995 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5996 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5997 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5998 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5999 "%s", coord_param.param_str);
6000 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6003 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6005 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6006 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6007 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6008 struct glsl_sample_function sample_function;
6009 DWORD flags = 0;
6011 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6012 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6013 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6014 flags |= WINED3D_GLSL_SAMPLE_LOD;
6015 if (wined3d_shader_instruction_has_texel_offset(ins))
6016 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6018 resource_idx = ins->src[1].reg.idx[0].offset;
6019 sampler_idx = ins->src[2].reg.idx[0].offset;
6021 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6022 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6024 switch (ins->handler_idx)
6026 case WINED3DSIH_SAMPLE:
6027 break;
6028 case WINED3DSIH_SAMPLE_B:
6029 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6030 lod_param_str = lod_param.param_str;
6031 break;
6032 case WINED3DSIH_SAMPLE_GRAD:
6033 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6034 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6035 dx_param_str = dx_param.param_str;
6036 dy_param_str = dy_param.param_str;
6037 break;
6038 case WINED3DSIH_SAMPLE_LOD:
6039 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6040 lod_param_str = lod_param.param_str;
6041 break;
6042 default:
6043 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6044 break;
6047 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6048 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6049 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6050 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6053 /* GLSL doesn't provide a function to sample from level zero with depth
6054 * comparison for array textures and cube textures. We use textureGrad*()
6055 * to implement sample_c_lz.
6057 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6058 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6059 unsigned int coord_size, const char *coord_param, const char *ref_param)
6061 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6062 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6063 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6064 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6065 char dst_swizzle[6];
6067 WARN("Emitting textureGrad() for sample_c_lz.\n");
6069 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6070 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6071 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6072 sample_function->offset_size ? "Offset" : "",
6073 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6074 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6075 if (sample_function->offset_size)
6077 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6078 shader_addline(buffer, ", ");
6079 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6081 shader_addline(buffer, "))%s);\n", dst_swizzle);
6084 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6086 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6087 const struct wined3d_shader_resource_info *resource_info;
6088 struct glsl_src_param coord_param, compare_param;
6089 struct glsl_sample_function sample_function;
6090 const char *lod_param = NULL;
6091 unsigned int coord_size;
6092 DWORD flags = 0;
6094 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6096 lod_param = "0";
6097 flags |= WINED3D_GLSL_SAMPLE_LOD;
6100 if (wined3d_shader_instruction_has_texel_offset(ins))
6101 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6103 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6104 return;
6105 resource_idx = ins->src[1].reg.idx[0].offset;
6106 sampler_idx = ins->src[2].reg.idx[0].offset;
6108 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6109 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6110 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6111 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6112 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6113 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6114 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6115 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6117 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6118 coord_size, coord_param.param_str, compare_param.param_str);
6120 else
6122 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6123 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6124 coord_size, coord_param.param_str, compare_param.param_str);
6126 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6129 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6131 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6132 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6133 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6134 struct glsl_src_param coord_param, compare_param, offset_param;
6135 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6136 const struct wined3d_shader_resource_info *resource_info;
6137 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6138 unsigned int coord_size, offset_size;
6139 char dst_swizzle[6];
6140 BOOL has_offset;
6142 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6144 FIXME("OpenGL implementation does not support textureGather.\n");
6145 return;
6148 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6149 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6150 || wined3d_shader_instruction_has_texel_offset(ins);
6152 resource_param_idx =
6153 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6154 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6155 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6156 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6157 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6159 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6160 return;
6162 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6164 ERR("Unexpected resource type %#x.\n", resource_info->type);
6165 return;
6167 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6169 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6170 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6172 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6174 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6175 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6176 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6178 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6179 shader_addline(buffer, ", %s", compare_param.param_str);
6181 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6183 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6184 shader_addline(buffer, ", %s", offset_param.param_str);
6186 else if (has_offset)
6188 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6189 shader_addline(buffer, ", ");
6190 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6192 if (component_idx)
6193 shader_addline(buffer, ", %u", component_idx);
6195 shader_addline(buffer, ")%s);\n", dst_swizzle);
6198 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6200 /* FIXME: Make this work for more than just 2D textures */
6201 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6202 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6204 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6206 char dst_mask[6];
6208 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6209 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6210 ins->dst[0].reg.idx[0].offset, dst_mask);
6212 else
6214 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6215 DWORD reg = ins->src[0].reg.idx[0].offset;
6216 char dst_swizzle[6];
6218 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6220 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6222 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6223 struct glsl_src_param div_param;
6224 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6226 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6228 if (mask_size > 1)
6229 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6230 else
6231 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6233 else
6235 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6240 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6241 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6242 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6243 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6245 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6246 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6247 struct glsl_sample_function sample_function;
6248 struct glsl_src_param src0_param;
6249 UINT mask_size;
6251 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6253 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6254 * scalar, and projected sampling would require 4.
6256 * It is a dependent read - not valid with conditional NP2 textures
6258 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6259 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6261 switch(mask_size)
6263 case 1:
6264 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6265 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6266 break;
6268 case 2:
6269 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6270 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6271 break;
6273 case 3:
6274 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6275 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6276 break;
6278 default:
6279 FIXME("Unexpected mask size %u\n", mask_size);
6280 break;
6282 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6285 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6286 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6287 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6289 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6290 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6291 struct glsl_src_param src0_param;
6292 DWORD dst_mask;
6293 unsigned int mask_size;
6295 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6296 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6297 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6299 if (mask_size > 1) {
6300 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6301 } else {
6302 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6306 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6307 * Calculate the depth as dst.x / dst.y */
6308 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6310 struct glsl_dst_param dst_param;
6312 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6314 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6315 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6316 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6317 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6318 * >= 1.0 or < 0.0
6320 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6321 dst_param.reg_name, dst_param.reg_name);
6324 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6325 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6326 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6327 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6329 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6331 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6332 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6333 struct glsl_src_param src0_param;
6335 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6337 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6338 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6341 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6342 * Calculate the 1st of a 2-row matrix multiplication. */
6343 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6345 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6346 DWORD reg = ins->dst[0].reg.idx[0].offset;
6347 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6348 struct glsl_src_param src0_param;
6350 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6351 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6354 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6355 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6356 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6358 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6359 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6360 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6361 DWORD reg = ins->dst[0].reg.idx[0].offset;
6362 struct glsl_src_param src0_param;
6364 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6365 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6366 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6369 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6371 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6372 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6373 struct glsl_sample_function sample_function;
6374 DWORD reg = ins->dst[0].reg.idx[0].offset;
6375 struct glsl_src_param src0_param;
6377 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6378 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6380 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6382 /* Sample the texture using the calculated coordinates */
6383 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6384 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6387 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6388 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6389 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6391 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6392 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6393 struct glsl_sample_function sample_function;
6394 DWORD reg = ins->dst[0].reg.idx[0].offset;
6395 struct glsl_src_param src0_param;
6397 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6398 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6400 /* Dependent read, not valid with conditional NP2 */
6401 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6403 /* Sample the texture using the calculated coordinates */
6404 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6405 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6407 tex_mx->current_row = 0;
6410 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6411 * Perform the 3rd row of a 3x3 matrix multiply */
6412 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6414 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6415 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6416 DWORD reg = ins->dst[0].reg.idx[0].offset;
6417 struct glsl_src_param src0_param;
6418 char dst_mask[6];
6420 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6422 shader_glsl_append_dst(ins->ctx->buffer, ins);
6423 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6424 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6426 tex_mx->current_row = 0;
6429 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6430 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6431 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6433 struct glsl_src_param src0_param;
6434 struct glsl_src_param src1_param;
6435 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6436 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6437 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6438 struct glsl_sample_function sample_function;
6439 DWORD reg = ins->dst[0].reg.idx[0].offset;
6440 char coord_mask[6];
6442 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6443 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6445 /* Perform the last matrix multiply operation */
6446 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6447 /* Reflection calculation */
6448 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6450 /* Dependent read, not valid with conditional NP2 */
6451 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6452 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6454 /* Sample the texture */
6455 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6456 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6457 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6459 tex_mx->current_row = 0;
6462 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6463 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6464 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6466 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6467 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6468 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6469 struct glsl_sample_function sample_function;
6470 DWORD reg = ins->dst[0].reg.idx[0].offset;
6471 struct glsl_src_param src0_param;
6472 char coord_mask[6];
6474 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6476 /* Perform the last matrix multiply operation */
6477 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6479 /* Construct the eye-ray vector from w coordinates */
6480 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6481 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6482 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
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 using the calculated coordinates */
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_TEXBEM instruction in GLSL.
6497 * Apply a fake bump map transform.
6498 * texbem is pshader <= 1.3 only, this saves a few version checks
6500 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6502 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6503 struct glsl_sample_function sample_function;
6504 struct glsl_src_param coord_param;
6505 DWORD sampler_idx;
6506 DWORD mask;
6507 DWORD flags;
6508 char coord_mask[6];
6510 sampler_idx = ins->dst[0].reg.idx[0].offset;
6511 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6512 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6514 /* Dependent read, not valid with conditional NP2 */
6515 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6516 mask = sample_function.coord_mask;
6518 shader_glsl_write_mask_to_str(mask, coord_mask);
6520 /* With projected textures, texbem only divides the static texture coord,
6521 * not the displacement, so we can't let GL handle this. */
6522 if (flags & WINED3D_PSARGS_PROJECTED)
6524 DWORD div_mask=0;
6525 char coord_div_mask[3];
6526 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6528 case WINED3D_TTFF_COUNT1:
6529 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6530 break;
6531 case WINED3D_TTFF_COUNT2:
6532 div_mask = WINED3DSP_WRITEMASK_1;
6533 break;
6534 case WINED3D_TTFF_COUNT3:
6535 div_mask = WINED3DSP_WRITEMASK_2;
6536 break;
6537 case WINED3D_TTFF_COUNT4:
6538 case WINED3D_TTFF_DISABLE:
6539 div_mask = WINED3DSP_WRITEMASK_3;
6540 break;
6542 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6543 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6546 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6548 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6549 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6550 coord_param.param_str, coord_mask);
6552 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6554 struct glsl_src_param luminance_param;
6555 struct glsl_dst_param dst_param;
6557 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6558 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6560 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6561 dst_param.reg_name, dst_param.mask_str,
6562 luminance_param.param_str, sampler_idx, sampler_idx);
6564 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6567 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6569 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6570 struct glsl_src_param src0_param, src1_param;
6572 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6573 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6575 shader_glsl_append_dst(ins->ctx->buffer, ins);
6576 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6577 src0_param.param_str, sampler_idx, src1_param.param_str);
6580 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6581 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6582 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6584 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6585 struct glsl_sample_function sample_function;
6586 struct glsl_src_param src0_param;
6588 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6590 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6591 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6592 "%s.wx", src0_param.reg_name);
6593 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6596 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6597 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6598 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6600 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6601 struct glsl_sample_function sample_function;
6602 struct glsl_src_param src0_param;
6604 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6606 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6607 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6608 "%s.yz", src0_param.reg_name);
6609 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6612 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6613 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6614 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6616 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6617 struct glsl_sample_function sample_function;
6618 struct glsl_src_param src0_param;
6620 /* Dependent read, not valid with conditional NP2 */
6621 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6622 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6624 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6625 "%s", src0_param.param_str);
6626 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6629 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6630 * If any of the first 3 components are < 0, discard this pixel */
6631 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6633 if (ins->ctx->reg_maps->shader_version.major >= 4)
6635 shader_glsl_generate_conditional_op(ins, "discard;");
6637 else
6639 struct glsl_dst_param dst_param;
6641 /* The argument is a destination parameter, and no writemasks are allowed */
6642 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6644 /* 2.0 shaders compare all 4 components in texkill. */
6645 if (ins->ctx->reg_maps->shader_version.major >= 2)
6646 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6647 /* 1.x shaders only compare the first 3 components, probably due to
6648 * the nature of the texkill instruction as a tex* instruction, and
6649 * phase, which kills all .w components. Even if all 4 components are
6650 * defined, only the first 3 are used. */
6651 else
6652 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6656 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6657 * dst = dot2(src0, src1) + src2 */
6658 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6660 struct glsl_src_param src0_param;
6661 struct glsl_src_param src1_param;
6662 struct glsl_src_param src2_param;
6663 DWORD write_mask;
6664 unsigned int mask_size;
6666 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6667 mask_size = shader_glsl_get_write_mask_size(write_mask);
6669 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6670 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6671 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6673 if (mask_size > 1) {
6674 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6675 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6676 } else {
6677 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6678 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6682 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6683 const struct wined3d_shader_signature *input_signature,
6684 const struct wined3d_shader_reg_maps *reg_maps,
6685 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6687 unsigned int i;
6689 for (i = 0; i < input_signature->element_count; ++i)
6691 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6692 const char *semantic_name;
6693 UINT semantic_idx;
6694 char reg_mask[6];
6696 /* Unused */
6697 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6698 continue;
6700 semantic_name = input->semantic_name;
6701 semantic_idx = input->semantic_idx;
6702 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6704 if (args->vp_mode == vertexshader)
6706 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6708 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6709 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6711 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6713 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6715 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6717 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6718 input->register_idx, reg_mask);
6720 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6722 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6723 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6724 input->register_idx, reg_mask);
6725 else
6726 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6728 else
6730 if (input->sysval_semantic)
6731 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6732 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6733 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6734 shader_glsl_shader_input_name(gl_info),
6735 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6738 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6740 if (args->pointsprite)
6741 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6742 shader->u.ps.input_reg_map[input->register_idx]);
6743 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6744 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6745 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6746 needs_legacy_glsl_syntax(gl_info)
6747 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6748 else
6749 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6750 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6752 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6754 if (!semantic_idx)
6755 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6756 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6757 else if (semantic_idx == 1)
6758 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6759 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6760 else
6761 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6762 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6764 else
6766 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6767 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6772 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6774 struct glsl_program_key key;
6776 key.vs_id = entry->vs.id;
6777 key.hs_id = entry->hs.id;
6778 key.ds_id = entry->ds.id;
6779 key.gs_id = entry->gs.id;
6780 key.ps_id = entry->ps.id;
6781 key.cs_id = entry->cs.id;
6783 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6785 ERR("Failed to insert program entry.\n");
6789 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6790 const struct glsl_program_key *key)
6792 struct wine_rb_entry *entry;
6794 entry = wine_rb_get(&priv->program_lookup, key);
6795 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6798 /* Context activation is done by the caller. */
6799 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6800 struct glsl_shader_prog_link *entry)
6802 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6804 GL_EXTCALL(glDeleteProgram(entry->id));
6805 if (entry->vs.id)
6806 list_remove(&entry->vs.shader_entry);
6807 if (entry->hs.id)
6808 list_remove(&entry->hs.shader_entry);
6809 if (entry->ds.id)
6810 list_remove(&entry->ds.shader_entry);
6811 if (entry->gs.id)
6812 list_remove(&entry->gs.shader_entry);
6813 if (entry->ps.id)
6814 list_remove(&entry->ps.shader_entry);
6815 if (entry->cs.id)
6816 list_remove(&entry->cs.shader_entry);
6817 HeapFree(GetProcessHeap(), 0, entry);
6820 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6821 const struct wined3d_gl_info *gl_info, const DWORD *map,
6822 const struct wined3d_shader_signature *input_signature,
6823 const struct wined3d_shader_reg_maps *reg_maps_in,
6824 const struct wined3d_shader_signature *output_signature,
6825 const struct wined3d_shader_reg_maps *reg_maps_out)
6827 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6828 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6829 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6830 unsigned int in_count = vec4_varyings(3, gl_info);
6831 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6832 DWORD in_idx, *set = NULL;
6833 unsigned int i, j;
6834 char reg_mask[6];
6836 set = wined3d_calloc(max_varyings, sizeof(*set));
6838 for (i = 0; i < input_signature->element_count; ++i)
6840 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6842 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6843 continue;
6845 in_idx = map[input->register_idx];
6846 /* Declared, but not read register */
6847 if (in_idx == ~0u)
6848 continue;
6849 if (in_idx >= max_varyings)
6851 FIXME("More input varyings declared than supported, expect issues.\n");
6852 continue;
6855 if (in_idx == in_count)
6856 string_buffer_sprintf(destination, "gl_FrontColor");
6857 else if (in_idx == in_count + 1)
6858 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6859 else
6860 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6862 if (!set[in_idx])
6863 set[in_idx] = ~0u;
6865 for (j = 0; j < output_signature->element_count; ++j)
6867 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6868 DWORD mask;
6870 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6871 || input->semantic_idx != output->semantic_idx
6872 || strcmp(input->semantic_name, output->semantic_name)
6873 || !(mask = input->mask & output->mask))
6874 continue;
6876 if (set[in_idx] == ~0u)
6877 set[in_idx] = 0;
6878 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6879 shader_glsl_write_mask_to_str(mask, reg_mask);
6881 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6882 destination->buffer, reg_mask, output->register_idx, reg_mask);
6886 for (i = 0; i < max_varyings; ++i)
6888 unsigned int size;
6890 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6891 continue;
6893 if (set[i] == ~0u)
6894 set[i] = 0;
6896 size = 0;
6897 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6898 reg_mask[size++] = 'x';
6899 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6900 reg_mask[size++] = 'y';
6901 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6902 reg_mask[size++] = 'z';
6903 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6904 reg_mask[size++] = 'w';
6905 reg_mask[size] = '\0';
6907 if (i == in_count)
6908 string_buffer_sprintf(destination, "gl_FrontColor");
6909 else if (i == in_count + 1)
6910 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6911 else
6912 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6914 if (size == 1)
6915 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6916 else
6917 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6920 HeapFree(GetProcessHeap(), 0, set);
6921 string_buffer_release(&priv->string_buffers, destination);
6924 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6925 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6926 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6927 BOOL rasterizer_setup)
6929 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6930 char reg_mask[6];
6931 unsigned int i;
6933 for (i = 0; i < output_signature->element_count; ++i)
6935 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6937 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6938 continue;
6940 if (output->stream_idx)
6941 continue;
6943 if (output->register_idx >= input_count)
6944 continue;
6946 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6948 shader_addline(buffer,
6949 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
6950 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
6954 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
6955 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
6957 unsigned int i, clip_or_cull_index;
6958 const char *name;
6959 char reg_mask[6];
6961 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
6962 /* Assign consecutive indices starting from 0. */
6963 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
6964 for (i = 0; i < 4; ++i)
6966 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
6967 continue;
6969 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
6970 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
6971 name, clip_or_cull_index, element->register_idx, reg_mask);
6972 ++clip_or_cull_index;
6976 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6977 const struct wined3d_gl_info *gl_info, const DWORD *map,
6978 const struct wined3d_shader_signature *input_signature,
6979 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6980 const struct wined3d_shader_signature *output_signature,
6981 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6983 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6984 const char *semantic_name;
6985 unsigned int semantic_idx;
6986 char reg_mask[6];
6987 unsigned int i;
6989 /* First, sort out position and point size system values. */
6990 for (i = 0; i < output_signature->element_count; ++i)
6992 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6994 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6995 continue;
6997 if (output->stream_idx)
6998 continue;
7000 semantic_name = output->semantic_name;
7001 semantic_idx = output->semantic_idx;
7002 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7004 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7006 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7007 reg_mask, output->register_idx, reg_mask);
7009 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7011 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7012 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7014 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7016 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7017 output->register_idx, reg_mask);
7019 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7021 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7023 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7025 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7027 else if (output->sysval_semantic)
7029 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7033 /* Then, setup the pixel shader input. */
7034 if (reg_maps_out->shader_version.major < 4)
7035 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7036 output_signature, reg_maps_out);
7037 else
7038 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7041 /* Context activation is done by the caller. */
7042 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7043 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7044 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7046 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7047 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7048 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7049 const char *semantic_name;
7050 UINT semantic_idx;
7051 char reg_mask[6];
7052 unsigned int i;
7053 GLuint ret;
7055 string_buffer_clear(buffer);
7057 shader_glsl_add_version_declaration(buffer, gl_info);
7059 if (per_vertex_point_size)
7061 shader_addline(buffer, "uniform struct\n{\n");
7062 shader_addline(buffer, " float size_min;\n");
7063 shader_addline(buffer, " float size_max;\n");
7064 shader_addline(buffer, "} ffp_point;\n");
7067 if (ps_major < 3)
7069 DWORD colors_written_mask[2] = {0};
7070 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7072 if (!legacy_syntax)
7074 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7075 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7076 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7077 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7080 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7082 for (i = 0; i < vs->output_signature.element_count; ++i)
7084 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7085 DWORD write_mask;
7087 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7088 continue;
7090 semantic_name = output->semantic_name;
7091 semantic_idx = output->semantic_idx;
7092 write_mask = output->mask;
7093 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7095 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7097 if (legacy_syntax)
7098 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7099 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7100 else
7101 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7102 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7104 colors_written_mask[semantic_idx] = write_mask;
7106 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7108 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7109 reg_mask, output->register_idx, reg_mask);
7111 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7113 if (semantic_idx < MAX_TEXTURES)
7115 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7116 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7117 semantic_idx, reg_mask, output->register_idx, reg_mask);
7118 texcoords_written_mask[semantic_idx] = write_mask;
7121 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7123 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7124 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7126 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7128 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7129 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7130 output->register_idx, reg_mask[1]);
7134 for (i = 0; i < 2; ++i)
7136 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7138 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7139 if (!i)
7140 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7141 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7142 reg_mask, reg_mask);
7143 else
7144 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7145 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7146 reg_mask, reg_mask);
7149 for (i = 0; i < MAX_TEXTURES; ++i)
7151 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7152 continue;
7154 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7156 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7157 && !texcoords_written_mask[i])
7158 continue;
7160 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7161 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7162 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7166 else
7168 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7170 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7171 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7172 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7173 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7176 shader_addline(buffer, "}\n");
7178 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7179 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7180 shader_glsl_compile(gl_info, ret, buffer->buffer);
7182 return ret;
7185 static void shader_glsl_generate_stream_output_setup(struct shader_glsl_priv *priv,
7186 const struct wined3d_shader *shader, const struct wined3d_stream_output_desc *so_desc)
7188 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7189 unsigned int i;
7191 shader_addline(buffer, "out shader_in_out\n{\n");
7192 for (i = 0; i < so_desc->element_count; ++i)
7194 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7196 if (e->stream_idx)
7198 FIXME("Unhandled stream %u.\n", e->stream_idx);
7199 continue;
7201 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7202 continue;
7204 if (e->component_idx || e->component_count != 4)
7206 if (e->component_count == 1)
7207 shader_addline(buffer, "float");
7208 else
7209 shader_addline(buffer, "vec%u", e->component_count);
7210 shader_addline(buffer, " reg%u_%u_%u;\n",
7211 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
7213 else
7215 shader_addline(buffer, "vec4 reg%u;\n", e->register_idx);
7218 shader_addline(buffer, "} shader_out;\n");
7220 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7221 shader->limits->packed_output);
7222 for (i = 0; i < so_desc->element_count; ++i)
7224 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7226 if (e->stream_idx)
7228 FIXME("Unhandled stream %u.\n", e->stream_idx);
7229 continue;
7231 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7232 continue;
7234 if (e->component_idx || e->component_count != 4)
7236 DWORD write_mask;
7237 char str_mask[6];
7239 write_mask = ((1u << e->component_count) - 1) << e->component_idx;
7240 shader_glsl_write_mask_to_str(write_mask, str_mask);
7241 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7242 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1,
7243 e->register_idx, str_mask);
7245 else
7247 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n",
7248 e->register_idx, e->register_idx);
7251 shader_addline(buffer, "}\n");
7254 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7255 const struct wined3d_shader *shader, unsigned int input_count,
7256 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7258 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7259 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7261 if (rasterizer_setup)
7262 input_count = min(vec4_varyings(4, gl_info), input_count);
7264 if (input_count)
7265 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7267 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7268 prefix, shader->limits->packed_output);
7270 if (rasterizer_setup)
7271 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7272 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7273 else
7274 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7275 &shader->reg_maps, "shader_out", rasterizer_setup);
7277 shader_addline(buffer, "}\n");
7280 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7281 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7282 const char *reg_mask)
7284 if (!constant->sysval_semantic)
7286 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7287 return;
7290 switch (constant->sysval_semantic)
7292 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7293 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7294 case WINED3D_SV_TESS_FACTOR_LINEDET:
7295 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7296 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7297 break;
7298 case WINED3D_SV_TESS_FACTOR_QUADINT:
7299 case WINED3D_SV_TESS_FACTOR_TRIINT:
7300 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7301 break;
7302 default:
7303 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7304 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7308 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7309 const struct wined3d_shader_signature *signature, BOOL input_setup)
7311 unsigned int i, register_count, user_constant_index, user_constant_count;
7313 register_count = user_constant_count = 0;
7314 for (i = 0; i < signature->element_count; ++i)
7316 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7317 register_count = max(constant->register_idx + 1, register_count);
7318 if (!constant->sysval_semantic)
7319 ++user_constant_count;
7322 if (user_constant_count)
7323 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7324 input_setup ? "in" : "out", user_constant_count);
7325 if (input_setup)
7326 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7328 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7329 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7331 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7332 char reg_mask[6];
7334 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7336 if (input_setup)
7337 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7338 else
7339 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7341 shader_addline(buffer, " = ");
7343 if (input_setup)
7344 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7345 else
7346 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7348 shader_addline(buffer, ";\n");
7350 shader_addline(buffer, "}\n");
7353 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7354 const struct wined3d_gl_info *gl_info)
7356 const char *output = get_fragment_output(gl_info);
7358 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7359 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7360 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7361 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7362 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7363 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7366 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7367 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7369 const char *output = get_fragment_output(gl_info);
7371 switch (mode)
7373 case WINED3D_FFP_PS_FOG_OFF:
7374 return;
7376 case WINED3D_FFP_PS_FOG_LINEAR:
7377 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7378 break;
7380 case WINED3D_FFP_PS_FOG_EXP:
7381 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7382 break;
7384 case WINED3D_FFP_PS_FOG_EXP2:
7385 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7386 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7387 break;
7389 default:
7390 ERR("Invalid fog mode %#x.\n", mode);
7391 return;
7394 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7395 output, output);
7398 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7399 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7401 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7402 * flipping all the operators here, just negate the comparison below. */
7403 static const char * const comparison_operator[] =
7405 "", /* WINED3D_CMP_NEVER */
7406 "<", /* WINED3D_CMP_LESS */
7407 "==", /* WINED3D_CMP_EQUAL */
7408 "<=", /* WINED3D_CMP_LESSEQUAL */
7409 ">", /* WINED3D_CMP_GREATER */
7410 "!=", /* WINED3D_CMP_NOTEQUAL */
7411 ">=", /* WINED3D_CMP_GREATEREQUAL */
7412 "" /* WINED3D_CMP_ALWAYS */
7415 if (alpha_func == WINED3D_CMP_ALWAYS)
7416 return;
7418 if (alpha_func != WINED3D_CMP_NEVER)
7419 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7420 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7421 shader_addline(buffer, " discard;\n");
7424 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7425 const struct wined3d_gl_info *gl_info)
7427 if (gl_info->supported[ARB_CULL_DISTANCE])
7428 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7429 if (gl_info->supported[ARB_GPU_SHADER5])
7430 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7431 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7432 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7433 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7434 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7435 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7436 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7437 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7438 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7439 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7440 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7441 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7442 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7443 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7444 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7445 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7446 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7447 if (gl_info->supported[ARB_TEXTURE_GATHER])
7448 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7449 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7450 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7451 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7452 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7453 if (gl_info->supported[EXT_GPU_SHADER4])
7454 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7455 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7456 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7459 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7460 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7461 const struct ps_compile_args *args)
7463 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7465 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7466 if (reg_maps->shader_version.major < 2)
7467 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7469 if (args->srgb_correction)
7470 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7472 /* SM < 3 does not replace the fog stage. */
7473 if (reg_maps->shader_version.major < 3)
7474 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7476 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7479 /* Context activation is done by the caller. */
7480 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7481 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7482 const struct wined3d_shader *shader,
7483 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7485 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7486 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7487 const char *prefix = shader_glsl_get_prefix(version->type);
7488 const struct wined3d_gl_info *gl_info = context->gl_info;
7489 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7490 unsigned int i, extra_constants_needed = 0;
7491 struct shader_glsl_ctx_priv priv_ctx;
7492 GLuint shader_id;
7493 DWORD map;
7495 memset(&priv_ctx, 0, sizeof(priv_ctx));
7496 priv_ctx.cur_ps_args = args;
7497 priv_ctx.cur_np2fixup_info = np2fixup_info;
7498 priv_ctx.string_buffers = string_buffers;
7500 shader_glsl_add_version_declaration(buffer, gl_info);
7502 shader_glsl_enable_extensions(buffer, gl_info);
7503 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7504 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7505 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7506 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7507 if (shader_glsl_use_explicit_attrib_location(gl_info))
7508 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7509 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7510 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7511 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7512 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7513 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7514 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7515 /* The spec says that it doesn't have to be explicitly enabled, but the
7516 * nvidia drivers write a warning if we don't do so. */
7517 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7518 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7520 /* Base Declarations */
7521 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7523 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7525 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7526 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7527 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7528 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7531 /* Declare uniforms for NP2 texcoord fixup:
7532 * This is NOT done inside the loop that declares the texture samplers
7533 * since the NP2 fixup code is currently only used for the GeforceFX
7534 * series and when forcing the ARB_npot extension off. Modern cards just
7535 * skip the code anyway, so put it inside a separate loop. */
7536 if (args->np2_fixup)
7538 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7539 unsigned int cur = 0;
7541 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7542 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7543 * samplerNP2Fixup stores texture dimensions and is updated through
7544 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7546 for (i = 0; i < shader->limits->sampler; ++i)
7548 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7549 continue;
7551 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7553 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7554 continue;
7557 fixup->idx[i] = cur++;
7560 fixup->num_consts = (cur + 1) >> 1;
7561 fixup->active = args->np2_fixup;
7562 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7565 if (version->major < 3 || args->vp_mode != vertexshader)
7567 shader_addline(buffer, "uniform struct\n{\n");
7568 shader_addline(buffer, " vec4 color;\n");
7569 shader_addline(buffer, " float density;\n");
7570 shader_addline(buffer, " float end;\n");
7571 shader_addline(buffer, " float scale;\n");
7572 shader_addline(buffer, "} ffp_fog;\n");
7574 if (needs_legacy_glsl_syntax(gl_info))
7576 if (glsl_is_color_reg_read(shader, 0))
7577 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7578 if (glsl_is_color_reg_read(shader, 1))
7579 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7580 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7581 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7583 else
7585 if (glsl_is_color_reg_read(shader, 0))
7586 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7587 if (glsl_is_color_reg_read(shader, 1))
7588 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7589 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7590 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7591 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7595 if (version->major >= 3)
7597 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7599 if (args->vp_mode == vertexshader && reg_maps->input_registers)
7600 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7601 shader->u.ps.interpolation_mode, version->major >= 4);
7602 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7605 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7607 if (!(map & 1))
7608 continue;
7610 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7612 if (reg_maps->luminanceparams & (1u << i))
7614 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7615 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7616 extra_constants_needed++;
7619 extra_constants_needed++;
7622 if (args->srgb_correction)
7624 shader_addline(buffer, "const vec4 srgb_const0 = ");
7625 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7626 shader_addline(buffer, ";\n");
7627 shader_addline(buffer, "const vec4 srgb_const1 = ");
7628 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7629 shader_addline(buffer, ";\n");
7631 if (reg_maps->vpos || reg_maps->usesdsy)
7633 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7635 ++extra_constants_needed;
7636 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7638 if (reg_maps->vpos)
7640 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7642 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7643 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7644 args->render_offscreen ? "" : "origin_upper_left, ");
7645 else if (!args->render_offscreen)
7646 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7648 shader_addline(buffer, "vec4 vpos;\n");
7652 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7653 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7655 if (!needs_legacy_glsl_syntax(gl_info))
7657 if (shader_glsl_use_explicit_attrib_location(gl_info))
7658 shader_addline(buffer, "layout(location = 0) ");
7659 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
7662 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7663 FIXME("Insufficient uniforms to run this shader.\n");
7665 if (shader->u.ps.force_early_depth_stencil)
7666 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7668 shader_addline(buffer, "void main()\n{\n");
7670 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7671 * add approximately 0.5. This causes off-by-one problems as spotted by
7672 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7673 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7674 * causes precision troubles when we just subtract 0.5.
7676 * To deal with that, just floor() the position. This will eliminate the
7677 * fraction on all cards.
7679 * TODO: Test how this behaves with multisampling.
7681 * An advantage of floor is that it works even if the driver doesn't add
7682 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7683 * to return in gl_FragCoord, even though coordinates specify the pixel
7684 * centers instead of the pixel corners. This code will behave correctly
7685 * on drivers that returns integer values. */
7686 if (reg_maps->vpos)
7688 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7689 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7690 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7691 shader_addline(buffer,
7692 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7693 else
7694 shader_addline(buffer,
7695 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7698 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
7700 unsigned int i;
7701 WORD map = reg_maps->texcoord;
7703 if (legacy_syntax)
7705 if (glsl_is_color_reg_read(shader, 0))
7706 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7707 if (glsl_is_color_reg_read(shader, 1))
7708 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7711 for (i = 0; map; map >>= 1, ++i)
7713 if (map & 1)
7715 if (args->pointsprite)
7716 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7717 else if (args->texcoords_initialized & (1u << i))
7718 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7719 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7720 else
7721 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7722 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7726 if (legacy_syntax)
7727 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7730 /* Pack 3.0 inputs */
7731 if (reg_maps->shader_version.major >= 3)
7732 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7733 reg_maps->shader_version.major >= 4);
7735 /* Base Shader Body */
7736 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7737 return 0;
7739 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7740 if (reg_maps->shader_version.major < 4)
7741 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
7743 shader_addline(buffer, "}\n");
7745 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7746 TRACE("Compiling shader object %u.\n", shader_id);
7747 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7749 return shader_id;
7752 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7753 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7754 const struct vs_compile_args *args)
7756 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7757 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7758 unsigned int i;
7760 /* Unpack outputs. */
7761 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7763 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7764 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7765 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7766 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7768 if (reg_maps->shader_version.major < 3)
7770 if (args->fog_src == VS_FOG_Z)
7771 shader_addline(buffer, "%s = gl_Position.z;\n",
7772 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7773 else if (!reg_maps->fog)
7774 shader_addline(buffer, "%s = 0.0;\n",
7775 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7778 /* We always store the clipplanes without y inversion. */
7779 if (args->clip_enabled)
7781 if (legacy_syntax)
7782 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7783 else
7784 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7785 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7788 if (args->point_size && !args->per_vertex_point_size)
7789 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7791 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7792 shader_glsl_fixup_position(buffer);
7795 /* Context activation is done by the caller. */
7796 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
7797 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7799 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7800 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7801 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7802 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7803 const struct wined3d_gl_info *gl_info = context->gl_info;
7804 struct shader_glsl_ctx_priv priv_ctx;
7805 GLuint shader_id;
7806 unsigned int i;
7808 memset(&priv_ctx, 0, sizeof(priv_ctx));
7809 priv_ctx.cur_vs_args = args;
7810 priv_ctx.string_buffers = string_buffers;
7812 shader_glsl_add_version_declaration(buffer, gl_info);
7814 shader_glsl_enable_extensions(buffer, gl_info);
7815 if (gl_info->supported[ARB_DRAW_INSTANCED])
7816 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7817 if (shader_glsl_use_explicit_attrib_location(gl_info))
7818 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7820 /* Base Declarations */
7821 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7823 for (i = 0; i < shader->input_signature.element_count; ++i)
7824 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7826 if (args->point_size && !args->per_vertex_point_size)
7828 shader_addline(buffer, "uniform struct\n{\n");
7829 shader_addline(buffer, " float size;\n");
7830 shader_addline(buffer, " float size_min;\n");
7831 shader_addline(buffer, " float size_max;\n");
7832 shader_addline(buffer, "} ffp_point;\n");
7835 if (!needs_legacy_glsl_syntax(gl_info))
7837 if (args->clip_enabled)
7838 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7840 if (version->major < 3)
7842 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7843 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7844 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7845 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7849 if (version->major < 4)
7850 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
7852 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7853 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7855 if (reg_maps->shader_version.major >= 4)
7856 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
7857 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
7859 shader_addline(buffer, "void main()\n{\n");
7861 if (reg_maps->input_rel_addressing)
7863 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
7864 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
7865 for (i = 0; i < shader->input_signature.element_count; ++i)
7867 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
7868 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
7872 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7873 return 0;
7875 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7876 if (reg_maps->shader_version.major < 4)
7877 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7879 shader_addline(buffer, "}\n");
7881 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7882 TRACE("Compiling shader object %u.\n", shader_id);
7883 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7885 return shader_id;
7888 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
7889 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
7891 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7892 char reg_mask[6];
7893 unsigned int i;
7895 for (i = 0; i < output_signature->element_count; ++i)
7897 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7899 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7900 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
7901 output->register_idx, reg_mask, output->register_idx, reg_mask);
7905 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
7906 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
7907 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
7908 const char *phase_name, unsigned phase_idx)
7910 unsigned int i;
7911 HRESULT hr;
7913 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
7914 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
7915 for (i = 0; i < phase->temporary_count; ++i)
7916 shader_addline(buffer, "vec4 R%u;\n", i);
7917 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
7918 shader_addline(buffer, "}\n");
7919 return hr;
7922 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
7923 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
7925 if (phase->instance_count)
7927 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
7928 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
7929 shader_addline(buffer, "}\n");
7931 else
7933 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
7937 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
7938 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
7940 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7941 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7942 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7943 const struct wined3d_gl_info *gl_info = context->gl_info;
7944 const struct wined3d_hull_shader *hs = &shader->u.hs;
7945 const struct wined3d_shader_phase *phase;
7946 struct shader_glsl_ctx_priv priv_ctx;
7947 GLuint shader_id;
7948 unsigned int i;
7950 memset(&priv_ctx, 0, sizeof(priv_ctx));
7951 priv_ctx.string_buffers = string_buffers;
7953 shader_glsl_add_version_declaration(buffer, gl_info);
7955 shader_glsl_enable_extensions(buffer, gl_info);
7956 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
7958 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7960 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
7962 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
7963 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
7965 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
7967 if (hs->phases.control_point)
7969 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
7970 shader->limits->packed_output);
7971 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
7972 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
7973 shader_addline(buffer, "}\n");
7976 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
7977 if ((phase = hs->phases.control_point))
7979 for (i = 0; i < phase->temporary_count; ++i)
7980 shader_addline(buffer, "vec4 R%u;\n", i);
7981 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
7982 return 0;
7983 shader_addline(buffer, "setup_hs_output(hs_out);\n");
7985 else
7987 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
7989 shader_addline(buffer, "}\n");
7991 for (i = 0; i < hs->phases.fork_count; ++i)
7993 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
7994 &hs->phases.fork[i], "fork", i)))
7995 return 0;
7998 for (i = 0; i < hs->phases.join_count; ++i)
8000 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8001 &hs->phases.join[i], "join", i)))
8002 return 0;
8005 shader_addline(buffer, "void main()\n{\n");
8006 shader_addline(buffer, "hs_control_point_phase();\n");
8007 if (reg_maps->vocp)
8008 shader_addline(buffer, "barrier();\n");
8009 for (i = 0; i < hs->phases.fork_count; ++i)
8010 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8011 for (i = 0; i < hs->phases.join_count; ++i)
8012 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8013 shader_addline(buffer, "setup_patch_constant_output();\n");
8014 shader_addline(buffer, "}\n");
8016 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8017 TRACE("Compiling shader object %u.\n", shader_id);
8018 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8020 return shader_id;
8023 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8024 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8025 const struct ds_compile_args *args)
8027 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8029 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8030 shader_glsl_fixup_position(buffer);
8033 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
8034 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8036 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8037 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8038 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8039 const struct wined3d_gl_info *gl_info = context->gl_info;
8040 struct shader_glsl_ctx_priv priv_ctx;
8041 GLuint shader_id;
8043 memset(&priv_ctx, 0, sizeof(priv_ctx));
8044 priv_ctx.cur_ds_args = args;
8045 priv_ctx.string_buffers = string_buffers;
8047 shader_glsl_add_version_declaration(buffer, gl_info);
8049 shader_glsl_enable_extensions(buffer, gl_info);
8050 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8052 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8054 shader_addline(buffer, "layout(");
8055 switch (shader->u.ds.tessellator_domain)
8057 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8058 shader_addline(buffer, "isolines");
8059 break;
8060 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8061 shader_addline(buffer, "quads");
8062 break;
8063 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8064 shader_addline(buffer, "triangles");
8065 break;
8067 switch (args->tessellator_output_primitive)
8069 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8070 if (args->render_offscreen)
8071 shader_addline(buffer, ", ccw");
8072 else
8073 shader_addline(buffer, ", cw");
8074 break;
8075 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8076 if (args->render_offscreen)
8077 shader_addline(buffer, ", cw");
8078 else
8079 shader_addline(buffer, ", ccw");
8080 break;
8081 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8082 shader_addline(buffer, ", point_mode");
8083 break;
8084 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8085 break;
8087 switch (args->tessellator_partitioning)
8089 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8090 shader_addline(buffer, ", fractional_odd_spacing");
8091 break;
8092 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8093 shader_addline(buffer, ", fractional_even_spacing");
8094 break;
8095 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8096 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8097 shader_addline(buffer, ", equal_spacing");
8098 break;
8100 shader_addline(buffer, ") in;\n");
8102 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8104 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8105 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8107 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8108 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8109 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8111 shader_addline(buffer, "void main()\n{\n");
8112 shader_addline(buffer, "setup_patch_constant_input();\n");
8114 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8115 return 0;
8117 shader_addline(buffer, "}\n");
8119 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8120 TRACE("Compiling shader object %u.\n", shader_id);
8121 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8123 return shader_id;
8126 /* Context activation is done by the caller. */
8127 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
8128 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8130 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8131 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8132 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8133 const struct wined3d_gl_info *gl_info = context->gl_info;
8134 struct shader_glsl_ctx_priv priv_ctx;
8135 GLuint shader_id;
8137 memset(&priv_ctx, 0, sizeof(priv_ctx));
8138 priv_ctx.string_buffers = string_buffers;
8140 shader_glsl_add_version_declaration(buffer, gl_info);
8142 shader_glsl_enable_extensions(buffer, gl_info);
8144 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8146 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
8147 if (shader->u.gs.instance_count > 1)
8148 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8149 shader_addline(buffer, ") in;\n");
8150 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8151 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
8152 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8154 if (!gl_info->supported[ARB_CLIP_CONTROL])
8155 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8157 if (is_rasterization_disabled(shader))
8159 shader_glsl_generate_stream_output_setup(priv, shader, &shader->u.gs.so_desc);
8161 else
8163 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8164 gl_info, TRUE, args->interpolation_mode);
8166 shader_addline(buffer, "void main()\n{\n");
8167 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8168 return 0;
8169 shader_addline(buffer, "}\n");
8171 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8172 TRACE("Compiling shader object %u.\n", shader_id);
8173 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8175 return shader_id;
8178 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8180 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8181 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8182 struct wined3d_string_buffer *buffer = ctx->buffer;
8183 const struct wined3d_shader *shader = ctx->shader;
8185 switch (shader->reg_maps.shader_version.type)
8187 case WINED3D_SHADER_TYPE_PIXEL:
8188 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args);
8189 break;
8190 case WINED3D_SHADER_TYPE_VERTEX:
8191 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8192 break;
8193 case WINED3D_SHADER_TYPE_DOMAIN:
8194 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8195 break;
8196 case WINED3D_SHADER_TYPE_GEOMETRY:
8197 case WINED3D_SHADER_TYPE_COMPUTE:
8198 break;
8199 default:
8200 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8201 break;
8205 /* Context activation is done by the caller. */
8206 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8207 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8208 const struct wined3d_shader *shader)
8210 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8211 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8212 const struct wined3d_gl_info *gl_info = context->gl_info;
8213 struct shader_glsl_ctx_priv priv_ctx;
8214 GLuint shader_id;
8215 unsigned int i;
8217 memset(&priv_ctx, 0, sizeof(priv_ctx));
8218 priv_ctx.string_buffers = string_buffers;
8220 shader_glsl_add_version_declaration(buffer, gl_info);
8222 shader_glsl_enable_extensions(buffer, gl_info);
8223 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8225 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8227 for (i = 0; i < reg_maps->tgsm_count; ++i)
8229 if (reg_maps->tgsm[i].size)
8230 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8233 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8234 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8236 shader_addline(buffer, "void main()\n{\n");
8237 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8238 shader_addline(buffer, "}\n");
8240 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8241 TRACE("Compiling shader object %u.\n", shader_id);
8242 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8244 return shader_id;
8247 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8248 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8249 struct wined3d_shader *shader,
8250 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8252 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8253 struct glsl_shader_private *shader_data;
8254 struct ps_np2fixup_info *np2fixup;
8255 UINT i;
8256 DWORD new_size;
8257 GLuint ret;
8259 if (!shader->backend_data)
8261 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8262 if (!shader->backend_data)
8264 ERR("Failed to allocate backend data.\n");
8265 return 0;
8268 shader_data = shader->backend_data;
8269 gl_shaders = shader_data->gl_shaders.ps;
8271 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8272 * so a linear search is more performant than a hashmap or a binary search
8273 * (cache coherency etc)
8275 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8277 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8279 if (args->np2_fixup)
8280 *np2fixup_info = &gl_shaders[i].np2fixup;
8281 return gl_shaders[i].id;
8285 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8286 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8287 if (shader_data->num_gl_shaders)
8289 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8290 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
8291 new_size * sizeof(*gl_shaders));
8293 else
8295 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8296 new_size = 1;
8299 if(!new_array) {
8300 ERR("Out of memory\n");
8301 return 0;
8303 shader_data->gl_shaders.ps = new_array;
8304 shader_data->shader_array_size = new_size;
8305 gl_shaders = new_array;
8308 gl_shaders[shader_data->num_gl_shaders].args = *args;
8310 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8311 memset(np2fixup, 0, sizeof(*np2fixup));
8312 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8314 pixelshader_update_resource_types(shader, args->tex_types);
8316 string_buffer_clear(buffer);
8317 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8318 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8320 return ret;
8323 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8324 const DWORD use_map)
8326 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
8327 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
8328 if (stored->point_size != new->point_size)
8329 return FALSE;
8330 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8331 return FALSE;
8332 if (stored->flatshading != new->flatshading)
8333 return FALSE;
8334 if (stored->next_shader_type != new->next_shader_type)
8335 return FALSE;
8336 if (stored->next_shader_input_count != new->next_shader_input_count)
8337 return FALSE;
8338 return stored->fog_src == new->fog_src;
8341 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8342 struct wined3d_shader *shader, const struct vs_compile_args *args)
8344 UINT i;
8345 DWORD new_size;
8346 DWORD use_map = context->stream_info.use_map;
8347 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8348 struct glsl_shader_private *shader_data;
8349 GLuint ret;
8351 if (!shader->backend_data)
8353 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
8354 if (!shader->backend_data)
8356 ERR("Failed to allocate backend data.\n");
8357 return 0;
8360 shader_data = shader->backend_data;
8361 gl_shaders = shader_data->gl_shaders.vs;
8363 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8364 * so a linear search is more performant than a hashmap or a binary search
8365 * (cache coherency etc)
8367 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8369 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8370 return gl_shaders[i].id;
8373 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8375 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
8376 if (shader_data->num_gl_shaders)
8378 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8379 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
8380 new_size * sizeof(*gl_shaders));
8382 else
8384 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
8385 new_size = 1;
8388 if(!new_array) {
8389 ERR("Out of memory\n");
8390 return 0;
8392 shader_data->gl_shaders.vs = new_array;
8393 shader_data->shader_array_size = new_size;
8394 gl_shaders = new_array;
8397 gl_shaders[shader_data->num_gl_shaders].args = *args;
8399 string_buffer_clear(&priv->shader_buffer);
8400 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8401 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8403 return ret;
8406 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8407 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8409 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8410 struct glsl_shader_private *shader_data;
8411 unsigned int new_size;
8412 GLuint ret;
8414 if (!shader->backend_data)
8416 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8418 ERR("Failed to allocate backend data.\n");
8419 return 0;
8422 shader_data = shader->backend_data;
8423 gl_shaders = shader_data->gl_shaders.hs;
8425 if (shader_data->num_gl_shaders > 0)
8427 assert(shader_data->num_gl_shaders == 1);
8428 return gl_shaders[0].id;
8431 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8433 assert(!shader_data->gl_shaders.hs);
8434 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8435 new_size = 1;
8436 if (!new_array)
8438 ERR("Failed to allocate GL shaders array.\n");
8439 return 0;
8441 shader_data->gl_shaders.hs = new_array;
8442 shader_data->shader_array_size = new_size;
8443 gl_shaders = new_array;
8445 string_buffer_clear(&priv->shader_buffer);
8446 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8447 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8449 return ret;
8452 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8453 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8455 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8456 struct glsl_shader_private *shader_data;
8457 unsigned int i, new_size;
8458 GLuint ret;
8460 if (!shader->backend_data)
8462 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8464 ERR("Failed to allocate backend data.\n");
8465 return 0;
8468 shader_data = shader->backend_data;
8469 gl_shaders = shader_data->gl_shaders.ds;
8471 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8473 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8474 return gl_shaders[i].id;
8477 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8479 if (shader_data->num_gl_shaders)
8481 new_size = shader_data->shader_array_size + 1;
8482 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ds,
8483 new_size * sizeof(*new_array));
8485 else
8487 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8488 new_size = 1;
8491 if (!new_array)
8493 ERR("Failed to allocate GL shaders array.\n");
8494 return 0;
8496 shader_data->gl_shaders.ds = new_array;
8497 shader_data->shader_array_size = new_size;
8498 gl_shaders = new_array;
8500 string_buffer_clear(&priv->shader_buffer);
8501 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8502 gl_shaders[shader_data->num_gl_shaders].args = *args;
8503 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8505 return ret;
8508 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8509 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8511 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8512 struct glsl_shader_private *shader_data;
8513 unsigned int i, new_size;
8514 GLuint ret;
8516 if (!shader->backend_data)
8518 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
8520 ERR("Failed to allocate backend data.\n");
8521 return 0;
8524 shader_data = shader->backend_data;
8525 gl_shaders = shader_data->gl_shaders.gs;
8527 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8529 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8530 return gl_shaders[i].id;
8533 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8535 if (shader_data->num_gl_shaders)
8537 new_size = shader_data->shader_array_size + 1;
8538 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
8539 new_size * sizeof(*new_array));
8541 else
8543 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
8544 new_size = 1;
8547 if (!new_array)
8549 ERR("Failed to allocate GL shaders array.\n");
8550 return 0;
8552 shader_data->gl_shaders.gs = new_array;
8553 shader_data->shader_array_size = new_size;
8554 gl_shaders = new_array;
8556 string_buffer_clear(&priv->shader_buffer);
8557 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8558 gl_shaders[shader_data->num_gl_shaders].args = *args;
8559 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8561 return ret;
8564 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8566 switch (mcs)
8568 case WINED3D_MCS_MATERIAL:
8569 return material;
8570 case WINED3D_MCS_COLOR1:
8571 return "ffp_attrib_diffuse";
8572 case WINED3D_MCS_COLOR2:
8573 return "ffp_attrib_specular";
8574 default:
8575 ERR("Invalid material color source %#x.\n", mcs);
8576 return "<invalid>";
8580 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8581 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8583 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8584 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8585 if (settings->localviewer)
8586 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8587 else
8588 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8589 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8590 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8593 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8594 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8596 const char *diffuse, *specular, *emissive, *ambient;
8597 unsigned int i, idx;
8599 if (!settings->lighting)
8601 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8602 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8603 return;
8606 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8607 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8608 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8609 shader_addline(buffer, "vec3 dir, dst;\n");
8610 shader_addline(buffer, "float att, t;\n");
8612 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8613 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8614 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8615 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8617 idx = 0;
8618 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8620 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8621 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8622 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8623 shader_addline(buffer, "dst.x = 1.0;\n");
8624 if (legacy_lighting)
8626 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8627 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8628 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8630 else
8632 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8634 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8635 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8636 if (!legacy_lighting)
8637 shader_addline(buffer, "att = 1.0 / att;\n");
8638 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8639 if (!settings->normal)
8641 shader_addline(buffer, "}\n");
8642 continue;
8644 shader_addline(buffer, "dir = normalize(dir);\n");
8645 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8646 shader_addline(buffer, "}\n");
8649 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8651 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8652 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8653 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8654 shader_addline(buffer, "dst.x = 1.0;\n");
8655 if (legacy_lighting)
8657 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8658 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8659 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8661 else
8663 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8665 shader_addline(buffer, "dir = normalize(dir);\n");
8666 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8667 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8668 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8669 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8670 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8671 idx, idx, idx, idx);
8672 if (legacy_lighting)
8673 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8674 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8675 idx, idx, idx);
8676 else
8677 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8678 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8679 idx, idx, idx);
8680 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8681 if (!settings->normal)
8683 shader_addline(buffer, "}\n");
8684 continue;
8686 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8687 shader_addline(buffer, "}\n");
8690 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8692 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8693 if (!settings->normal)
8694 continue;
8695 shader_addline(buffer, "att = 1.0;\n");
8696 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8697 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8700 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8702 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8703 if (!settings->normal)
8704 continue;
8705 shader_addline(buffer, "att = 1.0;\n");
8706 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8707 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8710 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8711 ambient, diffuse, emissive);
8712 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8713 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8716 /* Context activation is done by the caller. */
8717 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8718 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8720 static const struct attrib_info
8722 const char type[6];
8723 const char name[24];
8725 attrib_info[] =
8727 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8728 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8729 /* TODO: Indexed vertex blending */
8730 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8731 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8732 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8733 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8734 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8736 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8737 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8738 BOOL output_legacy_fogcoord = legacy_syntax;
8739 BOOL legacy_lighting = priv->legacy_lighting;
8740 GLuint shader_obj;
8741 unsigned int i;
8743 string_buffer_clear(buffer);
8745 shader_glsl_add_version_declaration(buffer, gl_info);
8747 if (shader_glsl_use_explicit_attrib_location(gl_info))
8748 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8750 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8752 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8754 if (shader_glsl_use_explicit_attrib_location(gl_info))
8755 shader_addline(buffer, "layout(location = %u) ", i);
8756 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8758 shader_addline(buffer, "\n");
8760 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8761 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8762 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8763 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
8765 shader_addline(buffer, "uniform struct\n{\n");
8766 shader_addline(buffer, " vec4 emissive;\n");
8767 shader_addline(buffer, " vec4 ambient;\n");
8768 shader_addline(buffer, " vec4 diffuse;\n");
8769 shader_addline(buffer, " vec4 specular;\n");
8770 shader_addline(buffer, " float shininess;\n");
8771 shader_addline(buffer, "} ffp_material;\n");
8773 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8774 shader_addline(buffer, "uniform struct\n{\n");
8775 shader_addline(buffer, " vec4 diffuse;\n");
8776 shader_addline(buffer, " vec4 specular;\n");
8777 shader_addline(buffer, " vec4 ambient;\n");
8778 shader_addline(buffer, " vec4 position;\n");
8779 shader_addline(buffer, " vec3 direction;\n");
8780 shader_addline(buffer, " float range;\n");
8781 shader_addline(buffer, " float falloff;\n");
8782 shader_addline(buffer, " float c_att;\n");
8783 shader_addline(buffer, " float l_att;\n");
8784 shader_addline(buffer, " float q_att;\n");
8785 shader_addline(buffer, " float cos_htheta;\n");
8786 shader_addline(buffer, " float cos_hphi;\n");
8787 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
8789 if (settings->point_size)
8791 shader_addline(buffer, "uniform struct\n{\n");
8792 shader_addline(buffer, " float size;\n");
8793 shader_addline(buffer, " float size_min;\n");
8794 shader_addline(buffer, " float size_max;\n");
8795 shader_addline(buffer, " float c_att;\n");
8796 shader_addline(buffer, " float l_att;\n");
8797 shader_addline(buffer, " float q_att;\n");
8798 shader_addline(buffer, "} ffp_point;\n");
8801 if (legacy_syntax)
8803 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8804 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8805 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8806 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8808 else
8810 if (settings->clipping)
8811 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8813 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8814 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8815 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8816 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8819 shader_addline(buffer, "\nvoid main()\n{\n");
8820 shader_addline(buffer, "float m;\n");
8821 shader_addline(buffer, "vec3 r;\n");
8823 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
8825 if (attrib_info[i].name[0])
8826 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
8827 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
8829 for (i = 0; i < MAX_TEXTURES; ++i)
8831 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
8832 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
8833 && settings->texcoords & (1u << i))
8834 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
8837 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
8839 if (settings->transformed)
8841 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
8842 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8843 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
8845 else
8847 for (i = 0; i < settings->vertexblends; ++i)
8848 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
8850 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
8851 for (i = 0; i < settings->vertexblends + 1; ++i)
8852 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
8854 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8855 if (settings->clipping)
8857 if (legacy_syntax)
8858 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
8859 else
8860 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8861 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
8863 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
8866 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
8867 if (settings->normal)
8869 if (!settings->vertexblends)
8871 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
8873 else
8875 for (i = 0; i < settings->vertexblends + 1; ++i)
8876 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
8879 if (settings->normalize)
8880 shader_addline(buffer, "normal = normalize(normal);\n");
8883 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
8884 if (legacy_syntax)
8886 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
8887 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
8889 else
8891 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
8892 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
8895 for (i = 0; i < MAX_TEXTURES; ++i)
8897 BOOL output_legacy_texcoord = legacy_syntax;
8899 switch (settings->texgen[i] & 0xffff0000)
8901 case WINED3DTSS_TCI_PASSTHRU:
8902 if (settings->texcoords & (1u << i))
8903 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
8904 i, i, i);
8905 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
8906 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
8907 else
8908 output_legacy_texcoord = FALSE;
8909 break;
8911 case WINED3DTSS_TCI_CAMERASPACENORMAL:
8912 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
8913 break;
8915 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
8916 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
8917 break;
8919 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
8920 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8921 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
8922 break;
8924 case WINED3DTSS_TCI_SPHEREMAP:
8925 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
8926 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
8927 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
8928 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
8929 break;
8931 default:
8932 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
8933 break;
8935 if (output_legacy_texcoord)
8936 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
8939 switch (settings->fog_mode)
8941 case WINED3D_FFP_VS_FOG_OFF:
8942 output_legacy_fogcoord = FALSE;
8943 break;
8945 case WINED3D_FFP_VS_FOG_FOGCOORD:
8946 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
8947 break;
8949 case WINED3D_FFP_VS_FOG_RANGE:
8950 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
8951 break;
8953 case WINED3D_FFP_VS_FOG_DEPTH:
8954 if (settings->ortho_fog)
8956 if (gl_info->supported[ARB_CLIP_CONTROL])
8957 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
8958 else
8959 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
8960 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
8962 else if (settings->transformed)
8964 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
8966 else
8968 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
8970 break;
8972 default:
8973 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
8974 break;
8976 if (output_legacy_fogcoord)
8977 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
8979 if (settings->point_size)
8981 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
8982 " + ffp_point.l_att * length(ec_pos.xyz)"
8983 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
8984 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
8985 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
8988 shader_addline(buffer, "}\n");
8990 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8991 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
8993 return shader_obj;
8996 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
8997 DWORD argnum, unsigned int stage, DWORD arg)
8999 const char *ret;
9001 if (arg == ARG_UNUSED)
9002 return "<unused arg>";
9004 switch (arg & WINED3DTA_SELECTMASK)
9006 case WINED3DTA_DIFFUSE:
9007 ret = "ffp_varying_diffuse";
9008 break;
9010 case WINED3DTA_CURRENT:
9011 ret = "ret";
9012 break;
9014 case WINED3DTA_TEXTURE:
9015 switch (stage)
9017 case 0: ret = "tex0"; break;
9018 case 1: ret = "tex1"; break;
9019 case 2: ret = "tex2"; break;
9020 case 3: ret = "tex3"; break;
9021 case 4: ret = "tex4"; break;
9022 case 5: ret = "tex5"; break;
9023 case 6: ret = "tex6"; break;
9024 case 7: ret = "tex7"; break;
9025 default:
9026 ret = "<invalid texture>";
9027 break;
9029 break;
9031 case WINED3DTA_TFACTOR:
9032 ret = "tex_factor";
9033 break;
9035 case WINED3DTA_SPECULAR:
9036 ret = "ffp_varying_specular";
9037 break;
9039 case WINED3DTA_TEMP:
9040 ret = "temp_reg";
9041 break;
9043 case WINED3DTA_CONSTANT:
9044 switch (stage)
9046 case 0: ret = "tss_const0"; break;
9047 case 1: ret = "tss_const1"; break;
9048 case 2: ret = "tss_const2"; break;
9049 case 3: ret = "tss_const3"; break;
9050 case 4: ret = "tss_const4"; break;
9051 case 5: ret = "tss_const5"; break;
9052 case 6: ret = "tss_const6"; break;
9053 case 7: ret = "tss_const7"; break;
9054 default:
9055 ret = "<invalid constant>";
9056 break;
9058 break;
9060 default:
9061 return "<unhandled arg>";
9064 if (arg & WINED3DTA_COMPLEMENT)
9066 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9067 if (argnum == 0)
9068 ret = "arg0";
9069 else if (argnum == 1)
9070 ret = "arg1";
9071 else if (argnum == 2)
9072 ret = "arg2";
9075 if (arg & WINED3DTA_ALPHAREPLICATE)
9077 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9078 if (argnum == 0)
9079 ret = "arg0";
9080 else if (argnum == 1)
9081 ret = "arg1";
9082 else if (argnum == 2)
9083 ret = "arg2";
9086 return ret;
9089 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9090 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9092 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9094 if (color && alpha)
9095 dstmask = "";
9096 else if (color)
9097 dstmask = ".xyz";
9098 else
9099 dstmask = ".w";
9101 if (dst == tempreg)
9102 dstreg = "temp_reg";
9103 else
9104 dstreg = "ret";
9106 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9107 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9108 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9110 switch (op)
9112 case WINED3D_TOP_DISABLE:
9113 break;
9115 case WINED3D_TOP_SELECT_ARG1:
9116 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9117 break;
9119 case WINED3D_TOP_SELECT_ARG2:
9120 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9121 break;
9123 case WINED3D_TOP_MODULATE:
9124 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9125 break;
9127 case WINED3D_TOP_MODULATE_4X:
9128 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9129 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9130 break;
9132 case WINED3D_TOP_MODULATE_2X:
9133 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9134 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9135 break;
9137 case WINED3D_TOP_ADD:
9138 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9139 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9140 break;
9142 case WINED3D_TOP_ADD_SIGNED:
9143 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9144 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9145 break;
9147 case WINED3D_TOP_ADD_SIGNED_2X:
9148 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9149 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9150 break;
9152 case WINED3D_TOP_SUBTRACT:
9153 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9154 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9155 break;
9157 case WINED3D_TOP_ADD_SMOOTH:
9158 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9159 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9160 break;
9162 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9163 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9164 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9165 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9166 break;
9168 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9169 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9170 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9171 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9172 break;
9174 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9175 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9176 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9177 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9178 break;
9180 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9181 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9182 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9183 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9184 break;
9186 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9187 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9188 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9189 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9190 break;
9192 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9193 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9194 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9195 break;
9197 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9198 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9199 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9200 break;
9202 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9203 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9204 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9205 break;
9206 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9207 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9208 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9209 break;
9211 case WINED3D_TOP_BUMPENVMAP:
9212 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9213 /* These are handled in the first pass, nothing to do. */
9214 break;
9216 case WINED3D_TOP_DOTPRODUCT3:
9217 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9218 dstreg, dstmask, arg1, arg2, dstmask);
9219 break;
9221 case WINED3D_TOP_MULTIPLY_ADD:
9222 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9223 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9224 break;
9226 case WINED3D_TOP_LERP:
9227 /* MSDN isn't quite right here. */
9228 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9229 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9230 break;
9232 default:
9233 FIXME("Unhandled operation %#x.\n", op);
9234 break;
9238 /* Context activation is done by the caller. */
9239 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9240 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9242 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9243 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9244 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9245 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9246 const struct wined3d_gl_info *gl_info = context->gl_info;
9247 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9248 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9249 UINT lowest_disabled_stage;
9250 GLuint shader_id;
9251 DWORD arg0, arg1, arg2;
9252 unsigned int stage;
9254 string_buffer_clear(buffer);
9256 /* Find out which textures are read */
9257 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9259 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9260 break;
9262 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9263 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9264 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9266 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9267 || (stage == 0 && settings->color_key_enabled))
9268 tex_map |= 1u << stage;
9269 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9270 tfactor_used = TRUE;
9271 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9272 tempreg_used = TRUE;
9273 if (settings->op[stage].dst == tempreg)
9274 tempreg_used = TRUE;
9275 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9276 tss_const_map |= 1u << stage;
9278 switch (settings->op[stage].cop)
9280 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9281 lum_map |= 1u << stage;
9282 /* fall through */
9283 case WINED3D_TOP_BUMPENVMAP:
9284 bump_map |= 1u << stage;
9285 /* fall through */
9286 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9287 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9288 tex_map |= 1u << stage;
9289 break;
9291 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9292 tfactor_used = TRUE;
9293 break;
9295 default:
9296 break;
9299 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9300 continue;
9302 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9303 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9304 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9306 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9307 tex_map |= 1u << stage;
9308 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9309 tfactor_used = TRUE;
9310 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9311 tempreg_used = TRUE;
9312 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9313 tss_const_map |= 1u << stage;
9315 lowest_disabled_stage = stage;
9317 shader_glsl_add_version_declaration(buffer, gl_info);
9319 if (shader_glsl_use_explicit_attrib_location(gl_info))
9320 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9321 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9322 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9323 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9324 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9326 if (!needs_legacy_glsl_syntax(gl_info))
9328 if (shader_glsl_use_explicit_attrib_location(gl_info))
9329 shader_addline(buffer, "layout(location = 0) ");
9330 shader_addline(buffer, "out vec4 ps_out[1];\n");
9333 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9334 shader_addline(buffer, "vec4 ret;\n");
9335 if (tempreg_used || settings->sRGB_write)
9336 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9337 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9339 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9341 const char *sampler_type;
9343 if (tss_const_map & (1u << stage))
9344 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9346 if (!(tex_map & (1u << stage)))
9347 continue;
9349 switch (settings->op[stage].tex_type)
9351 case WINED3D_GL_RES_TYPE_TEX_1D:
9352 sampler_type = "1D";
9353 break;
9354 case WINED3D_GL_RES_TYPE_TEX_2D:
9355 sampler_type = "2D";
9356 break;
9357 case WINED3D_GL_RES_TYPE_TEX_3D:
9358 sampler_type = "3D";
9359 break;
9360 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9361 sampler_type = "Cube";
9362 break;
9363 case WINED3D_GL_RES_TYPE_TEX_RECT:
9364 sampler_type = "2DRect";
9365 break;
9366 default:
9367 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9368 sampler_type = NULL;
9369 break;
9371 if (sampler_type)
9373 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9374 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9375 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9378 shader_addline(buffer, "vec4 tex%u;\n", stage);
9380 if (!(bump_map & (1u << stage)))
9381 continue;
9382 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9384 if (!(lum_map & (1u << stage)))
9385 continue;
9386 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9387 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9389 if (tfactor_used)
9390 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9391 if (settings->color_key_enabled)
9392 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9393 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9395 if (settings->sRGB_write)
9397 shader_addline(buffer, "const vec4 srgb_const0 = ");
9398 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9399 shader_addline(buffer, ";\n");
9400 shader_addline(buffer, "const vec4 srgb_const1 = ");
9401 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9402 shader_addline(buffer, ";\n");
9405 shader_addline(buffer, "uniform struct\n{\n");
9406 shader_addline(buffer, " vec4 color;\n");
9407 shader_addline(buffer, " float density;\n");
9408 shader_addline(buffer, " float end;\n");
9409 shader_addline(buffer, " float scale;\n");
9410 shader_addline(buffer, "} ffp_fog;\n");
9412 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9413 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9415 if (legacy_syntax)
9417 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9418 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9419 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9420 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9421 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9423 else
9425 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9426 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9427 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9428 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9429 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9432 shader_addline(buffer, "void main()\n{\n");
9434 if (legacy_syntax)
9436 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9437 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9440 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9442 if (tex_map & (1u << stage))
9444 if (settings->pointsprite)
9445 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9446 else if (settings->texcoords_initialized & (1u << stage))
9447 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9448 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9449 else
9450 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9454 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9455 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9457 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9458 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9460 /* Generate texture sampling instructions */
9461 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9463 const char *texture_function, *coord_mask;
9464 BOOL proj;
9466 if (!(tex_map & (1u << stage)))
9467 continue;
9469 if (settings->op[stage].projected == proj_none)
9471 proj = FALSE;
9473 else if (settings->op[stage].projected == proj_count4
9474 || settings->op[stage].projected == proj_count3)
9476 proj = TRUE;
9478 else
9480 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9481 proj = TRUE;
9484 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9485 proj = FALSE;
9487 switch (settings->op[stage].tex_type)
9489 case WINED3D_GL_RES_TYPE_TEX_1D:
9490 if (proj)
9492 texture_function = "texture1DProj";
9493 coord_mask = "xw";
9495 else
9497 texture_function = "texture1D";
9498 coord_mask = "x";
9500 break;
9501 case WINED3D_GL_RES_TYPE_TEX_2D:
9502 if (proj)
9504 texture_function = "texture2DProj";
9505 coord_mask = "xyw";
9507 else
9509 texture_function = "texture2D";
9510 coord_mask = "xy";
9512 break;
9513 case WINED3D_GL_RES_TYPE_TEX_3D:
9514 if (proj)
9516 texture_function = "texture3DProj";
9517 coord_mask = "xyzw";
9519 else
9521 texture_function = "texture3D";
9522 coord_mask = "xyz";
9524 break;
9525 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9526 texture_function = "textureCube";
9527 coord_mask = "xyz";
9528 break;
9529 case WINED3D_GL_RES_TYPE_TEX_RECT:
9530 if (proj)
9532 texture_function = "texture2DRectProj";
9533 coord_mask = "xyw";
9535 else
9537 texture_function = "texture2DRect";
9538 coord_mask = "xy";
9540 break;
9541 default:
9542 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9543 texture_function = "";
9544 coord_mask = "xyzw";
9545 break;
9547 if (!legacy_syntax)
9548 texture_function = proj ? "textureProj" : "texture";
9550 if (stage > 0
9551 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9552 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9554 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9556 /* With projective textures, texbem only divides the static
9557 * texture coord, not the displacement, so multiply the
9558 * displacement with the dividing parameter before passing it to
9559 * TXP. */
9560 if (settings->op[stage].projected != proj_none)
9562 if (settings->op[stage].projected == proj_count4)
9564 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9565 stage, stage);
9566 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9568 else
9570 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9571 stage, stage);
9572 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9575 else
9577 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9580 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9581 stage, texture_function, stage, coord_mask);
9583 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9584 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9585 stage, stage - 1, stage - 1, stage - 1);
9587 else if (settings->op[stage].projected == proj_count3)
9589 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9590 stage, texture_function, stage, stage);
9592 else
9594 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9595 stage, texture_function, stage, stage, coord_mask);
9598 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9599 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9600 settings->op[stage].color_fixup);
9603 if (settings->color_key_enabled)
9605 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9606 shader_addline(buffer, " discard;\n");
9609 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9611 /* Generate the main shader */
9612 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9614 BOOL op_equal;
9616 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9617 break;
9619 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9620 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9621 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9622 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9623 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9624 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9625 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9626 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9627 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9628 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9629 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9630 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9631 else
9632 op_equal = settings->op[stage].aop == settings->op[stage].cop
9633 && settings->op[stage].carg0 == settings->op[stage].aarg0
9634 && settings->op[stage].carg1 == settings->op[stage].aarg1
9635 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9637 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9639 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9640 settings->op[stage].cop, settings->op[stage].carg0,
9641 settings->op[stage].carg1, settings->op[stage].carg2);
9643 else if (op_equal)
9645 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
9646 settings->op[stage].cop, settings->op[stage].carg0,
9647 settings->op[stage].carg1, settings->op[stage].carg2);
9649 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9650 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9652 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
9653 settings->op[stage].cop, settings->op[stage].carg0,
9654 settings->op[stage].carg1, settings->op[stage].carg2);
9655 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
9656 settings->op[stage].aop, settings->op[stage].aarg0,
9657 settings->op[stage].aarg1, settings->op[stage].aarg2);
9661 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9662 get_fragment_output(gl_info));
9664 if (settings->sRGB_write)
9665 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9667 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9669 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9671 shader_addline(buffer, "}\n");
9673 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9674 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9676 string_buffer_release(&priv->string_buffers, tex_reg_name);
9677 return shader_id;
9680 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9681 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9683 struct glsl_ffp_vertex_shader *shader;
9684 const struct wine_rb_entry *entry;
9686 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9687 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9689 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
9690 return NULL;
9692 shader->desc.settings = *settings;
9693 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9694 list_init(&shader->linked_programs);
9695 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9696 ERR("Failed to insert ffp vertex shader.\n");
9698 return shader;
9701 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9702 const struct ffp_frag_settings *args, const struct wined3d_context *context)
9704 struct glsl_ffp_fragment_shader *glsl_desc;
9705 const struct ffp_frag_desc *desc;
9707 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9708 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9710 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
9711 return NULL;
9713 glsl_desc->entry.settings = *args;
9714 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
9715 list_init(&glsl_desc->linked_programs);
9716 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9718 return glsl_desc;
9722 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9723 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9725 unsigned int i;
9726 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9728 for (i = 0; i < vs_c_count; ++i)
9730 string_buffer_sprintf(name, "vs_c[%u]", i);
9731 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9733 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9735 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9737 string_buffer_sprintf(name, "vs_i[%u]", i);
9738 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9741 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9743 string_buffer_sprintf(name, "vs_b[%u]", i);
9744 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9747 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9749 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9751 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9752 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9754 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9755 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9756 for (i = 0; i < MAX_TEXTURES; ++i)
9758 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9759 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9761 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9762 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9763 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9764 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9765 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9766 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9767 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
9769 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9770 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9771 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9772 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9773 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9774 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9775 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9776 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9777 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9778 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9779 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9780 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9781 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9782 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9783 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9784 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9785 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9786 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9787 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9788 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9789 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9790 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9791 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9792 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9794 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
9795 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
9796 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
9797 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
9798 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
9799 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
9800 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
9802 string_buffer_release(&priv->string_buffers, name);
9805 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
9806 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
9808 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9811 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
9812 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
9814 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9817 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
9818 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
9820 unsigned int i;
9821 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9823 for (i = 0; i < ps_c_count; ++i)
9825 string_buffer_sprintf(name, "ps_c[%u]", i);
9826 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9828 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
9830 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9832 string_buffer_sprintf(name, "ps_i[%u]", i);
9833 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9836 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9838 string_buffer_sprintf(name, "ps_b[%u]", i);
9839 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9842 for (i = 0; i < MAX_TEXTURES; ++i)
9844 string_buffer_sprintf(name, "bumpenv_mat%u", i);
9845 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9846 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
9847 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9848 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
9849 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9850 string_buffer_sprintf(name, "tss_const%u", i);
9851 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9854 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
9855 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
9857 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
9858 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
9859 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
9860 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
9862 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
9864 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
9865 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
9866 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
9868 string_buffer_release(&priv->string_buffers, name);
9871 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
9872 const struct wined3d_context *context, struct wined3d_shader *shader)
9874 struct glsl_context_data *ctx_data = context->shader_backend_data;
9875 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9876 const struct wined3d_gl_info *gl_info = context->gl_info;
9877 struct glsl_cs_compiled_shader *gl_shaders;
9878 struct glsl_shader_private *shader_data;
9879 struct glsl_shader_prog_link *entry;
9880 GLuint shader_id, program_id;
9882 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
9884 ERR("Out of memory.\n");
9885 return E_OUTOFMEMORY;
9888 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
9890 ERR("Failed to allocate backend data.\n");
9891 HeapFree(GetProcessHeap(), 0, entry);
9892 return E_OUTOFMEMORY;
9894 shader_data = shader->backend_data;
9895 gl_shaders = shader_data->gl_shaders.cs;
9897 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
9899 ERR("Failed to allocate GL shader array.\n");
9900 HeapFree(GetProcessHeap(), 0, entry);
9901 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9902 shader->backend_data = NULL;
9903 return E_OUTOFMEMORY;
9905 shader_data->shader_array_size = 1;
9906 gl_shaders = shader_data->gl_shaders.cs;
9908 TRACE("Compiling compute shader %p.\n", shader);
9910 string_buffer_clear(buffer);
9911 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
9912 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
9914 program_id = GL_EXTCALL(glCreateProgram());
9915 TRACE("Created new GLSL shader program %u.\n", program_id);
9917 entry->id = program_id;
9918 entry->vs.id = 0;
9919 entry->hs.id = 0;
9920 entry->ds.id = 0;
9921 entry->gs.id = 0;
9922 entry->ps.id = 0;
9923 entry->cs.id = shader_id;
9924 entry->constant_version = 0;
9925 entry->shader_controlled_clip_distances = 0;
9926 entry->ps.np2_fixup_info = NULL;
9927 add_glsl_program_entry(priv, entry);
9929 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
9930 GL_EXTCALL(glAttachShader(program_id, shader_id));
9931 checkGLcall("glAttachShader");
9933 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
9935 TRACE("Linking GLSL shader program %u.\n", program_id);
9936 GL_EXTCALL(glLinkProgram(program_id));
9937 shader_glsl_validate_link(gl_info, program_id);
9939 GL_EXTCALL(glUseProgram(program_id));
9940 checkGLcall("glUseProgram");
9941 shader_glsl_load_program_resources(context, priv, program_id, shader);
9942 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
9944 entry->constant_update_mask = 0;
9946 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
9947 checkGLcall("glUseProgram");
9948 return WINED3D_OK;
9951 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
9952 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
9954 struct glsl_shader_private *shader_data;
9956 if (!shader->backend_data)
9958 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
9959 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
9961 ERR("Failed to compile compute shader %p.\n", shader);
9962 return 0;
9965 shader_data = shader->backend_data;
9966 return shader_data->gl_shaders.cs[0].id;
9969 /* Context activation is done by the caller. */
9970 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
9971 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
9973 struct glsl_shader_prog_link *entry;
9974 struct wined3d_shader *shader;
9975 struct glsl_program_key key;
9976 GLuint cs_id;
9978 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
9979 return;
9981 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
9983 WARN("Compute shader is NULL.\n");
9984 ctx_data->glsl_program = NULL;
9985 return;
9988 cs_id = find_glsl_compute_shader(context, priv, shader);
9989 memset(&key, 0, sizeof(key));
9990 key.cs_id = cs_id;
9991 if (!(entry = get_glsl_program_entry(priv, &key)))
9992 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
9993 ctx_data->glsl_program = entry;
9996 /* Context activation is done by the caller. */
9997 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
9998 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10000 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
10001 const struct wined3d_gl_info *gl_info = context->gl_info;
10002 const struct wined3d_shader *pre_rasterization_shader;
10003 const struct ps_np2fixup_info *np2fixup_info = NULL;
10004 struct wined3d_shader *hshader, *dshader, *gshader;
10005 struct glsl_shader_prog_link *entry = NULL;
10006 struct wined3d_shader *vshader = NULL;
10007 struct wined3d_shader *pshader = NULL;
10008 GLuint reorder_shader_id = 0;
10009 struct glsl_program_key key;
10010 GLuint program_id;
10011 unsigned int i;
10012 GLuint vs_id = 0;
10013 GLuint hs_id = 0;
10014 GLuint ds_id = 0;
10015 GLuint gs_id = 0;
10016 GLuint ps_id = 0;
10017 struct list *ps_list, *vs_list;
10018 WORD attribs_map;
10019 struct wined3d_string_buffer *tmp_name;
10021 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10023 vs_id = ctx_data->glsl_program->vs.id;
10024 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10026 if (use_vs(state))
10027 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10029 else if (use_vs(state))
10031 struct vs_compile_args vs_compile_args;
10033 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10035 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
10036 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
10037 vs_list = &vshader->linked_programs;
10039 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10041 struct glsl_ffp_vertex_shader *ffp_shader;
10042 struct wined3d_ffp_vs_settings settings;
10044 wined3d_ffp_get_vs_settings(context, state, &settings);
10045 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10046 vs_id = ffp_shader->id;
10047 vs_list = &ffp_shader->linked_programs;
10050 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10051 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10052 hs_id = ctx_data->glsl_program->hs.id;
10053 else if (hshader)
10054 hs_id = find_glsl_hull_shader(context, priv, hshader);
10056 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10057 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10059 ds_id = ctx_data->glsl_program->ds.id;
10061 else if (dshader)
10063 struct ds_compile_args args;
10065 find_ds_compile_args(state, dshader, &args, context);
10066 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
10069 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10070 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10072 gs_id = ctx_data->glsl_program->gs.id;
10074 else if (gshader)
10076 struct gs_compile_args args;
10078 find_gs_compile_args(state, gshader, &args, context);
10079 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
10082 /* A pixel shader is not used when rasterization is disabled. */
10083 if (is_rasterization_disabled(gshader))
10085 ps_id = 0;
10086 ps_list = NULL;
10088 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10090 ps_id = ctx_data->glsl_program->ps.id;
10091 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10093 if (use_ps(state))
10094 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10096 else if (use_ps(state))
10098 struct ps_compile_args ps_compile_args;
10099 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10100 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
10101 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
10102 pshader, &ps_compile_args, &np2fixup_info);
10103 ps_list = &pshader->linked_programs;
10105 else if (priv->fragment_pipe == &glsl_fragment_pipe
10106 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10108 struct glsl_ffp_fragment_shader *ffp_shader;
10109 struct ffp_frag_settings settings;
10111 gen_ffp_frag_op(context, state, &settings, FALSE);
10112 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
10113 ps_id = ffp_shader->id;
10114 ps_list = &ffp_shader->linked_programs;
10117 key.vs_id = vs_id;
10118 key.hs_id = hs_id;
10119 key.ds_id = ds_id;
10120 key.gs_id = gs_id;
10121 key.ps_id = ps_id;
10122 key.cs_id = 0;
10123 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10125 ctx_data->glsl_program = entry;
10126 return;
10129 /* If we get to this point, then no matching program exists, so we create one */
10130 program_id = GL_EXTCALL(glCreateProgram());
10131 TRACE("Created new GLSL shader program %u.\n", program_id);
10133 /* Create the entry */
10134 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
10135 entry->id = program_id;
10136 entry->vs.id = vs_id;
10137 entry->hs.id = hs_id;
10138 entry->ds.id = ds_id;
10139 entry->gs.id = gs_id;
10140 entry->ps.id = ps_id;
10141 entry->cs.id = 0;
10142 entry->constant_version = 0;
10143 entry->shader_controlled_clip_distances = 0;
10144 entry->ps.np2_fixup_info = np2fixup_info;
10145 /* Add the hash table entry */
10146 add_glsl_program_entry(priv, entry);
10148 /* Set the current program */
10149 ctx_data->glsl_program = entry;
10151 /* Attach GLSL vshader */
10152 if (vs_id)
10154 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10155 GL_EXTCALL(glAttachShader(program_id, vs_id));
10156 checkGLcall("glAttachShader");
10158 list_add_head(vs_list, &entry->vs.shader_entry);
10161 if (vshader)
10163 attribs_map = vshader->reg_maps.input_registers;
10164 if (vshader->reg_maps.shader_version.major < 4)
10166 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10167 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10168 d3d_info->emulated_flatshading
10169 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10170 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10171 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10172 checkGLcall("glAttachShader");
10173 /* Flag the reorder function for deletion, it will be freed
10174 * automatically when the program is destroyed. */
10175 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10178 else
10180 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10183 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10185 /* Bind vertex attributes to a corresponding index number to match
10186 * the same index numbers as ARB_vertex_programs (makes loading
10187 * vertex attributes simpler). With this method, we can use the
10188 * exact same code to load the attributes later for both ARB and
10189 * GLSL shaders.
10191 * We have to do this here because we need to know the Program ID
10192 * in order to make the bindings work, and it has to be done prior
10193 * to linking the GLSL program. */
10194 tmp_name = string_buffer_get(&priv->string_buffers);
10195 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10197 if (!(attribs_map & 1))
10198 continue;
10200 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10201 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10202 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10204 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10205 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10206 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10207 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10210 checkGLcall("glBindAttribLocation");
10211 string_buffer_release(&priv->string_buffers, tmp_name);
10213 if (!needs_legacy_glsl_syntax(gl_info))
10215 GL_EXTCALL(glBindFragDataLocation(program_id, 0, "ps_out"));
10216 checkGLcall("glBindFragDataLocation");
10220 if (hshader)
10222 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10223 GL_EXTCALL(glAttachShader(program_id, hs_id));
10224 checkGLcall("glAttachShader");
10226 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10229 if (dshader)
10231 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10232 GL_EXTCALL(glAttachShader(program_id, ds_id));
10233 checkGLcall("glAttachShader");
10235 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10238 if (gshader)
10240 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10241 GL_EXTCALL(glAttachShader(program_id, gs_id));
10242 checkGLcall("glAttachShader");
10244 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10246 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10249 /* Attach GLSL pshader */
10250 if (ps_id)
10252 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10253 GL_EXTCALL(glAttachShader(program_id, ps_id));
10254 checkGLcall("glAttachShader");
10256 list_add_head(ps_list, &entry->ps.shader_entry);
10259 /* Link the program */
10260 TRACE("Linking GLSL shader program %u.\n", program_id);
10261 GL_EXTCALL(glLinkProgram(program_id));
10262 shader_glsl_validate_link(gl_info, program_id);
10264 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10265 vshader ? vshader->limits->constant_float : 0);
10266 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10267 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10268 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10269 pshader ? pshader->limits->constant_float : 0);
10270 checkGLcall("find glsl program uniform locations");
10272 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10273 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10275 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10276 entry->shader_controlled_clip_distances = 1;
10277 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10280 if (needs_legacy_glsl_syntax(gl_info))
10282 if (pshader && pshader->reg_maps.shader_version.major >= 3
10283 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10285 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10286 entry->vs.vertex_color_clamp = GL_FALSE;
10288 else
10290 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10293 else
10295 /* With core profile we never change vertex_color_clamp from
10296 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10297 * glClampColorARB(). */
10298 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10301 /* Set the shader to allow uniform loading on it */
10302 GL_EXTCALL(glUseProgram(program_id));
10303 checkGLcall("glUseProgram");
10305 entry->constant_update_mask = 0;
10306 if (vshader)
10308 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10309 if (vshader->reg_maps.integer_constants)
10310 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10311 if (vshader->reg_maps.boolean_constants)
10312 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10313 if (entry->vs.pos_fixup_location != -1)
10314 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10316 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10318 else
10320 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10321 | WINED3D_SHADER_CONST_FFP_PROJ;
10323 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10325 if (entry->vs.modelview_matrix_location[i] != -1)
10327 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10328 break;
10332 for (i = 0; i < MAX_TEXTURES; ++i)
10334 if (entry->vs.texture_matrix_location[i] != -1)
10336 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10337 break;
10340 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10341 || entry->vs.material_specular_location != -1
10342 || entry->vs.material_emissive_location != -1
10343 || entry->vs.material_shininess_location != -1)
10344 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10345 if (entry->vs.light_ambient_location != -1)
10346 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10348 if (entry->vs.clip_planes_location != -1)
10349 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10350 if (entry->vs.pointsize_min_location != -1)
10351 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10353 if (hshader)
10354 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10356 if (dshader)
10358 if (entry->ds.pos_fixup_location != -1)
10359 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10361 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10364 if (gshader)
10366 if (entry->gs.pos_fixup_location != -1)
10367 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10369 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10372 if (ps_id)
10374 if (pshader)
10376 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10377 if (pshader->reg_maps.integer_constants)
10378 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10379 if (pshader->reg_maps.boolean_constants)
10380 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10381 if (entry->ps.ycorrection_location != -1)
10382 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10384 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10385 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10387 else
10389 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10391 shader_glsl_load_samplers(context, priv, program_id, NULL);
10394 for (i = 0; i < MAX_TEXTURES; ++i)
10396 if (entry->ps.bumpenv_mat_location[i] != -1)
10398 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10399 break;
10403 if (entry->ps.fog_color_location != -1)
10404 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10405 if (entry->ps.alpha_test_ref_location != -1)
10406 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10407 if (entry->ps.np2_fixup_location != -1)
10408 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10409 if (entry->ps.color_key_location != -1)
10410 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10414 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10416 struct wined3d_device *device = shader->device;
10417 struct wined3d_context *context;
10419 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10421 context = context_acquire(device, NULL, 0);
10422 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10423 context_release(context);
10427 /* Context activation is done by the caller. */
10428 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10429 const struct wined3d_state *state)
10431 struct glsl_context_data *ctx_data = context->shader_backend_data;
10432 const struct wined3d_gl_info *gl_info = context->gl_info;
10433 struct shader_glsl_priv *priv = shader_priv;
10434 struct glsl_shader_prog_link *glsl_program;
10435 GLenum current_vertex_color_clamp;
10436 GLuint program_id, prev_id;
10438 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10439 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10441 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10442 set_glsl_shader_program(context, state, priv, ctx_data);
10443 glsl_program = ctx_data->glsl_program;
10445 if (glsl_program)
10447 program_id = glsl_program->id;
10448 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10449 if (glsl_program->shader_controlled_clip_distances)
10450 context_enable_clip_distances(context, glsl_program->clip_distance_mask);
10452 else
10454 program_id = 0;
10455 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10458 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10460 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10461 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10463 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10464 checkGLcall("glClampColorARB");
10466 else
10468 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10472 TRACE("Using GLSL program %u.\n", program_id);
10474 if (prev_id != program_id)
10476 GL_EXTCALL(glUseProgram(program_id));
10477 checkGLcall("glUseProgram");
10479 if (glsl_program)
10480 context->constant_update_mask |= glsl_program->constant_update_mask;
10483 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10486 /* Context activation is done by the caller. */
10487 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10488 const struct wined3d_state *state)
10490 struct glsl_context_data *ctx_data = context->shader_backend_data;
10491 const struct wined3d_gl_info *gl_info = context->gl_info;
10492 struct shader_glsl_priv *priv = shader_priv;
10493 GLuint program_id, prev_id;
10495 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10496 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10497 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10499 TRACE("Using GLSL program %u.\n", program_id);
10501 if (prev_id != program_id)
10503 GL_EXTCALL(glUseProgram(program_id));
10504 checkGLcall("glUseProgram");
10507 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10508 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10509 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10510 | (1u << WINED3D_SHADER_TYPE_HULL)
10511 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10514 /* "context" is not necessarily the currently active context. */
10515 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10517 struct glsl_context_data *ctx_data = context->shader_backend_data;
10519 ctx_data->glsl_program = NULL;
10520 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10521 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10522 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10523 | (1u << WINED3D_SHADER_TYPE_HULL)
10524 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10525 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10528 /* Context activation is done by the caller. */
10529 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10531 struct glsl_context_data *ctx_data = context->shader_backend_data;
10532 const struct wined3d_gl_info *gl_info = context->gl_info;
10533 struct shader_glsl_priv *priv = shader_priv;
10535 shader_glsl_invalidate_current_program(context);
10536 GL_EXTCALL(glUseProgram(0));
10537 checkGLcall("glUseProgram");
10539 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10540 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10542 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10544 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10545 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10546 checkGLcall("glClampColorARB");
10550 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10551 const struct glsl_shader_prog_link *program)
10553 const struct glsl_context_data *ctx_data;
10554 struct wined3d_context *context;
10555 unsigned int i;
10557 for (i = 0; i < device->context_count; ++i)
10559 context = device->contexts[i];
10560 ctx_data = context->shader_backend_data;
10562 if (ctx_data->glsl_program == program)
10563 shader_glsl_invalidate_current_program(context);
10567 static void shader_glsl_destroy(struct wined3d_shader *shader)
10569 struct glsl_shader_private *shader_data = shader->backend_data;
10570 struct wined3d_device *device = shader->device;
10571 struct shader_glsl_priv *priv = device->shader_priv;
10572 const struct wined3d_gl_info *gl_info;
10573 const struct list *linked_programs;
10574 struct wined3d_context *context;
10576 if (!shader_data || !shader_data->num_gl_shaders)
10578 HeapFree(GetProcessHeap(), 0, shader_data);
10579 shader->backend_data = NULL;
10580 return;
10583 context = context_acquire(device, NULL, 0);
10584 gl_info = context->gl_info;
10586 TRACE("Deleting linked programs.\n");
10587 linked_programs = &shader->linked_programs;
10588 if (linked_programs->next)
10590 struct glsl_shader_prog_link *entry, *entry2;
10591 UINT i;
10593 switch (shader->reg_maps.shader_version.type)
10595 case WINED3D_SHADER_TYPE_PIXEL:
10597 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10599 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10601 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10602 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10603 checkGLcall("glDeleteShader");
10605 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
10607 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10608 struct glsl_shader_prog_link, ps.shader_entry)
10610 shader_glsl_invalidate_contexts_program(device, entry);
10611 delete_glsl_program_entry(priv, gl_info, entry);
10614 break;
10617 case WINED3D_SHADER_TYPE_VERTEX:
10619 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10621 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10623 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10624 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10625 checkGLcall("glDeleteShader");
10627 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
10629 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10630 struct glsl_shader_prog_link, vs.shader_entry)
10632 shader_glsl_invalidate_contexts_program(device, entry);
10633 delete_glsl_program_entry(priv, gl_info, entry);
10636 break;
10639 case WINED3D_SHADER_TYPE_HULL:
10641 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10643 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10645 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10646 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10647 checkGLcall("glDeleteShader");
10649 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.hs);
10651 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10652 struct glsl_shader_prog_link, hs.shader_entry)
10654 shader_glsl_invalidate_contexts_program(device, entry);
10655 delete_glsl_program_entry(priv, gl_info, entry);
10658 break;
10661 case WINED3D_SHADER_TYPE_DOMAIN:
10663 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10665 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10667 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10668 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10669 checkGLcall("glDeleteShader");
10671 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ds);
10673 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10674 struct glsl_shader_prog_link, ds.shader_entry)
10676 shader_glsl_invalidate_contexts_program(device, entry);
10677 delete_glsl_program_entry(priv, gl_info, entry);
10680 break;
10683 case WINED3D_SHADER_TYPE_GEOMETRY:
10685 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10687 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10689 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10690 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10691 checkGLcall("glDeleteShader");
10693 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
10695 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10696 struct glsl_shader_prog_link, gs.shader_entry)
10698 shader_glsl_invalidate_contexts_program(device, entry);
10699 delete_glsl_program_entry(priv, gl_info, entry);
10702 break;
10705 case WINED3D_SHADER_TYPE_COMPUTE:
10707 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10709 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10711 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10712 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10713 checkGLcall("glDeleteShader");
10715 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
10717 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10718 struct glsl_shader_prog_link, cs.shader_entry)
10720 shader_glsl_invalidate_contexts_program(device, entry);
10721 delete_glsl_program_entry(priv, gl_info, entry);
10724 break;
10727 default:
10728 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10729 break;
10733 HeapFree(GetProcessHeap(), 0, shader->backend_data);
10734 shader->backend_data = NULL;
10736 context_release(context);
10739 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10741 const struct glsl_program_key *k = key;
10742 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10743 const struct glsl_shader_prog_link, program_lookup_entry);
10745 if (k->vs_id > prog->vs.id) return 1;
10746 else if (k->vs_id < prog->vs.id) return -1;
10748 if (k->gs_id > prog->gs.id) return 1;
10749 else if (k->gs_id < prog->gs.id) return -1;
10751 if (k->ps_id > prog->ps.id) return 1;
10752 else if (k->ps_id < prog->ps.id) return -1;
10754 if (k->hs_id > prog->hs.id) return 1;
10755 else if (k->hs_id < prog->hs.id) return -1;
10757 if (k->ds_id > prog->ds.id) return 1;
10758 else if (k->ds_id < prog->ds.id) return -1;
10760 if (k->cs_id > prog->cs.id) return 1;
10761 else if (k->cs_id < prog->cs.id) return -1;
10763 return 0;
10766 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10768 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10769 + constant_count * sizeof(*heap->contained)
10770 + constant_count * sizeof(*heap->positions);
10771 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
10773 if (!mem)
10775 ERR("Failed to allocate memory\n");
10776 return FALSE;
10779 heap->entries = mem;
10780 heap->entries[1].version = 0;
10781 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10782 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10783 heap->positions = (unsigned int *)(heap->contained + constant_count);
10784 heap->size = 1;
10786 return TRUE;
10789 static void constant_heap_free(struct constant_heap *heap)
10791 HeapFree(GetProcessHeap(), 0, heap->entries);
10794 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
10795 const struct fragment_pipeline *fragment_pipe)
10797 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
10798 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
10799 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
10800 struct fragment_caps fragment_caps;
10801 void *vertex_priv, *fragment_priv;
10803 string_buffer_list_init(&priv->string_buffers);
10805 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
10807 ERR("Failed to initialize vertex pipe.\n");
10808 HeapFree(GetProcessHeap(), 0, priv);
10809 return E_FAIL;
10812 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
10814 ERR("Failed to initialize fragment pipe.\n");
10815 vertex_pipe->vp_free(device);
10816 HeapFree(GetProcessHeap(), 0, priv);
10817 return E_FAIL;
10820 if (!string_buffer_init(&priv->shader_buffer))
10822 ERR("Failed to initialize shader buffer.\n");
10823 goto fail;
10826 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
10828 ERR("Failed to allocate memory.\n");
10829 goto fail;
10832 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
10834 ERR("Failed to initialize vertex shader constant heap\n");
10835 goto fail;
10838 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
10840 ERR("Failed to initialize pixel shader constant heap\n");
10841 goto fail;
10844 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
10846 priv->next_constant_version = 1;
10847 priv->vertex_pipe = vertex_pipe;
10848 priv->fragment_pipe = fragment_pipe;
10849 fragment_pipe->get_caps(gl_info, &fragment_caps);
10850 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
10851 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
10853 device->vertex_priv = vertex_priv;
10854 device->fragment_priv = fragment_priv;
10855 device->shader_priv = priv;
10857 return WINED3D_OK;
10859 fail:
10860 constant_heap_free(&priv->pconst_heap);
10861 constant_heap_free(&priv->vconst_heap);
10862 HeapFree(GetProcessHeap(), 0, priv->stack);
10863 string_buffer_free(&priv->shader_buffer);
10864 fragment_pipe->free_private(device);
10865 vertex_pipe->vp_free(device);
10866 HeapFree(GetProcessHeap(), 0, priv);
10867 return E_OUTOFMEMORY;
10870 /* Context activation is done by the caller. */
10871 static void shader_glsl_free(struct wined3d_device *device)
10873 struct shader_glsl_priv *priv = device->shader_priv;
10875 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
10876 constant_heap_free(&priv->pconst_heap);
10877 constant_heap_free(&priv->vconst_heap);
10878 HeapFree(GetProcessHeap(), 0, priv->stack);
10879 string_buffer_list_cleanup(&priv->string_buffers);
10880 string_buffer_free(&priv->shader_buffer);
10881 priv->fragment_pipe->free_private(device);
10882 priv->vertex_pipe->vp_free(device);
10884 HeapFree(GetProcessHeap(), 0, device->shader_priv);
10885 device->shader_priv = NULL;
10888 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
10890 struct glsl_context_data *ctx_data;
10891 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
10892 return FALSE;
10893 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10894 context->shader_backend_data = ctx_data;
10895 return TRUE;
10898 static void shader_glsl_free_context_data(struct wined3d_context *context)
10900 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
10903 static void shader_glsl_init_context_state(struct wined3d_context *context)
10905 const struct wined3d_gl_info *gl_info = context->gl_info;
10907 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
10908 checkGLcall("GL_PROGRAM_POINT_SIZE");
10911 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
10913 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
10914 && gl_info->supported[WINED3D_GL_VERSION_3_2]
10915 && gl_info->supported[ARB_SAMPLER_OBJECTS]
10916 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
10917 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
10919 if (shader_model_4
10920 && gl_info->supported[ARB_COMPUTE_SHADER]
10921 && gl_info->supported[ARB_CULL_DISTANCE]
10922 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
10923 && gl_info->supported[ARB_DRAW_INDIRECT]
10924 && gl_info->supported[ARB_GPU_SHADER5]
10925 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
10926 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
10927 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
10928 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
10929 && gl_info->supported[ARB_TESSELLATION_SHADER]
10930 && gl_info->supported[ARB_TEXTURE_GATHER]
10931 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
10932 return 5;
10934 if (shader_model_4)
10935 return 4;
10937 /* Support for texldd and texldl instructions in pixel shaders is required
10938 * for SM3. */
10939 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
10940 return 3;
10942 return 2;
10945 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
10947 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
10949 TRACE("Shader model %u.\n", shader_model);
10951 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
10952 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
10953 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
10954 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
10955 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
10956 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
10958 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
10959 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
10961 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
10962 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
10963 caps->varying_count = gl_info->limits.glsl_varyings;
10965 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
10966 * Direct3D minimum requirement.
10968 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
10969 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
10971 * The problem is that the refrast clamps temporary results in the shader to
10972 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
10973 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
10974 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
10975 * offer a way to query this.
10977 if (shader_model >= 4)
10978 caps->ps_1x_max_value = FLT_MAX;
10979 else
10980 caps->ps_1x_max_value = 1024.0f;
10982 /* Ideally we'd only set caps like sRGB writes here if supported by both
10983 * the shader backend and the fragment pipe, but we can get called before
10984 * shader_glsl_alloc(). */
10985 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
10986 | WINED3D_SHADER_CAP_SRGB_WRITE;
10989 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
10991 /* We support everything except YUV conversions. */
10992 return !is_complex_fixup(fixup);
10995 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
10997 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
10998 /* WINED3DSIH_ADD */ shader_glsl_binop,
10999 /* WINED3DSIH_AND */ shader_glsl_binop,
11000 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11001 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11002 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11003 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11004 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11005 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11006 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11007 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11008 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11009 /* WINED3DSIH_BEM */ shader_glsl_bem,
11010 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11011 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11012 /* WINED3DSIH_BREAK */ shader_glsl_break,
11013 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11014 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11015 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11016 /* WINED3DSIH_CALL */ shader_glsl_call,
11017 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11018 /* WINED3DSIH_CASE */ shader_glsl_case,
11019 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11020 /* WINED3DSIH_CND */ shader_glsl_cnd,
11021 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11022 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11023 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11024 /* WINED3DSIH_CRS */ shader_glsl_cross,
11025 /* WINED3DSIH_CUT */ shader_glsl_cut,
11026 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11027 /* WINED3DSIH_DCL */ shader_glsl_nop,
11028 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11029 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11030 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11031 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11032 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11033 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11034 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11035 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11036 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11037 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11038 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11039 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11040 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11041 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11042 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11043 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
11044 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
11045 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11046 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11047 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11048 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11049 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11050 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11051 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11052 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11053 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11054 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11055 /* WINED3DSIH_DCL_STREAM */ NULL,
11056 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11057 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11058 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11059 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11060 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11061 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11062 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11063 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11064 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11065 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11066 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11067 /* WINED3DSIH_DEF */ shader_glsl_nop,
11068 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11069 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11070 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11071 /* WINED3DSIH_DIV */ shader_glsl_binop,
11072 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11073 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11074 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11075 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11076 /* WINED3DSIH_DST */ shader_glsl_dst,
11077 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11078 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11079 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11080 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11081 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11082 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11083 /* WINED3DSIH_ELSE */ shader_glsl_else,
11084 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11085 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11086 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11087 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11088 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11089 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11090 /* WINED3DSIH_EQ */ shader_glsl_relop,
11091 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11092 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11093 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11094 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11095 /* WINED3DSIH_FCALL */ NULL,
11096 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11097 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11098 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11099 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11100 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11101 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11102 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11103 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11104 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11105 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11106 /* WINED3DSIH_GE */ shader_glsl_relop,
11107 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11108 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11109 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11110 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11111 /* WINED3DSIH_IADD */ shader_glsl_binop,
11112 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11113 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11114 /* WINED3DSIH_IF */ shader_glsl_if,
11115 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11116 /* WINED3DSIH_IGE */ shader_glsl_relop,
11117 /* WINED3DSIH_ILT */ shader_glsl_relop,
11118 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11119 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11120 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11121 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11122 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11123 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11124 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11125 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11126 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11127 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11128 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11129 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11130 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11131 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11132 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11133 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11134 /* WINED3DSIH_INE */ shader_glsl_relop,
11135 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11136 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11137 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11138 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11139 /* WINED3DSIH_LABEL */ shader_glsl_label,
11140 /* WINED3DSIH_LD */ shader_glsl_ld,
11141 /* WINED3DSIH_LD2DMS */ NULL,
11142 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11143 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11144 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11145 /* WINED3DSIH_LIT */ shader_glsl_lit,
11146 /* WINED3DSIH_LOD */ NULL,
11147 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11148 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11149 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11150 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11151 /* WINED3DSIH_LT */ shader_glsl_relop,
11152 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11153 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11154 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11155 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11156 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11157 /* WINED3DSIH_MAD */ shader_glsl_mad,
11158 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11159 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11160 /* WINED3DSIH_MOV */ shader_glsl_mov,
11161 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11162 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11163 /* WINED3DSIH_MUL */ shader_glsl_binop,
11164 /* WINED3DSIH_NE */ shader_glsl_relop,
11165 /* WINED3DSIH_NOP */ shader_glsl_nop,
11166 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11167 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11168 /* WINED3DSIH_OR */ shader_glsl_binop,
11169 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11170 /* WINED3DSIH_POW */ shader_glsl_pow,
11171 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11172 /* WINED3DSIH_REP */ shader_glsl_rep,
11173 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11174 /* WINED3DSIH_RET */ shader_glsl_ret,
11175 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11176 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11177 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11178 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11179 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11180 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11181 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11182 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11183 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11184 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11185 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11186 /* WINED3DSIH_SAMPLE_INFO */ NULL,
11187 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11188 /* WINED3DSIH_SAMPLE_POS */ NULL,
11189 /* WINED3DSIH_SETP */ NULL,
11190 /* WINED3DSIH_SGE */ shader_glsl_compare,
11191 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11192 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11193 /* WINED3DSIH_SLT */ shader_glsl_compare,
11194 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11195 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11196 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11197 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11198 /* WINED3DSIH_SUB */ shader_glsl_binop,
11199 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11200 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11201 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11202 /* WINED3DSIH_TEX */ shader_glsl_tex,
11203 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11204 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11205 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11206 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11207 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11208 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11209 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11210 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11211 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11212 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11213 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11214 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11215 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11216 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11217 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11218 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11219 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11220 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11221 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11222 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11223 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11224 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11225 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11226 /* WINED3DSIH_UGE */ shader_glsl_relop,
11227 /* WINED3DSIH_ULT */ shader_glsl_relop,
11228 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11229 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11230 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11231 /* WINED3DSIH_USHR */ shader_glsl_binop,
11232 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11233 /* WINED3DSIH_XOR */ shader_glsl_binop,
11236 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11237 SHADER_HANDLER hw_fct;
11239 /* Select handler */
11240 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11242 /* Unhandled opcode */
11243 if (!hw_fct)
11245 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11246 return;
11248 hw_fct(ins);
11250 shader_glsl_add_instruction_modifiers(ins);
11253 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11255 struct shader_glsl_priv *priv = shader_priv;
11257 return priv->ffp_proj_control;
11260 const struct wined3d_shader_backend_ops glsl_shader_backend =
11262 shader_glsl_handle_instruction,
11263 shader_glsl_precompile,
11264 shader_glsl_select,
11265 shader_glsl_select_compute,
11266 shader_glsl_disable,
11267 shader_glsl_update_float_vertex_constants,
11268 shader_glsl_update_float_pixel_constants,
11269 shader_glsl_load_constants,
11270 shader_glsl_destroy,
11271 shader_glsl_alloc,
11272 shader_glsl_free,
11273 shader_glsl_allocate_context_data,
11274 shader_glsl_free_context_data,
11275 shader_glsl_init_context_state,
11276 shader_glsl_get_caps,
11277 shader_glsl_color_fixup_supported,
11278 shader_glsl_has_ffp_proj_control,
11281 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11283 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11285 caps->xyzrhw = TRUE;
11286 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11287 caps->ffp_generic_attributes = TRUE;
11288 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11289 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11290 caps->max_vertex_blend_matrix_index = 0;
11291 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11292 | WINED3DVTXPCAPS_MATERIALSOURCE7
11293 | WINED3DVTXPCAPS_VERTEXFOG
11294 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11295 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11296 | WINED3DVTXPCAPS_LOCALVIEWER
11297 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11298 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11299 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11300 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11303 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11305 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11306 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11307 return 0;
11310 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11312 struct shader_glsl_priv *priv;
11314 if (shader_backend == &glsl_shader_backend)
11316 priv = shader_priv;
11317 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11318 return priv;
11321 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11323 return NULL;
11326 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11328 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11329 struct glsl_ffp_vertex_shader, desc.entry);
11330 struct glsl_shader_prog_link *program, *program2;
11331 struct glsl_ffp_destroy_ctx *ctx = context;
11333 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11334 struct glsl_shader_prog_link, vs.shader_entry)
11336 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11338 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11339 HeapFree(GetProcessHeap(), 0, shader);
11342 /* Context activation is done by the caller. */
11343 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11345 struct shader_glsl_priv *priv = device->vertex_priv;
11346 struct glsl_ffp_destroy_ctx ctx;
11348 ctx.priv = priv;
11349 ctx.gl_info = &device->adapter->gl_info;
11350 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11353 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11354 const struct wined3d_state *state, DWORD state_id) {}
11356 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11357 const struct wined3d_state *state, DWORD state_id)
11359 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11362 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11363 const struct wined3d_state *state, DWORD state_id)
11365 const struct wined3d_gl_info *gl_info = context->gl_info;
11366 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11367 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11368 BOOL transformed = context->stream_info.position_transformed;
11369 BOOL wasrhw = context->last_was_rhw;
11370 unsigned int i;
11372 context->last_was_rhw = transformed;
11374 /* If the vertex declaration contains a transformed position attribute,
11375 * the draw uses the fixed function vertex pipeline regardless of any
11376 * vertex shader set by the application. */
11377 if (transformed != wasrhw
11378 || context->stream_info.swizzle_map != context->last_swizzle_map)
11379 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11381 context->last_swizzle_map = context->stream_info.swizzle_map;
11383 if (!use_vs(state))
11385 if (context->last_was_vshader)
11387 if (legacy_clip_planes)
11388 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11389 clipplane(context, state, STATE_CLIPPLANE(i));
11390 else
11391 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11394 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11396 /* Because of settings->texcoords, we have to regenerate the vertex
11397 * shader on a vdecl change if there aren't enough varyings to just
11398 * always output all the texture coordinates. */
11399 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11400 || normal != context->last_was_normal)
11401 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11403 if (use_ps(state)
11404 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11405 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11406 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11408 else
11410 if (!context->last_was_vshader)
11412 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11413 if (legacy_clip_planes)
11414 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11415 clipplane(context, state, STATE_CLIPPLANE(i));
11416 else
11417 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11421 context->last_was_vshader = use_vs(state);
11422 context->last_was_normal = normal;
11425 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11426 const struct wined3d_state *state, DWORD state_id)
11428 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11429 /* Different vertex shaders potentially require a different vertex attributes setup. */
11430 if (!isStateDirty(context, STATE_VDECL))
11431 context_apply_state(context, state, STATE_VDECL);
11434 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11435 const struct wined3d_state *state, DWORD state_id)
11437 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11438 * winding) are defined in Hull Shaders, while in GLSL those are
11439 * specified in Tessellation Evaluation Shaders. */
11440 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11442 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11443 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11446 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11447 const struct wined3d_state *state, DWORD state_id)
11449 struct glsl_context_data *ctx_data = context->shader_backend_data;
11450 BOOL rasterization_disabled;
11452 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11453 if (ctx_data->rasterization_disabled != rasterization_disabled)
11454 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11455 ctx_data->rasterization_disabled = rasterization_disabled;
11457 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11458 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11459 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11460 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11461 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11464 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11465 const struct wined3d_state *state, DWORD state_id)
11467 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11468 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11469 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11470 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11471 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11472 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11473 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11476 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11477 const struct wined3d_state *state, DWORD state_id)
11479 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11482 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11483 const struct wined3d_state *state, DWORD state_id)
11485 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11488 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11490 const struct wined3d_gl_info *gl_info = context->gl_info;
11491 unsigned int k;
11493 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11494 | WINED3D_SHADER_CONST_FFP_LIGHTS
11495 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11497 if (needs_legacy_glsl_syntax(gl_info))
11499 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11501 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11502 clipplane(context, state, STATE_CLIPPLANE(k));
11505 else
11507 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11511 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11512 const struct wined3d_state *state, DWORD state_id)
11514 /* Table fog behavior depends on the projection matrix. */
11515 if (state->render_states[WINED3D_RS_FOGENABLE]
11516 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11517 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11518 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11521 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11522 const struct wined3d_state *state, DWORD state_id)
11524 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11525 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11526 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11527 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11528 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11529 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11532 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11533 const struct wined3d_state *state, DWORD state_id)
11535 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11538 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11539 const struct wined3d_state *state, DWORD state_id)
11541 DWORD sampler = state_id - STATE_SAMPLER(0);
11542 const struct wined3d_texture *texture = state->textures[sampler];
11543 BOOL np2;
11545 if (!texture)
11546 return;
11548 if (sampler >= MAX_TEXTURES)
11549 return;
11551 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11552 || context->lastWasPow2Texture & (1u << sampler))
11554 if (np2)
11555 context->lastWasPow2Texture |= 1u << sampler;
11556 else
11557 context->lastWasPow2Texture &= ~(1u << sampler);
11559 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11563 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11564 const struct wined3d_state *state, DWORD state_id)
11566 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11569 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11570 const struct wined3d_state *state, DWORD state_id)
11572 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11575 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11576 const struct wined3d_state *state, DWORD state_id)
11578 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11581 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11582 const struct wined3d_state *state, DWORD state_id)
11584 if (!use_vs(state))
11585 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11588 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11589 const struct wined3d_state *state, DWORD state_id)
11591 static unsigned int once;
11593 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11594 FIXME("Non-point sprite points not supported in core profile.\n");
11597 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11598 const struct wined3d_state *state, DWORD state_id)
11600 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11603 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11604 const struct wined3d_state *state, DWORD state_id)
11606 const struct wined3d_gl_info *gl_info = context->gl_info;
11607 UINT index = state_id - STATE_CLIPPLANE(0);
11609 if (index >= gl_info->limits.user_clip_distances)
11610 return;
11612 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11615 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11617 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11618 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11619 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11620 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11621 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11622 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11623 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11624 /* Clip planes */
11625 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11626 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11627 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11628 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11629 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11630 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11631 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11632 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11633 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11634 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11635 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11636 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11637 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11638 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11639 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11640 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11641 /* Lights */
11642 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11643 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11644 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11645 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11646 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11647 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11648 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11649 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11650 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11651 /* Viewport */
11652 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11653 /* Transform states */
11654 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11655 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11656 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11657 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11658 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11659 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11660 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11661 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11662 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11663 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11664 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11665 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11666 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11667 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11668 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11669 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11670 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11671 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11672 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11673 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11674 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11675 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11676 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11677 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11678 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11679 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11680 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11681 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11682 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11683 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11684 /* Fog */
11685 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11686 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11687 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11688 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11689 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11690 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11691 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11692 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11693 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11694 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11695 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11696 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11697 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11698 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11699 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11700 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11701 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11702 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11703 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11704 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11705 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11706 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11707 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11708 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11709 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11710 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11711 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11712 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11713 /* NP2 texture matrix fixups. They are not needed if
11714 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11715 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11716 * matrix. */
11717 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11718 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11719 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11720 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11721 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11722 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11723 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11724 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11725 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11726 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11727 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11728 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11729 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11730 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11731 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11732 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11733 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11734 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11735 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11736 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11737 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11738 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11739 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11740 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11741 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11742 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11743 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11744 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11747 /* TODO:
11748 * - Implement vertex tweening. */
11749 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11751 glsl_vertex_pipe_vp_enable,
11752 glsl_vertex_pipe_vp_get_caps,
11753 glsl_vertex_pipe_vp_get_emul_mask,
11754 glsl_vertex_pipe_vp_alloc,
11755 glsl_vertex_pipe_vp_free,
11756 glsl_vertex_pipe_vp_states,
11759 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
11761 /* Nothing to do. */
11764 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
11766 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11767 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11768 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11769 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11770 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11771 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11772 | WINED3DTEXOPCAPS_SELECTARG1
11773 | WINED3DTEXOPCAPS_SELECTARG2
11774 | WINED3DTEXOPCAPS_MODULATE4X
11775 | WINED3DTEXOPCAPS_MODULATE2X
11776 | WINED3DTEXOPCAPS_MODULATE
11777 | WINED3DTEXOPCAPS_ADDSIGNED2X
11778 | WINED3DTEXOPCAPS_ADDSIGNED
11779 | WINED3DTEXOPCAPS_ADD
11780 | WINED3DTEXOPCAPS_SUBTRACT
11781 | WINED3DTEXOPCAPS_ADDSMOOTH
11782 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11783 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11784 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11785 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11786 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
11787 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
11788 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
11789 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
11790 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
11791 | WINED3DTEXOPCAPS_DOTPRODUCT3
11792 | WINED3DTEXOPCAPS_MULTIPLYADD
11793 | WINED3DTEXOPCAPS_LERP
11794 | WINED3DTEXOPCAPS_BUMPENVMAP
11795 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
11796 caps->MaxTextureBlendStages = MAX_TEXTURES;
11797 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
11800 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
11802 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11803 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11804 return 0;
11807 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11809 struct shader_glsl_priv *priv;
11811 if (shader_backend == &glsl_shader_backend)
11813 priv = shader_priv;
11814 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
11815 return priv;
11818 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
11820 return NULL;
11823 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
11825 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11826 struct glsl_ffp_fragment_shader, entry.entry);
11827 struct glsl_shader_prog_link *program, *program2;
11828 struct glsl_ffp_destroy_ctx *ctx = context;
11830 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11831 struct glsl_shader_prog_link, ps.shader_entry)
11833 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11835 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11836 HeapFree(GetProcessHeap(), 0, shader);
11839 /* Context activation is done by the caller. */
11840 static void glsl_fragment_pipe_free(struct wined3d_device *device)
11842 struct shader_glsl_priv *priv = device->fragment_priv;
11843 struct glsl_ffp_destroy_ctx ctx;
11845 ctx.priv = priv;
11846 ctx.gl_info = &device->adapter->gl_info;
11847 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
11850 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
11851 const struct wined3d_state *state, DWORD state_id)
11853 context->last_was_pshader = use_ps(state);
11855 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11858 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
11859 const struct wined3d_state *state, DWORD state_id)
11861 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11864 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
11865 const struct wined3d_state *state, DWORD state_id)
11867 BOOL use_vshader = use_vs(state);
11868 enum fogsource new_source;
11869 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
11870 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
11872 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11874 if (!state->render_states[WINED3D_RS_FOGENABLE])
11875 return;
11877 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
11879 if (use_vshader)
11880 new_source = FOGSOURCE_VS;
11881 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
11882 new_source = FOGSOURCE_COORD;
11883 else
11884 new_source = FOGSOURCE_FFP;
11886 else
11888 new_source = FOGSOURCE_FFP;
11891 if (new_source != context->fog_source || fogstart == fogend)
11893 context->fog_source = new_source;
11894 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11898 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
11899 const struct wined3d_state *state, DWORD state_id)
11901 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11902 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11903 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11905 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
11906 glsl_fragment_pipe_fog(context, state, state_id);
11909 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
11910 const struct wined3d_state *state, DWORD state_id)
11912 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
11913 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
11914 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11917 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
11918 const struct wined3d_state *state, DWORD state_id)
11920 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11923 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
11924 const struct wined3d_state *state, DWORD state_id)
11926 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
11929 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
11930 const struct wined3d_state *state, DWORD state_id)
11932 const struct wined3d_gl_info *gl_info = context->gl_info;
11933 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
11934 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
11936 if (func)
11938 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
11939 checkGLcall("glAlphaFunc");
11943 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
11944 const struct wined3d_state *state, DWORD state_id)
11946 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11949 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
11950 const struct wined3d_state *state, DWORD state_id)
11952 const struct wined3d_gl_info *gl_info = context->gl_info;
11954 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
11956 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
11957 checkGLcall("glEnable(GL_ALPHA_TEST)");
11959 else
11961 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
11962 checkGLcall("glDisable(GL_ALPHA_TEST)");
11966 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
11967 const struct wined3d_state *state, DWORD state_id)
11969 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
11972 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
11973 const struct wined3d_state *state, DWORD state_id)
11975 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
11978 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
11979 const struct wined3d_state *state, DWORD state_id)
11981 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11984 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
11986 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11987 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
11988 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
11989 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11990 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11991 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11992 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11993 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11994 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11995 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11996 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11997 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11998 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
11999 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12000 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12001 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12002 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12003 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12004 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12005 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12006 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12007 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12008 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12009 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12010 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12011 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12012 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12013 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12014 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12015 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12016 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12017 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12018 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12019 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12020 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12021 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12022 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12023 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12024 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12025 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12026 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12027 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12028 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12029 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12030 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12031 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12032 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12033 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12034 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12035 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12036 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12037 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12038 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12039 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12040 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12041 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12042 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12043 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12044 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12045 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12046 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12047 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12048 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12049 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12050 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12051 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12052 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12053 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12054 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12055 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12056 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12057 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12058 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12059 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12060 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12061 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12062 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12063 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12064 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12065 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12066 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12067 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12068 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12069 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12070 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12071 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12072 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12073 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12074 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12075 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12076 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12077 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12078 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12079 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12080 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12081 {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 },
12082 {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 },
12083 {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 },
12084 {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 },
12085 {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 },
12086 {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 },
12087 {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 },
12088 {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 },
12089 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12090 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12091 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12092 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12093 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12094 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12095 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12096 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12097 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12098 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12099 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12100 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12101 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12104 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12106 return TRUE;
12109 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12113 const struct fragment_pipeline glsl_fragment_pipe =
12115 glsl_fragment_pipe_enable,
12116 glsl_fragment_pipe_get_caps,
12117 glsl_fragment_pipe_get_emul_mask,
12118 glsl_fragment_pipe_alloc,
12119 glsl_fragment_pipe_free,
12120 glsl_fragment_pipe_alloc_context_data,
12121 glsl_fragment_pipe_free_context_data,
12122 shader_glsl_color_fixup_supported,
12123 glsl_fragment_pipe_state_template,