wined3d: Implement YUV blits in the GLSL blitter.
[wine.git] / dlls / wined3d / glsl_shader.c
blobe4339064e283cfc5cac33758aba990cf1e91ee99
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
32 #include "config.h"
33 #include "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
37 #ifdef HAVE_FLOAT_H
38 # include <float.h>
39 #endif
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
48 #define WINED3D_GLSL_SAMPLE_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
53 static const struct
55 unsigned int coord_size;
56 unsigned int resinfo_size;
57 const char *type_part;
59 resource_type_info[] =
61 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
62 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
63 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
64 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
66 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
67 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
68 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
69 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
70 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
71 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
74 struct glsl_dst_param
76 char reg_name[150];
77 char mask_str[6];
80 struct glsl_src_param
82 char reg_name[150];
83 char param_str[200];
86 struct glsl_sample_function
88 struct wined3d_string_buffer *name;
89 unsigned int coord_mask;
90 unsigned int deriv_mask;
91 enum wined3d_data_type data_type;
92 BOOL output_single_component;
93 unsigned int offset_size;
96 enum heap_node_op
98 HEAP_NODE_TRAVERSE_LEFT,
99 HEAP_NODE_TRAVERSE_RIGHT,
100 HEAP_NODE_POP,
103 struct constant_entry
105 unsigned int idx;
106 unsigned int version;
109 struct constant_heap
111 struct constant_entry *entries;
112 BOOL *contained;
113 unsigned int *positions;
114 unsigned int size;
117 /* GLSL shader private data */
118 struct shader_glsl_priv
120 struct wined3d_string_buffer shader_buffer;
121 struct wined3d_string_buffer_list string_buffers;
122 struct wine_rb_tree program_lookup;
123 struct constant_heap vconst_heap;
124 struct constant_heap pconst_heap;
125 unsigned char *stack;
126 UINT next_constant_version;
128 const struct wined3d_vertex_pipe_ops *vertex_pipe;
129 const struct fragment_pipeline *fragment_pipe;
130 struct wine_rb_tree ffp_vertex_shaders;
131 struct wine_rb_tree ffp_fragment_shaders;
132 BOOL ffp_proj_control;
133 BOOL legacy_lighting;
136 struct glsl_vs_program
138 struct list shader_entry;
139 GLuint id;
140 GLenum vertex_color_clamp;
141 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
142 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
143 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
144 GLint pos_fixup_location;
146 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
147 GLint projection_matrix_location;
148 GLint normal_matrix_location;
149 GLint texture_matrix_location[MAX_TEXTURES];
150 GLint material_ambient_location;
151 GLint material_diffuse_location;
152 GLint material_specular_location;
153 GLint material_emissive_location;
154 GLint material_shininess_location;
155 GLint light_ambient_location;
156 struct
158 GLint diffuse;
159 GLint specular;
160 GLint ambient;
161 GLint position;
162 GLint direction;
163 GLint range;
164 GLint falloff;
165 GLint c_att;
166 GLint l_att;
167 GLint q_att;
168 GLint cos_htheta;
169 GLint cos_hphi;
170 } light_location[MAX_ACTIVE_LIGHTS];
171 GLint pointsize_location;
172 GLint pointsize_min_location;
173 GLint pointsize_max_location;
174 GLint pointsize_c_att_location;
175 GLint pointsize_l_att_location;
176 GLint pointsize_q_att_location;
177 GLint clip_planes_location;
180 struct glsl_hs_program
182 struct list shader_entry;
183 GLuint id;
186 struct glsl_ds_program
188 struct list shader_entry;
189 GLuint id;
191 GLint pos_fixup_location;
194 struct glsl_gs_program
196 struct list shader_entry;
197 GLuint id;
199 GLint pos_fixup_location;
202 struct glsl_ps_program
204 struct list shader_entry;
205 GLuint id;
206 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
207 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
208 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
209 GLint bumpenv_mat_location[MAX_TEXTURES];
210 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
211 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
212 GLint tss_constant_location[MAX_TEXTURES];
213 GLint tex_factor_location;
214 GLint specular_enable_location;
215 GLint fog_color_location;
216 GLint fog_density_location;
217 GLint fog_end_location;
218 GLint fog_scale_location;
219 GLint alpha_test_ref_location;
220 GLint ycorrection_location;
221 GLint np2_fixup_location;
222 GLint color_key_location;
223 const struct ps_np2fixup_info *np2_fixup_info;
226 struct glsl_cs_program
228 struct list shader_entry;
229 GLuint id;
232 /* Struct to maintain data about a linked GLSL program */
233 struct glsl_shader_prog_link
235 struct wine_rb_entry program_lookup_entry;
236 struct glsl_vs_program vs;
237 struct glsl_hs_program hs;
238 struct glsl_ds_program ds;
239 struct glsl_gs_program gs;
240 struct glsl_ps_program ps;
241 struct glsl_cs_program cs;
242 GLuint id;
243 DWORD constant_update_mask;
244 unsigned int constant_version;
245 DWORD shader_controlled_clip_distances : 1;
246 DWORD clip_distance_mask : 8; /* MAX_CLIP_DISTANCES, 8 */
247 DWORD padding : 23;
250 struct glsl_program_key
252 GLuint vs_id;
253 GLuint hs_id;
254 GLuint ds_id;
255 GLuint gs_id;
256 GLuint ps_id;
257 GLuint cs_id;
260 struct shader_glsl_ctx_priv {
261 const struct vs_compile_args *cur_vs_args;
262 const struct ds_compile_args *cur_ds_args;
263 const struct ps_compile_args *cur_ps_args;
264 struct ps_np2fixup_info *cur_np2fixup_info;
265 struct wined3d_string_buffer_list *string_buffers;
268 struct glsl_context_data
270 struct glsl_shader_prog_link *glsl_program;
271 GLenum vertex_color_clamp;
272 BOOL rasterization_disabled;
275 struct glsl_ps_compiled_shader
277 struct ps_compile_args args;
278 struct ps_np2fixup_info np2fixup;
279 GLuint id;
282 struct glsl_vs_compiled_shader
284 struct vs_compile_args args;
285 GLuint id;
288 struct glsl_hs_compiled_shader
290 GLuint id;
293 struct glsl_ds_compiled_shader
295 struct ds_compile_args args;
296 GLuint id;
299 struct glsl_gs_compiled_shader
301 struct gs_compile_args args;
302 GLuint id;
305 struct glsl_cs_compiled_shader
307 GLuint id;
310 struct glsl_shader_private
312 union
314 struct glsl_vs_compiled_shader *vs;
315 struct glsl_hs_compiled_shader *hs;
316 struct glsl_ds_compiled_shader *ds;
317 struct glsl_gs_compiled_shader *gs;
318 struct glsl_ps_compiled_shader *ps;
319 struct glsl_cs_compiled_shader *cs;
320 } gl_shaders;
321 unsigned int num_gl_shaders, shader_array_size;
324 struct glsl_ffp_vertex_shader
326 struct wined3d_ffp_vs_desc desc;
327 GLuint id;
328 struct list linked_programs;
331 struct glsl_ffp_fragment_shader
333 struct ffp_frag_desc entry;
334 GLuint id;
335 struct list linked_programs;
338 struct glsl_ffp_destroy_ctx
340 struct shader_glsl_priv *priv;
341 const struct wined3d_gl_info *gl_info;
344 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
346 static const char *debug_gl_shader_type(GLenum type)
348 switch (type)
350 #define WINED3D_TO_STR(u) case u: return #u
351 WINED3D_TO_STR(GL_VERTEX_SHADER);
352 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
353 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
354 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
355 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
356 WINED3D_TO_STR(GL_COMPUTE_SHADER);
357 #undef WINED3D_TO_STR
358 default:
359 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
363 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
365 switch (type)
367 case WINED3D_SHADER_TYPE_VERTEX:
368 return "vs";
370 case WINED3D_SHADER_TYPE_HULL:
371 return "hs";
373 case WINED3D_SHADER_TYPE_DOMAIN:
374 return "ds";
376 case WINED3D_SHADER_TYPE_GEOMETRY:
377 return "gs";
379 case WINED3D_SHADER_TYPE_PIXEL:
380 return "ps";
382 case WINED3D_SHADER_TYPE_COMPUTE:
383 return "cs";
385 default:
386 FIXME("Unhandled shader type %#x.\n", type);
387 return "unknown";
391 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
393 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
394 return 440;
395 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
396 return 150;
397 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
398 return 130;
399 else
400 return 120;
403 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
404 const struct wined3d_gl_info *gl_info)
406 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
409 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
411 char str[4][17];
413 wined3d_ftoa(values[0], str[0]);
414 wined3d_ftoa(values[1], str[1]);
415 wined3d_ftoa(values[2], str[2]);
416 wined3d_ftoa(values[3], str[3]);
417 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
420 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
421 const int *values, unsigned int size)
423 int i;
425 if (!size || size > 4)
427 ERR("Invalid vector size %u.\n", size);
428 return;
431 if (size > 1)
432 shader_addline(buffer, "ivec%u(", size);
434 for (i = 0; i < size; ++i)
435 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
437 if (size > 1)
438 shader_addline(buffer, ")");
441 static const char *get_info_log_line(const char **ptr)
443 const char *p, *q;
445 p = *ptr;
446 if (!(q = strstr(p, "\n")))
448 if (!*p) return NULL;
449 *ptr += strlen(p);
450 return p;
452 *ptr = q + 1;
454 return p;
457 /* Context activation is done by the caller. */
458 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
460 int length = 0;
461 char *log;
463 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
464 return;
466 if (program)
467 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
468 else
469 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
471 /* A size of 1 is just a null-terminated string, so the log should be bigger than
472 * that if there are errors. */
473 if (length > 1)
475 const char *ptr, *line;
477 log = heap_alloc(length);
478 /* The info log is supposed to be zero-terminated, but at least some
479 * versions of fglrx don't terminate the string properly. The reported
480 * length does include the terminator, so explicitly set it to zero
481 * here. */
482 log[length - 1] = 0;
483 if (program)
484 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
485 else
486 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
488 ptr = log;
489 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
491 WARN("Info log received from GLSL shader #%u:\n", id);
492 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
494 else
496 FIXME("Info log received from GLSL shader #%u:\n", id);
497 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
499 heap_free(log);
503 /* Context activation is done by the caller. */
504 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
506 const char *ptr, *line;
508 TRACE("Compiling shader object %u.\n", shader);
510 if (TRACE_ON(d3d_shader))
512 ptr = src;
513 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
516 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
517 checkGLcall("glShaderSource");
518 GL_EXTCALL(glCompileShader(shader));
519 checkGLcall("glCompileShader");
520 print_glsl_info_log(gl_info, shader, FALSE);
523 /* Context activation is done by the caller. */
524 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
526 GLint i, shader_count, source_size = -1;
527 GLuint *shaders;
528 char *source = NULL;
530 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
531 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
533 ERR("Failed to allocate shader array memory.\n");
534 return;
537 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
538 for (i = 0; i < shader_count; ++i)
540 const char *ptr, *line;
541 GLint tmp;
543 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
545 if (source_size < tmp)
547 heap_free(source);
549 if (!(source = heap_alloc_zero(tmp)))
551 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
552 heap_free(shaders);
553 return;
555 source_size = tmp;
558 FIXME("Shader %u:\n", shaders[i]);
559 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
560 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
561 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
562 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
563 FIXME("\n");
565 ptr = source;
566 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
567 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
568 FIXME("\n");
571 heap_free(source);
572 heap_free(shaders);
575 /* Context activation is done by the caller. */
576 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
578 GLint tmp;
580 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
581 return;
583 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
584 if (!tmp)
586 FIXME("Program %u link status invalid.\n", program);
587 shader_glsl_dump_program_source(gl_info, program);
590 print_glsl_info_log(gl_info, program, TRUE);
593 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
595 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
596 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
597 * versions. */
598 return shader_glsl_get_version(gl_info) >= 140;
601 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
603 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
606 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
607 struct shader_glsl_priv *priv, GLuint program_id,
608 const struct wined3d_shader_reg_maps *reg_maps)
610 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
611 struct wined3d_string_buffer *name;
612 unsigned int i, base, count;
613 GLuint block_idx;
615 if (shader_glsl_use_layout_binding_qualifier(gl_info))
616 return;
618 name = string_buffer_get(&priv->string_buffers);
619 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
620 for (i = 0; i < count; ++i)
622 if (!reg_maps->cb_sizes[i])
623 continue;
625 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
626 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
627 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
629 checkGLcall("glUniformBlockBinding");
630 string_buffer_release(&priv->string_buffers, name);
633 /* Context activation is done by the caller. */
634 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
635 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
636 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
638 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
639 unsigned int i, mapped_unit;
640 GLint name_loc;
642 for (i = 0; i < count; ++i)
644 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
645 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
646 if (name_loc == -1)
647 continue;
649 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
650 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
652 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
653 continue;
656 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
657 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
659 checkGLcall("Load sampler bindings");
660 string_buffer_release(&priv->string_buffers, sampler_name);
663 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
664 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
666 const DWORD *tex_unit_map;
667 unsigned int base, count;
669 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
670 if (sampler_idx >= count)
671 return WINED3D_UNMAPPED_STAGE;
672 if (!tex_unit_map)
673 return base + sampler_idx;
674 return tex_unit_map[base + sampler_idx];
677 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
678 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
679 unsigned int sampler_idx)
681 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
682 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
683 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
684 else
685 ERR("Unmapped sampler %u.\n", sampler_idx);
688 /* Context activation is done by the caller. */
689 static void shader_glsl_load_samplers(const struct wined3d_context *context,
690 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
692 const struct wined3d_gl_info *gl_info = context->gl_info;
693 const struct wined3d_shader_version *shader_version;
694 const DWORD *tex_unit_map;
695 unsigned int base, count;
696 const char *prefix;
698 if (shader_glsl_use_layout_binding_qualifier(gl_info))
699 return;
701 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
702 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
703 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
704 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
707 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
708 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
710 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
712 if (icb)
714 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
715 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
716 GLint icb_location;
718 string_buffer_sprintf(icb_name, "%s_icb", prefix);
719 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
720 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
721 checkGLcall("Load immediate constant buffer");
723 string_buffer_release(&priv->string_buffers, icb_name);
727 /* Context activation is done by the caller. */
728 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
729 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
731 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
732 struct wined3d_string_buffer *name;
733 GLint location;
734 unsigned int i;
736 if (shader_glsl_use_layout_binding_qualifier(gl_info))
737 return;
739 name = string_buffer_get(&priv->string_buffers);
740 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
742 if (!reg_maps->uav_resource_info[i].type)
743 continue;
745 string_buffer_sprintf(name, "%s_image%u", prefix, i);
746 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
747 if (location == -1)
748 continue;
750 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
751 GL_EXTCALL(glUniform1i(location, i));
753 checkGLcall("Load image bindings");
754 string_buffer_release(&priv->string_buffers, name);
757 /* Context activation is done by the caller. */
758 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
759 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
761 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
763 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
764 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
765 /* Texture unit mapping is set up to be the same each time the shader
766 * program is used so we can hardcode the sampler uniform values. */
767 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
770 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
771 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
773 if (varyings && *strings)
775 char *ptr = *strings;
777 varyings[*varying_count] = ptr;
779 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
780 ptr += buffer->content_size + 1;
782 *strings = ptr;
785 *strings_length += buffer->content_size + 1;
786 ++(*varying_count);
789 static void append_transform_feedback_skip_components(const char **varyings,
790 unsigned int *varying_count, char **strings, unsigned int *strings_length,
791 struct wined3d_string_buffer *buffer, unsigned int component_count)
793 unsigned int j;
795 for (j = 0; j < component_count / 4; ++j)
797 string_buffer_sprintf(buffer, "gl_SkipComponents4");
798 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
800 if (component_count % 4)
802 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
803 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
807 static BOOL shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
808 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
809 char *strings, unsigned int *strings_length, GLenum buffer_mode)
811 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
812 BOOL have_varyings_to_record = FALSE;
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);
863 have_varyings_to_record = TRUE;
866 if (buffer_idx < so_desc->buffer_stride_count
867 && stride < so_desc->buffer_strides[buffer_idx] / 4)
869 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
870 append_transform_feedback_skip_components(varyings, &count,
871 &strings, &length, buffer, component_count);
874 if (highest_output_slot <= buffer_idx)
875 break;
877 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
879 string_buffer_sprintf(buffer, "gl_NextBuffer");
880 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
884 if (varying_count)
885 *varying_count = count;
886 if (strings_length)
887 *strings_length = length;
889 return have_varyings_to_record;
892 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
893 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
895 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
896 const struct wined3d_gl_info *gl_info = context->gl_info;
897 struct wined3d_string_buffer *buffer;
898 unsigned int i, count, length;
899 const char **varyings;
900 char *strings;
901 GLenum mode;
903 if (!so_desc->element_count)
904 return;
906 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
908 mode = GL_INTERLEAVED_ATTRIBS;
910 else
912 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
914 for (i = 0; i < so_desc->element_count; ++i)
916 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
918 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
919 return;
921 ++element_count[so_desc->elements[i].output_slot];
924 if (element_count[0] == so_desc->element_count)
926 mode = GL_INTERLEAVED_ATTRIBS;
928 else
930 mode = GL_SEPARATE_ATTRIBS;
931 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
933 if (element_count[i] != 1)
934 break;
936 for (; i < ARRAY_SIZE(element_count); ++i)
938 if (element_count[i])
940 FIXME("Only single element per buffer is allowed in separate mode.\n");
941 return;
947 buffer = string_buffer_get(&priv->string_buffers);
949 if (!shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode))
951 FIXME("No varyings to record, disabling transform feedback.\n");
952 shader->u.gs.so_desc.element_count = 0;
953 string_buffer_release(&priv->string_buffers, buffer);
954 return;
957 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
959 ERR("Out of memory.\n");
960 string_buffer_release(&priv->string_buffers, buffer);
961 return;
963 if (!(strings = heap_calloc(length, sizeof(*strings))))
965 ERR("Out of memory.\n");
966 heap_free(varyings);
967 string_buffer_release(&priv->string_buffers, buffer);
968 return;
971 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
972 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
973 checkGLcall("glTransformFeedbackVaryings");
975 heap_free(varyings);
976 heap_free(strings);
977 string_buffer_release(&priv->string_buffers, buffer);
980 /* Context activation is done by the caller. */
981 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
982 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
984 unsigned int start = ~0U, end = 0;
985 int stack_idx = 0;
986 unsigned int heap_idx = 1;
987 unsigned int idx;
989 if (heap->entries[heap_idx].version <= version) return;
991 idx = heap->entries[heap_idx].idx;
992 if (constant_locations[idx] != -1)
993 start = end = idx;
994 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
996 while (stack_idx >= 0)
998 /* Note that we fall through to the next case statement. */
999 switch(stack[stack_idx])
1001 case HEAP_NODE_TRAVERSE_LEFT:
1003 unsigned int left_idx = heap_idx << 1;
1004 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1006 heap_idx = left_idx;
1007 idx = heap->entries[heap_idx].idx;
1008 if (constant_locations[idx] != -1)
1010 if (start > idx)
1011 start = idx;
1012 if (end < idx)
1013 end = idx;
1016 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1017 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1018 break;
1022 case HEAP_NODE_TRAVERSE_RIGHT:
1024 unsigned int right_idx = (heap_idx << 1) + 1;
1025 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1027 heap_idx = right_idx;
1028 idx = heap->entries[heap_idx].idx;
1029 if (constant_locations[idx] != -1)
1031 if (start > idx)
1032 start = idx;
1033 if (end < idx)
1034 end = idx;
1037 stack[stack_idx++] = HEAP_NODE_POP;
1038 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1039 break;
1043 case HEAP_NODE_POP:
1044 heap_idx >>= 1;
1045 --stack_idx;
1046 break;
1049 if (start <= end)
1050 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1051 checkGLcall("walk_constant_heap()");
1054 /* Context activation is done by the caller. */
1055 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1056 GLint location, const struct wined3d_vec4 *data)
1058 GLfloat clamped_constant[4];
1060 if (location == -1) return;
1062 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1063 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1064 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1065 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1067 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1070 /* Context activation is done by the caller. */
1071 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1072 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1073 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1075 int stack_idx = 0;
1076 unsigned int heap_idx = 1;
1077 unsigned int idx;
1079 if (heap->entries[heap_idx].version <= version) return;
1081 idx = heap->entries[heap_idx].idx;
1082 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1083 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1085 while (stack_idx >= 0)
1087 /* Note that we fall through to the next case statement. */
1088 switch(stack[stack_idx])
1090 case HEAP_NODE_TRAVERSE_LEFT:
1092 unsigned int left_idx = heap_idx << 1;
1093 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1095 heap_idx = left_idx;
1096 idx = heap->entries[heap_idx].idx;
1097 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1099 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1100 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1101 break;
1105 case HEAP_NODE_TRAVERSE_RIGHT:
1107 unsigned int right_idx = (heap_idx << 1) + 1;
1108 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1110 heap_idx = right_idx;
1111 idx = heap->entries[heap_idx].idx;
1112 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1114 stack[stack_idx++] = HEAP_NODE_POP;
1115 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1116 break;
1120 case HEAP_NODE_POP:
1121 heap_idx >>= 1;
1122 --stack_idx;
1123 break;
1126 checkGLcall("walk_constant_heap_clamped()");
1129 /* Context activation is done by the caller. */
1130 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1131 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1132 unsigned char *stack, unsigned int version)
1134 const struct wined3d_shader_lconst *lconst;
1136 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1137 if (shader->reg_maps.shader_version.major == 1
1138 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1139 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1140 else
1141 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1143 if (!shader->load_local_constsF)
1145 TRACE("No need to load local float constants for this shader.\n");
1146 return;
1149 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1150 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1152 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1154 checkGLcall("glUniform4fv()");
1157 /* Context activation is done by the caller. */
1158 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1159 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1161 unsigned int i;
1162 struct list* ptr;
1164 for (i = 0; constants_set; constants_set >>= 1, ++i)
1166 if (!(constants_set & 1)) continue;
1168 /* We found this uniform name in the program - go ahead and send the data */
1169 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1172 /* Load immediate constants */
1173 ptr = list_head(&shader->constantsI);
1174 while (ptr)
1176 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1177 unsigned int idx = lconst->idx;
1178 const GLint *values = (const GLint *)lconst->value;
1180 /* We found this uniform name in the program - go ahead and send the data */
1181 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1182 ptr = list_next(&shader->constantsI, ptr);
1184 checkGLcall("glUniform4iv()");
1187 /* Context activation is done by the caller. */
1188 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1189 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1191 unsigned int i;
1192 struct list* ptr;
1194 for (i = 0; constants_set; constants_set >>= 1, ++i)
1196 if (!(constants_set & 1)) continue;
1198 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1201 /* Load immediate constants */
1202 ptr = list_head(&shader->constantsB);
1203 while (ptr)
1205 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1206 unsigned int idx = lconst->idx;
1207 const GLint *values = (const GLint *)lconst->value;
1209 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1210 ptr = list_next(&shader->constantsB, ptr);
1212 checkGLcall("glUniform1iv()");
1215 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1217 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1220 /* Context activation is done by the caller (state handler). */
1221 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1222 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1224 struct
1226 float sx, sy;
1228 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1229 UINT fixup = ps->np2_fixup_info->active;
1230 UINT i;
1232 for (i = 0; fixup; fixup >>= 1, ++i)
1234 const struct wined3d_texture *tex = state->textures[i];
1235 unsigned char idx = ps->np2_fixup_info->idx[i];
1237 if (!tex)
1239 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1240 continue;
1243 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1244 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1247 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1250 /* Taken and adapted from Mesa. */
1251 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1253 float pos, neg, t, det;
1254 struct wined3d_matrix temp;
1256 /* Calculate the determinant of upper left 3x3 submatrix and
1257 * determine if the matrix is singular. */
1258 pos = neg = 0.0f;
1259 t = in->_11 * in->_22 * in->_33;
1260 if (t >= 0.0f)
1261 pos += t;
1262 else
1263 neg += t;
1265 t = in->_21 * in->_32 * in->_13;
1266 if (t >= 0.0f)
1267 pos += t;
1268 else
1269 neg += t;
1270 t = in->_31 * in->_12 * in->_23;
1271 if (t >= 0.0f)
1272 pos += t;
1273 else
1274 neg += t;
1276 t = -in->_31 * in->_22 * in->_13;
1277 if (t >= 0.0f)
1278 pos += t;
1279 else
1280 neg += t;
1281 t = -in->_21 * in->_12 * in->_33;
1282 if (t >= 0.0f)
1283 pos += t;
1284 else
1285 neg += t;
1287 t = -in->_11 * in->_32 * in->_23;
1288 if (t >= 0.0f)
1289 pos += t;
1290 else
1291 neg += t;
1293 det = pos + neg;
1295 if (fabsf(det) < 1e-25f)
1296 return FALSE;
1298 det = 1.0f / det;
1299 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1300 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1301 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1302 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1303 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1304 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1305 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1306 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1307 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1309 *out = temp;
1310 return TRUE;
1313 static void swap_rows(float **a, float **b)
1315 float *tmp = *a;
1317 *a = *b;
1318 *b = tmp;
1321 static BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1323 float wtmp[4][8];
1324 float m0, m1, m2, m3, s;
1325 float *r0, *r1, *r2, *r3;
1327 r0 = wtmp[0];
1328 r1 = wtmp[1];
1329 r2 = wtmp[2];
1330 r3 = wtmp[3];
1332 r0[0] = m->_11;
1333 r0[1] = m->_12;
1334 r0[2] = m->_13;
1335 r0[3] = m->_14;
1336 r0[4] = 1.0f;
1337 r0[5] = r0[6] = r0[7] = 0.0f;
1339 r1[0] = m->_21;
1340 r1[1] = m->_22;
1341 r1[2] = m->_23;
1342 r1[3] = m->_24;
1343 r1[5] = 1.0f;
1344 r1[4] = r1[6] = r1[7] = 0.0f;
1346 r2[0] = m->_31;
1347 r2[1] = m->_32;
1348 r2[2] = m->_33;
1349 r2[3] = m->_34;
1350 r2[6] = 1.0f;
1351 r2[4] = r2[5] = r2[7] = 0.0f;
1353 r3[0] = m->_41;
1354 r3[1] = m->_42;
1355 r3[2] = m->_43;
1356 r3[3] = m->_44;
1357 r3[7] = 1.0f;
1358 r3[4] = r3[5] = r3[6] = 0.0f;
1360 /* Choose pivot - or die. */
1361 if (fabsf(r3[0]) > fabsf(r2[0]))
1362 swap_rows(&r3, &r2);
1363 if (fabsf(r2[0]) > fabsf(r1[0]))
1364 swap_rows(&r2, &r1);
1365 if (fabsf(r1[0]) > fabsf(r0[0]))
1366 swap_rows(&r1, &r0);
1367 if (r0[0] == 0.0f)
1368 return FALSE;
1370 /* Eliminate first variable. */
1371 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1372 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1373 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1374 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1375 s = r0[4];
1376 if (s != 0.0f)
1378 r1[4] -= m1 * s;
1379 r2[4] -= m2 * s;
1380 r3[4] -= m3 * s;
1382 s = r0[5];
1383 if (s != 0.0f)
1385 r1[5] -= m1 * s;
1386 r2[5] -= m2 * s;
1387 r3[5] -= m3 * s;
1389 s = r0[6];
1390 if (s != 0.0f)
1392 r1[6] -= m1 * s;
1393 r2[6] -= m2 * s;
1394 r3[6] -= m3 * s;
1396 s = r0[7];
1397 if (s != 0.0f)
1399 r1[7] -= m1 * s;
1400 r2[7] -= m2 * s;
1401 r3[7] -= m3 * s;
1404 /* Choose pivot - or die. */
1405 if (fabsf(r3[1]) > fabsf(r2[1]))
1406 swap_rows(&r3, &r2);
1407 if (fabsf(r2[1]) > fabsf(r1[1]))
1408 swap_rows(&r2, &r1);
1409 if (r1[1] == 0.0f)
1410 return FALSE;
1412 /* Eliminate second variable. */
1413 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1414 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1415 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1416 s = r1[4];
1417 if (s != 0.0f)
1419 r2[4] -= m2 * s;
1420 r3[4] -= m3 * s;
1422 s = r1[5];
1423 if (s != 0.0f)
1425 r2[5] -= m2 * s;
1426 r3[5] -= m3 * s;
1428 s = r1[6];
1429 if (s != 0.0f)
1431 r2[6] -= m2 * s;
1432 r3[6] -= m3 * s;
1434 s = r1[7];
1435 if (s != 0.0f)
1437 r2[7] -= m2 * s;
1438 r3[7] -= m3 * s;
1441 /* Choose pivot - or die. */
1442 if (fabsf(r3[2]) > fabsf(r2[2]))
1443 swap_rows(&r3, &r2);
1444 if (r2[2] == 0.0f)
1445 return FALSE;
1447 /* Eliminate third variable. */
1448 m3 = r3[2] / r2[2];
1449 r3[3] -= m3 * r2[3];
1450 r3[4] -= m3 * r2[4];
1451 r3[5] -= m3 * r2[5];
1452 r3[6] -= m3 * r2[6];
1453 r3[7] -= m3 * r2[7];
1455 /* Last check. */
1456 if (r3[3] == 0.0f)
1457 return FALSE;
1459 /* Back substitute row 3. */
1460 s = 1.0f / r3[3];
1461 r3[4] *= s;
1462 r3[5] *= s;
1463 r3[6] *= s;
1464 r3[7] *= s;
1466 /* Back substitute row 2. */
1467 m2 = r2[3];
1468 s = 1.0f / r2[2];
1469 r2[4] = s * (r2[4] - r3[4] * m2);
1470 r2[5] = s * (r2[5] - r3[5] * m2);
1471 r2[6] = s * (r2[6] - r3[6] * m2);
1472 r2[7] = s * (r2[7] - r3[7] * m2);
1473 m1 = r1[3];
1474 r1[4] -= r3[4] * m1;
1475 r1[5] -= r3[5] * m1;
1476 r1[6] -= r3[6] * m1;
1477 r1[7] -= r3[7] * m1;
1478 m0 = r0[3];
1479 r0[4] -= r3[4] * m0;
1480 r0[5] -= r3[5] * m0;
1481 r0[6] -= r3[6] * m0;
1482 r0[7] -= r3[7] * m0;
1484 /* Back substitute row 1. */
1485 m1 = r1[2];
1486 s = 1.0f / r1[1];
1487 r1[4] = s * (r1[4] - r2[4] * m1);
1488 r1[5] = s * (r1[5] - r2[5] * m1);
1489 r1[6] = s * (r1[6] - r2[6] * m1);
1490 r1[7] = s * (r1[7] - r2[7] * m1);
1491 m0 = r0[2];
1492 r0[4] -= r2[4] * m0;
1493 r0[5] -= r2[5] * m0;
1494 r0[6] -= r2[6] * m0;
1495 r0[7] -= r2[7] * m0;
1497 /* Back substitute row 0. */
1498 m0 = r0[1];
1499 s = 1.0f / r0[0];
1500 r0[4] = s * (r0[4] - r1[4] * m0);
1501 r0[5] = s * (r0[5] - r1[5] * m0);
1502 r0[6] = s * (r0[6] - r1[6] * m0);
1503 r0[7] = s * (r0[7] - r1[7] * m0);
1505 out->_11 = r0[4];
1506 out->_12 = r0[5];
1507 out->_13 = r0[6];
1508 out->_14 = r0[7];
1509 out->_21 = r1[4];
1510 out->_22 = r1[5];
1511 out->_23 = r1[6];
1512 out->_24 = r1[7];
1513 out->_31 = r2[4];
1514 out->_32 = r2[5];
1515 out->_33 = r2[6];
1516 out->_34 = r2[7];
1517 out->_41 = r3[4];
1518 out->_42 = r3[5];
1519 out->_43 = r3[6];
1520 out->_44 = r3[7];
1522 return TRUE;
1525 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1527 struct wined3d_matrix temp;
1528 unsigned int i, j;
1530 for (i = 0; i < 4; ++i)
1531 for (j = 0; j < 4; ++j)
1532 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1534 *out = temp;
1537 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1538 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1540 const struct wined3d_gl_info *gl_info = context->gl_info;
1541 float mat[3 * 3];
1542 struct wined3d_matrix mv;
1543 unsigned int i, j;
1545 if (prog->vs.normal_matrix_location == -1)
1546 return;
1548 get_modelview_matrix(context, state, 0, &mv);
1549 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1550 invert_matrix_3d(&mv, &mv);
1551 else
1552 invert_matrix(&mv, &mv);
1553 /* Tests show that singular modelview matrices are used unchanged as normal
1554 * matrices on D3D3 and older. There seems to be no clearly consistent
1555 * behavior on newer D3D versions so always follow older ddraw behavior. */
1556 for (i = 0; i < 3; ++i)
1557 for (j = 0; j < 3; ++j)
1558 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1560 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1561 checkGLcall("glUniformMatrix3fv");
1564 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1565 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1567 const struct wined3d_gl_info *gl_info = context->gl_info;
1568 struct wined3d_matrix mat;
1570 if (tex >= MAX_TEXTURES)
1571 return;
1572 if (prog->vs.texture_matrix_location[tex] == -1)
1573 return;
1575 get_texture_matrix(context, state, tex, &mat);
1576 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1577 checkGLcall("glUniformMatrix4fv");
1580 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1581 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1583 const struct wined3d_gl_info *gl_info = context->gl_info;
1585 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1587 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1588 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1590 else
1592 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1594 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1596 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1597 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1598 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1599 checkGLcall("setting FFP material uniforms");
1602 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1603 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1605 const struct wined3d_gl_info *gl_info = context->gl_info;
1606 struct wined3d_color color;
1608 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1609 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1610 checkGLcall("glUniform3fv");
1613 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1614 const struct wined3d_matrix *src2)
1616 struct wined3d_vec4 temp;
1618 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1619 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1620 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1621 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1623 *dest = temp;
1626 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1627 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1628 struct glsl_shader_prog_link *prog)
1630 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1631 const struct wined3d_gl_info *gl_info = context->gl_info;
1632 struct wined3d_vec4 vec4;
1634 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1635 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1636 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1638 switch (light_info->OriginalParms.type)
1640 case WINED3D_LIGHT_POINT:
1641 multiply_vector_matrix(&vec4, &light_info->position, view);
1642 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1643 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1644 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1645 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1646 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1647 break;
1649 case WINED3D_LIGHT_SPOT:
1650 multiply_vector_matrix(&vec4, &light_info->position, view);
1651 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1653 multiply_vector_matrix(&vec4, &light_info->direction, view);
1654 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1656 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1657 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1658 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1659 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1660 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1661 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1662 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1663 break;
1665 case WINED3D_LIGHT_DIRECTIONAL:
1666 multiply_vector_matrix(&vec4, &light_info->direction, view);
1667 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1668 break;
1670 case WINED3D_LIGHT_PARALLELPOINT:
1671 multiply_vector_matrix(&vec4, &light_info->position, view);
1672 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1673 break;
1675 default:
1676 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1678 checkGLcall("setting FFP lights uniforms");
1681 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1682 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1684 const struct wined3d_gl_info *gl_info = context->gl_info;
1685 float min, max;
1686 float size, att[3];
1688 get_pointsize_minmax(context, state, &min, &max);
1690 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1691 checkGLcall("glUniform1f");
1692 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1693 checkGLcall("glUniform1f");
1695 get_pointsize(context, state, &size, att);
1697 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1698 checkGLcall("glUniform1f");
1699 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1700 checkGLcall("glUniform1f");
1701 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1702 checkGLcall("glUniform1f");
1703 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1704 checkGLcall("glUniform1f");
1707 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1708 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1710 const struct wined3d_gl_info *gl_info = context->gl_info;
1711 struct wined3d_color color;
1712 float start, end, scale;
1713 union
1715 DWORD d;
1716 float f;
1717 } tmpvalue;
1719 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1720 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1721 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1722 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1723 get_fog_start_end(context, state, &start, &end);
1724 scale = 1.0f / (end - start);
1725 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1726 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1727 checkGLcall("fog emulation uniforms");
1730 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1731 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1733 const struct wined3d_gl_info *gl_info = context->gl_info;
1734 struct wined3d_matrix matrix;
1735 struct wined3d_vec4 plane;
1737 plane = state->clip_planes[index];
1739 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1740 if (!use_vs(state))
1742 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1743 transpose_matrix(&matrix, &matrix);
1744 multiply_vector_matrix(&plane, &plane, &matrix);
1747 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1750 /* Context activation is done by the caller (state handler). */
1751 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1752 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1754 struct wined3d_color float_key[2];
1755 const struct wined3d_texture *texture = state->textures[0];
1757 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1758 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1761 /* Context activation is done by the caller (state handler). */
1762 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1763 const struct wined3d_state *state)
1765 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1766 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1767 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1768 const struct wined3d_gl_info *gl_info = context->gl_info;
1769 struct shader_glsl_priv *priv = shader_priv;
1770 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1771 DWORD update_mask;
1773 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1774 UINT constant_version;
1775 int i;
1777 if (!prog) {
1778 /* No GLSL program set - nothing to do. */
1779 return;
1781 constant_version = prog->constant_version;
1782 update_mask = context->constant_update_mask & prog->constant_update_mask;
1784 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1785 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1786 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1788 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1789 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1790 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1792 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1793 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1794 vshader->reg_maps.boolean_constants);
1796 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1798 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1799 shader_glsl_clip_plane_uniform(context, state, i, prog);
1802 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1803 shader_glsl_pointsize_uniform(context, state, prog);
1805 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1807 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1808 max(state->viewport_count, 1) : 1;
1809 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1810 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1811 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1812 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1813 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1814 else
1815 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1816 checkGLcall("glUniform4fv");
1819 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1821 struct wined3d_matrix mat;
1823 get_modelview_matrix(context, state, 0, &mat);
1824 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1825 checkGLcall("glUniformMatrix4fv");
1827 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1830 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1832 struct wined3d_matrix mat;
1834 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1836 if (prog->vs.modelview_matrix_location[i] == -1)
1837 break;
1839 get_modelview_matrix(context, state, i, &mat);
1840 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1841 checkGLcall("glUniformMatrix4fv");
1845 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1847 struct wined3d_matrix projection;
1849 get_projection_matrix(context, state, &projection);
1850 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1851 checkGLcall("glUniformMatrix4fv");
1854 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1856 for (i = 0; i < MAX_TEXTURES; ++i)
1857 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1860 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1861 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1863 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1865 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1866 DWORD point_count = 0;
1867 DWORD spot_count = 0;
1868 DWORD directional_count = 0;
1869 DWORD parallel_point_count = 0;
1871 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1873 if (!state->lights[i])
1874 continue;
1876 switch (state->lights[i]->OriginalParms.type)
1878 case WINED3D_LIGHT_POINT:
1879 ++point_count;
1880 break;
1881 case WINED3D_LIGHT_SPOT:
1882 ++spot_count;
1883 break;
1884 case WINED3D_LIGHT_DIRECTIONAL:
1885 ++directional_count;
1886 break;
1887 case WINED3D_LIGHT_PARALLELPOINT:
1888 ++parallel_point_count;
1889 break;
1890 default:
1891 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1892 break;
1895 point_idx = 0;
1896 spot_idx = point_idx + point_count;
1897 directional_idx = spot_idx + spot_count;
1898 parallel_point_idx = directional_idx + directional_count;
1900 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1901 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1903 const struct wined3d_light_info *light_info = state->lights[i];
1904 unsigned int idx;
1906 if (!light_info)
1907 continue;
1909 switch (light_info->OriginalParms.type)
1911 case WINED3D_LIGHT_POINT:
1912 idx = point_idx++;
1913 break;
1914 case WINED3D_LIGHT_SPOT:
1915 idx = spot_idx++;
1916 break;
1917 case WINED3D_LIGHT_DIRECTIONAL:
1918 idx = directional_idx++;
1919 break;
1920 case WINED3D_LIGHT_PARALLELPOINT:
1921 idx = parallel_point_idx++;
1922 break;
1923 default:
1924 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1925 continue;
1927 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1931 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1932 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1933 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1935 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1936 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1937 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1939 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1940 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1941 pshader->reg_maps.boolean_constants);
1943 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1945 for (i = 0; i < MAX_TEXTURES; ++i)
1947 if (prog->ps.bumpenv_mat_location[i] == -1)
1948 continue;
1950 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1951 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1953 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1955 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1956 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1957 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1958 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1962 checkGLcall("bump env uniforms");
1965 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1967 const struct wined3d_vec4 correction_params =
1969 /* Position is relative to the framebuffer, not the viewport. */
1970 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1971 context->render_offscreen ? 1.0f : -1.0f,
1972 0.0f,
1973 0.0f,
1976 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1979 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1980 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1981 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1982 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1984 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1986 struct wined3d_color color;
1988 if (prog->ps.tex_factor_location != -1)
1990 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1991 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1994 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1995 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1996 else
1997 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1999 for (i = 0; i < MAX_TEXTURES; ++i)
2001 if (prog->ps.tss_constant_location[i] == -1)
2002 continue;
2004 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
2005 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
2008 checkGLcall("fixed function uniforms");
2011 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
2012 shader_glsl_load_fog_uniform(context, state, prog);
2014 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
2016 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
2018 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
2019 checkGLcall("alpha test emulation uniform");
2022 if (priv->next_constant_version == UINT_MAX)
2024 TRACE("Max constant version reached, resetting to 0.\n");
2025 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
2026 priv->next_constant_version = 1;
2028 else
2030 prog->constant_version = priv->next_constant_version++;
2034 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
2036 struct constant_entry *entries = heap->entries;
2037 unsigned int *positions = heap->positions;
2038 unsigned int heap_idx, parent_idx;
2040 if (!heap->contained[idx])
2042 heap_idx = heap->size++;
2043 heap->contained[idx] = TRUE;
2045 else
2047 heap_idx = positions[idx];
2050 while (heap_idx > 1)
2052 parent_idx = heap_idx >> 1;
2054 if (new_version <= entries[parent_idx].version) break;
2056 entries[heap_idx] = entries[parent_idx];
2057 positions[entries[parent_idx].idx] = heap_idx;
2058 heap_idx = parent_idx;
2061 entries[heap_idx].version = new_version;
2062 entries[heap_idx].idx = idx;
2063 positions[idx] = heap_idx;
2066 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2068 struct shader_glsl_priv *priv = device->shader_priv;
2069 struct constant_heap *heap = &priv->vconst_heap;
2070 UINT i;
2072 for (i = start; i < count + start; ++i)
2074 update_heap_entry(heap, i, priv->next_constant_version);
2078 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2080 struct shader_glsl_priv *priv = device->shader_priv;
2081 struct constant_heap *heap = &priv->pconst_heap;
2082 UINT i;
2084 for (i = start; i < count + start; ++i)
2086 update_heap_entry(heap, i, priv->next_constant_version);
2090 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2092 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2093 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2094 if(shader_major > 3) return ret;
2096 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2097 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2098 return ret;
2101 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2103 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2106 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2108 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2109 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2112 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2114 return shader_glsl_get_version(gl_info) >= 150;
2117 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2119 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2122 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2123 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2125 va_list args;
2126 int ret;
2128 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2129 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2130 for (;;)
2132 va_start(args, format);
2133 ret = shader_vaddline(buffer, format, args);
2134 va_end(args);
2135 if (!ret)
2136 return;
2137 if (!string_buffer_resize(buffer, ret))
2138 return;
2142 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2143 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2145 va_list args;
2146 int ret;
2148 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2149 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2150 for (;;)
2152 va_start(args, format);
2153 ret = shader_vaddline(buffer, format, args);
2154 va_end(args);
2155 if (!ret)
2156 return;
2157 if (!string_buffer_resize(buffer, ret))
2158 return;
2162 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2164 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2167 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2169 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2172 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2174 switch (mode)
2176 case WINED3DSIM_CONSTANT:
2177 return "flat";
2178 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2179 return "noperspective";
2180 default:
2181 FIXME("Unhandled interpolation mode %#x.\n", mode);
2182 case WINED3DSIM_NONE:
2183 case WINED3DSIM_LINEAR:
2184 return "";
2188 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2189 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2191 return wined3d_extract_bits(packed_interpolation_mode,
2192 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2195 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2196 struct wined3d_string_buffer *buffer, unsigned int element_count,
2197 const DWORD *interpolation_mode, BOOL unroll)
2199 enum wined3d_shader_interpolation_mode mode;
2200 unsigned int i;
2202 if (shader_glsl_use_interface_blocks(gl_info))
2204 if (unroll)
2206 shader_addline(buffer, "in shader_in_out {\n");
2207 for (i = 0; i < element_count; ++i)
2209 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2210 shader_addline(buffer, "%s vec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2212 shader_addline(buffer, "} shader_in;\n");
2214 else
2216 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2219 else
2221 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2225 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2226 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2227 const DWORD *interpolation_mode)
2229 enum wined3d_shader_interpolation_mode mode;
2230 unsigned int i;
2232 if (shader_glsl_use_interface_blocks(gl_info))
2234 if (rasterizer_setup)
2236 shader_addline(buffer, "out shader_in_out {\n");
2237 for (i = 0; i < element_count; ++i)
2239 const char *interpolation_qualifiers = "";
2240 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2242 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2243 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2245 shader_addline(buffer, "%s vec4 reg%u;\n", interpolation_qualifiers, i);
2247 shader_addline(buffer, "} shader_out;\n");
2249 else
2251 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2254 else
2256 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2260 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2262 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2265 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2267 switch (primitive_type)
2269 case WINED3D_PT_POINTLIST:
2270 return "points";
2272 case WINED3D_PT_LINELIST:
2273 return "lines";
2275 case WINED3D_PT_LINESTRIP:
2276 return "line_strip";
2278 case WINED3D_PT_TRIANGLELIST:
2279 return "triangles";
2281 case WINED3D_PT_TRIANGLESTRIP:
2282 return "triangle_strip";
2284 case WINED3D_PT_LINELIST_ADJ:
2285 return "lines_adjacency";
2287 case WINED3D_PT_TRIANGLELIST_ADJ:
2288 return "triangles_adjacency";
2290 default:
2291 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2292 return "";
2296 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2298 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2299 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2300 DWORD input_reg_used = shader->u.ps.input_reg_used;
2301 unsigned int i;
2303 if (reg_maps->shader_version.major < 3)
2304 return input_reg_used & (1u << idx);
2306 for (i = 0; i < input_signature->element_count; ++i)
2308 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2310 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2311 continue;
2313 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2314 && input->semantic_idx == idx)
2315 return input_reg_used & (1u << input->register_idx);
2317 return FALSE;
2320 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2321 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2323 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2325 if (version->major >= 4)
2326 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2327 else
2328 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2331 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2332 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2333 unsigned int index)
2335 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2336 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2337 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2338 index, scalar_type, scalar_type, index);
2341 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2342 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2344 unsigned int index = e->register_idx;
2346 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2348 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2349 index);
2350 return;
2352 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2354 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2355 index);
2356 return;
2358 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2359 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2361 if (shader_glsl_use_explicit_attrib_location(gl_info))
2362 shader_addline(buffer, "layout(location = %u) ", index);
2364 switch (e->component_type)
2366 case WINED3D_TYPE_UINT:
2367 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
2368 break;
2369 case WINED3D_TYPE_INT:
2370 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
2371 break;
2373 default:
2374 FIXME("Unhandled type %#x.\n", e->component_type);
2375 /* Fall through. */
2376 case WINED3D_TYPE_UNKNOWN:
2377 case WINED3D_TYPE_FLOAT:
2378 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2379 break;
2383 /** Generate the variable & register declarations for the GLSL output target */
2384 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2385 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2386 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2388 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2389 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2390 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2391 const struct wined3d_gl_info *gl_info = context->gl_info;
2392 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2393 unsigned int uniform_block_base, uniform_block_count;
2394 const struct wined3d_shader_lconst *lconst;
2395 const char *prefix;
2396 unsigned int i;
2397 DWORD map;
2399 prefix = shader_glsl_get_prefix(version->type);
2401 /* Prototype the subroutines */
2402 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2404 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2407 /* Declare the constants (aka uniforms) */
2408 if (shader->limits->constant_float > 0)
2410 unsigned max_constantsF;
2412 /* Unless the shader uses indirect addressing, always declare the
2413 * maximum array size and ignore that we need some uniforms privately.
2414 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2415 * and immediate values, still declare VC[256]. If the shader needs
2416 * more uniforms than we have it won't work in any case. If it uses
2417 * less, the compiler will figure out which uniforms are really used
2418 * and strip them out. This allows a shader to use c255 on a dx9 card,
2419 * as long as it doesn't also use all the other constants.
2421 * If the shader uses indirect addressing the compiler must assume
2422 * that all declared uniforms are used. In this case, declare only the
2423 * amount that we're assured to have.
2425 * Thus we run into problems in these two cases:
2426 * 1) The shader really uses more uniforms than supported.
2427 * 2) The shader uses indirect addressing, less constants than
2428 * supported, but uses a constant index > #supported consts. */
2429 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2431 /* No indirect addressing here. */
2432 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2434 else
2436 if (reg_maps->usesrelconstF)
2438 /* Subtract the other potential uniforms from the max
2439 * available (bools, ints, and 1 row of projection matrix).
2440 * Subtract another uniform for immediate values, which have
2441 * to be loaded via uniform by the driver as well. The shader
2442 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2443 * shader code, so one vec4 should be enough. (Unfortunately
2444 * the Nvidia driver doesn't store 128 and -128 in one float).
2446 * Writing gl_ClipVertex requires one uniform for each
2447 * clipplane as well. */
2448 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2449 if (vs_args->clip_enabled)
2450 max_constantsF -= gl_info->limits.user_clip_distances;
2451 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2452 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2453 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2454 * for now take this into account when calculating the number of available constants
2456 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2457 /* Set by driver quirks in directx.c */
2458 max_constantsF -= gl_info->reserved_glsl_constants;
2460 if (max_constantsF < shader->limits->constant_float)
2462 static unsigned int once;
2464 if (!once++)
2465 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2466 " it may not render correctly.\n");
2467 else
2468 WARN("The hardware does not support enough uniform components to run this shader.\n");
2471 else
2473 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2476 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2477 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2480 /* Always declare the full set of constants, the compiler can remove the
2481 * unused ones because d3d doesn't (yet) support indirect int and bool
2482 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2483 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2484 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2486 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2487 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2489 /* Declare immediate constant buffer */
2490 if (reg_maps->icb)
2491 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2493 /* Declare constant buffers */
2494 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2495 &uniform_block_base, &uniform_block_count);
2496 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2498 if (reg_maps->cb_sizes[i])
2500 shader_addline(buffer, "layout(std140");
2501 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2502 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2503 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2504 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2508 /* Declare texture samplers */
2509 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2511 struct wined3d_shader_sampler_map_entry *entry;
2512 const char *sampler_type_prefix, *sampler_type;
2513 BOOL shadow_sampler, tex_rect;
2515 entry = &reg_maps->sampler_map.entries[i];
2517 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2519 ERR("Invalid resource index %u.\n", entry->resource_idx);
2520 continue;
2523 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2525 case WINED3D_DATA_FLOAT:
2526 case WINED3D_DATA_UNORM:
2527 case WINED3D_DATA_SNORM:
2528 sampler_type_prefix = "";
2529 break;
2531 case WINED3D_DATA_INT:
2532 sampler_type_prefix = "i";
2533 break;
2535 case WINED3D_DATA_UINT:
2536 sampler_type_prefix = "u";
2537 break;
2539 default:
2540 sampler_type_prefix = "";
2541 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2542 break;
2545 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2546 switch (reg_maps->resource_info[entry->resource_idx].type)
2548 case WINED3D_SHADER_RESOURCE_BUFFER:
2549 sampler_type = "samplerBuffer";
2550 break;
2552 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2553 if (shadow_sampler)
2554 sampler_type = "sampler1DShadow";
2555 else
2556 sampler_type = "sampler1D";
2557 break;
2559 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2560 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2561 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2562 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2563 if (shadow_sampler)
2565 if (tex_rect)
2566 sampler_type = "sampler2DRectShadow";
2567 else
2568 sampler_type = "sampler2DShadow";
2570 else
2572 if (tex_rect)
2573 sampler_type = "sampler2DRect";
2574 else
2575 sampler_type = "sampler2D";
2577 break;
2579 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2580 if (shadow_sampler)
2581 FIXME("Unsupported 3D shadow sampler.\n");
2582 sampler_type = "sampler3D";
2583 break;
2585 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2586 if (shadow_sampler)
2587 sampler_type = "samplerCubeShadow";
2588 else
2589 sampler_type = "samplerCube";
2590 break;
2592 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2593 if (shadow_sampler)
2594 sampler_type = "sampler1DArrayShadow";
2595 else
2596 sampler_type = "sampler1DArray";
2597 break;
2599 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2600 if (shadow_sampler)
2601 sampler_type = "sampler2DArrayShadow";
2602 else
2603 sampler_type = "sampler2DArray";
2604 break;
2606 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2607 if (shadow_sampler)
2608 sampler_type = "samplerCubeArrayShadow";
2609 else
2610 sampler_type = "samplerCubeArray";
2611 break;
2613 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2614 sampler_type = "sampler2DMS";
2615 break;
2617 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2618 sampler_type = "sampler2DMSArray";
2619 break;
2621 default:
2622 sampler_type = "unsupported_sampler";
2623 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2624 break;
2627 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2628 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2629 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2630 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2633 /* Declare images */
2634 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2636 const char *image_type_prefix, *image_type, *read_format;
2638 if (!reg_maps->uav_resource_info[i].type)
2639 continue;
2641 switch (reg_maps->uav_resource_info[i].data_type)
2643 case WINED3D_DATA_FLOAT:
2644 case WINED3D_DATA_UNORM:
2645 case WINED3D_DATA_SNORM:
2646 image_type_prefix = "";
2647 read_format = "r32f";
2648 break;
2650 case WINED3D_DATA_INT:
2651 image_type_prefix = "i";
2652 read_format = "r32i";
2653 break;
2655 case WINED3D_DATA_UINT:
2656 image_type_prefix = "u";
2657 read_format = "r32ui";
2658 break;
2660 default:
2661 image_type_prefix = "";
2662 read_format = "";
2663 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2664 break;
2667 switch (reg_maps->uav_resource_info[i].type)
2669 case WINED3D_SHADER_RESOURCE_BUFFER:
2670 image_type = "imageBuffer";
2671 break;
2673 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2674 image_type = "image2D";
2675 break;
2677 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2678 image_type = "image3D";
2679 break;
2681 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2682 image_type = "image2DArray";
2683 break;
2685 default:
2686 image_type = "unsupported_image";
2687 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2688 break;
2691 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2692 shader_addline(buffer, "layout(binding = %u)\n", i);
2693 if (reg_maps->uav_read_mask & (1u << i))
2694 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2695 read_format, image_type_prefix, image_type, prefix, i);
2696 else
2697 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2698 image_type_prefix, image_type, prefix, i);
2700 if (reg_maps->uav_counter_mask & (1u << i))
2701 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2702 i, prefix, i);
2705 /* Declare address variables */
2706 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2708 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2711 /* Declare output register temporaries */
2712 if (shader->limits->packed_output)
2713 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2715 /* Declare temporary variables */
2716 if (reg_maps->temporary_count)
2718 for (i = 0; i < reg_maps->temporary_count; ++i)
2719 shader_addline(buffer, "vec4 R%u;\n", i);
2721 else if (version->major < 4)
2723 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2725 if (map & 1)
2726 shader_addline(buffer, "vec4 R%u;\n", i);
2730 /* Declare indexable temporary variables */
2731 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2733 if (idx_temp_reg->component_count != 4)
2734 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2735 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2738 /* Declare loop registers aLx */
2739 if (version->major < 4)
2741 for (i = 0; i < reg_maps->loop_depth; ++i)
2743 shader_addline(buffer, "int aL%u;\n", i);
2744 shader_addline(buffer, "int tmpInt%u;\n", i);
2748 /* Temporary variables for matrix operations */
2749 shader_addline(buffer, "vec4 tmp0;\n");
2750 shader_addline(buffer, "vec4 tmp1;\n");
2752 if (!shader->load_local_constsF)
2754 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2756 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2757 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2758 shader_addline(buffer, ";\n");
2763 /* Prototypes */
2764 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2765 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2767 /** Used for opcode modifiers - They multiply the result by the specified amount */
2768 static const char * const shift_glsl_tab[] = {
2769 "", /* 0 (none) */
2770 "2.0 * ", /* 1 (x2) */
2771 "4.0 * ", /* 2 (x4) */
2772 "8.0 * ", /* 3 (x8) */
2773 "16.0 * ", /* 4 (x16) */
2774 "32.0 * ", /* 5 (x32) */
2775 "", /* 6 (x64) */
2776 "", /* 7 (x128) */
2777 "", /* 8 (d256) */
2778 "", /* 9 (d128) */
2779 "", /* 10 (d64) */
2780 "", /* 11 (d32) */
2781 "0.0625 * ", /* 12 (d16) */
2782 "0.125 * ", /* 13 (d8) */
2783 "0.25 * ", /* 14 (d4) */
2784 "0.5 * " /* 15 (d2) */
2787 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2788 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2789 const char *in_reg, const char *in_regswizzle, char *out_str)
2791 switch (src_modifier)
2793 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2794 case WINED3DSPSM_DW:
2795 case WINED3DSPSM_NONE:
2796 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2797 break;
2798 case WINED3DSPSM_NEG:
2799 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2800 break;
2801 case WINED3DSPSM_NOT:
2802 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2803 break;
2804 case WINED3DSPSM_BIAS:
2805 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2806 break;
2807 case WINED3DSPSM_BIASNEG:
2808 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2809 break;
2810 case WINED3DSPSM_SIGN:
2811 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2812 break;
2813 case WINED3DSPSM_SIGNNEG:
2814 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2815 break;
2816 case WINED3DSPSM_COMP:
2817 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2818 break;
2819 case WINED3DSPSM_X2:
2820 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2821 break;
2822 case WINED3DSPSM_X2NEG:
2823 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2824 break;
2825 case WINED3DSPSM_ABS:
2826 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2827 break;
2828 case WINED3DSPSM_ABSNEG:
2829 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2830 break;
2831 default:
2832 FIXME("Unhandled modifier %u\n", src_modifier);
2833 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2837 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2838 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2840 /* The ARB_shading_language_420pack extension allows swizzle operations on
2841 * scalars. */
2842 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2843 sprintf(register_name, "%s", glsl_variable);
2844 else
2845 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2848 /** Writes the GLSL variable name that corresponds to the register that the
2849 * DX opcode parameter is trying to access */
2850 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2851 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2852 const struct wined3d_shader_instruction *ins)
2854 /* oPos, oFog and oPts in D3D */
2855 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2857 const struct wined3d_shader *shader = ins->ctx->shader;
2858 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2859 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2860 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2861 const char *prefix = shader_glsl_get_prefix(version->type);
2862 struct glsl_src_param rel_param0, rel_param1;
2863 char imm_str[4][17];
2865 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2866 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2867 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2868 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2869 *is_color = FALSE;
2871 switch (reg->type)
2873 case WINED3DSPR_TEMP:
2874 sprintf(register_name, "R%u", reg->idx[0].offset);
2875 break;
2877 case WINED3DSPR_INPUT:
2878 case WINED3DSPR_INCONTROLPOINT:
2879 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2881 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2883 if (reg->idx[0].rel_addr)
2884 FIXME("VS3 input registers relative addressing.\n");
2885 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2886 *is_color = TRUE;
2887 if (reg->idx[0].rel_addr)
2889 sprintf(register_name, "%s_in[%s + %u]",
2890 prefix, rel_param0.param_str, reg->idx[0].offset);
2892 else
2894 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2896 break;
2899 if (version->type == WINED3D_SHADER_TYPE_HULL
2900 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2901 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2903 if (reg->idx[0].rel_addr)
2905 if (reg->idx[1].rel_addr)
2906 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2907 rel_param0.param_str, reg->idx[0].offset,
2908 rel_param1.param_str, reg->idx[1].offset);
2909 else
2910 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2911 rel_param0.param_str, reg->idx[0].offset,
2912 reg->idx[1].offset);
2914 else if (reg->idx[1].rel_addr)
2915 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2916 rel_param1.param_str, reg->idx[1].offset);
2917 else
2918 sprintf(register_name, "shader_in[%u].reg[%u]",
2919 reg->idx[0].offset, reg->idx[1].offset);
2920 break;
2923 /* pixel shaders >= 3.0 */
2924 if (version->major >= 3)
2926 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2927 unsigned int in_count = vec4_varyings(version->major, gl_info);
2929 if (reg->idx[0].rel_addr)
2931 /* Removing a + 0 would be an obvious optimization, but
2932 * OS X doesn't see the NOP operation there. */
2933 if (idx)
2935 if (needs_legacy_glsl_syntax(gl_info)
2936 && shader->u.ps.declared_in_count > in_count)
2938 sprintf(register_name,
2939 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2940 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2941 prefix, rel_param0.param_str, idx);
2943 else
2945 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2948 else
2950 if (needs_legacy_glsl_syntax(gl_info)
2951 && shader->u.ps.declared_in_count > in_count)
2953 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2954 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2955 prefix, rel_param0.param_str);
2957 else
2959 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2963 else
2965 if (idx == in_count) sprintf(register_name, "gl_Color");
2966 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2967 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2970 else
2972 if (!reg->idx[0].offset)
2973 strcpy(register_name, "ffp_varying_diffuse");
2974 else
2975 strcpy(register_name, "ffp_varying_specular");
2976 break;
2978 break;
2980 case WINED3DSPR_CONST:
2982 /* Relative addressing */
2983 if (reg->idx[0].rel_addr)
2985 if (wined3d_settings.check_float_constants)
2986 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2987 rel_param0.param_str, reg->idx[0].offset,
2988 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2989 prefix, rel_param0.param_str, reg->idx[0].offset);
2990 else if (reg->idx[0].offset)
2991 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2992 else
2993 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2995 else
2997 if (shader_constant_is_local(shader, reg->idx[0].offset))
2998 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2999 else
3000 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
3003 break;
3005 case WINED3DSPR_CONSTINT:
3006 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
3007 break;
3009 case WINED3DSPR_CONSTBOOL:
3010 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
3011 break;
3013 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
3014 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
3015 sprintf(register_name, "T%u", reg->idx[0].offset);
3016 else
3017 sprintf(register_name, "A%u", reg->idx[0].offset);
3018 break;
3020 case WINED3DSPR_LOOP:
3021 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
3022 break;
3024 case WINED3DSPR_SAMPLER:
3025 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
3026 break;
3028 case WINED3DSPR_COLOROUT:
3029 if (reg->idx[0].offset >= gl_info->limits.buffers)
3030 WARN("Write to render target %u, only %d supported.\n",
3031 reg->idx[0].offset, gl_info->limits.buffers);
3033 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
3034 break;
3036 case WINED3DSPR_RASTOUT:
3037 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
3038 break;
3040 case WINED3DSPR_DEPTHOUT:
3041 case WINED3DSPR_DEPTHOUTGE:
3042 case WINED3DSPR_DEPTHOUTLE:
3043 sprintf(register_name, "gl_FragDepth");
3044 break;
3046 case WINED3DSPR_ATTROUT:
3047 if (!reg->idx[0].offset)
3048 sprintf(register_name, "%s_out[8]", prefix);
3049 else
3050 sprintf(register_name, "%s_out[9]", prefix);
3051 break;
3053 case WINED3DSPR_TEXCRDOUT:
3054 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
3055 if (reg->idx[0].rel_addr)
3056 sprintf(register_name, "%s_out[%s + %u]",
3057 prefix, rel_param0.param_str, reg->idx[0].offset);
3058 else
3059 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3060 break;
3062 case WINED3DSPR_MISCTYPE:
3063 if (!reg->idx[0].offset)
3065 /* vPos */
3066 sprintf(register_name, "vpos");
3068 else if (reg->idx[0].offset == 1)
3070 /* Note that gl_FrontFacing is a bool, while vFace is
3071 * a float for which the sign determines front/back */
3072 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3074 else
3076 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3077 sprintf(register_name, "unrecognized_register");
3079 break;
3081 case WINED3DSPR_IMMCONST:
3082 switch (reg->immconst_type)
3084 case WINED3D_IMMCONST_SCALAR:
3085 switch (data_type)
3087 case WINED3D_DATA_FLOAT:
3088 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3089 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3090 else
3091 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3092 break;
3093 case WINED3D_DATA_INT:
3094 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3095 break;
3096 case WINED3D_DATA_RESOURCE:
3097 case WINED3D_DATA_SAMPLER:
3098 case WINED3D_DATA_UINT:
3099 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3100 break;
3101 default:
3102 sprintf(register_name, "<unhandled data type %#x>", data_type);
3103 break;
3105 break;
3107 case WINED3D_IMMCONST_VEC4:
3108 switch (data_type)
3110 case WINED3D_DATA_FLOAT:
3111 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3113 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3114 reg->u.immconst_data[0], reg->u.immconst_data[1],
3115 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3117 else
3119 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3120 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3121 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3122 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3123 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3124 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3126 break;
3127 case WINED3D_DATA_INT:
3128 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3129 reg->u.immconst_data[0], reg->u.immconst_data[1],
3130 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3131 break;
3132 case WINED3D_DATA_RESOURCE:
3133 case WINED3D_DATA_SAMPLER:
3134 case WINED3D_DATA_UINT:
3135 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3136 reg->u.immconst_data[0], reg->u.immconst_data[1],
3137 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3138 break;
3139 default:
3140 sprintf(register_name, "<unhandled data type %#x>", data_type);
3141 break;
3143 break;
3145 default:
3146 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3147 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3149 break;
3151 case WINED3DSPR_CONSTBUFFER:
3152 if (reg->idx[1].rel_addr)
3153 sprintf(register_name, "%s_cb%u[%s + %u]",
3154 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3155 else
3156 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3157 break;
3159 case WINED3DSPR_IMMCONSTBUFFER:
3160 if (reg->idx[0].rel_addr)
3161 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3162 else
3163 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3164 break;
3166 case WINED3DSPR_PRIMID:
3167 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3168 sprintf(register_name, "gl_PrimitiveIDIn");
3169 else
3170 sprintf(register_name, "gl_PrimitiveID");
3171 break;
3173 case WINED3DSPR_IDXTEMP:
3174 if (reg->idx[1].rel_addr)
3175 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3176 else
3177 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3178 break;
3180 case WINED3DSPR_LOCALTHREADINDEX:
3181 shader_glsl_fixup_scalar_register_variable(register_name,
3182 "int(gl_LocalInvocationIndex)", gl_info);
3183 break;
3185 case WINED3DSPR_GSINSTID:
3186 case WINED3DSPR_OUTPOINTID:
3187 shader_glsl_fixup_scalar_register_variable(register_name,
3188 "gl_InvocationID", gl_info);
3189 break;
3191 case WINED3DSPR_THREADID:
3192 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3193 break;
3195 case WINED3DSPR_THREADGROUPID:
3196 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3197 break;
3199 case WINED3DSPR_LOCALTHREADID:
3200 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3201 break;
3203 case WINED3DSPR_FORKINSTID:
3204 case WINED3DSPR_JOININSTID:
3205 shader_glsl_fixup_scalar_register_variable(register_name,
3206 "phase_instance_id", gl_info);
3207 break;
3209 case WINED3DSPR_TESSCOORD:
3210 sprintf(register_name, "gl_TessCoord");
3211 break;
3213 case WINED3DSPR_OUTCONTROLPOINT:
3214 if (reg->idx[0].rel_addr)
3216 if (reg->idx[1].rel_addr)
3217 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3218 rel_param0.param_str, reg->idx[0].offset,
3219 rel_param1.param_str, reg->idx[1].offset);
3220 else
3221 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3222 rel_param0.param_str, reg->idx[0].offset,
3223 reg->idx[1].offset);
3225 else if (reg->idx[1].rel_addr)
3227 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3228 reg->idx[0].offset, rel_param1.param_str,
3229 reg->idx[1].offset);
3231 else
3233 sprintf(register_name, "shader_out[%u].reg[%u]",
3234 reg->idx[0].offset, reg->idx[1].offset);
3236 break;
3238 case WINED3DSPR_PATCHCONST:
3239 if (version->type == WINED3D_SHADER_TYPE_HULL)
3240 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3241 else
3242 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3243 break;
3245 case WINED3DSPR_SAMPLEMASK:
3246 sprintf(register_name, "sample_mask");
3247 break;
3249 default:
3250 FIXME("Unhandled register type %#x.\n", reg->type);
3251 sprintf(register_name, "unrecognized_register");
3252 break;
3256 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3258 *str++ = '.';
3259 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3260 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3261 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3262 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3263 *str = '\0';
3266 /* Get the GLSL write mask for the destination register */
3267 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3269 DWORD mask = param->write_mask;
3271 if (shader_is_scalar(&param->reg))
3273 mask = WINED3DSP_WRITEMASK_0;
3274 *write_mask = '\0';
3276 else
3278 shader_glsl_write_mask_to_str(mask, write_mask);
3281 return mask;
3284 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3286 unsigned int size = 0;
3288 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3289 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3290 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3291 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3293 return size;
3296 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3297 unsigned int component_idx)
3299 /* swizzle bits fields: wwzzyyxx */
3300 return (swizzle >> (2 * component_idx)) & 0x3;
3303 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3305 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3306 * but addressed as "rgba". To fix this we need to swap the register's x
3307 * and z components. */
3308 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3309 unsigned int i;
3311 *str++ = '.';
3312 for (i = 0; i < 4; ++i)
3314 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3315 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3317 *str = '\0';
3320 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3321 BOOL fixup, DWORD mask, char *swizzle_str)
3323 if (shader_is_scalar(&param->reg))
3324 *swizzle_str = '\0';
3325 else
3326 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3329 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3330 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3332 if (dst_data_type == src_data_type)
3334 string_buffer_sprintf(dst_param, "%s", src_param);
3335 return;
3338 if (src_data_type == WINED3D_DATA_FLOAT)
3340 switch (dst_data_type)
3342 case WINED3D_DATA_INT:
3343 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3344 return;
3345 case WINED3D_DATA_RESOURCE:
3346 case WINED3D_DATA_SAMPLER:
3347 case WINED3D_DATA_UINT:
3348 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3349 return;
3350 default:
3351 break;
3355 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3357 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3358 return;
3361 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3363 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3364 return;
3367 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3368 string_buffer_sprintf(dst_param, "%s", src_param);
3371 /* From a given parameter token, generate the corresponding GLSL string.
3372 * Also, return the actual register name and swizzle in case the
3373 * caller needs this information as well. */
3374 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3375 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3376 enum wined3d_data_type data_type)
3378 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3379 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3380 enum wined3d_data_type param_data_type;
3381 BOOL is_color = FALSE;
3382 char swizzle_str[6];
3384 glsl_src->reg_name[0] = '\0';
3385 glsl_src->param_str[0] = '\0';
3386 swizzle_str[0] = '\0';
3388 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ins);
3389 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3391 switch (wined3d_src->reg.type)
3393 case WINED3DSPR_IMMCONST:
3394 param_data_type = data_type;
3395 break;
3396 case WINED3DSPR_FORKINSTID:
3397 case WINED3DSPR_GSINSTID:
3398 case WINED3DSPR_JOININSTID:
3399 case WINED3DSPR_LOCALTHREADID:
3400 case WINED3DSPR_LOCALTHREADINDEX:
3401 case WINED3DSPR_OUTPOINTID:
3402 case WINED3DSPR_PRIMID:
3403 case WINED3DSPR_THREADGROUPID:
3404 case WINED3DSPR_THREADID:
3405 param_data_type = WINED3D_DATA_INT;
3406 break;
3407 default:
3408 param_data_type = WINED3D_DATA_FLOAT;
3409 break;
3412 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3413 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3415 string_buffer_release(priv->string_buffers, reg_name);
3418 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3419 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3421 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3424 /* From a given parameter token, generate the corresponding GLSL string.
3425 * Also, return the actual register name and swizzle in case the
3426 * caller needs this information as well. */
3427 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3428 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3430 BOOL is_color = FALSE;
3432 glsl_dst->mask_str[0] = '\0';
3433 glsl_dst->reg_name[0] = '\0';
3435 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3436 glsl_dst->reg_name, &is_color, ins);
3437 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3440 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3441 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3442 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3443 enum wined3d_data_type data_type)
3445 struct glsl_dst_param glsl_dst;
3446 DWORD mask;
3448 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3450 switch (data_type)
3452 case WINED3D_DATA_FLOAT:
3453 shader_addline(buffer, "%s%s = %s(",
3454 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3455 break;
3456 case WINED3D_DATA_INT:
3457 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3458 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3459 break;
3460 case WINED3D_DATA_RESOURCE:
3461 case WINED3D_DATA_SAMPLER:
3462 case WINED3D_DATA_UINT:
3463 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3464 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3465 break;
3466 default:
3467 FIXME("Unhandled data type %#x.\n", data_type);
3468 shader_addline(buffer, "%s%s = %s(",
3469 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3470 break;
3474 return mask;
3477 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3478 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3480 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3483 /** Process GLSL instruction modifiers */
3484 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3486 struct glsl_dst_param dst_param;
3487 DWORD modifiers;
3489 if (!ins->dst_count) return;
3491 modifiers = ins->dst[0].modifiers;
3492 if (!modifiers) return;
3494 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3496 if (modifiers & WINED3DSPDM_SATURATE)
3498 /* _SAT means to clamp the value of the register to between 0 and 1 */
3499 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3500 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3503 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3505 FIXME("_centroid modifier not handled\n");
3508 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3510 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3514 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3516 switch (op)
3518 case WINED3D_SHADER_REL_OP_GT: return ">";
3519 case WINED3D_SHADER_REL_OP_EQ: return "==";
3520 case WINED3D_SHADER_REL_OP_GE: return ">=";
3521 case WINED3D_SHADER_REL_OP_LT: return "<";
3522 case WINED3D_SHADER_REL_OP_NE: return "!=";
3523 case WINED3D_SHADER_REL_OP_LE: return "<=";
3524 default:
3525 FIXME("Unrecognized operator %#x.\n", op);
3526 return "(\?\?)";
3530 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3532 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3535 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3536 unsigned int *coord_size, unsigned int *deriv_size)
3538 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3539 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3541 *coord_size = resource_type_info[resource_type].coord_size;
3542 *deriv_size = *coord_size;
3543 if (is_array)
3544 --(*deriv_size);
3547 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3548 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3550 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3551 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3552 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3553 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3554 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3555 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3556 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3557 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3558 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3559 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3560 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3561 const char *base = "texture", *type_part = "", *suffix = "";
3562 unsigned int coord_size, deriv_size;
3564 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3566 if (resource_type >= ARRAY_SIZE(resource_type_info))
3568 ERR("Unexpected resource type %#x.\n", resource_type);
3569 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3572 /* Note that there's no such thing as a projected cube texture. */
3573 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3574 projected = FALSE;
3576 if (needs_legacy_glsl_syntax(gl_info))
3578 if (shadow)
3579 base = "shadow";
3581 type_part = resource_type_info[resource_type].type_part;
3582 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3583 type_part = "2DRect";
3584 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3585 FIXME("Unhandled resource type %#x.\n", resource_type);
3587 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3589 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3590 suffix = "ARB";
3591 else
3592 FIXME("Unsupported grad function.\n");
3596 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3598 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3599 if (flags & ~texel_fetch_flags)
3600 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3602 base = "texelFetch";
3603 type_part = "";
3606 sample_function->name = string_buffer_get(priv->string_buffers);
3607 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3608 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3610 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3611 if (shadow)
3612 ++coord_size;
3613 sample_function->offset_size = offset ? deriv_size : 0;
3614 sample_function->coord_mask = (1u << coord_size) - 1;
3615 sample_function->deriv_mask = (1u << deriv_size) - 1;
3616 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3619 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3620 struct glsl_sample_function *sample_function)
3622 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3624 string_buffer_release(priv->string_buffers, sample_function->name);
3627 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3628 BOOL sign_fixup, enum fixup_channel_source channel_source)
3630 switch(channel_source)
3632 case CHANNEL_SOURCE_ZERO:
3633 strcat(arguments, "0.0");
3634 break;
3636 case CHANNEL_SOURCE_ONE:
3637 strcat(arguments, "1.0");
3638 break;
3640 case CHANNEL_SOURCE_X:
3641 strcat(arguments, reg_name);
3642 strcat(arguments, ".x");
3643 break;
3645 case CHANNEL_SOURCE_Y:
3646 strcat(arguments, reg_name);
3647 strcat(arguments, ".y");
3648 break;
3650 case CHANNEL_SOURCE_Z:
3651 strcat(arguments, reg_name);
3652 strcat(arguments, ".z");
3653 break;
3655 case CHANNEL_SOURCE_W:
3656 strcat(arguments, reg_name);
3657 strcat(arguments, ".w");
3658 break;
3660 default:
3661 FIXME("Unhandled channel source %#x\n", channel_source);
3662 strcat(arguments, "undefined");
3663 break;
3666 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3669 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3670 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3672 unsigned int mask_size, remaining;
3673 DWORD fixup_mask = 0;
3674 char arguments[256];
3675 char mask_str[6];
3677 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3678 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3679 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3680 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3681 if (!(mask &= fixup_mask))
3682 return;
3684 if (is_complex_fixup(fixup))
3686 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3687 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3688 return;
3691 shader_glsl_write_mask_to_str(mask, mask_str);
3692 mask_size = shader_glsl_get_write_mask_size(mask);
3694 arguments[0] = '\0';
3695 remaining = mask_size;
3696 if (mask & WINED3DSP_WRITEMASK_0)
3698 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3699 if (--remaining) strcat(arguments, ", ");
3701 if (mask & WINED3DSP_WRITEMASK_1)
3703 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3704 if (--remaining) strcat(arguments, ", ");
3706 if (mask & WINED3DSP_WRITEMASK_2)
3708 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3709 if (--remaining) strcat(arguments, ", ");
3711 if (mask & WINED3DSP_WRITEMASK_3)
3713 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3714 if (--remaining) strcat(arguments, ", ");
3717 if (mask_size > 1)
3718 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3719 else
3720 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3723 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3725 char reg_name[256];
3726 BOOL is_color;
3728 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins);
3729 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3732 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3733 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3734 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3735 const char *coord_reg_fmt, ...)
3737 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3738 char dst_swizzle[6];
3739 struct color_fixup_desc fixup;
3740 BOOL np2_fixup = FALSE;
3741 va_list args;
3742 int ret;
3744 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3746 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3747 * We actually rely on it for vertex shaders and SM4+. */
3748 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3750 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3751 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3753 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3754 np2_fixup = TRUE;
3756 else
3758 fixup = COLOR_FIXUP_IDENTITY;
3761 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3763 if (sample_function->output_single_component)
3764 shader_addline(ins->ctx->buffer, "vec4(");
3766 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3767 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3769 for (;;)
3771 va_start(args, coord_reg_fmt);
3772 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3773 va_end(args);
3774 if (!ret)
3775 break;
3776 if (!string_buffer_resize(ins->ctx->buffer, ret))
3777 break;
3780 if (np2_fixup)
3782 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3783 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3785 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3787 case 1:
3788 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3789 idx >> 1, (idx % 2) ? "z" : "x");
3790 break;
3791 case 2:
3792 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3793 idx >> 1, (idx % 2) ? "zw" : "xy");
3794 break;
3795 case 3:
3796 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3797 idx >> 1, (idx % 2) ? "zw" : "xy");
3798 break;
3799 case 4:
3800 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3801 idx >> 1, (idx % 2) ? "zw" : "xy");
3802 break;
3805 if (dx && dy)
3806 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3807 else if (bias)
3808 shader_addline(ins->ctx->buffer, ", %s", bias);
3809 if (sample_function->offset_size)
3811 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3812 shader_addline(ins->ctx->buffer, ", ");
3813 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3815 shader_addline(ins->ctx->buffer, ")");
3817 if (sample_function->output_single_component)
3818 shader_addline(ins->ctx->buffer, ")");
3820 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3822 if (!is_identity_fixup(fixup))
3823 shader_glsl_color_correction(ins, fixup);
3826 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3828 /* Write the final position.
3830 * OpenGL coordinates specify the center of the pixel while D3D coords
3831 * specify the corner. The offsets are stored in z and w in
3832 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3833 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3834 * a MAD. */
3835 if (use_viewport_index)
3837 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3838 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3840 else
3842 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3843 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3846 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3847 * in utils.c
3849 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3850 * shaders are run before the homogeneous divide, so we have to take the w
3851 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3852 * z = z * 2 - w. */
3853 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3856 /*****************************************************************************
3857 * Begin processing individual instruction opcodes
3858 ****************************************************************************/
3860 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3862 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3863 struct glsl_src_param src0_param;
3864 struct glsl_src_param src1_param;
3865 DWORD write_mask;
3866 const char *op;
3868 /* Determine the GLSL operator to use based on the opcode */
3869 switch (ins->handler_idx)
3871 case WINED3DSIH_ADD: op = "+"; break;
3872 case WINED3DSIH_AND: op = "&"; break;
3873 case WINED3DSIH_DIV: op = "/"; break;
3874 case WINED3DSIH_IADD: op = "+"; break;
3875 case WINED3DSIH_ISHL: op = "<<"; break;
3876 case WINED3DSIH_ISHR: op = ">>"; break;
3877 case WINED3DSIH_MUL: op = "*"; break;
3878 case WINED3DSIH_OR: op = "|"; break;
3879 case WINED3DSIH_SUB: op = "-"; break;
3880 case WINED3DSIH_USHR: op = ">>"; break;
3881 case WINED3DSIH_XOR: op = "^"; break;
3882 default:
3883 op = "<unhandled operator>";
3884 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3885 break;
3888 write_mask = shader_glsl_append_dst(buffer, ins);
3889 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3890 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3891 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3894 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3896 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3897 struct glsl_src_param src0_param;
3898 struct glsl_src_param src1_param;
3899 unsigned int mask_size;
3900 DWORD write_mask;
3901 const char *op;
3903 write_mask = shader_glsl_append_dst(buffer, ins);
3904 mask_size = shader_glsl_get_write_mask_size(write_mask);
3905 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3906 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3908 if (mask_size > 1)
3910 switch (ins->handler_idx)
3912 case WINED3DSIH_EQ: op = "equal"; break;
3913 case WINED3DSIH_IEQ: op = "equal"; break;
3914 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3915 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3916 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3917 case WINED3DSIH_LT: op = "lessThan"; break;
3918 case WINED3DSIH_ILT: op = "lessThan"; break;
3919 case WINED3DSIH_ULT: op = "lessThan"; break;
3920 case WINED3DSIH_NE: op = "notEqual"; break;
3921 case WINED3DSIH_INE: op = "notEqual"; break;
3922 default:
3923 op = "<unhandled operator>";
3924 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3925 break;
3928 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3929 mask_size, op, src0_param.param_str, src1_param.param_str);
3931 else
3933 switch (ins->handler_idx)
3935 case WINED3DSIH_EQ: op = "=="; break;
3936 case WINED3DSIH_IEQ: op = "=="; break;
3937 case WINED3DSIH_GE: op = ">="; break;
3938 case WINED3DSIH_IGE: op = ">="; break;
3939 case WINED3DSIH_UGE: op = ">="; break;
3940 case WINED3DSIH_LT: op = "<"; break;
3941 case WINED3DSIH_ILT: op = "<"; break;
3942 case WINED3DSIH_ULT: op = "<"; break;
3943 case WINED3DSIH_NE: op = "!="; break;
3944 case WINED3DSIH_INE: op = "!="; break;
3945 default:
3946 op = "<unhandled operator>";
3947 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3948 break;
3951 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3952 src0_param.param_str, op, src1_param.param_str);
3956 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3958 struct glsl_src_param src_param;
3959 DWORD write_mask;
3960 const char *op;
3962 switch (ins->handler_idx)
3964 case WINED3DSIH_INEG: op = "-"; break;
3965 case WINED3DSIH_NOT: op = "~"; break;
3966 default:
3967 op = "<unhandled operator>";
3968 ERR("Unhandled opcode %s.\n",
3969 debug_d3dshaderinstructionhandler(ins->handler_idx));
3970 break;
3973 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3974 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3975 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3978 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3980 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3981 struct glsl_src_param src0_param;
3982 struct glsl_src_param src1_param;
3983 DWORD write_mask;
3985 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3986 * If not, we can emulate it. */
3987 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3988 FIXME("64-bit integer multiplies not implemented.\n");
3990 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3992 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3993 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3994 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3996 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3997 src0_param.param_str, src1_param.param_str);
4001 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
4003 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4004 struct glsl_src_param src0_param, src1_param;
4005 DWORD write_mask;
4007 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4009 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4011 char dst_mask[6];
4013 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4014 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4015 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4016 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
4017 dst_mask, src0_param.param_str, src1_param.param_str);
4019 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4020 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4021 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4022 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4024 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4025 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4027 else
4029 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4030 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4031 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4032 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
4035 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4037 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4038 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4039 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4040 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4044 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
4045 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
4047 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4048 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4049 struct glsl_src_param src0_param;
4050 DWORD write_mask;
4052 write_mask = shader_glsl_append_dst(buffer, ins);
4053 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4055 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
4056 * shader versions WINED3DSIO_MOVA is used for this. */
4057 if (ins->ctx->reg_maps->shader_version.major == 1
4058 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
4059 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
4061 /* This is a simple floor() */
4062 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4063 if (mask_size > 1) {
4064 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4065 } else {
4066 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4069 else if (ins->handler_idx == WINED3DSIH_MOVA)
4071 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4073 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4075 if (mask_size > 1)
4076 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4077 else
4078 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4080 else
4082 if (mask_size > 1)
4083 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4084 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4085 else
4086 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4087 src0_param.param_str, src0_param.param_str);
4090 else
4092 shader_addline(buffer, "%s);\n", src0_param.param_str);
4096 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4097 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4099 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4100 struct glsl_src_param src0_param;
4101 struct glsl_src_param src1_param;
4102 DWORD dst_write_mask, src_write_mask;
4103 unsigned int dst_size;
4105 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4106 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4108 /* dp4 works on vec4, dp3 on vec3, etc. */
4109 if (ins->handler_idx == WINED3DSIH_DP4)
4110 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4111 else if (ins->handler_idx == WINED3DSIH_DP3)
4112 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4113 else
4114 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4116 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4117 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4119 if (dst_size > 1) {
4120 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4121 } else {
4122 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4126 /* Note that this instruction has some restrictions. The destination write mask
4127 * can't contain the w component, and the source swizzles have to be .xyzw */
4128 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4130 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4131 struct glsl_src_param src0_param;
4132 struct glsl_src_param src1_param;
4133 char dst_mask[6];
4135 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4136 shader_glsl_append_dst(ins->ctx->buffer, ins);
4137 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4138 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4139 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4142 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4144 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4146 if (!stream)
4147 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4148 else
4149 FIXME("Unhandled primitive stream %u.\n", stream);
4152 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4153 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4154 * GLSL uses the value as-is. */
4155 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4157 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4158 struct glsl_src_param src0_param;
4159 struct glsl_src_param src1_param;
4160 DWORD dst_write_mask;
4161 unsigned int dst_size;
4163 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4164 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4166 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4167 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4169 if (dst_size > 1)
4171 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4172 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4174 else
4176 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4177 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4181 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4182 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4184 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4185 struct glsl_src_param src_param;
4186 const char *instruction;
4187 DWORD write_mask;
4188 unsigned i;
4190 /* Determine the GLSL function to use based on the opcode */
4191 /* TODO: Possibly make this a table for faster lookups */
4192 switch (ins->handler_idx)
4194 case WINED3DSIH_ABS: instruction = "abs"; break;
4195 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4196 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4197 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4198 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4199 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4200 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4201 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4202 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4203 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4204 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4205 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4206 case WINED3DSIH_FRC: instruction = "fract"; break;
4207 case WINED3DSIH_IMAX: instruction = "max"; break;
4208 case WINED3DSIH_IMIN: instruction = "min"; break;
4209 case WINED3DSIH_MAX: instruction = "max"; break;
4210 case WINED3DSIH_MIN: instruction = "min"; break;
4211 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4212 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4213 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4214 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4215 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4216 case WINED3DSIH_UMAX: instruction = "max"; break;
4217 case WINED3DSIH_UMIN: instruction = "min"; break;
4218 default: instruction = "";
4219 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4220 break;
4223 write_mask = shader_glsl_append_dst(buffer, ins);
4225 /* In D3D bits are numbered from the most significant bit. */
4226 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4227 shader_addline(buffer, "31 - ");
4228 shader_addline(buffer, "%s(", instruction);
4230 if (ins->src_count)
4232 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4233 shader_addline(buffer, "%s", src_param.param_str);
4234 for (i = 1; i < ins->src_count; ++i)
4236 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4237 shader_addline(buffer, ", %s", src_param.param_str);
4241 shader_addline(buffer, "));\n");
4244 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4246 struct wined3d_shader_dst_param dst;
4247 struct glsl_src_param src;
4248 DWORD write_mask;
4249 const char *fmt;
4250 unsigned int i;
4252 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4253 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4255 dst = ins->dst[0];
4256 for (i = 0; i < 4; ++i)
4258 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4259 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4260 &dst, dst.reg.data_type)))
4261 continue;
4263 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4264 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4268 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4270 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4271 struct wined3d_shader_dst_param dst;
4272 struct glsl_src_param src[4];
4273 const char *instruction;
4274 BOOL tmp_dst = FALSE;
4275 char mask_char[6];
4276 unsigned int i, j;
4277 DWORD write_mask;
4279 switch (ins->handler_idx)
4281 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4282 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4283 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4284 default:
4285 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4286 return;
4289 for (i = 0; i < ins->src_count; ++i)
4291 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4292 && ins->dst[0].reg.type == ins->src[i].reg.type)
4293 tmp_dst = TRUE;
4296 dst = ins->dst[0];
4297 for (i = 0; i < 4; ++i)
4299 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4300 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4301 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4302 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4303 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4304 continue;
4306 for (j = 0; j < ins->src_count; ++j)
4307 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4308 shader_addline(buffer, "%s(", instruction);
4309 for (j = 0; j < ins->src_count - 2; ++j)
4310 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4311 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4314 if (tmp_dst)
4316 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4317 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4318 shader_addline(buffer, "tmp0%s);\n", mask_char);
4322 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4324 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4326 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4327 struct glsl_src_param src_param;
4328 unsigned int mask_size;
4329 DWORD write_mask;
4330 char dst_mask[6];
4332 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4333 mask_size = shader_glsl_get_write_mask_size(write_mask);
4334 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4336 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4337 src_param.param_str, src_param.param_str);
4338 shader_glsl_append_dst(buffer, ins);
4340 if (mask_size > 1)
4342 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4343 mask_size, src_param.param_str);
4345 else
4347 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4348 src_param.param_str);
4352 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4354 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4355 ins->ctx->reg_maps->shader_version.minor);
4356 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4357 struct glsl_src_param src0_param;
4358 const char *prefix, *suffix;
4359 unsigned int dst_size;
4360 DWORD dst_write_mask;
4362 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4363 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4365 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4366 dst_write_mask = WINED3DSP_WRITEMASK_3;
4368 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4370 switch (ins->handler_idx)
4372 case WINED3DSIH_EXP:
4373 case WINED3DSIH_EXPP:
4374 prefix = "exp2(";
4375 suffix = ")";
4376 break;
4378 case WINED3DSIH_LOG:
4379 case WINED3DSIH_LOGP:
4380 prefix = "log2(abs(";
4381 suffix = "))";
4382 break;
4384 case WINED3DSIH_RCP:
4385 prefix = "1.0 / ";
4386 suffix = "";
4387 break;
4389 case WINED3DSIH_RSQ:
4390 prefix = "inversesqrt(abs(";
4391 suffix = "))";
4392 break;
4394 default:
4395 prefix = "";
4396 suffix = "";
4397 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4398 break;
4401 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4402 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4403 else
4404 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4407 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4408 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4409 * dst.x = 2^(floor(src))
4410 * dst.y = src - floor(src)
4411 * dst.z = 2^src (partial precision is allowed, but optional)
4412 * dst.w = 1.0;
4413 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4414 * dst = 2^src; (partial precision is allowed, but optional)
4416 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4418 if (ins->ctx->reg_maps->shader_version.major < 2)
4420 struct glsl_src_param src_param;
4421 char dst_mask[6];
4423 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4425 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4426 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4427 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4428 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4430 shader_glsl_append_dst(ins->ctx->buffer, ins);
4431 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4432 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4433 return;
4436 shader_glsl_scalar_op(ins);
4439 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4440 const char *vector_constructor, const char *scalar_constructor)
4442 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4443 struct glsl_src_param src_param;
4444 unsigned int mask_size;
4445 DWORD write_mask;
4447 write_mask = shader_glsl_append_dst(buffer, ins);
4448 mask_size = shader_glsl_get_write_mask_size(write_mask);
4449 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4451 if (mask_size > 1)
4452 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4453 else
4454 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4457 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4459 shader_glsl_cast(ins, "ivec", "int");
4462 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4464 shader_glsl_cast(ins, "uvec", "uint");
4467 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4469 shader_glsl_cast(ins, "vec", "float");
4472 /** Process signed comparison opcodes in GLSL. */
4473 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4475 struct glsl_src_param src0_param;
4476 struct glsl_src_param src1_param;
4477 DWORD write_mask;
4478 unsigned int mask_size;
4480 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4481 mask_size = shader_glsl_get_write_mask_size(write_mask);
4482 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4483 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4485 if (mask_size > 1) {
4486 const char *compare;
4488 switch(ins->handler_idx)
4490 case WINED3DSIH_SLT: compare = "lessThan"; break;
4491 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4492 default: compare = "";
4493 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4496 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4497 src0_param.param_str, src1_param.param_str);
4498 } else {
4499 switch(ins->handler_idx)
4501 case WINED3DSIH_SLT:
4502 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4503 * to return 0.0 but step returns 1.0 because step is not < x
4504 * An alternative is a bvec compare padded with an unused second component.
4505 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4506 * issue. Playing with not() is not possible either because not() does not accept
4507 * a scalar.
4509 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4510 src0_param.param_str, src1_param.param_str);
4511 break;
4512 case WINED3DSIH_SGE:
4513 /* Here we can use the step() function and safe a conditional */
4514 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4515 break;
4516 default:
4517 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4523 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4525 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4526 struct wined3d_shader_dst_param dst[2];
4527 struct glsl_src_param src[3];
4528 unsigned int i, j, k;
4529 char mask_char[6];
4530 DWORD write_mask;
4531 BOOL tmp_dst[2];
4533 for (i = 0; i < ins->dst_count; ++i)
4535 tmp_dst[i] = FALSE;
4536 for (j = 0; j < ins->src_count; ++j)
4538 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4539 && ins->dst[i].reg.type == ins->src[j].reg.type)
4540 tmp_dst[i] = TRUE;
4544 dst[0] = ins->dst[0];
4545 dst[1] = ins->dst[1];
4546 for (i = 0; i < 4; ++i)
4548 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4550 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4551 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4552 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4553 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4554 continue;
4556 for (k = 0; k < ARRAY_SIZE(src); ++k)
4557 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4559 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4560 src[0].param_str, src[1].param_str, src[2].param_str);
4564 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4566 if (tmp_dst[i])
4568 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4569 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4570 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4575 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4577 const char *condition_prefix, *condition_suffix;
4578 struct wined3d_shader_dst_param dst;
4579 struct glsl_src_param src0_param;
4580 struct glsl_src_param src1_param;
4581 struct glsl_src_param src2_param;
4582 BOOL temp_destination = FALSE;
4583 DWORD cmp_channel = 0;
4584 unsigned int i, j;
4585 char mask_char[6];
4586 DWORD write_mask;
4588 switch (ins->handler_idx)
4590 case WINED3DSIH_CMP:
4591 condition_prefix = "";
4592 condition_suffix = " >= 0.0";
4593 break;
4595 case WINED3DSIH_CND:
4596 condition_prefix = "";
4597 condition_suffix = " > 0.5";
4598 break;
4600 case WINED3DSIH_MOVC:
4601 condition_prefix = "bool(";
4602 condition_suffix = ")";
4603 break;
4605 default:
4606 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4607 condition_prefix = "<unhandled prefix>";
4608 condition_suffix = "<unhandled suffix>";
4609 break;
4612 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4614 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4615 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4616 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4617 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4619 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4620 condition_prefix, src0_param.param_str, condition_suffix,
4621 src1_param.param_str, src2_param.param_str);
4622 return;
4625 dst = ins->dst[0];
4627 /* Splitting the instruction up in multiple lines imposes a problem:
4628 * The first lines may overwrite source parameters of the following lines.
4629 * Deal with that by using a temporary destination register if needed. */
4630 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4631 && ins->src[0].reg.type == dst.reg.type)
4632 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4633 && ins->src[1].reg.type == dst.reg.type)
4634 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4635 && ins->src[2].reg.type == dst.reg.type))
4636 temp_destination = TRUE;
4638 /* Cycle through all source0 channels. */
4639 for (i = 0; i < 4; ++i)
4641 write_mask = 0;
4642 /* Find the destination channels which use the current source0 channel. */
4643 for (j = 0; j < 4; ++j)
4645 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4647 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4648 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4651 dst.write_mask = ins->dst[0].write_mask & write_mask;
4653 if (temp_destination)
4655 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4656 continue;
4657 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4659 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4660 continue;
4662 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4663 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4664 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4666 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4667 condition_prefix, src0_param.param_str, condition_suffix,
4668 src1_param.param_str, src2_param.param_str);
4671 if (temp_destination)
4673 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4674 shader_glsl_append_dst(ins->ctx->buffer, ins);
4675 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4679 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4680 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4681 * the compare is done per component of src0. */
4682 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4684 struct glsl_src_param src0_param;
4685 struct glsl_src_param src1_param;
4686 struct glsl_src_param src2_param;
4687 DWORD write_mask;
4688 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4689 ins->ctx->reg_maps->shader_version.minor);
4691 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4693 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4694 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4695 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4696 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4698 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4699 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4700 else
4701 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4702 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4703 return;
4706 shader_glsl_conditional_move(ins);
4709 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4710 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4712 struct glsl_src_param src0_param;
4713 struct glsl_src_param src1_param;
4714 struct glsl_src_param src2_param;
4715 DWORD write_mask;
4717 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4718 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4719 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4720 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4721 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4722 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4725 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4726 Vertex shaders to GLSL codes */
4727 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4729 int i;
4730 int nComponents = 0;
4731 struct wined3d_shader_dst_param tmp_dst = {{0}};
4732 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4733 struct wined3d_shader_instruction tmp_ins;
4735 memset(&tmp_ins, 0, sizeof(tmp_ins));
4737 /* Set constants for the temporary argument */
4738 tmp_ins.ctx = ins->ctx;
4739 tmp_ins.dst_count = 1;
4740 tmp_ins.dst = &tmp_dst;
4741 tmp_ins.src_count = 2;
4742 tmp_ins.src = tmp_src;
4744 switch(ins->handler_idx)
4746 case WINED3DSIH_M4x4:
4747 nComponents = 4;
4748 tmp_ins.handler_idx = WINED3DSIH_DP4;
4749 break;
4750 case WINED3DSIH_M4x3:
4751 nComponents = 3;
4752 tmp_ins.handler_idx = WINED3DSIH_DP4;
4753 break;
4754 case WINED3DSIH_M3x4:
4755 nComponents = 4;
4756 tmp_ins.handler_idx = WINED3DSIH_DP3;
4757 break;
4758 case WINED3DSIH_M3x3:
4759 nComponents = 3;
4760 tmp_ins.handler_idx = WINED3DSIH_DP3;
4761 break;
4762 case WINED3DSIH_M3x2:
4763 nComponents = 2;
4764 tmp_ins.handler_idx = WINED3DSIH_DP3;
4765 break;
4766 default:
4767 break;
4770 tmp_dst = ins->dst[0];
4771 tmp_src[0] = ins->src[0];
4772 tmp_src[1] = ins->src[1];
4773 for (i = 0; i < nComponents; ++i)
4775 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4776 shader_glsl_dot(&tmp_ins);
4777 ++tmp_src[1].reg.idx[0].offset;
4782 The LRP instruction performs a component-wise linear interpolation
4783 between the second and third operands using the first operand as the
4784 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4785 This is equivalent to mix(src2, src1, src0);
4787 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4789 struct glsl_src_param src0_param;
4790 struct glsl_src_param src1_param;
4791 struct glsl_src_param src2_param;
4792 DWORD write_mask;
4794 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4796 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4797 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4798 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4800 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4801 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4804 /** Process the WINED3DSIO_LIT instruction in GLSL:
4805 * dst.x = dst.w = 1.0
4806 * dst.y = (src0.x > 0) ? src0.x
4807 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4808 * where src.w is clamped at +- 128
4810 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4812 struct glsl_src_param src0_param;
4813 struct glsl_src_param src1_param;
4814 struct glsl_src_param src3_param;
4815 char dst_mask[6];
4817 shader_glsl_append_dst(ins->ctx->buffer, ins);
4818 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4820 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4821 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4822 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4824 /* The sdk specifies the instruction like this
4825 * dst.x = 1.0;
4826 * if(src.x > 0.0) dst.y = src.x
4827 * else dst.y = 0.0.
4828 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4829 * else dst.z = 0.0;
4830 * dst.w = 1.0;
4831 * (where power = src.w clamped between -128 and 128)
4833 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4834 * dst.x = 1.0 ... No further explanation needed
4835 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4836 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4837 * dst.w = 1.0. ... Nothing fancy.
4839 * So we still have one conditional in there. So do this:
4840 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4842 * 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),
4843 * 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.
4844 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4846 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4847 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4848 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4850 shader_addline(ins->ctx->buffer,
4851 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4852 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4853 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4854 src0_param.param_str, src3_param.param_str, dst_mask);
4857 /** Process the WINED3DSIO_DST instruction in GLSL:
4858 * dst.x = 1.0
4859 * dst.y = src0.x * src0.y
4860 * dst.z = src0.z
4861 * dst.w = src1.w
4863 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4865 struct glsl_src_param src0y_param;
4866 struct glsl_src_param src0z_param;
4867 struct glsl_src_param src1y_param;
4868 struct glsl_src_param src1w_param;
4869 char dst_mask[6];
4871 shader_glsl_append_dst(ins->ctx->buffer, ins);
4872 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4874 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4875 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4876 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4877 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4879 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4880 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4883 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4884 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4885 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4887 * dst.x = cos(src0.?)
4888 * dst.y = sin(src0.?)
4889 * dst.z = dst.z
4890 * dst.w = dst.w
4892 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4894 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4895 struct glsl_src_param src0_param;
4896 DWORD write_mask;
4898 if (ins->ctx->reg_maps->shader_version.major < 4)
4900 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4902 write_mask = shader_glsl_append_dst(buffer, ins);
4903 switch (write_mask)
4905 case WINED3DSP_WRITEMASK_0:
4906 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4907 break;
4909 case WINED3DSP_WRITEMASK_1:
4910 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4911 break;
4913 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4914 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4915 src0_param.param_str, src0_param.param_str);
4916 break;
4918 default:
4919 ERR("Write mask should be .x, .y or .xy\n");
4920 break;
4923 return;
4926 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4929 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4931 char dst_mask[6];
4933 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4934 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4935 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4937 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4938 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4939 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4941 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4942 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4944 else
4946 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4947 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4948 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4951 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4953 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4954 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4955 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4959 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4960 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4961 * generate invalid code
4963 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4965 struct glsl_src_param src0_param;
4966 DWORD write_mask;
4968 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4969 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4971 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4974 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4975 * Start a for() loop where src1.y is the initial value of aL,
4976 * increment aL by src1.z for a total of src1.x iterations.
4977 * Need to use a temporary variable for this operation.
4979 /* FIXME: I don't think nested loops will work correctly this way. */
4980 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4982 struct wined3d_shader_parser_state *state = ins->ctx->state;
4983 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4984 const struct wined3d_shader *shader = ins->ctx->shader;
4985 const struct wined3d_shader_lconst *constant;
4986 struct glsl_src_param src1_param;
4987 const DWORD *control_values = NULL;
4989 if (ins->ctx->reg_maps->shader_version.major < 4)
4991 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4993 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4994 * class hardware doesn't support real varying indexing, but Microsoft
4995 * designed this feature for Shader model 2.x+. If the loop control is
4996 * known at compile time, the GLSL compiler can unroll the loop, and
4997 * replace indirect addressing with direct addressing. */
4998 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
5000 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5002 if (constant->idx == ins->src[1].reg.idx[0].offset)
5004 control_values = constant->value;
5005 break;
5010 if (control_values)
5012 struct wined3d_shader_loop_control loop_control;
5013 loop_control.count = control_values[0];
5014 loop_control.start = control_values[1];
5015 loop_control.step = (int)control_values[2];
5017 if (loop_control.step > 0)
5019 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
5020 state->current_loop_depth, loop_control.start,
5021 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
5022 state->current_loop_depth, loop_control.step);
5024 else if (loop_control.step < 0)
5026 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
5027 state->current_loop_depth, loop_control.start,
5028 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
5029 state->current_loop_depth, loop_control.step);
5031 else
5033 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
5034 state->current_loop_depth, loop_control.start, state->current_loop_depth,
5035 state->current_loop_depth, loop_control.count,
5036 state->current_loop_depth);
5039 else
5041 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
5042 state->current_loop_depth, state->current_loop_reg,
5043 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
5044 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
5047 ++state->current_loop_reg;
5049 else
5051 shader_addline(buffer, "for (;;)\n{\n");
5054 ++state->current_loop_depth;
5057 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
5059 struct wined3d_shader_parser_state *state = ins->ctx->state;
5061 shader_addline(ins->ctx->buffer, "}\n");
5063 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5065 --state->current_loop_depth;
5066 --state->current_loop_reg;
5069 if (ins->handler_idx == WINED3DSIH_ENDREP)
5071 --state->current_loop_depth;
5075 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5077 struct wined3d_shader_parser_state *state = ins->ctx->state;
5078 const struct wined3d_shader *shader = ins->ctx->shader;
5079 const struct wined3d_shader_lconst *constant;
5080 struct glsl_src_param src0_param;
5081 const DWORD *control_values = NULL;
5083 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5084 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5086 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5088 if (constant->idx == ins->src[0].reg.idx[0].offset)
5090 control_values = constant->value;
5091 break;
5096 if (control_values)
5098 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5099 state->current_loop_depth, state->current_loop_depth,
5100 control_values[0], state->current_loop_depth);
5102 else
5104 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5105 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5106 state->current_loop_depth, state->current_loop_depth,
5107 src0_param.param_str, state->current_loop_depth);
5110 ++state->current_loop_depth;
5113 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5115 struct glsl_src_param src0_param;
5117 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5118 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5121 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5123 struct glsl_src_param src0_param;
5125 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5126 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5129 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5131 shader_addline(ins->ctx->buffer, "default:\n");
5134 static void shader_glsl_generate_conditional_op(const struct wined3d_shader_instruction *ins,
5135 const char *op)
5137 struct glsl_src_param src_param;
5138 const char *condition;
5140 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5141 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5142 shader_addline(ins->ctx->buffer, "if (%s(%s)) %s\n", condition, src_param.param_str, op);
5145 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5147 shader_glsl_generate_conditional_op(ins, "{");
5150 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5152 struct glsl_src_param src0_param;
5153 struct glsl_src_param src1_param;
5155 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5156 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5158 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5159 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5162 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5164 shader_addline(ins->ctx->buffer, "} else {\n");
5167 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5169 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5170 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5172 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5173 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5174 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5176 if (!stream)
5177 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5178 else
5179 FIXME("Unhandled primitive stream %u.\n", stream);
5182 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5184 shader_addline(ins->ctx->buffer, "break;\n");
5187 /* FIXME: According to MSDN the compare is done per component. */
5188 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5190 struct glsl_src_param src0_param;
5191 struct glsl_src_param src1_param;
5193 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5194 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5196 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5197 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5200 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5202 const char *op;
5204 switch (ins->handler_idx)
5206 case WINED3DSIH_BREAKP: op = "break;"; break;
5207 case WINED3DSIH_CONTINUEP: op = "continue;"; break;
5208 case WINED3DSIH_RETP: op = "return;"; break;
5209 default:
5210 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5211 return;
5214 shader_glsl_generate_conditional_op(ins, op);
5217 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5219 shader_addline(ins->ctx->buffer, "continue;\n");
5222 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5224 shader_addline(ins->ctx->buffer, "}\n");
5225 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5227 /* Subroutines appear at the end of the shader. */
5228 ins->ctx->state->in_subroutine = TRUE;
5231 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5233 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5236 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5238 struct glsl_src_param src1_param;
5240 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5241 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5242 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5245 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5247 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5249 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5251 shader_glsl_generate_shader_epilogue(ins->ctx);
5252 shader_addline(ins->ctx->buffer, "return;\n");
5256 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5258 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5259 ins->ctx->reg_maps->shader_version.minor);
5260 struct glsl_sample_function sample_function;
5261 DWORD sample_flags = 0;
5262 DWORD resource_idx;
5263 DWORD mask = 0, swizzle;
5264 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5266 /* 1.0-1.4: Use destination register as sampler source.
5267 * 2.0+: Use provided sampler source. */
5268 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5269 resource_idx = ins->dst[0].reg.idx[0].offset;
5270 else
5271 resource_idx = ins->src[1].reg.idx[0].offset;
5273 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5275 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5276 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5277 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5279 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5280 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5282 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5283 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5285 case WINED3D_TTFF_COUNT1:
5286 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5287 break;
5288 case WINED3D_TTFF_COUNT2:
5289 mask = WINED3DSP_WRITEMASK_1;
5290 break;
5291 case WINED3D_TTFF_COUNT3:
5292 mask = WINED3DSP_WRITEMASK_2;
5293 break;
5294 case WINED3D_TTFF_COUNT4:
5295 case WINED3D_TTFF_DISABLE:
5296 mask = WINED3DSP_WRITEMASK_3;
5297 break;
5301 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5303 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5305 if (src_mod == WINED3DSPSM_DZ) {
5306 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5307 mask = WINED3DSP_WRITEMASK_2;
5308 } else if (src_mod == WINED3DSPSM_DW) {
5309 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5310 mask = WINED3DSP_WRITEMASK_3;
5313 else
5315 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5316 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5318 /* ps 2.0 texldp instruction always divides by the fourth component. */
5319 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5320 mask = WINED3DSP_WRITEMASK_3;
5324 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5325 mask |= sample_function.coord_mask;
5326 sample_function.coord_mask = mask;
5328 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5329 else swizzle = ins->src[1].swizzle;
5331 /* 1.0-1.3: Use destination register as coordinate source.
5332 1.4+: Use provided coordinate source register. */
5333 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5335 char coord_mask[6];
5336 shader_glsl_write_mask_to_str(mask, coord_mask);
5337 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5338 "T%u%s", resource_idx, coord_mask);
5340 else
5342 struct glsl_src_param coord_param;
5343 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5344 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5346 struct glsl_src_param bias;
5347 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5348 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5349 NULL, "%s", coord_param.param_str);
5350 } else {
5351 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5352 "%s", coord_param.param_str);
5355 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5358 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5360 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5361 struct glsl_src_param coord_param, dx_param, dy_param;
5362 struct glsl_sample_function sample_function;
5363 DWORD sampler_idx;
5364 DWORD swizzle = ins->src[1].swizzle;
5366 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5368 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5369 shader_glsl_tex(ins);
5370 return;
5373 sampler_idx = ins->src[1].reg.idx[0].offset;
5375 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5376 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5377 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5378 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5380 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5381 NULL, NULL, "%s", coord_param.param_str);
5382 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5385 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5387 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5388 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5389 struct glsl_src_param coord_param, lod_param;
5390 struct glsl_sample_function sample_function;
5391 DWORD swizzle = ins->src[1].swizzle;
5392 DWORD sampler_idx;
5394 sampler_idx = ins->src[1].reg.idx[0].offset;
5396 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5397 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5399 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5401 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5402 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5404 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5405 * However, the NVIDIA drivers allow them in fragment shaders as well,
5406 * even without the appropriate extension. */
5407 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5409 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5410 "%s", coord_param.param_str);
5411 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5414 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5415 unsigned int resource_idx, unsigned int sampler_idx)
5417 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5418 unsigned int i;
5420 for (i = 0; i < sampler_map->count; ++i)
5422 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5423 return entries[i].bind_idx;
5426 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5428 return ~0u;
5431 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5433 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5434 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5435 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5436 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5437 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5438 struct glsl_src_param structure_idx, offset, data, data2;
5439 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5440 enum wined3d_shader_resource_type resource_type;
5441 struct wined3d_string_buffer *address;
5442 enum wined3d_data_type data_type;
5443 unsigned int resource_idx, stride;
5444 const char *op, *resource;
5445 DWORD coord_mask;
5446 BOOL is_tgsm;
5448 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5449 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5450 if (is_tgsm)
5452 if (resource_idx >= reg_maps->tgsm_count)
5454 ERR("Invalid TGSM index %u.\n", resource_idx);
5455 return;
5457 resource = "g";
5458 data_type = WINED3D_DATA_UINT;
5459 coord_mask = 1;
5460 stride = reg_maps->tgsm[resource_idx].stride;
5462 else
5464 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5466 ERR("Invalid UAV index %u.\n", resource_idx);
5467 return;
5469 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5470 if (resource_type >= ARRAY_SIZE(resource_type_info))
5472 ERR("Unexpected resource type %#x.\n", resource_type);
5473 return;
5475 resource = "image";
5476 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5477 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5478 stride = reg_maps->uav_resource_info[resource_idx].stride;
5481 switch (ins->handler_idx)
5483 case WINED3DSIH_ATOMIC_AND:
5484 case WINED3DSIH_IMM_ATOMIC_AND:
5485 if (is_tgsm)
5486 op = "atomicAnd";
5487 else
5488 op = "imageAtomicAnd";
5489 break;
5490 case WINED3DSIH_ATOMIC_CMP_STORE:
5491 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5492 if (is_tgsm)
5493 op = "atomicCompSwap";
5494 else
5495 op = "imageAtomicCompSwap";
5496 break;
5497 case WINED3DSIH_ATOMIC_IADD:
5498 case WINED3DSIH_IMM_ATOMIC_IADD:
5499 if (is_tgsm)
5500 op = "atomicAdd";
5501 else
5502 op = "imageAtomicAdd";
5503 break;
5504 case WINED3DSIH_ATOMIC_IMAX:
5505 case WINED3DSIH_IMM_ATOMIC_IMAX:
5506 if (is_tgsm)
5507 op = "atomicMax";
5508 else
5509 op = "imageAtomicMax";
5510 if (data_type != WINED3D_DATA_INT)
5512 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5513 return;
5515 break;
5516 case WINED3DSIH_ATOMIC_IMIN:
5517 case WINED3DSIH_IMM_ATOMIC_IMIN:
5518 if (is_tgsm)
5519 op = "atomicMin";
5520 else
5521 op = "imageAtomicMin";
5522 if (data_type != WINED3D_DATA_INT)
5524 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5525 return;
5527 break;
5528 case WINED3DSIH_ATOMIC_OR:
5529 case WINED3DSIH_IMM_ATOMIC_OR:
5530 if (is_tgsm)
5531 op = "atomicOr";
5532 else
5533 op = "imageAtomicOr";
5534 break;
5535 case WINED3DSIH_ATOMIC_UMAX:
5536 case WINED3DSIH_IMM_ATOMIC_UMAX:
5537 if (is_tgsm)
5538 op = "atomicMax";
5539 else
5540 op = "imageAtomicMax";
5541 if (data_type != WINED3D_DATA_UINT)
5543 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5544 return;
5546 break;
5547 case WINED3DSIH_ATOMIC_UMIN:
5548 case WINED3DSIH_IMM_ATOMIC_UMIN:
5549 if (is_tgsm)
5550 op = "atomicMin";
5551 else
5552 op = "imageAtomicMin";
5553 if (data_type != WINED3D_DATA_UINT)
5555 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5556 return;
5558 break;
5559 case WINED3DSIH_ATOMIC_XOR:
5560 case WINED3DSIH_IMM_ATOMIC_XOR:
5561 if (is_tgsm)
5562 op = "atomicXor";
5563 else
5564 op = "imageAtomicXor";
5565 break;
5566 case WINED3DSIH_IMM_ATOMIC_EXCH:
5567 if (is_tgsm)
5568 op = "atomicExchange";
5569 else
5570 op = "imageAtomicExchange";
5571 break;
5572 default:
5573 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5574 return;
5577 address = string_buffer_get(priv->string_buffers);
5578 if (stride)
5580 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5581 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5582 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5584 else
5586 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5587 string_buffer_sprintf(address, "%s", offset.param_str);
5588 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5589 shader_addline(address, "/ 4");
5592 if (is_imm_instruction)
5593 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5595 if (is_tgsm)
5596 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5597 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5598 else
5599 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5600 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5602 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5603 shader_addline(buffer, "%s", data.param_str);
5604 if (ins->src_count >= 3)
5606 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5607 shader_addline(buffer, ", %s", data2.param_str);
5610 if (is_imm_instruction)
5611 shader_addline(buffer, ")");
5612 shader_addline(buffer, ");\n");
5614 string_buffer_release(priv->string_buffers, address);
5617 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5619 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5620 const char *op;
5622 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5623 op = "atomicCounterIncrement";
5624 else
5625 op = "atomicCounterDecrement";
5627 shader_glsl_append_dst(ins->ctx->buffer, ins);
5628 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5631 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5633 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5634 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5635 enum wined3d_shader_resource_type resource_type;
5636 struct glsl_src_param image_coord_param;
5637 enum wined3d_data_type data_type;
5638 DWORD coord_mask, write_mask;
5639 unsigned int uav_idx;
5640 char dst_swizzle[6];
5642 uav_idx = ins->src[1].reg.idx[0].offset;
5643 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5645 ERR("Invalid UAV index %u.\n", uav_idx);
5646 return;
5648 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5649 if (resource_type >= ARRAY_SIZE(resource_type_info))
5651 ERR("Unexpected resource type %#x.\n", resource_type);
5652 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5654 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5655 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5657 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5658 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5660 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5661 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5662 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5665 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5667 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5668 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5669 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5670 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5671 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5672 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5673 struct glsl_src_param structure_idx, offset;
5674 struct wined3d_string_buffer *address;
5675 struct wined3d_shader_dst_param dst;
5676 const char *function, *resource;
5678 resource_idx = src->reg.idx[0].offset;
5679 if (src->reg.type == WINED3DSPR_RESOURCE)
5681 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5683 ERR("Invalid resource index %u.\n", resource_idx);
5684 return;
5686 stride = reg_maps->resource_info[resource_idx].stride;
5687 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5688 function = "texelFetch";
5689 resource = "sampler";
5691 else if (src->reg.type == WINED3DSPR_UAV)
5693 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5695 ERR("Invalid UAV index %u.\n", resource_idx);
5696 return;
5698 stride = reg_maps->uav_resource_info[resource_idx].stride;
5699 bind_idx = resource_idx;
5700 function = "imageLoad";
5701 resource = "image";
5703 else
5705 if (resource_idx >= reg_maps->tgsm_count)
5707 ERR("Invalid TGSM index %u.\n", resource_idx);
5708 return;
5710 stride = reg_maps->tgsm[resource_idx].stride;
5711 bind_idx = resource_idx;
5712 function = NULL;
5713 resource = "g";
5716 address = string_buffer_get(priv->string_buffers);
5717 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5719 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5720 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5722 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5723 shader_addline(address, "%s / 4", offset.param_str);
5725 dst = ins->dst[0];
5726 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5728 /* The instruction is split into multiple lines. The first lines may
5729 * overwrite source parameters of the following lines. */
5730 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5731 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5734 for (i = 0; i < 4; ++i)
5736 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5737 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5738 continue;
5740 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5741 if (function)
5742 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5743 function, prefix, resource, bind_idx, address->buffer, swizzle);
5744 else
5745 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5746 prefix, resource, bind_idx, address->buffer, swizzle);
5749 string_buffer_release(priv->string_buffers, address);
5752 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5754 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5755 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5756 struct glsl_src_param image_coord_param, image_data_param;
5757 enum wined3d_shader_resource_type resource_type;
5758 enum wined3d_data_type data_type;
5759 unsigned int uav_idx;
5760 DWORD coord_mask;
5762 uav_idx = ins->dst[0].reg.idx[0].offset;
5763 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5765 ERR("Invalid UAV index %u.\n", uav_idx);
5766 return;
5768 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5769 if (resource_type >= ARRAY_SIZE(resource_type_info))
5771 ERR("Unexpected resource type %#x.\n", resource_type);
5772 return;
5774 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5775 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5777 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5778 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5779 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5780 shader_glsl_get_prefix(version->type), uav_idx,
5781 image_coord_param.param_str, image_data_param.param_str);
5784 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5786 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5787 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5788 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5789 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5790 struct glsl_src_param structure_idx, offset, data;
5791 unsigned int i, resource_idx, stride, src_idx = 0;
5792 struct wined3d_string_buffer *address;
5793 DWORD write_mask;
5794 BOOL is_tgsm;
5796 resource_idx = ins->dst[0].reg.idx[0].offset;
5797 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5798 if (is_tgsm)
5800 if (resource_idx >= reg_maps->tgsm_count)
5802 ERR("Invalid TGSM index %u.\n", resource_idx);
5803 return;
5805 stride = reg_maps->tgsm[resource_idx].stride;
5807 else
5809 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5811 ERR("Invalid UAV index %u.\n", resource_idx);
5812 return;
5814 stride = reg_maps->uav_resource_info[resource_idx].stride;
5817 address = string_buffer_get(priv->string_buffers);
5818 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5820 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5821 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5823 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5824 shader_addline(address, "%s / 4", offset.param_str);
5826 for (i = 0; i < 4; ++i)
5828 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5829 continue;
5831 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5833 if (is_tgsm)
5834 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5835 prefix, resource_idx, address->buffer, i, data.param_str);
5836 else
5837 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5838 prefix, resource_idx, address->buffer, i, data.param_str);
5841 string_buffer_release(priv->string_buffers, address);
5844 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5846 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5847 unsigned int sync_flags = ins->flags;
5849 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5851 shader_addline(buffer, "barrier();\n");
5852 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5855 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5857 shader_addline(buffer, "memoryBarrierShared();\n");
5858 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5861 if (sync_flags)
5862 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5865 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5866 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5868 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5869 unsigned int idx = reg->idx[0].offset;
5871 if (reg->type == WINED3DSPR_RESOURCE)
5873 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5875 ERR("Invalid resource index %u.\n", idx);
5876 return NULL;
5878 return &reg_maps->resource_info[idx];
5881 if (reg->type == WINED3DSPR_UAV)
5883 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5885 ERR("Invalid UAV index %u.\n", idx);
5886 return NULL;
5888 return &reg_maps->uav_resource_info[idx];
5891 FIXME("Unhandled register type %#x.\n", reg->type);
5892 return NULL;
5895 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5897 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5898 const struct wined3d_shader_resource_info *resource_info;
5899 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5900 unsigned int resource_idx;
5901 char dst_swizzle[6];
5902 DWORD write_mask;
5904 write_mask = shader_glsl_append_dst(buffer, ins);
5905 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5907 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5908 return;
5909 resource_idx = ins->src[0].reg.idx[0].offset;
5911 shader_addline(buffer, "ivec2(");
5912 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5914 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5915 resource_idx, WINED3D_SAMPLER_DEFAULT);
5916 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5918 else
5920 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5922 if (resource_info->stride)
5923 shader_addline(buffer, " / %u", resource_info->stride);
5924 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5925 shader_addline(buffer, " * 4");
5926 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5929 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5931 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5932 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5935 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5937 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5940 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5942 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5943 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5944 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5945 enum wined3d_shader_resource_type resource_type;
5946 enum wined3d_shader_register_type reg_type;
5947 unsigned int resource_idx, bind_idx, i;
5948 enum wined3d_data_type dst_data_type;
5949 struct glsl_src_param lod_param;
5950 BOOL supports_mipmaps;
5951 char dst_swizzle[6];
5952 DWORD write_mask;
5954 dst_data_type = ins->dst[0].reg.data_type;
5955 if (ins->flags == WINED3DSI_RESINFO_UINT)
5956 dst_data_type = WINED3D_DATA_UINT;
5957 else if (ins->flags)
5958 FIXME("Unhandled flags %#x.\n", ins->flags);
5960 reg_type = ins->src[1].reg.type;
5961 resource_idx = ins->src[1].reg.idx[0].offset;
5962 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5963 if (reg_type == WINED3DSPR_RESOURCE)
5965 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5966 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5967 resource_idx, WINED3D_SAMPLER_DEFAULT);
5969 else
5971 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5972 bind_idx = resource_idx;
5975 if (resource_type >= ARRAY_SIZE(resource_type_info))
5977 ERR("Unexpected resource type %#x.\n", resource_type);
5978 return;
5981 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], dst_data_type);
5982 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5984 if (dst_data_type == WINED3D_DATA_UINT)
5985 shader_addline(buffer, "uvec4(");
5986 else
5987 shader_addline(buffer, "vec4(");
5989 if (reg_type == WINED3DSPR_RESOURCE)
5991 shader_addline(buffer, "textureSize(%s_sampler%u",
5992 shader_glsl_get_prefix(version->type), bind_idx);
5994 else
5996 shader_addline(buffer, "imageSize(%s_image%u",
5997 shader_glsl_get_prefix(version->type), bind_idx);
6000 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
6001 if (supports_mipmaps)
6002 shader_addline(buffer, ", %s", lod_param.param_str);
6003 shader_addline(buffer, "), ");
6005 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
6006 shader_addline(buffer, "0, ");
6008 if (supports_mipmaps)
6010 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6012 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
6013 shader_glsl_get_prefix(version->type), bind_idx);
6015 else
6017 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
6018 shader_addline(buffer, "1");
6021 else
6023 shader_addline(buffer, "1");
6026 shader_addline(buffer, ")%s);\n", dst_swizzle);
6029 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
6031 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6032 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6033 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6034 const struct wined3d_shader_dst_param *dst = ins->dst;
6035 const struct wined3d_shader_src_param *src = ins->src;
6036 enum wined3d_shader_resource_type resource_type;
6037 enum wined3d_data_type dst_data_type;
6038 unsigned int resource_idx, bind_idx;
6039 char dst_swizzle[6];
6040 DWORD write_mask;
6042 dst_data_type = dst->reg.data_type;
6043 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
6044 dst_data_type = WINED3D_DATA_UINT;
6045 else if (ins->flags)
6046 FIXME("Unhandled flags %#x.\n", ins->flags);
6048 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, dst_data_type);
6049 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
6051 if (dst_data_type == WINED3D_DATA_UINT)
6052 shader_addline(buffer, "uvec4(");
6053 else
6054 shader_addline(buffer, "vec4(");
6056 if (src->reg.type == WINED3DSPR_RASTERIZER)
6058 if (gl_info->supported[ARB_SAMPLE_SHADING])
6060 shader_addline(buffer, "gl_NumSamples");
6062 else
6064 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
6065 shader_addline(buffer, "1");
6068 else
6070 resource_idx = src->reg.idx[0].offset;
6071 resource_type = reg_maps->resource_info[resource_idx].type;
6072 if (resource_type >= ARRAY_SIZE(resource_type_info))
6074 ERR("Unexpected resource type %#x.\n", resource_type);
6075 return;
6077 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
6079 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
6081 shader_addline(buffer, "textureSamples(%s_sampler%u)",
6082 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
6084 else
6086 FIXME("textureSamples() is not supported.\n");
6087 shader_addline(buffer, "1");
6091 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
6094 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6096 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6097 struct glsl_src_param coord_param, lod_param, sample_param;
6098 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6099 struct glsl_sample_function sample_function;
6100 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6101 BOOL has_lod_param;
6103 if (wined3d_shader_instruction_has_texel_offset(ins))
6104 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6106 resource_idx = ins->src[1].reg.idx[0].offset;
6107 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6109 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6111 ERR("Invalid resource index %u.\n", resource_idx);
6112 return;
6114 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6116 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6117 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6118 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6119 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6120 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6122 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6123 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6124 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6126 else
6128 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6129 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6130 "%s", coord_param.param_str);
6132 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6135 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6137 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6138 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6139 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6140 struct glsl_sample_function sample_function;
6141 DWORD flags = 0;
6143 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6144 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6145 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6146 flags |= WINED3D_GLSL_SAMPLE_LOD;
6147 if (wined3d_shader_instruction_has_texel_offset(ins))
6148 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6150 resource_idx = ins->src[1].reg.idx[0].offset;
6151 sampler_idx = ins->src[2].reg.idx[0].offset;
6153 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6154 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6156 switch (ins->handler_idx)
6158 case WINED3DSIH_SAMPLE:
6159 break;
6160 case WINED3DSIH_SAMPLE_B:
6161 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6162 lod_param_str = lod_param.param_str;
6163 break;
6164 case WINED3DSIH_SAMPLE_GRAD:
6165 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6166 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6167 dx_param_str = dx_param.param_str;
6168 dy_param_str = dy_param.param_str;
6169 break;
6170 case WINED3DSIH_SAMPLE_LOD:
6171 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6172 lod_param_str = lod_param.param_str;
6173 break;
6174 default:
6175 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6176 break;
6179 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6180 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6181 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6182 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6185 /* GLSL doesn't provide a function to sample from level zero with depth
6186 * comparison for array textures and cube textures. We use textureGrad*()
6187 * to implement sample_c_lz.
6189 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6190 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6191 unsigned int coord_size, const char *coord_param, const char *ref_param)
6193 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6194 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6195 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6196 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6197 char dst_swizzle[6];
6199 WARN("Emitting textureGrad() for sample_c_lz.\n");
6201 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6202 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6203 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6204 sample_function->offset_size ? "Offset" : "",
6205 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6206 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6207 if (sample_function->offset_size)
6209 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6210 shader_addline(buffer, ", ");
6211 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6213 shader_addline(buffer, "))%s);\n", dst_swizzle);
6216 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6218 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6219 const struct wined3d_shader_resource_info *resource_info;
6220 struct glsl_src_param coord_param, compare_param;
6221 struct glsl_sample_function sample_function;
6222 const char *lod_param = NULL;
6223 unsigned int coord_size;
6224 DWORD flags = 0;
6226 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6228 lod_param = "0";
6229 flags |= WINED3D_GLSL_SAMPLE_LOD;
6232 if (wined3d_shader_instruction_has_texel_offset(ins))
6233 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6235 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6236 return;
6237 resource_idx = ins->src[1].reg.idx[0].offset;
6238 sampler_idx = ins->src[2].reg.idx[0].offset;
6240 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6241 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6242 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6243 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6244 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6245 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6246 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6247 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6249 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6250 coord_size, coord_param.param_str, compare_param.param_str);
6252 else
6254 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6255 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6256 coord_size, coord_param.param_str, compare_param.param_str);
6258 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6261 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6263 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6264 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6265 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6266 struct glsl_src_param coord_param, compare_param, offset_param;
6267 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6268 const struct wined3d_shader_resource_info *resource_info;
6269 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6270 unsigned int coord_size, offset_size;
6271 char dst_swizzle[6];
6272 BOOL has_offset;
6274 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6276 FIXME("OpenGL implementation does not support textureGather.\n");
6277 return;
6280 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6281 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6282 || wined3d_shader_instruction_has_texel_offset(ins);
6284 resource_param_idx =
6285 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6286 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6287 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6288 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6289 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6291 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6292 return;
6294 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6296 ERR("Unexpected resource type %#x.\n", resource_info->type);
6297 return;
6299 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6301 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6302 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6304 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6306 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6307 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6308 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6310 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6311 shader_addline(buffer, ", %s", compare_param.param_str);
6313 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6315 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6316 shader_addline(buffer, ", %s", offset_param.param_str);
6318 else if (has_offset)
6320 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6321 shader_addline(buffer, ", ");
6322 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6324 if (component_idx)
6325 shader_addline(buffer, ", %u", component_idx);
6327 shader_addline(buffer, ")%s);\n", dst_swizzle);
6330 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6332 /* FIXME: Make this work for more than just 2D textures */
6333 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6334 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6336 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6338 char dst_mask[6];
6340 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6341 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6342 ins->dst[0].reg.idx[0].offset, dst_mask);
6344 else
6346 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6347 DWORD reg = ins->src[0].reg.idx[0].offset;
6348 char dst_swizzle[6];
6350 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6352 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6354 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6355 struct glsl_src_param div_param;
6356 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6358 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6360 if (mask_size > 1)
6361 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6362 else
6363 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6365 else
6367 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6372 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6373 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6374 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6375 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6377 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6378 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6379 struct glsl_sample_function sample_function;
6380 struct glsl_src_param src0_param;
6381 UINT mask_size;
6383 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6385 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6386 * scalar, and projected sampling would require 4.
6388 * It is a dependent read - not valid with conditional NP2 textures
6390 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6391 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6393 switch(mask_size)
6395 case 1:
6396 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6397 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6398 break;
6400 case 2:
6401 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6402 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6403 break;
6405 case 3:
6406 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6407 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6408 break;
6410 default:
6411 FIXME("Unexpected mask size %u\n", mask_size);
6412 break;
6414 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6417 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6418 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6419 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6421 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6422 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6423 struct glsl_src_param src0_param;
6424 DWORD dst_mask;
6425 unsigned int mask_size;
6427 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6428 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6429 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6431 if (mask_size > 1) {
6432 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6433 } else {
6434 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6438 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6439 * Calculate the depth as dst.x / dst.y */
6440 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6442 struct glsl_dst_param dst_param;
6444 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6446 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6447 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6448 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6449 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6450 * >= 1.0 or < 0.0
6452 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6453 dst_param.reg_name, dst_param.reg_name);
6456 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6457 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6458 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6459 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6461 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6463 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6464 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6465 struct glsl_src_param src0_param;
6467 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6469 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6470 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6473 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6474 * Calculate the 1st of a 2-row matrix multiplication. */
6475 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6477 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6478 DWORD reg = ins->dst[0].reg.idx[0].offset;
6479 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6480 struct glsl_src_param src0_param;
6482 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6483 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6486 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6487 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6488 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6490 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6491 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6492 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6493 DWORD reg = ins->dst[0].reg.idx[0].offset;
6494 struct glsl_src_param src0_param;
6496 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6497 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6498 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6501 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6503 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6504 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6505 struct glsl_sample_function sample_function;
6506 DWORD reg = ins->dst[0].reg.idx[0].offset;
6507 struct glsl_src_param src0_param;
6509 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6510 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6512 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6514 /* Sample the texture using the calculated coordinates */
6515 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6516 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6519 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6520 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6521 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6523 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6524 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6525 struct glsl_sample_function sample_function;
6526 DWORD reg = ins->dst[0].reg.idx[0].offset;
6527 struct glsl_src_param src0_param;
6529 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6530 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6532 /* Dependent read, not valid with conditional NP2 */
6533 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6535 /* Sample the texture using the calculated coordinates */
6536 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6537 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6539 tex_mx->current_row = 0;
6542 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6543 * Perform the 3rd row of a 3x3 matrix multiply */
6544 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6546 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6547 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6548 DWORD reg = ins->dst[0].reg.idx[0].offset;
6549 struct glsl_src_param src0_param;
6550 char dst_mask[6];
6552 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6554 shader_glsl_append_dst(ins->ctx->buffer, ins);
6555 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6556 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6558 tex_mx->current_row = 0;
6561 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6562 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6563 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6565 struct glsl_src_param src0_param;
6566 struct glsl_src_param src1_param;
6567 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6568 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6569 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6570 struct glsl_sample_function sample_function;
6571 DWORD reg = ins->dst[0].reg.idx[0].offset;
6572 char coord_mask[6];
6574 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6575 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6577 /* Perform the last matrix multiply operation */
6578 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6579 /* Reflection calculation */
6580 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6582 /* Dependent read, not valid with conditional NP2 */
6583 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6584 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6586 /* Sample the texture */
6587 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6588 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6589 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6591 tex_mx->current_row = 0;
6594 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6595 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6596 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6598 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6599 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6600 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6601 struct glsl_sample_function sample_function;
6602 DWORD reg = ins->dst[0].reg.idx[0].offset;
6603 struct glsl_src_param src0_param;
6604 char coord_mask[6];
6606 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6608 /* Perform the last matrix multiply operation */
6609 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6611 /* Construct the eye-ray vector from w coordinates */
6612 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6613 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6614 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6616 /* Dependent read, not valid with conditional NP2 */
6617 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6618 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6620 /* Sample the texture using the calculated coordinates */
6621 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6622 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6623 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6625 tex_mx->current_row = 0;
6628 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6629 * Apply a fake bump map transform.
6630 * texbem is pshader <= 1.3 only, this saves a few version checks
6632 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6634 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6635 struct glsl_sample_function sample_function;
6636 struct glsl_src_param coord_param;
6637 DWORD sampler_idx;
6638 DWORD mask;
6639 DWORD flags;
6640 char coord_mask[6];
6642 sampler_idx = ins->dst[0].reg.idx[0].offset;
6643 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6644 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6646 /* Dependent read, not valid with conditional NP2 */
6647 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6648 mask = sample_function.coord_mask;
6650 shader_glsl_write_mask_to_str(mask, coord_mask);
6652 /* With projected textures, texbem only divides the static texture coord,
6653 * not the displacement, so we can't let GL handle this. */
6654 if (flags & WINED3D_PSARGS_PROJECTED)
6656 DWORD div_mask=0;
6657 char coord_div_mask[3];
6658 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6660 case WINED3D_TTFF_COUNT1:
6661 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6662 break;
6663 case WINED3D_TTFF_COUNT2:
6664 div_mask = WINED3DSP_WRITEMASK_1;
6665 break;
6666 case WINED3D_TTFF_COUNT3:
6667 div_mask = WINED3DSP_WRITEMASK_2;
6668 break;
6669 case WINED3D_TTFF_COUNT4:
6670 case WINED3D_TTFF_DISABLE:
6671 div_mask = WINED3DSP_WRITEMASK_3;
6672 break;
6674 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6675 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6678 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6680 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6681 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6682 coord_param.param_str, coord_mask);
6684 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6686 struct glsl_src_param luminance_param;
6687 struct glsl_dst_param dst_param;
6689 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6690 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6692 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6693 dst_param.reg_name, dst_param.mask_str,
6694 luminance_param.param_str, sampler_idx, sampler_idx);
6696 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6699 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6701 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6702 struct glsl_src_param src0_param, src1_param;
6704 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6705 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6707 shader_glsl_append_dst(ins->ctx->buffer, ins);
6708 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6709 src0_param.param_str, sampler_idx, src1_param.param_str);
6712 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6713 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6714 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6716 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6717 struct glsl_sample_function sample_function;
6718 struct glsl_src_param src0_param;
6720 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6722 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6723 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6724 "%s.wx", src0_param.reg_name);
6725 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6728 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6729 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6730 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6732 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6733 struct glsl_sample_function sample_function;
6734 struct glsl_src_param src0_param;
6736 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6738 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6739 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6740 "%s.yz", src0_param.reg_name);
6741 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6744 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6745 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6746 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6748 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6749 struct glsl_sample_function sample_function;
6750 struct glsl_src_param src0_param;
6752 /* Dependent read, not valid with conditional NP2 */
6753 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6754 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6756 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6757 "%s", src0_param.param_str);
6758 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6761 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6762 * If any of the first 3 components are < 0, discard this pixel */
6763 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6765 if (ins->ctx->reg_maps->shader_version.major >= 4)
6767 shader_glsl_generate_conditional_op(ins, "discard;");
6769 else
6771 struct glsl_dst_param dst_param;
6773 /* The argument is a destination parameter, and no writemasks are allowed */
6774 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6776 /* 2.0 shaders compare all 4 components in texkill. */
6777 if (ins->ctx->reg_maps->shader_version.major >= 2)
6778 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6779 /* 1.x shaders only compare the first 3 components, probably due to
6780 * the nature of the texkill instruction as a tex* instruction, and
6781 * phase, which kills all .w components. Even if all 4 components are
6782 * defined, only the first 3 are used. */
6783 else
6784 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6788 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6789 * dst = dot2(src0, src1) + src2 */
6790 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6792 struct glsl_src_param src0_param;
6793 struct glsl_src_param src1_param;
6794 struct glsl_src_param src2_param;
6795 DWORD write_mask;
6796 unsigned int mask_size;
6798 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6799 mask_size = shader_glsl_get_write_mask_size(write_mask);
6801 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6802 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6803 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6805 if (mask_size > 1) {
6806 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6807 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6808 } else {
6809 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6810 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6814 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6815 const struct wined3d_shader_signature *input_signature,
6816 const struct wined3d_shader_reg_maps *reg_maps,
6817 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6819 unsigned int i;
6821 for (i = 0; i < input_signature->element_count; ++i)
6823 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6824 const char *semantic_name;
6825 UINT semantic_idx;
6826 char reg_mask[6];
6828 /* Unused */
6829 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6830 continue;
6832 semantic_name = input->semantic_name;
6833 semantic_idx = input->semantic_idx;
6834 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6836 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6838 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6840 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6841 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6843 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6845 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6847 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6849 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6850 input->register_idx, reg_mask);
6852 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6854 if (gl_info->supported[ARB_SAMPLE_SHADING])
6855 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6856 input->register_idx, reg_mask);
6857 else
6858 FIXME("ARB_sample_shading is not supported.\n");
6860 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6862 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6863 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6864 input->register_idx, reg_mask);
6865 else
6866 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6868 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6870 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6871 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6872 input->register_idx, reg_mask);
6873 else
6874 FIXME("ARB_viewport_array is not supported.\n");
6876 else
6878 if (input->sysval_semantic)
6879 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6880 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6881 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6882 shader_glsl_shader_input_name(gl_info),
6883 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6886 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6888 if (args->pointsprite)
6889 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6890 shader->u.ps.input_reg_map[input->register_idx]);
6891 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6892 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6893 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6894 needs_legacy_glsl_syntax(gl_info)
6895 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6896 else
6897 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6898 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6900 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6902 if (!semantic_idx)
6903 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6904 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6905 else if (semantic_idx == 1)
6906 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6907 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6908 else
6909 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6910 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6912 else
6914 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6915 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6920 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6922 struct glsl_program_key key;
6924 key.vs_id = entry->vs.id;
6925 key.hs_id = entry->hs.id;
6926 key.ds_id = entry->ds.id;
6927 key.gs_id = entry->gs.id;
6928 key.ps_id = entry->ps.id;
6929 key.cs_id = entry->cs.id;
6931 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6933 ERR("Failed to insert program entry.\n");
6937 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6938 const struct glsl_program_key *key)
6940 struct wine_rb_entry *entry;
6942 entry = wine_rb_get(&priv->program_lookup, key);
6943 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6946 /* Context activation is done by the caller. */
6947 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6948 struct glsl_shader_prog_link *entry)
6950 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6952 GL_EXTCALL(glDeleteProgram(entry->id));
6953 if (entry->vs.id)
6954 list_remove(&entry->vs.shader_entry);
6955 if (entry->hs.id)
6956 list_remove(&entry->hs.shader_entry);
6957 if (entry->ds.id)
6958 list_remove(&entry->ds.shader_entry);
6959 if (entry->gs.id)
6960 list_remove(&entry->gs.shader_entry);
6961 if (entry->ps.id)
6962 list_remove(&entry->ps.shader_entry);
6963 if (entry->cs.id)
6964 list_remove(&entry->cs.shader_entry);
6965 heap_free(entry);
6968 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6969 const struct wined3d_gl_info *gl_info, const DWORD *map,
6970 const struct wined3d_shader_signature *input_signature,
6971 const struct wined3d_shader_reg_maps *reg_maps_in,
6972 const struct wined3d_shader_signature *output_signature,
6973 const struct wined3d_shader_reg_maps *reg_maps_out)
6975 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6976 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6977 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6978 unsigned int in_count = vec4_varyings(3, gl_info);
6979 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6980 DWORD in_idx, *set = NULL;
6981 unsigned int i, j;
6982 char reg_mask[6];
6984 set = heap_calloc(max_varyings, sizeof(*set));
6986 for (i = 0; i < input_signature->element_count; ++i)
6988 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6990 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6991 continue;
6993 in_idx = map[input->register_idx];
6994 /* Declared, but not read register */
6995 if (in_idx == ~0u)
6996 continue;
6997 if (in_idx >= max_varyings)
6999 FIXME("More input varyings declared than supported, expect issues.\n");
7000 continue;
7003 if (in_idx == in_count)
7004 string_buffer_sprintf(destination, "gl_FrontColor");
7005 else if (in_idx == in_count + 1)
7006 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7007 else
7008 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
7010 if (!set[in_idx])
7011 set[in_idx] = ~0u;
7013 for (j = 0; j < output_signature->element_count; ++j)
7015 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
7016 DWORD mask;
7018 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
7019 || input->semantic_idx != output->semantic_idx
7020 || strcmp(input->semantic_name, output->semantic_name)
7021 || !(mask = input->mask & output->mask))
7022 continue;
7024 if (set[in_idx] == ~0u)
7025 set[in_idx] = 0;
7026 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
7027 shader_glsl_write_mask_to_str(mask, reg_mask);
7029 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
7030 destination->buffer, reg_mask, output->register_idx, reg_mask);
7034 for (i = 0; i < max_varyings; ++i)
7036 unsigned int size;
7038 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
7039 continue;
7041 if (set[i] == ~0u)
7042 set[i] = 0;
7044 size = 0;
7045 if (!(set[i] & WINED3DSP_WRITEMASK_0))
7046 reg_mask[size++] = 'x';
7047 if (!(set[i] & WINED3DSP_WRITEMASK_1))
7048 reg_mask[size++] = 'y';
7049 if (!(set[i] & WINED3DSP_WRITEMASK_2))
7050 reg_mask[size++] = 'z';
7051 if (!(set[i] & WINED3DSP_WRITEMASK_3))
7052 reg_mask[size++] = 'w';
7053 reg_mask[size] = '\0';
7055 if (i == in_count)
7056 string_buffer_sprintf(destination, "gl_FrontColor");
7057 else if (i == in_count + 1)
7058 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7059 else
7060 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
7062 if (size == 1)
7063 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
7064 else
7065 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
7068 heap_free(set);
7069 string_buffer_release(&priv->string_buffers, destination);
7072 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
7073 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
7074 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
7075 BOOL rasterizer_setup)
7077 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7078 char reg_mask[6];
7079 unsigned int i;
7081 for (i = 0; i < output_signature->element_count; ++i)
7083 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7085 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7086 continue;
7088 if (output->stream_idx)
7089 continue;
7091 if (output->register_idx >= input_count)
7092 continue;
7094 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7096 shader_addline(buffer,
7097 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7098 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7102 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7103 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7105 unsigned int i, clip_or_cull_index;
7106 const char *name;
7107 char reg_mask[6];
7109 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7110 /* Assign consecutive indices starting from 0. */
7111 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7112 for (i = 0; i < 4; ++i)
7114 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7115 continue;
7117 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7118 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7119 name, clip_or_cull_index, element->register_idx, reg_mask);
7120 ++clip_or_cull_index;
7124 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7125 const struct wined3d_gl_info *gl_info, const DWORD *map,
7126 const struct wined3d_shader_signature *input_signature,
7127 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7128 const struct wined3d_shader_signature *output_signature,
7129 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7131 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7132 const char *semantic_name;
7133 unsigned int semantic_idx;
7134 char reg_mask[6];
7135 unsigned int i;
7137 /* First, sort out position and point size system values. */
7138 for (i = 0; i < output_signature->element_count; ++i)
7140 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7142 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7143 continue;
7145 if (output->stream_idx)
7146 continue;
7148 semantic_name = output->semantic_name;
7149 semantic_idx = output->semantic_idx;
7150 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7152 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7154 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7155 reg_mask, output->register_idx, reg_mask);
7157 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7159 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7160 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7162 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7164 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7165 output->register_idx, reg_mask);
7167 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7169 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7170 output->register_idx, reg_mask);
7172 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7174 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7176 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7178 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7180 else if (output->sysval_semantic)
7182 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7186 /* Then, setup the pixel shader input. */
7187 if (reg_maps_out->shader_version.major < 4)
7188 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7189 output_signature, reg_maps_out);
7190 else
7191 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7194 /* Context activation is done by the caller. */
7195 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7196 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7197 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7199 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7200 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7201 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7202 const char *semantic_name;
7203 UINT semantic_idx;
7204 char reg_mask[6];
7205 unsigned int i;
7206 GLuint ret;
7208 string_buffer_clear(buffer);
7210 shader_glsl_add_version_declaration(buffer, gl_info);
7212 if (per_vertex_point_size)
7214 shader_addline(buffer, "uniform struct\n{\n");
7215 shader_addline(buffer, " float size_min;\n");
7216 shader_addline(buffer, " float size_max;\n");
7217 shader_addline(buffer, "} ffp_point;\n");
7220 if (ps_major < 3)
7222 DWORD colors_written_mask[2] = {0};
7223 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7225 if (!legacy_syntax)
7227 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7228 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7229 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7230 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7233 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7235 for (i = 0; i < vs->output_signature.element_count; ++i)
7237 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7238 DWORD write_mask;
7240 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7241 continue;
7243 semantic_name = output->semantic_name;
7244 semantic_idx = output->semantic_idx;
7245 write_mask = output->mask;
7246 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7248 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7250 if (legacy_syntax)
7251 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7252 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7253 else
7254 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7255 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7257 colors_written_mask[semantic_idx] = write_mask;
7259 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7261 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7262 reg_mask, output->register_idx, reg_mask);
7264 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7266 if (semantic_idx < MAX_TEXTURES)
7268 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7269 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7270 semantic_idx, reg_mask, output->register_idx, reg_mask);
7271 texcoords_written_mask[semantic_idx] = write_mask;
7274 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7276 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7277 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7279 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7281 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7282 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7283 output->register_idx, reg_mask[1]);
7287 for (i = 0; i < 2; ++i)
7289 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7291 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7292 if (!i)
7293 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7294 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7295 reg_mask, reg_mask);
7296 else
7297 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7298 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7299 reg_mask, reg_mask);
7302 for (i = 0; i < MAX_TEXTURES; ++i)
7304 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7305 continue;
7307 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7309 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7310 && !texcoords_written_mask[i])
7311 continue;
7313 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7314 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7315 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7319 else
7321 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7323 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7324 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7325 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7326 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7329 shader_addline(buffer, "}\n");
7331 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7332 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7333 shader_glsl_compile(gl_info, ret, buffer->buffer);
7335 return ret;
7338 static void shader_glsl_generate_stream_output_setup(struct shader_glsl_priv *priv,
7339 const struct wined3d_shader *shader, const struct wined3d_stream_output_desc *so_desc)
7341 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7342 unsigned int i;
7344 shader_addline(buffer, "out shader_in_out\n{\n");
7345 for (i = 0; i < so_desc->element_count; ++i)
7347 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7349 if (e->stream_idx)
7351 FIXME("Unhandled stream %u.\n", e->stream_idx);
7352 continue;
7354 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7355 continue;
7357 if (e->component_idx || e->component_count != 4)
7359 if (e->component_count == 1)
7360 shader_addline(buffer, "float");
7361 else
7362 shader_addline(buffer, "vec%u", e->component_count);
7363 shader_addline(buffer, " reg%u_%u_%u;\n",
7364 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
7366 else
7368 shader_addline(buffer, "vec4 reg%u;\n", e->register_idx);
7371 shader_addline(buffer, "} shader_out;\n");
7373 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7374 shader->limits->packed_output);
7375 for (i = 0; i < so_desc->element_count; ++i)
7377 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7379 if (e->stream_idx)
7381 FIXME("Unhandled stream %u.\n", e->stream_idx);
7382 continue;
7384 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7385 continue;
7387 if (e->component_idx || e->component_count != 4)
7389 DWORD write_mask;
7390 char str_mask[6];
7392 write_mask = ((1u << e->component_count) - 1) << e->component_idx;
7393 shader_glsl_write_mask_to_str(write_mask, str_mask);
7394 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7395 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1,
7396 e->register_idx, str_mask);
7398 else
7400 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n",
7401 e->register_idx, e->register_idx);
7404 shader_addline(buffer, "}\n");
7407 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7408 const struct wined3d_shader *shader, unsigned int input_count,
7409 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7411 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7412 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7414 if (rasterizer_setup)
7415 input_count = min(vec4_varyings(4, gl_info), input_count);
7417 if (input_count)
7418 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7420 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7421 prefix, shader->limits->packed_output);
7423 if (rasterizer_setup)
7424 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7425 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7426 else
7427 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7428 &shader->reg_maps, "shader_out", rasterizer_setup);
7430 shader_addline(buffer, "}\n");
7433 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7434 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7435 const char *reg_mask)
7437 if (!constant->sysval_semantic)
7439 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7440 return;
7443 switch (constant->sysval_semantic)
7445 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7446 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7447 case WINED3D_SV_TESS_FACTOR_LINEDET:
7448 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7449 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7450 break;
7451 case WINED3D_SV_TESS_FACTOR_QUADINT:
7452 case WINED3D_SV_TESS_FACTOR_TRIINT:
7453 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7454 break;
7455 default:
7456 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7457 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7461 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7462 const struct wined3d_shader_signature *signature, BOOL input_setup)
7464 unsigned int i, register_count, user_constant_index, user_constant_count;
7466 register_count = user_constant_count = 0;
7467 for (i = 0; i < signature->element_count; ++i)
7469 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7470 register_count = max(constant->register_idx + 1, register_count);
7471 if (!constant->sysval_semantic)
7472 ++user_constant_count;
7475 if (user_constant_count)
7476 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7477 input_setup ? "in" : "out", user_constant_count);
7478 if (input_setup)
7479 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7481 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7482 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7484 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7485 char reg_mask[6];
7487 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7489 if (input_setup)
7490 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7491 else
7492 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7494 shader_addline(buffer, " = ");
7496 if (input_setup)
7497 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7498 else
7499 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7501 shader_addline(buffer, ";\n");
7503 shader_addline(buffer, "}\n");
7506 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7507 const struct wined3d_gl_info *gl_info)
7509 const char *output = get_fragment_output(gl_info);
7511 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7512 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7513 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7514 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7515 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7516 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7519 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7520 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7522 const char *output = get_fragment_output(gl_info);
7524 switch (mode)
7526 case WINED3D_FFP_PS_FOG_OFF:
7527 return;
7529 case WINED3D_FFP_PS_FOG_LINEAR:
7530 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7531 break;
7533 case WINED3D_FFP_PS_FOG_EXP:
7534 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7535 break;
7537 case WINED3D_FFP_PS_FOG_EXP2:
7538 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7539 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7540 break;
7542 default:
7543 ERR("Invalid fog mode %#x.\n", mode);
7544 return;
7547 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7548 output, output);
7551 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7552 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7554 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7555 * flipping all the operators here, just negate the comparison below. */
7556 static const char * const comparison_operator[] =
7558 "", /* WINED3D_CMP_NEVER */
7559 "<", /* WINED3D_CMP_LESS */
7560 "==", /* WINED3D_CMP_EQUAL */
7561 "<=", /* WINED3D_CMP_LESSEQUAL */
7562 ">", /* WINED3D_CMP_GREATER */
7563 "!=", /* WINED3D_CMP_NOTEQUAL */
7564 ">=", /* WINED3D_CMP_GREATEREQUAL */
7565 "" /* WINED3D_CMP_ALWAYS */
7568 if (alpha_func == WINED3D_CMP_ALWAYS)
7569 return;
7571 if (alpha_func != WINED3D_CMP_NEVER)
7572 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7573 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7574 shader_addline(buffer, " discard;\n");
7577 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7578 const struct wined3d_gl_info *gl_info)
7580 if (gl_info->supported[ARB_CULL_DISTANCE])
7581 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7582 if (gl_info->supported[ARB_GPU_SHADER5])
7583 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7584 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7585 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7586 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7587 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7588 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7589 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7590 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7591 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7592 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7593 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7594 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7595 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7596 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7597 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7598 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7599 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7600 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7601 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7602 if (gl_info->supported[ARB_TEXTURE_GATHER])
7603 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7604 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7605 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7606 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7607 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7608 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7609 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7610 if (gl_info->supported[EXT_GPU_SHADER4])
7611 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7612 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7613 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7616 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7617 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7618 const struct ps_compile_args *args)
7620 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7622 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7623 if (reg_maps->shader_version.major < 2)
7624 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7626 if (args->srgb_correction)
7627 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7629 /* SM < 3 does not replace the fog stage. */
7630 if (reg_maps->shader_version.major < 3)
7631 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7633 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7635 if (reg_maps->sample_mask)
7636 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7639 /* Context activation is done by the caller. */
7640 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7641 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7642 const struct wined3d_shader *shader,
7643 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7645 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7646 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7647 const char *prefix = shader_glsl_get_prefix(version->type);
7648 const struct wined3d_gl_info *gl_info = context->gl_info;
7649 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7650 unsigned int i, extra_constants_needed = 0;
7651 struct shader_glsl_ctx_priv priv_ctx;
7652 GLuint shader_id;
7653 DWORD map;
7655 memset(&priv_ctx, 0, sizeof(priv_ctx));
7656 priv_ctx.cur_ps_args = args;
7657 priv_ctx.cur_np2fixup_info = np2fixup_info;
7658 priv_ctx.string_buffers = string_buffers;
7660 shader_glsl_add_version_declaration(buffer, gl_info);
7662 shader_glsl_enable_extensions(buffer, gl_info);
7663 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7664 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7665 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7666 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7667 if (shader_glsl_use_explicit_attrib_location(gl_info))
7668 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7669 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7670 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7671 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7672 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7673 if (gl_info->supported[ARB_SAMPLE_SHADING])
7674 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7675 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7676 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7677 /* The spec says that it doesn't have to be explicitly enabled, but the
7678 * nvidia drivers write a warning if we don't do so. */
7679 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7680 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7682 /* Base Declarations */
7683 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7685 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7687 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7688 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7689 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7690 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7693 /* Declare uniforms for NP2 texcoord fixup:
7694 * This is NOT done inside the loop that declares the texture samplers
7695 * since the NP2 fixup code is currently only used for the GeforceFX
7696 * series and when forcing the ARB_npot extension off. Modern cards just
7697 * skip the code anyway, so put it inside a separate loop. */
7698 if (args->np2_fixup)
7700 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7701 unsigned int cur = 0;
7703 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7704 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7705 * samplerNP2Fixup stores texture dimensions and is updated through
7706 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7708 for (i = 0; i < shader->limits->sampler; ++i)
7710 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7711 continue;
7713 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7715 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7716 continue;
7719 fixup->idx[i] = cur++;
7722 fixup->num_consts = (cur + 1) >> 1;
7723 fixup->active = args->np2_fixup;
7724 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7727 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7729 shader_addline(buffer, "uniform struct\n{\n");
7730 shader_addline(buffer, " vec4 color;\n");
7731 shader_addline(buffer, " float density;\n");
7732 shader_addline(buffer, " float end;\n");
7733 shader_addline(buffer, " float scale;\n");
7734 shader_addline(buffer, "} ffp_fog;\n");
7736 if (needs_legacy_glsl_syntax(gl_info))
7738 if (glsl_is_color_reg_read(shader, 0))
7739 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7740 if (glsl_is_color_reg_read(shader, 1))
7741 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7742 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7743 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7745 else
7747 if (glsl_is_color_reg_read(shader, 0))
7748 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7749 if (glsl_is_color_reg_read(shader, 1))
7750 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7751 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7752 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7753 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7757 if (version->major >= 3)
7759 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7761 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7762 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7763 shader->u.ps.interpolation_mode, version->major >= 4);
7764 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7767 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7769 if (!(map & 1))
7770 continue;
7772 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7774 if (reg_maps->luminanceparams & (1u << i))
7776 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7777 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7778 extra_constants_needed++;
7781 extra_constants_needed++;
7784 if (args->srgb_correction)
7786 shader_addline(buffer, "const vec4 srgb_const0 = ");
7787 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7788 shader_addline(buffer, ";\n");
7789 shader_addline(buffer, "const vec4 srgb_const1 = ");
7790 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7791 shader_addline(buffer, ";\n");
7793 if (reg_maps->vpos || reg_maps->usesdsy)
7795 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7797 ++extra_constants_needed;
7798 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7800 if (reg_maps->vpos)
7802 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7804 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7805 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7806 args->render_offscreen ? "" : "origin_upper_left, ");
7807 else if (!args->render_offscreen)
7808 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7810 shader_addline(buffer, "vec4 vpos;\n");
7814 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7815 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7817 if (!needs_legacy_glsl_syntax(gl_info))
7819 if (shader_glsl_use_explicit_attrib_location(gl_info))
7820 shader_addline(buffer, "layout(location = 0) ");
7821 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
7824 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7825 FIXME("Insufficient uniforms to run this shader.\n");
7827 if (shader->u.ps.force_early_depth_stencil)
7828 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7830 shader_addline(buffer, "void main()\n{\n");
7832 if (reg_maps->sample_mask)
7833 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7835 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7836 * add approximately 0.5. This causes off-by-one problems as spotted by
7837 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7838 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7839 * causes precision troubles when we just subtract 0.5.
7841 * To deal with that, just floor() the position. This will eliminate the
7842 * fraction on all cards.
7844 * TODO: Test how this behaves with multisampling.
7846 * An advantage of floor is that it works even if the driver doesn't add
7847 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7848 * to return in gl_FragCoord, even though coordinates specify the pixel
7849 * centers instead of the pixel corners. This code will behave correctly
7850 * on drivers that returns integer values. */
7851 if (reg_maps->vpos)
7853 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7854 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7855 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7856 shader_addline(buffer,
7857 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7858 else
7859 shader_addline(buffer,
7860 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7863 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7865 unsigned int i;
7866 WORD map = reg_maps->texcoord;
7868 if (legacy_syntax)
7870 if (glsl_is_color_reg_read(shader, 0))
7871 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7872 if (glsl_is_color_reg_read(shader, 1))
7873 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7876 for (i = 0; map; map >>= 1, ++i)
7878 if (map & 1)
7880 if (args->pointsprite)
7881 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7882 else if (args->texcoords_initialized & (1u << i))
7883 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7884 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7885 else
7886 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7887 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7891 if (legacy_syntax)
7892 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7895 /* Pack 3.0 inputs */
7896 if (reg_maps->shader_version.major >= 3)
7897 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7898 reg_maps->shader_version.major >= 4);
7900 /* Base Shader Body */
7901 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7902 return 0;
7904 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7905 if (reg_maps->shader_version.major < 4)
7906 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
7908 shader_addline(buffer, "}\n");
7910 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7911 TRACE("Compiling shader object %u.\n", shader_id);
7912 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7914 return shader_id;
7917 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7918 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7919 const struct vs_compile_args *args)
7921 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7922 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7923 unsigned int i;
7925 /* Unpack outputs. */
7926 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7928 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7929 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7930 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7931 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7933 if (reg_maps->shader_version.major < 3)
7935 if (args->fog_src == VS_FOG_Z)
7936 shader_addline(buffer, "%s = gl_Position.z;\n",
7937 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7938 else if (!reg_maps->fog)
7939 shader_addline(buffer, "%s = 0.0;\n",
7940 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7943 /* We always store the clipplanes without y inversion. */
7944 if (args->clip_enabled)
7946 if (legacy_syntax)
7947 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7948 else
7949 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7950 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7953 if (args->point_size && !args->per_vertex_point_size)
7954 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7956 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7957 shader_glsl_fixup_position(buffer, FALSE);
7960 /* Context activation is done by the caller. */
7961 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
7962 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7964 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7965 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7966 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7967 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7968 const struct wined3d_gl_info *gl_info = context->gl_info;
7969 struct shader_glsl_ctx_priv priv_ctx;
7970 GLuint shader_id;
7971 unsigned int i;
7973 memset(&priv_ctx, 0, sizeof(priv_ctx));
7974 priv_ctx.cur_vs_args = args;
7975 priv_ctx.string_buffers = string_buffers;
7977 shader_glsl_add_version_declaration(buffer, gl_info);
7979 shader_glsl_enable_extensions(buffer, gl_info);
7980 if (gl_info->supported[ARB_DRAW_INSTANCED])
7981 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7982 if (shader_glsl_use_explicit_attrib_location(gl_info))
7983 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7985 /* Base Declarations */
7986 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7988 for (i = 0; i < shader->input_signature.element_count; ++i)
7989 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7991 if (args->point_size && !args->per_vertex_point_size)
7993 shader_addline(buffer, "uniform struct\n{\n");
7994 shader_addline(buffer, " float size;\n");
7995 shader_addline(buffer, " float size_min;\n");
7996 shader_addline(buffer, " float size_max;\n");
7997 shader_addline(buffer, "} ffp_point;\n");
8000 if (!needs_legacy_glsl_syntax(gl_info))
8002 if (args->clip_enabled)
8003 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8005 if (version->major < 3)
8007 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8008 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8009 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8010 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8014 if (version->major < 4)
8015 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8017 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8018 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8020 if (reg_maps->shader_version.major >= 4)
8021 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8022 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8024 shader_addline(buffer, "void main()\n{\n");
8026 if (reg_maps->input_rel_addressing)
8028 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8029 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8030 for (i = 0; i < shader->input_signature.element_count; ++i)
8032 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8033 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8037 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8038 return 0;
8040 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8041 if (reg_maps->shader_version.major < 4)
8042 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8044 shader_addline(buffer, "}\n");
8046 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8047 TRACE("Compiling shader object %u.\n", shader_id);
8048 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8050 return shader_id;
8053 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8054 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8056 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8057 char reg_mask[6];
8058 unsigned int i;
8060 for (i = 0; i < output_signature->element_count; ++i)
8062 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8064 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8065 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8066 output->register_idx, reg_mask, output->register_idx, reg_mask);
8070 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8071 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8072 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8073 const char *phase_name, unsigned phase_idx)
8075 unsigned int i;
8076 HRESULT hr;
8078 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8079 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8080 for (i = 0; i < phase->temporary_count; ++i)
8081 shader_addline(buffer, "vec4 R%u;\n", i);
8082 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8083 shader_addline(buffer, "}\n");
8084 return hr;
8087 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8088 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8090 if (phase->instance_count)
8092 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8093 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8094 shader_addline(buffer, "}\n");
8096 else
8098 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8102 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
8103 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8105 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8106 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8107 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8108 const struct wined3d_gl_info *gl_info = context->gl_info;
8109 const struct wined3d_hull_shader *hs = &shader->u.hs;
8110 const struct wined3d_shader_phase *phase;
8111 struct shader_glsl_ctx_priv priv_ctx;
8112 GLuint shader_id;
8113 unsigned int i;
8115 memset(&priv_ctx, 0, sizeof(priv_ctx));
8116 priv_ctx.string_buffers = string_buffers;
8118 shader_glsl_add_version_declaration(buffer, gl_info);
8120 shader_glsl_enable_extensions(buffer, gl_info);
8121 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8123 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8125 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8127 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8128 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8130 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8132 if (hs->phases.control_point)
8134 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8135 shader->limits->packed_output);
8136 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8137 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8138 shader_addline(buffer, "}\n");
8141 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8142 if ((phase = hs->phases.control_point))
8144 for (i = 0; i < phase->temporary_count; ++i)
8145 shader_addline(buffer, "vec4 R%u;\n", i);
8146 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8147 return 0;
8148 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8150 else
8152 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8154 shader_addline(buffer, "}\n");
8156 for (i = 0; i < hs->phases.fork_count; ++i)
8158 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8159 &hs->phases.fork[i], "fork", i)))
8160 return 0;
8163 for (i = 0; i < hs->phases.join_count; ++i)
8165 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8166 &hs->phases.join[i], "join", i)))
8167 return 0;
8170 shader_addline(buffer, "void main()\n{\n");
8171 shader_addline(buffer, "hs_control_point_phase();\n");
8172 if (reg_maps->vocp)
8173 shader_addline(buffer, "barrier();\n");
8174 for (i = 0; i < hs->phases.fork_count; ++i)
8175 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8176 for (i = 0; i < hs->phases.join_count; ++i)
8177 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8178 shader_addline(buffer, "setup_patch_constant_output();\n");
8179 shader_addline(buffer, "}\n");
8181 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8182 TRACE("Compiling shader object %u.\n", shader_id);
8183 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8185 return shader_id;
8188 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8189 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8190 const struct ds_compile_args *args)
8192 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8194 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8195 shader_glsl_fixup_position(buffer, FALSE);
8198 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
8199 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8201 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8202 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8203 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8204 const struct wined3d_gl_info *gl_info = context->gl_info;
8205 struct shader_glsl_ctx_priv priv_ctx;
8206 GLuint shader_id;
8208 memset(&priv_ctx, 0, sizeof(priv_ctx));
8209 priv_ctx.cur_ds_args = args;
8210 priv_ctx.string_buffers = string_buffers;
8212 shader_glsl_add_version_declaration(buffer, gl_info);
8214 shader_glsl_enable_extensions(buffer, gl_info);
8215 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8217 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8219 shader_addline(buffer, "layout(");
8220 switch (shader->u.ds.tessellator_domain)
8222 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8223 shader_addline(buffer, "isolines");
8224 break;
8225 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8226 shader_addline(buffer, "quads");
8227 break;
8228 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8229 shader_addline(buffer, "triangles");
8230 break;
8232 switch (args->tessellator_output_primitive)
8234 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8235 if (args->render_offscreen)
8236 shader_addline(buffer, ", ccw");
8237 else
8238 shader_addline(buffer, ", cw");
8239 break;
8240 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8241 if (args->render_offscreen)
8242 shader_addline(buffer, ", cw");
8243 else
8244 shader_addline(buffer, ", ccw");
8245 break;
8246 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8247 shader_addline(buffer, ", point_mode");
8248 break;
8249 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8250 break;
8252 switch (args->tessellator_partitioning)
8254 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8255 shader_addline(buffer, ", fractional_odd_spacing");
8256 break;
8257 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8258 shader_addline(buffer, ", fractional_even_spacing");
8259 break;
8260 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8261 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8262 shader_addline(buffer, ", equal_spacing");
8263 break;
8265 shader_addline(buffer, ") in;\n");
8267 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8269 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8270 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8272 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8273 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8274 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8276 shader_addline(buffer, "void main()\n{\n");
8277 shader_addline(buffer, "setup_patch_constant_input();\n");
8279 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8280 return 0;
8282 shader_addline(buffer, "}\n");
8284 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8285 TRACE("Compiling shader object %u.\n", shader_id);
8286 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8288 return shader_id;
8291 /* Context activation is done by the caller. */
8292 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
8293 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8295 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8296 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8297 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8298 const struct wined3d_gl_info *gl_info = context->gl_info;
8299 const struct wined3d_shader_signature_element *output;
8300 enum wined3d_primitive_type primitive_type;
8301 struct shader_glsl_ctx_priv priv_ctx;
8302 unsigned int max_vertices;
8303 unsigned int i, j;
8304 GLuint shader_id;
8306 memset(&priv_ctx, 0, sizeof(priv_ctx));
8307 priv_ctx.string_buffers = string_buffers;
8309 shader_glsl_add_version_declaration(buffer, gl_info);
8311 shader_glsl_enable_extensions(buffer, gl_info);
8313 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8315 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8316 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8317 if (shader->u.gs.instance_count > 1)
8318 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8319 shader_addline(buffer, ") in;\n");
8321 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8322 if (!(max_vertices = shader->u.gs.vertices_out))
8324 switch (args->primitive_type)
8326 case WINED3D_PT_POINTLIST:
8327 max_vertices = 1;
8328 break;
8329 case WINED3D_PT_LINELIST:
8330 max_vertices = 2;
8331 break;
8332 case WINED3D_PT_TRIANGLELIST:
8333 max_vertices = 3;
8334 break;
8335 default:
8336 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8337 break;
8340 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8341 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8342 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8344 if (!gl_info->supported[ARB_CLIP_CONTROL])
8346 shader_addline(buffer, "uniform vec4 pos_fixup");
8347 if (reg_maps->viewport_array)
8348 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8349 shader_addline(buffer, ";\n");
8352 if (is_rasterization_disabled(shader))
8354 shader_glsl_generate_stream_output_setup(priv, shader, &shader->u.gs.so_desc);
8356 else
8358 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8359 gl_info, TRUE, args->interpolation_mode);
8362 shader_addline(buffer, "void main()\n{\n");
8363 if (shader->function)
8365 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8366 return 0;
8368 else
8370 for (i = 0; i < max_vertices; ++i)
8372 for (j = 0; j < shader->output_signature.element_count; ++j)
8374 output = &shader->output_signature.elements[j];
8375 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8376 output->register_idx, i, output->register_idx);
8378 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8379 if (!gl_info->supported[ARB_CLIP_CONTROL])
8380 shader_glsl_fixup_position(buffer, FALSE);
8381 shader_addline(buffer, "EmitVertex();\n");
8384 shader_addline(buffer, "}\n");
8386 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8387 TRACE("Compiling shader object %u.\n", shader_id);
8388 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8390 return shader_id;
8393 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8395 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8396 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8397 struct wined3d_string_buffer *buffer = ctx->buffer;
8398 const struct wined3d_shader *shader = ctx->shader;
8400 switch (shader->reg_maps.shader_version.type)
8402 case WINED3D_SHADER_TYPE_PIXEL:
8403 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args);
8404 break;
8405 case WINED3D_SHADER_TYPE_VERTEX:
8406 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8407 break;
8408 case WINED3D_SHADER_TYPE_DOMAIN:
8409 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8410 break;
8411 case WINED3D_SHADER_TYPE_GEOMETRY:
8412 case WINED3D_SHADER_TYPE_COMPUTE:
8413 break;
8414 default:
8415 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8416 break;
8420 /* Context activation is done by the caller. */
8421 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8422 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8423 const struct wined3d_shader *shader)
8425 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8426 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8427 const struct wined3d_gl_info *gl_info = context->gl_info;
8428 struct shader_glsl_ctx_priv priv_ctx;
8429 GLuint shader_id;
8430 unsigned int i;
8432 memset(&priv_ctx, 0, sizeof(priv_ctx));
8433 priv_ctx.string_buffers = string_buffers;
8435 shader_glsl_add_version_declaration(buffer, gl_info);
8437 shader_glsl_enable_extensions(buffer, gl_info);
8438 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8440 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8442 for (i = 0; i < reg_maps->tgsm_count; ++i)
8444 if (reg_maps->tgsm[i].size)
8445 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8448 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8449 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8451 shader_addline(buffer, "void main()\n{\n");
8452 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8453 shader_addline(buffer, "}\n");
8455 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8456 TRACE("Compiling shader object %u.\n", shader_id);
8457 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8459 return shader_id;
8462 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8463 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8464 struct wined3d_shader *shader,
8465 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8467 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8468 struct glsl_shader_private *shader_data;
8469 struct ps_np2fixup_info *np2fixup;
8470 UINT i;
8471 DWORD new_size;
8472 GLuint ret;
8474 if (!shader->backend_data)
8476 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8478 ERR("Failed to allocate backend data.\n");
8479 return 0;
8482 shader_data = shader->backend_data;
8483 gl_shaders = shader_data->gl_shaders.ps;
8485 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8486 * so a linear search is more performant than a hashmap or a binary search
8487 * (cache coherency etc)
8489 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8491 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8493 if (args->np2_fixup)
8494 *np2fixup_info = &gl_shaders[i].np2fixup;
8495 return gl_shaders[i].id;
8499 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8500 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8502 if (shader_data->num_gl_shaders)
8504 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8505 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8507 else
8509 new_array = heap_alloc(sizeof(*gl_shaders));
8510 new_size = 1;
8513 if(!new_array) {
8514 ERR("Out of memory\n");
8515 return 0;
8517 shader_data->gl_shaders.ps = new_array;
8518 shader_data->shader_array_size = new_size;
8519 gl_shaders = new_array;
8522 gl_shaders[shader_data->num_gl_shaders].args = *args;
8524 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8525 memset(np2fixup, 0, sizeof(*np2fixup));
8526 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8528 pixelshader_update_resource_types(shader, args->tex_types);
8530 string_buffer_clear(buffer);
8531 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8532 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8534 return ret;
8537 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8538 const DWORD use_map)
8540 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
8541 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
8542 if (stored->point_size != new->point_size)
8543 return FALSE;
8544 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8545 return FALSE;
8546 if (stored->flatshading != new->flatshading)
8547 return FALSE;
8548 if (stored->next_shader_type != new->next_shader_type)
8549 return FALSE;
8550 if (stored->next_shader_input_count != new->next_shader_input_count)
8551 return FALSE;
8552 return stored->fog_src == new->fog_src;
8555 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8556 struct wined3d_shader *shader, const struct vs_compile_args *args)
8558 UINT i;
8559 DWORD new_size;
8560 DWORD use_map = context->stream_info.use_map;
8561 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8562 struct glsl_shader_private *shader_data;
8563 GLuint ret;
8565 if (!shader->backend_data)
8567 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8569 ERR("Failed to allocate backend data.\n");
8570 return 0;
8573 shader_data = shader->backend_data;
8574 gl_shaders = shader_data->gl_shaders.vs;
8576 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8577 * so a linear search is more performant than a hashmap or a binary search
8578 * (cache coherency etc)
8580 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8582 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8583 return gl_shaders[i].id;
8586 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8588 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8590 if (shader_data->num_gl_shaders)
8592 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8593 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8595 else
8597 new_array = heap_alloc(sizeof(*gl_shaders));
8598 new_size = 1;
8601 if(!new_array) {
8602 ERR("Out of memory\n");
8603 return 0;
8605 shader_data->gl_shaders.vs = new_array;
8606 shader_data->shader_array_size = new_size;
8607 gl_shaders = new_array;
8610 gl_shaders[shader_data->num_gl_shaders].args = *args;
8612 string_buffer_clear(&priv->shader_buffer);
8613 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8614 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8616 return ret;
8619 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8620 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8622 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8623 struct glsl_shader_private *shader_data;
8624 unsigned int new_size;
8625 GLuint ret;
8627 if (!shader->backend_data)
8629 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8631 ERR("Failed to allocate backend data.\n");
8632 return 0;
8635 shader_data = shader->backend_data;
8636 gl_shaders = shader_data->gl_shaders.hs;
8638 if (shader_data->num_gl_shaders > 0)
8640 assert(shader_data->num_gl_shaders == 1);
8641 return gl_shaders[0].id;
8644 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8646 assert(!shader_data->gl_shaders.hs);
8647 new_size = 1;
8648 if (!(new_array = heap_alloc(sizeof(*new_array))))
8650 ERR("Failed to allocate GL shaders array.\n");
8651 return 0;
8653 shader_data->gl_shaders.hs = new_array;
8654 shader_data->shader_array_size = new_size;
8655 gl_shaders = new_array;
8657 string_buffer_clear(&priv->shader_buffer);
8658 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8659 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8661 return ret;
8664 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8665 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8667 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8668 struct glsl_shader_private *shader_data;
8669 unsigned int i, new_size;
8670 GLuint ret;
8672 if (!shader->backend_data)
8674 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8676 ERR("Failed to allocate backend data.\n");
8677 return 0;
8680 shader_data = shader->backend_data;
8681 gl_shaders = shader_data->gl_shaders.ds;
8683 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8685 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8686 return gl_shaders[i].id;
8689 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8691 if (shader_data->num_gl_shaders)
8693 new_size = shader_data->shader_array_size + 1;
8694 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8696 else
8698 new_array = heap_alloc(sizeof(*new_array));
8699 new_size = 1;
8702 if (!new_array)
8704 ERR("Failed to allocate GL shaders array.\n");
8705 return 0;
8707 shader_data->gl_shaders.ds = new_array;
8708 shader_data->shader_array_size = new_size;
8709 gl_shaders = new_array;
8711 string_buffer_clear(&priv->shader_buffer);
8712 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8713 gl_shaders[shader_data->num_gl_shaders].args = *args;
8714 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8716 return ret;
8719 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8720 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8722 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8723 struct glsl_shader_private *shader_data;
8724 unsigned int i, new_size;
8725 GLuint ret;
8727 if (!shader->backend_data)
8729 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8731 ERR("Failed to allocate backend data.\n");
8732 return 0;
8735 shader_data = shader->backend_data;
8736 gl_shaders = shader_data->gl_shaders.gs;
8738 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8740 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8741 return gl_shaders[i].id;
8744 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8746 if (shader_data->num_gl_shaders)
8748 new_size = shader_data->shader_array_size + 1;
8749 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8751 else
8753 new_array = heap_alloc(sizeof(*new_array));
8754 new_size = 1;
8757 if (!new_array)
8759 ERR("Failed to allocate GL shaders array.\n");
8760 return 0;
8762 shader_data->gl_shaders.gs = new_array;
8763 shader_data->shader_array_size = new_size;
8764 gl_shaders = new_array;
8766 string_buffer_clear(&priv->shader_buffer);
8767 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8768 gl_shaders[shader_data->num_gl_shaders].args = *args;
8769 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8771 return ret;
8774 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8776 switch (mcs)
8778 case WINED3D_MCS_MATERIAL:
8779 return material;
8780 case WINED3D_MCS_COLOR1:
8781 return "ffp_attrib_diffuse";
8782 case WINED3D_MCS_COLOR2:
8783 return "ffp_attrib_specular";
8784 default:
8785 ERR("Invalid material color source %#x.\n", mcs);
8786 return "<invalid>";
8790 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8791 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8793 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8794 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8795 if (settings->localviewer)
8796 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8797 else
8798 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8799 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8800 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8803 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8804 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8806 const char *diffuse, *specular, *emissive, *ambient;
8807 unsigned int i, idx;
8809 if (!settings->lighting)
8811 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8812 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8813 return;
8816 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8817 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8818 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8819 shader_addline(buffer, "vec3 dir, dst;\n");
8820 shader_addline(buffer, "float att, t;\n");
8822 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8823 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8824 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8825 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8827 idx = 0;
8828 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8830 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8831 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8832 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8833 shader_addline(buffer, "dst.x = 1.0;\n");
8834 if (legacy_lighting)
8836 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8837 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8838 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8840 else
8842 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8844 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8845 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8846 if (!legacy_lighting)
8847 shader_addline(buffer, "att = 1.0 / att;\n");
8848 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8849 if (!settings->normal)
8851 shader_addline(buffer, "}\n");
8852 continue;
8854 shader_addline(buffer, "dir = normalize(dir);\n");
8855 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8856 shader_addline(buffer, "}\n");
8859 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8861 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8862 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8863 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8864 shader_addline(buffer, "dst.x = 1.0;\n");
8865 if (legacy_lighting)
8867 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8868 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8869 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8871 else
8873 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8875 shader_addline(buffer, "dir = normalize(dir);\n");
8876 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8877 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8878 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8879 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8880 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8881 idx, idx, idx, idx);
8882 if (legacy_lighting)
8883 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8884 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8885 idx, idx, idx);
8886 else
8887 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8888 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8889 idx, idx, idx);
8890 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8891 if (!settings->normal)
8893 shader_addline(buffer, "}\n");
8894 continue;
8896 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8897 shader_addline(buffer, "}\n");
8900 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8902 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8903 if (!settings->normal)
8904 continue;
8905 shader_addline(buffer, "att = 1.0;\n");
8906 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8907 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8910 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8912 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8913 if (!settings->normal)
8914 continue;
8915 shader_addline(buffer, "att = 1.0;\n");
8916 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8917 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8920 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8921 ambient, diffuse, emissive);
8922 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8923 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8926 /* Context activation is done by the caller. */
8927 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8928 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8930 static const struct attrib_info
8932 const char type[6];
8933 const char name[24];
8935 attrib_info[] =
8937 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8938 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8939 /* TODO: Indexed vertex blending */
8940 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8941 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8942 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8943 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8944 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8946 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8947 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8948 BOOL output_legacy_fogcoord = legacy_syntax;
8949 BOOL legacy_lighting = priv->legacy_lighting;
8950 GLuint shader_obj;
8951 unsigned int i;
8953 string_buffer_clear(buffer);
8955 shader_glsl_add_version_declaration(buffer, gl_info);
8957 if (shader_glsl_use_explicit_attrib_location(gl_info))
8958 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8960 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8962 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8964 if (shader_glsl_use_explicit_attrib_location(gl_info))
8965 shader_addline(buffer, "layout(location = %u) ", i);
8966 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8968 shader_addline(buffer, "\n");
8970 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8971 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8972 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8973 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
8975 shader_addline(buffer, "uniform struct\n{\n");
8976 shader_addline(buffer, " vec4 emissive;\n");
8977 shader_addline(buffer, " vec4 ambient;\n");
8978 shader_addline(buffer, " vec4 diffuse;\n");
8979 shader_addline(buffer, " vec4 specular;\n");
8980 shader_addline(buffer, " float shininess;\n");
8981 shader_addline(buffer, "} ffp_material;\n");
8983 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8984 shader_addline(buffer, "uniform struct\n{\n");
8985 shader_addline(buffer, " vec4 diffuse;\n");
8986 shader_addline(buffer, " vec4 specular;\n");
8987 shader_addline(buffer, " vec4 ambient;\n");
8988 shader_addline(buffer, " vec4 position;\n");
8989 shader_addline(buffer, " vec3 direction;\n");
8990 shader_addline(buffer, " float range;\n");
8991 shader_addline(buffer, " float falloff;\n");
8992 shader_addline(buffer, " float c_att;\n");
8993 shader_addline(buffer, " float l_att;\n");
8994 shader_addline(buffer, " float q_att;\n");
8995 shader_addline(buffer, " float cos_htheta;\n");
8996 shader_addline(buffer, " float cos_hphi;\n");
8997 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
8999 if (settings->point_size)
9001 shader_addline(buffer, "uniform struct\n{\n");
9002 shader_addline(buffer, " float size;\n");
9003 shader_addline(buffer, " float size_min;\n");
9004 shader_addline(buffer, " float size_max;\n");
9005 shader_addline(buffer, " float c_att;\n");
9006 shader_addline(buffer, " float l_att;\n");
9007 shader_addline(buffer, " float q_att;\n");
9008 shader_addline(buffer, "} ffp_point;\n");
9011 if (legacy_syntax)
9013 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9014 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9015 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9016 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9018 else
9020 if (settings->clipping)
9021 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9023 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9024 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9025 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9026 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9029 shader_addline(buffer, "\nvoid main()\n{\n");
9030 shader_addline(buffer, "float m;\n");
9031 shader_addline(buffer, "vec3 r;\n");
9033 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9035 if (attrib_info[i].name[0])
9036 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9037 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9039 for (i = 0; i < MAX_TEXTURES; ++i)
9041 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9042 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9043 && settings->texcoords & (1u << i))
9044 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9047 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9049 if (settings->transformed)
9051 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9052 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9053 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9055 else
9057 for (i = 0; i < settings->vertexblends; ++i)
9058 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9060 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9061 for (i = 0; i < settings->vertexblends + 1; ++i)
9062 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9064 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9065 if (settings->clipping)
9067 if (legacy_syntax)
9068 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9069 else
9070 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9071 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9073 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9076 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9077 if (settings->normal)
9079 if (!settings->vertexblends)
9081 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9083 else
9085 for (i = 0; i < settings->vertexblends + 1; ++i)
9086 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9089 if (settings->normalize)
9090 shader_addline(buffer, "normal = normalize(normal);\n");
9093 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9094 if (legacy_syntax)
9096 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9097 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9099 else
9101 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9102 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9105 for (i = 0; i < MAX_TEXTURES; ++i)
9107 BOOL output_legacy_texcoord = legacy_syntax;
9109 switch (settings->texgen[i] & 0xffff0000)
9111 case WINED3DTSS_TCI_PASSTHRU:
9112 if (settings->texcoords & (1u << i))
9113 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9114 i, i, i);
9115 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
9116 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9117 else
9118 output_legacy_texcoord = FALSE;
9119 break;
9121 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9122 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9123 break;
9125 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9126 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9127 break;
9129 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9130 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9131 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9132 break;
9134 case WINED3DTSS_TCI_SPHEREMAP:
9135 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9136 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9137 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9138 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9139 break;
9141 default:
9142 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9143 break;
9145 if (output_legacy_texcoord)
9146 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9149 switch (settings->fog_mode)
9151 case WINED3D_FFP_VS_FOG_OFF:
9152 output_legacy_fogcoord = FALSE;
9153 break;
9155 case WINED3D_FFP_VS_FOG_FOGCOORD:
9156 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9157 break;
9159 case WINED3D_FFP_VS_FOG_RANGE:
9160 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9161 break;
9163 case WINED3D_FFP_VS_FOG_DEPTH:
9164 if (settings->ortho_fog)
9166 if (gl_info->supported[ARB_CLIP_CONTROL])
9167 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9168 else
9169 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9170 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9172 else if (settings->transformed)
9174 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9176 else
9178 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9180 break;
9182 default:
9183 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9184 break;
9186 if (output_legacy_fogcoord)
9187 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9189 if (settings->point_size)
9191 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9192 " + ffp_point.l_att * length(ec_pos.xyz)"
9193 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9194 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9195 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9198 shader_addline(buffer, "}\n");
9200 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9201 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9203 return shader_obj;
9206 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9207 DWORD argnum, unsigned int stage, DWORD arg)
9209 const char *ret;
9211 if (arg == ARG_UNUSED)
9212 return "<unused arg>";
9214 switch (arg & WINED3DTA_SELECTMASK)
9216 case WINED3DTA_DIFFUSE:
9217 ret = "ffp_varying_diffuse";
9218 break;
9220 case WINED3DTA_CURRENT:
9221 ret = "ret";
9222 break;
9224 case WINED3DTA_TEXTURE:
9225 switch (stage)
9227 case 0: ret = "tex0"; break;
9228 case 1: ret = "tex1"; break;
9229 case 2: ret = "tex2"; break;
9230 case 3: ret = "tex3"; break;
9231 case 4: ret = "tex4"; break;
9232 case 5: ret = "tex5"; break;
9233 case 6: ret = "tex6"; break;
9234 case 7: ret = "tex7"; break;
9235 default:
9236 ret = "<invalid texture>";
9237 break;
9239 break;
9241 case WINED3DTA_TFACTOR:
9242 ret = "tex_factor";
9243 break;
9245 case WINED3DTA_SPECULAR:
9246 ret = "ffp_varying_specular";
9247 break;
9249 case WINED3DTA_TEMP:
9250 ret = "temp_reg";
9251 break;
9253 case WINED3DTA_CONSTANT:
9254 switch (stage)
9256 case 0: ret = "tss_const0"; break;
9257 case 1: ret = "tss_const1"; break;
9258 case 2: ret = "tss_const2"; break;
9259 case 3: ret = "tss_const3"; break;
9260 case 4: ret = "tss_const4"; break;
9261 case 5: ret = "tss_const5"; break;
9262 case 6: ret = "tss_const6"; break;
9263 case 7: ret = "tss_const7"; break;
9264 default:
9265 ret = "<invalid constant>";
9266 break;
9268 break;
9270 default:
9271 return "<unhandled arg>";
9274 if (arg & WINED3DTA_COMPLEMENT)
9276 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9277 if (argnum == 0)
9278 ret = "arg0";
9279 else if (argnum == 1)
9280 ret = "arg1";
9281 else if (argnum == 2)
9282 ret = "arg2";
9285 if (arg & WINED3DTA_ALPHAREPLICATE)
9287 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9288 if (argnum == 0)
9289 ret = "arg0";
9290 else if (argnum == 1)
9291 ret = "arg1";
9292 else if (argnum == 2)
9293 ret = "arg2";
9296 return ret;
9299 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9300 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9302 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9304 if (color && alpha)
9305 dstmask = "";
9306 else if (color)
9307 dstmask = ".xyz";
9308 else
9309 dstmask = ".w";
9311 dstreg = tmp_dst ? "temp_reg" : "ret";
9313 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9314 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9315 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9317 switch (op)
9319 case WINED3D_TOP_DISABLE:
9320 break;
9322 case WINED3D_TOP_SELECT_ARG1:
9323 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9324 break;
9326 case WINED3D_TOP_SELECT_ARG2:
9327 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9328 break;
9330 case WINED3D_TOP_MODULATE:
9331 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9332 break;
9334 case WINED3D_TOP_MODULATE_4X:
9335 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9336 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9337 break;
9339 case WINED3D_TOP_MODULATE_2X:
9340 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9341 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9342 break;
9344 case WINED3D_TOP_ADD:
9345 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9346 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9347 break;
9349 case WINED3D_TOP_ADD_SIGNED:
9350 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9351 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9352 break;
9354 case WINED3D_TOP_ADD_SIGNED_2X:
9355 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9356 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9357 break;
9359 case WINED3D_TOP_SUBTRACT:
9360 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9361 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9362 break;
9364 case WINED3D_TOP_ADD_SMOOTH:
9365 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9366 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9367 break;
9369 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9370 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9371 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9372 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9373 break;
9375 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9376 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9377 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9378 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9379 break;
9381 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9382 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9383 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9384 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9385 break;
9387 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9388 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9389 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9390 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9391 break;
9393 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9394 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9395 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9396 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9397 break;
9399 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9400 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9401 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9402 break;
9404 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9405 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9406 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9407 break;
9409 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9410 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9411 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9412 break;
9413 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9414 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9415 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9416 break;
9418 case WINED3D_TOP_BUMPENVMAP:
9419 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9420 /* These are handled in the first pass, nothing to do. */
9421 break;
9423 case WINED3D_TOP_DOTPRODUCT3:
9424 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9425 dstreg, dstmask, arg1, arg2, dstmask);
9426 break;
9428 case WINED3D_TOP_MULTIPLY_ADD:
9429 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9430 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9431 break;
9433 case WINED3D_TOP_LERP:
9434 /* MSDN isn't quite right here. */
9435 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9436 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9437 break;
9439 default:
9440 FIXME("Unhandled operation %#x.\n", op);
9441 break;
9445 /* Context activation is done by the caller. */
9446 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9447 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9449 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9450 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9451 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9452 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9453 const struct wined3d_gl_info *gl_info = context->gl_info;
9454 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9455 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9456 UINT lowest_disabled_stage;
9457 GLuint shader_id;
9458 DWORD arg0, arg1, arg2;
9459 unsigned int stage;
9461 string_buffer_clear(buffer);
9463 /* Find out which textures are read */
9464 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9466 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9467 break;
9469 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9470 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9471 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9473 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9474 || (stage == 0 && settings->color_key_enabled))
9475 tex_map |= 1u << stage;
9476 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9477 tfactor_used = TRUE;
9478 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9479 tempreg_used = TRUE;
9480 if (settings->op[stage].tmp_dst)
9481 tempreg_used = TRUE;
9482 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9483 tss_const_map |= 1u << stage;
9485 switch (settings->op[stage].cop)
9487 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9488 lum_map |= 1u << stage;
9489 /* fall through */
9490 case WINED3D_TOP_BUMPENVMAP:
9491 bump_map |= 1u << stage;
9492 /* fall through */
9493 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9494 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9495 tex_map |= 1u << stage;
9496 break;
9498 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9499 tfactor_used = TRUE;
9500 break;
9502 default:
9503 break;
9506 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9507 continue;
9509 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9510 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9511 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9513 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9514 tex_map |= 1u << stage;
9515 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9516 tfactor_used = TRUE;
9517 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9518 tempreg_used = TRUE;
9519 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9520 tss_const_map |= 1u << stage;
9522 lowest_disabled_stage = stage;
9524 shader_glsl_add_version_declaration(buffer, gl_info);
9526 if (shader_glsl_use_explicit_attrib_location(gl_info))
9527 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9528 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9529 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9530 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9531 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9533 if (!needs_legacy_glsl_syntax(gl_info))
9535 if (shader_glsl_use_explicit_attrib_location(gl_info))
9536 shader_addline(buffer, "layout(location = 0) ");
9537 shader_addline(buffer, "out vec4 ps_out[1];\n");
9540 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9541 shader_addline(buffer, "vec4 ret;\n");
9542 if (tempreg_used || settings->sRGB_write)
9543 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9544 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9546 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9548 const char *sampler_type;
9550 if (tss_const_map & (1u << stage))
9551 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9553 if (!(tex_map & (1u << stage)))
9554 continue;
9556 switch (settings->op[stage].tex_type)
9558 case WINED3D_GL_RES_TYPE_TEX_1D:
9559 sampler_type = "1D";
9560 break;
9561 case WINED3D_GL_RES_TYPE_TEX_2D:
9562 sampler_type = "2D";
9563 break;
9564 case WINED3D_GL_RES_TYPE_TEX_3D:
9565 sampler_type = "3D";
9566 break;
9567 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9568 sampler_type = "Cube";
9569 break;
9570 case WINED3D_GL_RES_TYPE_TEX_RECT:
9571 sampler_type = "2DRect";
9572 break;
9573 default:
9574 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9575 sampler_type = NULL;
9576 break;
9578 if (sampler_type)
9580 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9581 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9582 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9585 shader_addline(buffer, "vec4 tex%u;\n", stage);
9587 if (!(bump_map & (1u << stage)))
9588 continue;
9589 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9591 if (!(lum_map & (1u << stage)))
9592 continue;
9593 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9594 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9596 if (tfactor_used)
9597 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9598 if (settings->color_key_enabled)
9599 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9600 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9602 if (settings->sRGB_write)
9604 shader_addline(buffer, "const vec4 srgb_const0 = ");
9605 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9606 shader_addline(buffer, ";\n");
9607 shader_addline(buffer, "const vec4 srgb_const1 = ");
9608 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9609 shader_addline(buffer, ";\n");
9612 shader_addline(buffer, "uniform struct\n{\n");
9613 shader_addline(buffer, " vec4 color;\n");
9614 shader_addline(buffer, " float density;\n");
9615 shader_addline(buffer, " float end;\n");
9616 shader_addline(buffer, " float scale;\n");
9617 shader_addline(buffer, "} ffp_fog;\n");
9619 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9620 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9622 if (legacy_syntax)
9624 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9625 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9626 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9627 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9628 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9630 else
9632 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9633 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9634 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9635 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9636 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9639 shader_addline(buffer, "void main()\n{\n");
9641 if (legacy_syntax)
9643 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9644 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9647 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9649 if (tex_map & (1u << stage))
9651 if (settings->pointsprite)
9652 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9653 else if (settings->texcoords_initialized & (1u << stage))
9654 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9655 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9656 else
9657 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9661 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9662 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9664 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9665 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9667 /* Generate texture sampling instructions */
9668 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9670 const char *texture_function, *coord_mask;
9671 BOOL proj;
9673 if (!(tex_map & (1u << stage)))
9674 continue;
9676 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9678 proj = FALSE;
9680 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9681 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9683 proj = TRUE;
9685 else
9687 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9688 proj = TRUE;
9691 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9692 proj = FALSE;
9694 switch (settings->op[stage].tex_type)
9696 case WINED3D_GL_RES_TYPE_TEX_1D:
9697 if (proj)
9699 texture_function = "texture1DProj";
9700 coord_mask = "xw";
9702 else
9704 texture_function = "texture1D";
9705 coord_mask = "x";
9707 break;
9708 case WINED3D_GL_RES_TYPE_TEX_2D:
9709 if (proj)
9711 texture_function = "texture2DProj";
9712 coord_mask = "xyw";
9714 else
9716 texture_function = "texture2D";
9717 coord_mask = "xy";
9719 break;
9720 case WINED3D_GL_RES_TYPE_TEX_3D:
9721 if (proj)
9723 texture_function = "texture3DProj";
9724 coord_mask = "xyzw";
9726 else
9728 texture_function = "texture3D";
9729 coord_mask = "xyz";
9731 break;
9732 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9733 texture_function = "textureCube";
9734 coord_mask = "xyz";
9735 break;
9736 case WINED3D_GL_RES_TYPE_TEX_RECT:
9737 if (proj)
9739 texture_function = "texture2DRectProj";
9740 coord_mask = "xyw";
9742 else
9744 texture_function = "texture2DRect";
9745 coord_mask = "xy";
9747 break;
9748 default:
9749 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9750 texture_function = "";
9751 coord_mask = "xyzw";
9752 break;
9754 if (!legacy_syntax)
9755 texture_function = proj ? "textureProj" : "texture";
9757 if (stage > 0
9758 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9759 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9761 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9763 /* With projective textures, texbem only divides the static
9764 * texture coordinate, not the displacement, so multiply the
9765 * displacement with the dividing parameter before passing it to
9766 * TXP. */
9767 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9769 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9771 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9772 stage, stage);
9773 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9775 else
9777 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9778 stage, stage);
9779 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9782 else
9784 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9787 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9788 stage, texture_function, stage, coord_mask);
9790 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9791 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9792 stage, stage - 1, stage - 1, stage - 1);
9794 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9796 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9797 stage, texture_function, stage, stage);
9799 else
9801 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9802 stage, texture_function, stage, stage, coord_mask);
9805 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9806 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9807 settings->op[stage].color_fixup);
9810 if (settings->color_key_enabled)
9812 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9813 shader_addline(buffer, " discard;\n");
9816 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9818 /* Generate the main shader */
9819 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9821 BOOL op_equal;
9823 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9824 break;
9826 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9827 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9828 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9829 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9830 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9831 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9832 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9833 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9834 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9835 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9836 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9837 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9838 else
9839 op_equal = settings->op[stage].aop == settings->op[stage].cop
9840 && settings->op[stage].carg0 == settings->op[stage].aarg0
9841 && settings->op[stage].carg1 == settings->op[stage].aarg1
9842 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9844 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9846 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9847 settings->op[stage].cop, settings->op[stage].carg0,
9848 settings->op[stage].carg1, settings->op[stage].carg2);
9850 else if (op_equal)
9852 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9853 settings->op[stage].cop, settings->op[stage].carg0,
9854 settings->op[stage].carg1, settings->op[stage].carg2);
9856 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9857 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9859 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9860 settings->op[stage].cop, settings->op[stage].carg0,
9861 settings->op[stage].carg1, settings->op[stage].carg2);
9862 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9863 settings->op[stage].aop, settings->op[stage].aarg0,
9864 settings->op[stage].aarg1, settings->op[stage].aarg2);
9868 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9869 get_fragment_output(gl_info));
9871 if (settings->sRGB_write)
9872 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9874 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9876 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9878 shader_addline(buffer, "}\n");
9880 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9881 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9883 string_buffer_release(&priv->string_buffers, tex_reg_name);
9884 return shader_id;
9887 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9888 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9890 struct glsl_ffp_vertex_shader *shader;
9891 const struct wine_rb_entry *entry;
9893 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9894 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9896 if (!(shader = heap_alloc(sizeof(*shader))))
9897 return NULL;
9899 shader->desc.settings = *settings;
9900 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9901 list_init(&shader->linked_programs);
9902 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9903 ERR("Failed to insert ffp vertex shader.\n");
9905 return shader;
9908 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9909 const struct ffp_frag_settings *args, const struct wined3d_context *context)
9911 struct glsl_ffp_fragment_shader *glsl_desc;
9912 const struct ffp_frag_desc *desc;
9914 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9915 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9917 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9918 return NULL;
9920 glsl_desc->entry.settings = *args;
9921 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
9922 list_init(&glsl_desc->linked_programs);
9923 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9925 return glsl_desc;
9929 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9930 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9932 unsigned int i;
9933 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9935 for (i = 0; i < vs_c_count; ++i)
9937 string_buffer_sprintf(name, "vs_c[%u]", i);
9938 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9940 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9942 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9944 string_buffer_sprintf(name, "vs_i[%u]", i);
9945 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9948 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9950 string_buffer_sprintf(name, "vs_b[%u]", i);
9951 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9954 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9956 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9958 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9959 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9961 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9962 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9963 for (i = 0; i < MAX_TEXTURES; ++i)
9965 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9966 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9968 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9969 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9970 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9971 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9972 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9973 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9974 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
9976 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9977 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9978 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9979 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9980 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9981 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9982 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9983 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9984 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9985 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9986 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9987 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9988 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9989 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9990 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9991 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9992 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9993 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9994 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9995 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9996 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9997 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9998 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9999 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10001 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10002 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10003 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10004 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10005 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10006 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10007 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10009 string_buffer_release(&priv->string_buffers, name);
10012 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10013 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10015 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10018 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10019 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10021 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10024 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10025 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10027 unsigned int i;
10028 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10030 for (i = 0; i < ps_c_count; ++i)
10032 string_buffer_sprintf(name, "ps_c[%u]", i);
10033 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10035 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10037 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10039 string_buffer_sprintf(name, "ps_i[%u]", i);
10040 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10043 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10045 string_buffer_sprintf(name, "ps_b[%u]", i);
10046 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10049 for (i = 0; i < MAX_TEXTURES; ++i)
10051 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10052 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10053 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10054 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10055 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10056 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10057 string_buffer_sprintf(name, "tss_const%u", i);
10058 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10061 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10062 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10064 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10065 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10066 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10067 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10069 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10071 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10072 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10073 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10075 string_buffer_release(&priv->string_buffers, name);
10078 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10079 const struct wined3d_context *context, struct wined3d_shader *shader)
10081 struct glsl_context_data *ctx_data = context->shader_backend_data;
10082 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10083 const struct wined3d_gl_info *gl_info = context->gl_info;
10084 struct glsl_cs_compiled_shader *gl_shaders;
10085 struct glsl_shader_private *shader_data;
10086 struct glsl_shader_prog_link *entry;
10087 GLuint shader_id, program_id;
10089 if (!(entry = heap_alloc(sizeof(*entry))))
10091 ERR("Out of memory.\n");
10092 return E_OUTOFMEMORY;
10095 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10097 ERR("Failed to allocate backend data.\n");
10098 heap_free(entry);
10099 return E_OUTOFMEMORY;
10101 shader_data = shader->backend_data;
10102 gl_shaders = shader_data->gl_shaders.cs;
10104 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10106 ERR("Failed to allocate GL shader array.\n");
10107 heap_free(entry);
10108 heap_free(shader->backend_data);
10109 shader->backend_data = NULL;
10110 return E_OUTOFMEMORY;
10112 shader_data->shader_array_size = 1;
10113 gl_shaders = shader_data->gl_shaders.cs;
10115 TRACE("Compiling compute shader %p.\n", shader);
10117 string_buffer_clear(buffer);
10118 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
10119 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10121 program_id = GL_EXTCALL(glCreateProgram());
10122 TRACE("Created new GLSL shader program %u.\n", program_id);
10124 entry->id = program_id;
10125 entry->vs.id = 0;
10126 entry->hs.id = 0;
10127 entry->ds.id = 0;
10128 entry->gs.id = 0;
10129 entry->ps.id = 0;
10130 entry->cs.id = shader_id;
10131 entry->constant_version = 0;
10132 entry->shader_controlled_clip_distances = 0;
10133 entry->ps.np2_fixup_info = NULL;
10134 add_glsl_program_entry(priv, entry);
10136 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10137 GL_EXTCALL(glAttachShader(program_id, shader_id));
10138 checkGLcall("glAttachShader");
10140 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10142 TRACE("Linking GLSL shader program %u.\n", program_id);
10143 GL_EXTCALL(glLinkProgram(program_id));
10144 shader_glsl_validate_link(gl_info, program_id);
10146 GL_EXTCALL(glUseProgram(program_id));
10147 checkGLcall("glUseProgram");
10148 shader_glsl_load_program_resources(context, priv, program_id, shader);
10149 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10151 entry->constant_update_mask = 0;
10153 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10154 checkGLcall("glUseProgram");
10155 return WINED3D_OK;
10158 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
10159 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10161 struct glsl_shader_private *shader_data;
10163 if (!shader->backend_data)
10165 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10166 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
10168 ERR("Failed to compile compute shader %p.\n", shader);
10169 return 0;
10172 shader_data = shader->backend_data;
10173 return shader_data->gl_shaders.cs[0].id;
10176 /* Context activation is done by the caller. */
10177 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
10178 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10180 struct glsl_shader_prog_link *entry;
10181 struct wined3d_shader *shader;
10182 struct glsl_program_key key;
10183 GLuint cs_id;
10185 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10186 return;
10188 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10190 WARN("Compute shader is NULL.\n");
10191 ctx_data->glsl_program = NULL;
10192 return;
10195 cs_id = find_glsl_compute_shader(context, priv, shader);
10196 memset(&key, 0, sizeof(key));
10197 key.cs_id = cs_id;
10198 if (!(entry = get_glsl_program_entry(priv, &key)))
10199 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10200 ctx_data->glsl_program = entry;
10203 /* Context activation is done by the caller. */
10204 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
10205 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10207 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
10208 const struct wined3d_gl_info *gl_info = context->gl_info;
10209 const struct wined3d_shader *pre_rasterization_shader;
10210 const struct ps_np2fixup_info *np2fixup_info = NULL;
10211 struct wined3d_shader *hshader, *dshader, *gshader;
10212 struct glsl_shader_prog_link *entry = NULL;
10213 struct wined3d_shader *vshader = NULL;
10214 struct wined3d_shader *pshader = NULL;
10215 GLuint reorder_shader_id = 0;
10216 struct glsl_program_key key;
10217 GLuint program_id;
10218 unsigned int i;
10219 GLuint vs_id = 0;
10220 GLuint hs_id = 0;
10221 GLuint ds_id = 0;
10222 GLuint gs_id = 0;
10223 GLuint ps_id = 0;
10224 struct list *ps_list, *vs_list;
10225 WORD attribs_map;
10226 struct wined3d_string_buffer *tmp_name;
10228 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10230 vs_id = ctx_data->glsl_program->vs.id;
10231 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10233 if (use_vs(state))
10234 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10236 else if (use_vs(state))
10238 struct vs_compile_args vs_compile_args;
10240 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10242 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
10243 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
10244 vs_list = &vshader->linked_programs;
10246 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10248 struct glsl_ffp_vertex_shader *ffp_shader;
10249 struct wined3d_ffp_vs_settings settings;
10251 wined3d_ffp_get_vs_settings(context, state, &settings);
10252 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10253 vs_id = ffp_shader->id;
10254 vs_list = &ffp_shader->linked_programs;
10257 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10258 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10259 hs_id = ctx_data->glsl_program->hs.id;
10260 else if (hshader)
10261 hs_id = find_glsl_hull_shader(context, priv, hshader);
10263 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10264 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10266 ds_id = ctx_data->glsl_program->ds.id;
10268 else if (dshader)
10270 struct ds_compile_args args;
10272 find_ds_compile_args(state, dshader, &args, context);
10273 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
10276 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10277 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10279 gs_id = ctx_data->glsl_program->gs.id;
10281 else if (gshader)
10283 struct gs_compile_args args;
10285 find_gs_compile_args(state, gshader, &args, context);
10286 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
10289 /* A pixel shader is not used when rasterization is disabled. */
10290 if (is_rasterization_disabled(gshader))
10292 ps_id = 0;
10293 ps_list = NULL;
10295 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10297 ps_id = ctx_data->glsl_program->ps.id;
10298 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10300 if (use_ps(state))
10301 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10303 else if (use_ps(state))
10305 struct ps_compile_args ps_compile_args;
10306 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10307 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
10308 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
10309 pshader, &ps_compile_args, &np2fixup_info);
10310 ps_list = &pshader->linked_programs;
10312 else if (priv->fragment_pipe == &glsl_fragment_pipe
10313 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10315 struct glsl_ffp_fragment_shader *ffp_shader;
10316 struct ffp_frag_settings settings;
10318 gen_ffp_frag_op(context, state, &settings, FALSE);
10319 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
10320 ps_id = ffp_shader->id;
10321 ps_list = &ffp_shader->linked_programs;
10324 key.vs_id = vs_id;
10325 key.hs_id = hs_id;
10326 key.ds_id = ds_id;
10327 key.gs_id = gs_id;
10328 key.ps_id = ps_id;
10329 key.cs_id = 0;
10330 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10332 ctx_data->glsl_program = entry;
10333 return;
10336 /* If we get to this point, then no matching program exists, so we create one */
10337 program_id = GL_EXTCALL(glCreateProgram());
10338 TRACE("Created new GLSL shader program %u.\n", program_id);
10340 /* Create the entry */
10341 entry = heap_alloc(sizeof(*entry));
10342 entry->id = program_id;
10343 entry->vs.id = vs_id;
10344 entry->hs.id = hs_id;
10345 entry->ds.id = ds_id;
10346 entry->gs.id = gs_id;
10347 entry->ps.id = ps_id;
10348 entry->cs.id = 0;
10349 entry->constant_version = 0;
10350 entry->shader_controlled_clip_distances = 0;
10351 entry->ps.np2_fixup_info = np2fixup_info;
10352 /* Add the hash table entry */
10353 add_glsl_program_entry(priv, entry);
10355 /* Set the current program */
10356 ctx_data->glsl_program = entry;
10358 /* Attach GLSL vshader */
10359 if (vs_id)
10361 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10362 GL_EXTCALL(glAttachShader(program_id, vs_id));
10363 checkGLcall("glAttachShader");
10365 list_add_head(vs_list, &entry->vs.shader_entry);
10368 if (vshader)
10370 attribs_map = vshader->reg_maps.input_registers;
10371 if (vshader->reg_maps.shader_version.major < 4)
10373 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10374 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10375 d3d_info->emulated_flatshading
10376 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10377 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10378 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10379 checkGLcall("glAttachShader");
10380 /* Flag the reorder function for deletion, it will be freed
10381 * automatically when the program is destroyed. */
10382 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10385 else
10387 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10390 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10392 /* Bind vertex attributes to a corresponding index number to match
10393 * the same index numbers as ARB_vertex_programs (makes loading
10394 * vertex attributes simpler). With this method, we can use the
10395 * exact same code to load the attributes later for both ARB and
10396 * GLSL shaders.
10398 * We have to do this here because we need to know the Program ID
10399 * in order to make the bindings work, and it has to be done prior
10400 * to linking the GLSL program. */
10401 tmp_name = string_buffer_get(&priv->string_buffers);
10402 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10404 if (!(attribs_map & 1))
10405 continue;
10407 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10408 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10409 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10411 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10412 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10413 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10414 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10417 checkGLcall("glBindAttribLocation");
10418 string_buffer_release(&priv->string_buffers, tmp_name);
10420 if (!needs_legacy_glsl_syntax(gl_info))
10422 GL_EXTCALL(glBindFragDataLocation(program_id, 0, "ps_out"));
10423 checkGLcall("glBindFragDataLocation");
10427 if (hshader)
10429 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10430 GL_EXTCALL(glAttachShader(program_id, hs_id));
10431 checkGLcall("glAttachShader");
10433 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10436 if (dshader)
10438 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10439 GL_EXTCALL(glAttachShader(program_id, ds_id));
10440 checkGLcall("glAttachShader");
10442 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10445 if (gshader)
10447 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10448 GL_EXTCALL(glAttachShader(program_id, gs_id));
10449 checkGLcall("glAttachShader");
10451 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10453 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10456 /* Attach GLSL pshader */
10457 if (ps_id)
10459 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10460 GL_EXTCALL(glAttachShader(program_id, ps_id));
10461 checkGLcall("glAttachShader");
10463 list_add_head(ps_list, &entry->ps.shader_entry);
10466 /* Link the program */
10467 TRACE("Linking GLSL shader program %u.\n", program_id);
10468 GL_EXTCALL(glLinkProgram(program_id));
10469 shader_glsl_validate_link(gl_info, program_id);
10471 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10472 vshader ? vshader->limits->constant_float : 0);
10473 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10474 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10475 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10476 pshader ? pshader->limits->constant_float : 0);
10477 checkGLcall("find glsl program uniform locations");
10479 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10480 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10482 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10483 entry->shader_controlled_clip_distances = 1;
10484 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10487 if (needs_legacy_glsl_syntax(gl_info))
10489 if (pshader && pshader->reg_maps.shader_version.major >= 3
10490 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10492 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10493 entry->vs.vertex_color_clamp = GL_FALSE;
10495 else
10497 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10500 else
10502 /* With core profile we never change vertex_color_clamp from
10503 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10504 * glClampColorARB(). */
10505 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10508 /* Set the shader to allow uniform loading on it */
10509 GL_EXTCALL(glUseProgram(program_id));
10510 checkGLcall("glUseProgram");
10512 entry->constant_update_mask = 0;
10513 if (vshader)
10515 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10516 if (vshader->reg_maps.integer_constants)
10517 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10518 if (vshader->reg_maps.boolean_constants)
10519 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10520 if (entry->vs.pos_fixup_location != -1)
10521 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10523 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10525 else
10527 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10528 | WINED3D_SHADER_CONST_FFP_PROJ;
10530 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10532 if (entry->vs.modelview_matrix_location[i] != -1)
10534 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10535 break;
10539 for (i = 0; i < MAX_TEXTURES; ++i)
10541 if (entry->vs.texture_matrix_location[i] != -1)
10543 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10544 break;
10547 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10548 || entry->vs.material_specular_location != -1
10549 || entry->vs.material_emissive_location != -1
10550 || entry->vs.material_shininess_location != -1)
10551 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10552 if (entry->vs.light_ambient_location != -1)
10553 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10555 if (entry->vs.clip_planes_location != -1)
10556 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10557 if (entry->vs.pointsize_min_location != -1)
10558 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10560 if (hshader)
10561 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10563 if (dshader)
10565 if (entry->ds.pos_fixup_location != -1)
10566 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10568 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10571 if (gshader)
10573 if (entry->gs.pos_fixup_location != -1)
10574 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10576 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10579 if (ps_id)
10581 if (pshader)
10583 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10584 if (pshader->reg_maps.integer_constants)
10585 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10586 if (pshader->reg_maps.boolean_constants)
10587 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10588 if (entry->ps.ycorrection_location != -1)
10589 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10591 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10592 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10594 else
10596 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10598 shader_glsl_load_samplers(context, priv, program_id, NULL);
10601 for (i = 0; i < MAX_TEXTURES; ++i)
10603 if (entry->ps.bumpenv_mat_location[i] != -1)
10605 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10606 break;
10610 if (entry->ps.fog_color_location != -1)
10611 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10612 if (entry->ps.alpha_test_ref_location != -1)
10613 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10614 if (entry->ps.np2_fixup_location != -1)
10615 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10616 if (entry->ps.color_key_location != -1)
10617 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10621 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10623 struct wined3d_device *device = shader->device;
10624 struct wined3d_context *context;
10626 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10628 context = context_acquire(device, NULL, 0);
10629 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10630 context_release(context);
10634 /* Context activation is done by the caller. */
10635 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10636 const struct wined3d_state *state)
10638 struct glsl_context_data *ctx_data = context->shader_backend_data;
10639 const struct wined3d_gl_info *gl_info = context->gl_info;
10640 struct shader_glsl_priv *priv = shader_priv;
10641 struct glsl_shader_prog_link *glsl_program;
10642 GLenum current_vertex_color_clamp;
10643 GLuint program_id, prev_id;
10645 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10646 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10648 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10649 set_glsl_shader_program(context, state, priv, ctx_data);
10650 glsl_program = ctx_data->glsl_program;
10652 if (glsl_program)
10654 program_id = glsl_program->id;
10655 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10656 if (glsl_program->shader_controlled_clip_distances)
10657 context_enable_clip_distances(context, glsl_program->clip_distance_mask);
10659 else
10661 program_id = 0;
10662 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10665 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10667 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10668 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10670 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10671 checkGLcall("glClampColorARB");
10673 else
10675 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10679 TRACE("Using GLSL program %u.\n", program_id);
10681 if (prev_id != program_id)
10683 GL_EXTCALL(glUseProgram(program_id));
10684 checkGLcall("glUseProgram");
10686 if (glsl_program)
10687 context->constant_update_mask |= glsl_program->constant_update_mask;
10690 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10693 /* Context activation is done by the caller. */
10694 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10695 const struct wined3d_state *state)
10697 struct glsl_context_data *ctx_data = context->shader_backend_data;
10698 const struct wined3d_gl_info *gl_info = context->gl_info;
10699 struct shader_glsl_priv *priv = shader_priv;
10700 GLuint program_id, prev_id;
10702 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10703 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10704 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10706 TRACE("Using GLSL program %u.\n", program_id);
10708 if (prev_id != program_id)
10710 GL_EXTCALL(glUseProgram(program_id));
10711 checkGLcall("glUseProgram");
10714 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10715 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10716 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10717 | (1u << WINED3D_SHADER_TYPE_HULL)
10718 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10721 /* "context" is not necessarily the currently active context. */
10722 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10724 struct glsl_context_data *ctx_data = context->shader_backend_data;
10726 ctx_data->glsl_program = NULL;
10727 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10728 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10729 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10730 | (1u << WINED3D_SHADER_TYPE_HULL)
10731 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10732 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10735 /* Context activation is done by the caller. */
10736 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10738 struct glsl_context_data *ctx_data = context->shader_backend_data;
10739 const struct wined3d_gl_info *gl_info = context->gl_info;
10740 struct shader_glsl_priv *priv = shader_priv;
10742 shader_glsl_invalidate_current_program(context);
10743 GL_EXTCALL(glUseProgram(0));
10744 checkGLcall("glUseProgram");
10746 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10747 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10749 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10751 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10752 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10753 checkGLcall("glClampColorARB");
10757 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10758 const struct glsl_shader_prog_link *program)
10760 const struct glsl_context_data *ctx_data;
10761 struct wined3d_context *context;
10762 unsigned int i;
10764 for (i = 0; i < device->context_count; ++i)
10766 context = device->contexts[i];
10767 ctx_data = context->shader_backend_data;
10769 if (ctx_data->glsl_program == program)
10770 shader_glsl_invalidate_current_program(context);
10774 static void shader_glsl_destroy(struct wined3d_shader *shader)
10776 struct glsl_shader_private *shader_data = shader->backend_data;
10777 struct wined3d_device *device = shader->device;
10778 struct shader_glsl_priv *priv = device->shader_priv;
10779 const struct wined3d_gl_info *gl_info;
10780 const struct list *linked_programs;
10781 struct wined3d_context *context;
10783 if (!shader_data || !shader_data->num_gl_shaders)
10785 heap_free(shader_data);
10786 shader->backend_data = NULL;
10787 return;
10790 context = context_acquire(device, NULL, 0);
10791 gl_info = context->gl_info;
10793 TRACE("Deleting linked programs.\n");
10794 linked_programs = &shader->linked_programs;
10795 if (linked_programs->next)
10797 struct glsl_shader_prog_link *entry, *entry2;
10798 UINT i;
10800 switch (shader->reg_maps.shader_version.type)
10802 case WINED3D_SHADER_TYPE_PIXEL:
10804 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10806 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10808 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10809 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10810 checkGLcall("glDeleteShader");
10812 heap_free(shader_data->gl_shaders.ps);
10814 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10815 struct glsl_shader_prog_link, ps.shader_entry)
10817 shader_glsl_invalidate_contexts_program(device, entry);
10818 delete_glsl_program_entry(priv, gl_info, entry);
10821 break;
10824 case WINED3D_SHADER_TYPE_VERTEX:
10826 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10828 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10830 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10831 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10832 checkGLcall("glDeleteShader");
10834 heap_free(shader_data->gl_shaders.vs);
10836 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10837 struct glsl_shader_prog_link, vs.shader_entry)
10839 shader_glsl_invalidate_contexts_program(device, entry);
10840 delete_glsl_program_entry(priv, gl_info, entry);
10843 break;
10846 case WINED3D_SHADER_TYPE_HULL:
10848 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10850 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10852 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10853 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10854 checkGLcall("glDeleteShader");
10856 heap_free(shader_data->gl_shaders.hs);
10858 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10859 struct glsl_shader_prog_link, hs.shader_entry)
10861 shader_glsl_invalidate_contexts_program(device, entry);
10862 delete_glsl_program_entry(priv, gl_info, entry);
10865 break;
10868 case WINED3D_SHADER_TYPE_DOMAIN:
10870 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10872 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10874 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10875 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10876 checkGLcall("glDeleteShader");
10878 heap_free(shader_data->gl_shaders.ds);
10880 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10881 struct glsl_shader_prog_link, ds.shader_entry)
10883 shader_glsl_invalidate_contexts_program(device, entry);
10884 delete_glsl_program_entry(priv, gl_info, entry);
10887 break;
10890 case WINED3D_SHADER_TYPE_GEOMETRY:
10892 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10894 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10896 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10897 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10898 checkGLcall("glDeleteShader");
10900 heap_free(shader_data->gl_shaders.gs);
10902 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10903 struct glsl_shader_prog_link, gs.shader_entry)
10905 shader_glsl_invalidate_contexts_program(device, entry);
10906 delete_glsl_program_entry(priv, gl_info, entry);
10909 break;
10912 case WINED3D_SHADER_TYPE_COMPUTE:
10914 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10916 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10918 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10919 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10920 checkGLcall("glDeleteShader");
10922 heap_free(shader_data->gl_shaders.cs);
10924 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10925 struct glsl_shader_prog_link, cs.shader_entry)
10927 shader_glsl_invalidate_contexts_program(device, entry);
10928 delete_glsl_program_entry(priv, gl_info, entry);
10931 break;
10934 default:
10935 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10936 break;
10940 heap_free(shader->backend_data);
10941 shader->backend_data = NULL;
10943 context_release(context);
10946 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10948 const struct glsl_program_key *k = key;
10949 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10950 const struct glsl_shader_prog_link, program_lookup_entry);
10952 if (k->vs_id > prog->vs.id) return 1;
10953 else if (k->vs_id < prog->vs.id) return -1;
10955 if (k->gs_id > prog->gs.id) return 1;
10956 else if (k->gs_id < prog->gs.id) return -1;
10958 if (k->ps_id > prog->ps.id) return 1;
10959 else if (k->ps_id < prog->ps.id) return -1;
10961 if (k->hs_id > prog->hs.id) return 1;
10962 else if (k->hs_id < prog->hs.id) return -1;
10964 if (k->ds_id > prog->ds.id) return 1;
10965 else if (k->ds_id < prog->ds.id) return -1;
10967 if (k->cs_id > prog->cs.id) return 1;
10968 else if (k->cs_id < prog->cs.id) return -1;
10970 return 0;
10973 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10975 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10976 + constant_count * sizeof(*heap->contained)
10977 + constant_count * sizeof(*heap->positions);
10978 void *mem;
10980 if (!(mem = heap_alloc(size)))
10982 ERR("Failed to allocate memory\n");
10983 return FALSE;
10986 heap->entries = mem;
10987 heap->entries[1].version = 0;
10988 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10989 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10990 heap->positions = (unsigned int *)(heap->contained + constant_count);
10991 heap->size = 1;
10993 return TRUE;
10996 static void constant_heap_free(struct constant_heap *heap)
10998 heap_free(heap->entries);
11001 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11002 const struct fragment_pipeline *fragment_pipe)
11004 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11005 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
11006 struct fragment_caps fragment_caps;
11007 void *vertex_priv, *fragment_priv;
11008 struct shader_glsl_priv *priv;
11010 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11011 return E_OUTOFMEMORY;
11013 string_buffer_list_init(&priv->string_buffers);
11015 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11017 ERR("Failed to initialize vertex pipe.\n");
11018 heap_free(priv);
11019 return E_FAIL;
11022 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11024 ERR("Failed to initialize fragment pipe.\n");
11025 vertex_pipe->vp_free(device);
11026 heap_free(priv);
11027 return E_FAIL;
11030 if (!string_buffer_init(&priv->shader_buffer))
11032 ERR("Failed to initialize shader buffer.\n");
11033 goto fail;
11036 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11038 ERR("Failed to allocate memory.\n");
11039 goto fail;
11042 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11044 ERR("Failed to initialize vertex shader constant heap\n");
11045 goto fail;
11048 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11050 ERR("Failed to initialize pixel shader constant heap\n");
11051 goto fail;
11054 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11056 priv->next_constant_version = 1;
11057 priv->vertex_pipe = vertex_pipe;
11058 priv->fragment_pipe = fragment_pipe;
11059 fragment_pipe->get_caps(gl_info, &fragment_caps);
11060 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11061 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11063 device->vertex_priv = vertex_priv;
11064 device->fragment_priv = fragment_priv;
11065 device->shader_priv = priv;
11067 return WINED3D_OK;
11069 fail:
11070 constant_heap_free(&priv->pconst_heap);
11071 constant_heap_free(&priv->vconst_heap);
11072 heap_free(priv->stack);
11073 string_buffer_free(&priv->shader_buffer);
11074 fragment_pipe->free_private(device);
11075 vertex_pipe->vp_free(device);
11076 heap_free(priv);
11077 return E_OUTOFMEMORY;
11080 /* Context activation is done by the caller. */
11081 static void shader_glsl_free(struct wined3d_device *device)
11083 struct shader_glsl_priv *priv = device->shader_priv;
11085 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11086 constant_heap_free(&priv->pconst_heap);
11087 constant_heap_free(&priv->vconst_heap);
11088 heap_free(priv->stack);
11089 string_buffer_list_cleanup(&priv->string_buffers);
11090 string_buffer_free(&priv->shader_buffer);
11091 priv->fragment_pipe->free_private(device);
11092 priv->vertex_pipe->vp_free(device);
11094 heap_free(device->shader_priv);
11095 device->shader_priv = NULL;
11098 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11100 struct glsl_context_data *ctx_data;
11102 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11103 return FALSE;
11104 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11105 context->shader_backend_data = ctx_data;
11106 return TRUE;
11109 static void shader_glsl_free_context_data(struct wined3d_context *context)
11111 heap_free(context->shader_backend_data);
11114 static void shader_glsl_init_context_state(struct wined3d_context *context)
11116 const struct wined3d_gl_info *gl_info = context->gl_info;
11118 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11119 checkGLcall("GL_PROGRAM_POINT_SIZE");
11122 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11124 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11125 && gl_info->supported[WINED3D_GL_VERSION_3_2]
11126 && gl_info->supported[ARB_SAMPLER_OBJECTS]
11127 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11128 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11130 if (shader_model_4
11131 && gl_info->supported[ARB_COMPUTE_SHADER]
11132 && gl_info->supported[ARB_CULL_DISTANCE]
11133 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11134 && gl_info->supported[ARB_DRAW_INDIRECT]
11135 && gl_info->supported[ARB_GPU_SHADER5]
11136 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11137 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11138 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11139 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11140 && gl_info->supported[ARB_TESSELLATION_SHADER]
11141 && gl_info->supported[ARB_TEXTURE_COMPRESSION_BPTC]
11142 && gl_info->supported[ARB_TEXTURE_GATHER]
11143 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11144 return 5;
11146 if (shader_model_4)
11147 return 4;
11149 /* Support for texldd and texldl instructions in pixel shaders is required
11150 * for SM3. */
11151 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11152 return 3;
11154 return 2;
11157 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
11159 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11161 TRACE("Shader model %u.\n", shader_model);
11163 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11164 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11165 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11166 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11167 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11168 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11170 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11171 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11173 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11174 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11175 caps->varying_count = gl_info->limits.glsl_varyings;
11177 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11178 * Direct3D minimum requirement.
11180 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11181 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11183 * The problem is that the refrast clamps temporary results in the shader to
11184 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11185 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11186 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11187 * offer a way to query this.
11189 if (shader_model >= 4)
11190 caps->ps_1x_max_value = FLT_MAX;
11191 else
11192 caps->ps_1x_max_value = 1024.0f;
11194 /* Ideally we'd only set caps like sRGB writes here if supported by both
11195 * the shader backend and the fragment pipe, but we can get called before
11196 * shader_glsl_alloc(). */
11197 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11198 | WINED3D_SHADER_CAP_SRGB_WRITE;
11201 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11203 /* We support everything except YUV conversions. */
11204 return !is_complex_fixup(fixup);
11207 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11209 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11210 /* WINED3DSIH_ADD */ shader_glsl_binop,
11211 /* WINED3DSIH_AND */ shader_glsl_binop,
11212 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11213 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11214 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11215 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11216 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11217 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11218 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11219 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11220 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11221 /* WINED3DSIH_BEM */ shader_glsl_bem,
11222 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11223 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11224 /* WINED3DSIH_BREAK */ shader_glsl_break,
11225 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11226 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11227 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11228 /* WINED3DSIH_CALL */ shader_glsl_call,
11229 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11230 /* WINED3DSIH_CASE */ shader_glsl_case,
11231 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11232 /* WINED3DSIH_CND */ shader_glsl_cnd,
11233 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11234 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11235 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11236 /* WINED3DSIH_CRS */ shader_glsl_cross,
11237 /* WINED3DSIH_CUT */ shader_glsl_cut,
11238 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11239 /* WINED3DSIH_DCL */ shader_glsl_nop,
11240 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11241 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11242 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11243 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11244 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11245 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11246 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11247 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11248 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11249 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11250 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11251 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11252 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11253 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11254 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11255 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
11256 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
11257 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11258 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11259 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11260 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11261 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11262 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11263 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11264 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11265 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11266 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11267 /* WINED3DSIH_DCL_STREAM */ NULL,
11268 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11269 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11270 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11271 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11272 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11273 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11274 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11275 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11276 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11277 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11278 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11279 /* WINED3DSIH_DEF */ shader_glsl_nop,
11280 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11281 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11282 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11283 /* WINED3DSIH_DIV */ shader_glsl_binop,
11284 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11285 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11286 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11287 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11288 /* WINED3DSIH_DST */ shader_glsl_dst,
11289 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11290 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11291 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11292 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11293 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11294 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11295 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ NULL,
11296 /* WINED3DSIH_ELSE */ shader_glsl_else,
11297 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11298 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11299 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11300 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11301 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11302 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11303 /* WINED3DSIH_EQ */ shader_glsl_relop,
11304 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11305 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11306 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11307 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11308 /* WINED3DSIH_FCALL */ NULL,
11309 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11310 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11311 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11312 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11313 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11314 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11315 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11316 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11317 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11318 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11319 /* WINED3DSIH_GE */ shader_glsl_relop,
11320 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11321 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11322 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11323 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11324 /* WINED3DSIH_IADD */ shader_glsl_binop,
11325 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11326 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11327 /* WINED3DSIH_IF */ shader_glsl_if,
11328 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11329 /* WINED3DSIH_IGE */ shader_glsl_relop,
11330 /* WINED3DSIH_ILT */ shader_glsl_relop,
11331 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11332 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11333 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11334 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11335 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11336 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11337 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11338 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11339 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11340 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11341 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11342 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11343 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11344 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11345 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11346 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11347 /* WINED3DSIH_INE */ shader_glsl_relop,
11348 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11349 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11350 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11351 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11352 /* WINED3DSIH_LABEL */ shader_glsl_label,
11353 /* WINED3DSIH_LD */ shader_glsl_ld,
11354 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11355 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11356 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11357 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11358 /* WINED3DSIH_LIT */ shader_glsl_lit,
11359 /* WINED3DSIH_LOD */ NULL,
11360 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11361 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11362 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11363 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11364 /* WINED3DSIH_LT */ shader_glsl_relop,
11365 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11366 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11367 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11368 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11369 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11370 /* WINED3DSIH_MAD */ shader_glsl_mad,
11371 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11372 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11373 /* WINED3DSIH_MOV */ shader_glsl_mov,
11374 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11375 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11376 /* WINED3DSIH_MUL */ shader_glsl_binop,
11377 /* WINED3DSIH_NE */ shader_glsl_relop,
11378 /* WINED3DSIH_NOP */ shader_glsl_nop,
11379 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11380 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11381 /* WINED3DSIH_OR */ shader_glsl_binop,
11382 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11383 /* WINED3DSIH_POW */ shader_glsl_pow,
11384 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11385 /* WINED3DSIH_REP */ shader_glsl_rep,
11386 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11387 /* WINED3DSIH_RET */ shader_glsl_ret,
11388 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11389 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11390 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11391 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11392 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11393 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11394 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11395 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11396 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11397 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11398 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11399 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11400 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11401 /* WINED3DSIH_SAMPLE_POS */ NULL,
11402 /* WINED3DSIH_SETP */ NULL,
11403 /* WINED3DSIH_SGE */ shader_glsl_compare,
11404 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11405 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11406 /* WINED3DSIH_SLT */ shader_glsl_compare,
11407 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11408 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11409 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11410 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11411 /* WINED3DSIH_SUB */ shader_glsl_binop,
11412 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11413 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11414 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11415 /* WINED3DSIH_TEX */ shader_glsl_tex,
11416 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11417 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11418 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11419 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11420 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11421 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11422 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11423 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11424 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11425 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11426 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11427 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11428 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11429 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11430 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11431 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11432 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11433 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11434 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11435 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11436 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11437 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11438 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11439 /* WINED3DSIH_UGE */ shader_glsl_relop,
11440 /* WINED3DSIH_ULT */ shader_glsl_relop,
11441 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11442 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11443 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11444 /* WINED3DSIH_USHR */ shader_glsl_binop,
11445 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11446 /* WINED3DSIH_XOR */ shader_glsl_binop,
11449 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11450 SHADER_HANDLER hw_fct;
11452 /* Select handler */
11453 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11455 /* Unhandled opcode */
11456 if (!hw_fct)
11458 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11459 return;
11461 hw_fct(ins);
11463 shader_glsl_add_instruction_modifiers(ins);
11466 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11468 struct shader_glsl_priv *priv = shader_priv;
11470 return priv->ffp_proj_control;
11473 const struct wined3d_shader_backend_ops glsl_shader_backend =
11475 shader_glsl_handle_instruction,
11476 shader_glsl_precompile,
11477 shader_glsl_select,
11478 shader_glsl_select_compute,
11479 shader_glsl_disable,
11480 shader_glsl_update_float_vertex_constants,
11481 shader_glsl_update_float_pixel_constants,
11482 shader_glsl_load_constants,
11483 shader_glsl_destroy,
11484 shader_glsl_alloc,
11485 shader_glsl_free,
11486 shader_glsl_allocate_context_data,
11487 shader_glsl_free_context_data,
11488 shader_glsl_init_context_state,
11489 shader_glsl_get_caps,
11490 shader_glsl_color_fixup_supported,
11491 shader_glsl_has_ffp_proj_control,
11494 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11496 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11498 caps->xyzrhw = TRUE;
11499 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11500 caps->ffp_generic_attributes = TRUE;
11501 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11502 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11503 caps->max_vertex_blend_matrix_index = 0;
11504 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11505 | WINED3DVTXPCAPS_MATERIALSOURCE7
11506 | WINED3DVTXPCAPS_VERTEXFOG
11507 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11508 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11509 | WINED3DVTXPCAPS_LOCALVIEWER
11510 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11511 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11512 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11513 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11516 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11518 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11519 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11520 return 0;
11523 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11525 struct shader_glsl_priv *priv;
11527 if (shader_backend == &glsl_shader_backend)
11529 priv = shader_priv;
11530 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11531 return priv;
11534 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11536 return NULL;
11539 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11541 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11542 struct glsl_ffp_vertex_shader, desc.entry);
11543 struct glsl_shader_prog_link *program, *program2;
11544 struct glsl_ffp_destroy_ctx *ctx = context;
11546 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11547 struct glsl_shader_prog_link, vs.shader_entry)
11549 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11551 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11552 heap_free(shader);
11555 /* Context activation is done by the caller. */
11556 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11558 struct shader_glsl_priv *priv = device->vertex_priv;
11559 struct glsl_ffp_destroy_ctx ctx;
11561 ctx.priv = priv;
11562 ctx.gl_info = &device->adapter->gl_info;
11563 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11566 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11567 const struct wined3d_state *state, DWORD state_id) {}
11569 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11570 const struct wined3d_state *state, DWORD state_id)
11572 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11575 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11576 const struct wined3d_state *state, DWORD state_id)
11578 const struct wined3d_gl_info *gl_info = context->gl_info;
11579 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11580 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11581 BOOL transformed = context->stream_info.position_transformed;
11582 BOOL wasrhw = context->last_was_rhw;
11583 unsigned int i;
11585 context->last_was_rhw = transformed;
11587 /* If the vertex declaration contains a transformed position attribute,
11588 * the draw uses the fixed function vertex pipeline regardless of any
11589 * vertex shader set by the application. */
11590 if (transformed != wasrhw
11591 || context->stream_info.swizzle_map != context->last_swizzle_map)
11592 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11594 context->last_swizzle_map = context->stream_info.swizzle_map;
11596 if (!use_vs(state))
11598 if (context->last_was_vshader)
11600 if (legacy_clip_planes)
11601 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11602 clipplane(context, state, STATE_CLIPPLANE(i));
11603 else
11604 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11607 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11609 /* Because of settings->texcoords, we have to regenerate the vertex
11610 * shader on a vdecl change if there aren't enough varyings to just
11611 * always output all the texture coordinates. */
11612 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11613 || normal != context->last_was_normal)
11614 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11616 if (use_ps(state)
11617 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11618 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11619 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11621 else
11623 if (!context->last_was_vshader)
11625 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11626 if (legacy_clip_planes)
11627 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11628 clipplane(context, state, STATE_CLIPPLANE(i));
11629 else
11630 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11634 context->last_was_vshader = use_vs(state);
11635 context->last_was_normal = normal;
11638 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11639 const struct wined3d_state *state, DWORD state_id)
11641 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11642 /* Different vertex shaders potentially require a different vertex attributes setup. */
11643 if (!isStateDirty(context, STATE_VDECL))
11644 context_apply_state(context, state, STATE_VDECL);
11647 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11648 const struct wined3d_state *state, DWORD state_id)
11650 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11651 * winding) are defined in Hull Shaders, while in GLSL those are
11652 * specified in Tessellation Evaluation Shaders. */
11653 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11655 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11656 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11659 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11660 const struct wined3d_state *state, DWORD state_id)
11662 struct glsl_context_data *ctx_data = context->shader_backend_data;
11663 BOOL rasterization_disabled;
11665 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11666 if (ctx_data->rasterization_disabled != rasterization_disabled)
11667 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11668 ctx_data->rasterization_disabled = rasterization_disabled;
11670 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11671 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11672 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11673 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11674 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11677 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11678 const struct wined3d_state *state, DWORD state_id)
11680 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11681 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11682 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11683 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11684 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11685 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11686 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11689 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11690 const struct wined3d_state *state, DWORD state_id)
11692 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11695 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11696 const struct wined3d_state *state, DWORD state_id)
11698 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11701 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11703 const struct wined3d_gl_info *gl_info = context->gl_info;
11704 unsigned int k;
11706 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11707 | WINED3D_SHADER_CONST_FFP_LIGHTS
11708 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11710 if (needs_legacy_glsl_syntax(gl_info))
11712 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11714 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11715 clipplane(context, state, STATE_CLIPPLANE(k));
11718 else
11720 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11724 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11725 const struct wined3d_state *state, DWORD state_id)
11727 /* Table fog behavior depends on the projection matrix. */
11728 if (state->render_states[WINED3D_RS_FOGENABLE]
11729 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11730 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11731 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11734 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11735 const struct wined3d_state *state, DWORD state_id)
11737 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11738 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11739 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11740 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11741 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11742 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11745 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11746 const struct wined3d_state *state, DWORD state_id)
11748 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11751 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11752 const struct wined3d_state *state, DWORD state_id)
11754 DWORD sampler = state_id - STATE_SAMPLER(0);
11755 const struct wined3d_texture *texture = state->textures[sampler];
11756 BOOL np2;
11758 if (!texture)
11759 return;
11761 if (sampler >= MAX_TEXTURES)
11762 return;
11764 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11765 || context->lastWasPow2Texture & (1u << sampler))
11767 if (np2)
11768 context->lastWasPow2Texture |= 1u << sampler;
11769 else
11770 context->lastWasPow2Texture &= ~(1u << sampler);
11772 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11776 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11777 const struct wined3d_state *state, DWORD state_id)
11779 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11782 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11783 const struct wined3d_state *state, DWORD state_id)
11785 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11788 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11789 const struct wined3d_state *state, DWORD state_id)
11791 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11794 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11795 const struct wined3d_state *state, DWORD state_id)
11797 if (!use_vs(state))
11798 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11801 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11802 const struct wined3d_state *state, DWORD state_id)
11804 static unsigned int once;
11806 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11807 FIXME("Non-point sprite points not supported in core profile.\n");
11810 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11811 const struct wined3d_state *state, DWORD state_id)
11813 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11816 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11817 const struct wined3d_state *state, DWORD state_id)
11819 const struct wined3d_gl_info *gl_info = context->gl_info;
11820 UINT index = state_id - STATE_CLIPPLANE(0);
11822 if (index >= gl_info->limits.user_clip_distances)
11823 return;
11825 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11828 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11830 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11831 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11832 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11833 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11834 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11835 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11836 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11837 /* Clip planes */
11838 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11839 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11840 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11841 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11842 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11843 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11844 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11845 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11846 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11847 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11848 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11849 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11850 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11851 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11852 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11853 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11854 /* Lights */
11855 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11856 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11857 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11858 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11859 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11860 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11861 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11862 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11863 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11864 /* Viewport */
11865 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11866 /* Transform states */
11867 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11868 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11869 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11870 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11871 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11872 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11873 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11874 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11875 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11876 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11877 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11878 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11879 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11880 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11881 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11882 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11883 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11884 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11885 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11886 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11887 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11888 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11889 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11890 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11891 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11892 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11893 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11894 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11895 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11896 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11897 /* Fog */
11898 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11899 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11900 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11901 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11902 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11903 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11904 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11905 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11906 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11907 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11908 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11909 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11910 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11911 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11912 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11913 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11914 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11915 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11916 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11917 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11918 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11919 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11920 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11921 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11922 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11923 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11924 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11925 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11926 /* NP2 texture matrix fixups. They are not needed if
11927 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11928 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11929 * matrix. */
11930 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11931 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11932 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11933 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11934 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11935 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11936 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11937 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11938 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11939 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11940 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11941 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11942 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11943 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11944 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11945 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11946 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11947 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11948 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11949 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11950 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11951 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11952 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11953 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11954 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11955 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11956 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11957 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11960 /* TODO:
11961 * - Implement vertex tweening. */
11962 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11964 glsl_vertex_pipe_vp_enable,
11965 glsl_vertex_pipe_vp_get_caps,
11966 glsl_vertex_pipe_vp_get_emul_mask,
11967 glsl_vertex_pipe_vp_alloc,
11968 glsl_vertex_pipe_vp_free,
11969 glsl_vertex_pipe_vp_states,
11972 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
11974 /* Nothing to do. */
11977 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
11979 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11980 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11981 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11982 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11983 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11984 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11985 | WINED3DTEXOPCAPS_SELECTARG1
11986 | WINED3DTEXOPCAPS_SELECTARG2
11987 | WINED3DTEXOPCAPS_MODULATE4X
11988 | WINED3DTEXOPCAPS_MODULATE2X
11989 | WINED3DTEXOPCAPS_MODULATE
11990 | WINED3DTEXOPCAPS_ADDSIGNED2X
11991 | WINED3DTEXOPCAPS_ADDSIGNED
11992 | WINED3DTEXOPCAPS_ADD
11993 | WINED3DTEXOPCAPS_SUBTRACT
11994 | WINED3DTEXOPCAPS_ADDSMOOTH
11995 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11996 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11997 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11998 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11999 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12000 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12001 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12002 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12003 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12004 | WINED3DTEXOPCAPS_DOTPRODUCT3
12005 | WINED3DTEXOPCAPS_MULTIPLYADD
12006 | WINED3DTEXOPCAPS_LERP
12007 | WINED3DTEXOPCAPS_BUMPENVMAP
12008 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12009 caps->MaxTextureBlendStages = MAX_TEXTURES;
12010 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
12013 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12015 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12016 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12017 return 0;
12020 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12022 struct shader_glsl_priv *priv;
12024 if (shader_backend == &glsl_shader_backend)
12026 priv = shader_priv;
12027 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12028 return priv;
12031 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12033 return NULL;
12036 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
12038 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12039 struct glsl_ffp_fragment_shader, entry.entry);
12040 struct glsl_shader_prog_link *program, *program2;
12041 struct glsl_ffp_destroy_ctx *ctx = context;
12043 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12044 struct glsl_shader_prog_link, ps.shader_entry)
12046 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
12048 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
12049 heap_free(shader);
12052 /* Context activation is done by the caller. */
12053 static void glsl_fragment_pipe_free(struct wined3d_device *device)
12055 struct shader_glsl_priv *priv = device->fragment_priv;
12056 struct glsl_ffp_destroy_ctx ctx;
12058 ctx.priv = priv;
12059 ctx.gl_info = &device->adapter->gl_info;
12060 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12063 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12064 const struct wined3d_state *state, DWORD state_id)
12066 context->last_was_pshader = use_ps(state);
12068 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12071 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12072 const struct wined3d_state *state, DWORD state_id)
12074 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12077 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12078 const struct wined3d_state *state, DWORD state_id)
12080 BOOL use_vshader = use_vs(state);
12081 enum fogsource new_source;
12082 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12083 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12085 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12087 if (!state->render_states[WINED3D_RS_FOGENABLE])
12088 return;
12090 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12092 if (use_vshader)
12093 new_source = FOGSOURCE_VS;
12094 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12095 new_source = FOGSOURCE_COORD;
12096 else
12097 new_source = FOGSOURCE_FFP;
12099 else
12101 new_source = FOGSOURCE_FFP;
12104 if (new_source != context->fog_source || fogstart == fogend)
12106 context->fog_source = new_source;
12107 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12111 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12112 const struct wined3d_state *state, DWORD state_id)
12114 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12115 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
12116 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12118 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12119 glsl_fragment_pipe_fog(context, state, state_id);
12122 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12123 const struct wined3d_state *state, DWORD state_id)
12125 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12126 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
12127 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12130 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12131 const struct wined3d_state *state, DWORD state_id)
12133 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12136 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12137 const struct wined3d_state *state, DWORD state_id)
12139 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12142 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12143 const struct wined3d_state *state, DWORD state_id)
12145 const struct wined3d_gl_info *gl_info = context->gl_info;
12146 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12147 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
12149 if (func)
12151 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12152 checkGLcall("glAlphaFunc");
12156 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12157 const struct wined3d_state *state, DWORD state_id)
12159 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12162 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12163 const struct wined3d_state *state, DWORD state_id)
12165 const struct wined3d_gl_info *gl_info = context->gl_info;
12167 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12169 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12170 checkGLcall("glEnable(GL_ALPHA_TEST)");
12172 else
12174 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12175 checkGLcall("glDisable(GL_ALPHA_TEST)");
12179 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12180 const struct wined3d_state *state, DWORD state_id)
12182 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12185 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12186 const struct wined3d_state *state, DWORD state_id)
12188 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12191 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12192 const struct wined3d_state *state, DWORD state_id)
12194 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12197 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
12199 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12200 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12201 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12202 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12203 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12204 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12205 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12206 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12207 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12208 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12209 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12210 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12211 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12212 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12213 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12214 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12215 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12216 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12217 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12218 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12219 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12220 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12221 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12222 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12223 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12224 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12225 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12226 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12227 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12228 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12229 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12230 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12231 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12232 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12233 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12234 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12235 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12236 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12237 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12238 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12239 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12240 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12241 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12242 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12243 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12244 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12245 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12246 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12247 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12248 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12249 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12250 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12251 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12252 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12253 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12254 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12255 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12256 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12257 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12258 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12259 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12260 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12261 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12262 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12263 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12264 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12265 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12266 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12267 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12268 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12269 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12270 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12271 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12272 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12273 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12274 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12275 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12276 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12277 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12278 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12279 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12280 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12281 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12282 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12283 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12284 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12285 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12286 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12287 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12288 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12289 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12290 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12291 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12292 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12293 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12294 {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 },
12295 {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 },
12296 {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 },
12297 {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 },
12298 {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 },
12299 {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 },
12300 {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 },
12301 {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 },
12302 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12303 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12304 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12305 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12306 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12307 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12308 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12309 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12310 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12311 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12312 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12313 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12314 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12317 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12319 return TRUE;
12322 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12326 const struct fragment_pipeline glsl_fragment_pipe =
12328 glsl_fragment_pipe_enable,
12329 glsl_fragment_pipe_get_caps,
12330 glsl_fragment_pipe_get_emul_mask,
12331 glsl_fragment_pipe_alloc,
12332 glsl_fragment_pipe_free,
12333 glsl_fragment_pipe_alloc_context_data,
12334 glsl_fragment_pipe_free_context_data,
12335 shader_glsl_color_fixup_supported,
12336 glsl_fragment_pipe_state_template,
12339 struct glsl_blitter_args
12341 GLenum texture_type;
12342 struct color_fixup_desc fixup;
12343 unsigned short padding;
12346 struct glsl_blitter_program
12348 struct wine_rb_entry entry;
12349 struct glsl_blitter_args args;
12350 GLuint id;
12353 struct wined3d_glsl_blitter
12355 struct wined3d_blitter blitter;
12356 struct wined3d_string_buffer_list string_buffers;
12357 struct wine_rb_tree programs;
12358 GLuint palette_texture;
12361 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12363 const struct glsl_blitter_args *a = key;
12364 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12366 return memcmp(a, b, sizeof(*a));
12369 /* Context activation is done by the caller. */
12370 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12372 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12373 struct wined3d_context *context = ctx;
12374 const struct wined3d_gl_info *gl_info = context->gl_info;
12376 GL_EXTCALL(glDeleteProgram(program->id));
12377 checkGLcall("glDeleteProgram()");
12378 heap_free(program);
12381 /* Context activation is done by the caller. */
12382 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12384 const struct wined3d_gl_info *gl_info = context->gl_info;
12385 struct wined3d_glsl_blitter *glsl_blitter;
12386 struct wined3d_blitter *next;
12388 if ((next = blitter->next))
12389 next->ops->blitter_destroy(next, context);
12391 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12393 if (glsl_blitter->palette_texture)
12394 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12396 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context);
12397 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12399 heap_free(glsl_blitter);
12402 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12403 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12404 const char *output, const char *tex_type, const char *swizzle)
12406 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12407 shader_addline(buffer, "\nvoid main()\n{\n");
12408 /* The alpha-component contains the palette index. */
12409 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12410 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12411 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12412 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12413 * the middle. */
12414 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12415 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12416 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12417 shader_addline(buffer, "}\n");
12420 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12421 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12422 const char *tex_type)
12424 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12425 char chroma, luminance;
12426 const char *tex;
12428 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12429 * macropixel, giving effectively 16 bits per pixel. The color consists of
12430 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12431 * luminance values, one for each single pixel it contains, and one U and
12432 * one V value shared between both pixels.
12434 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12435 * component contains the luminance and alpha the chroma. With UYVY it is
12436 * vice versa. Thus take the format into account when generating the read
12437 * swizzles
12439 * Reading the Y value is straightforward - just sample the texture. The
12440 * hardware takes care of filtering in the horizontal and vertical
12441 * direction.
12443 * Reading the U and V values is harder. We have to avoid filtering
12444 * horizontally, because that would mix the U and V values of one pixel or
12445 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12446 * get an unfiltered read, regardless of the filtering setting. Vertical
12447 * filtering works automatically though - the U and V values of two rows
12448 * are mixed nicely.
12450 * Apart of avoiding filtering issues, the code has to know which value it
12451 * just read, and where it can find the other one. To determine this, it
12452 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12453 * accordingly.
12455 * Handling horizontal filtering of U and V values requires reading a 2nd
12456 * pair of pixels, extracting U and V and mixing them. This is not
12457 * implemented yet.
12459 * An alternative implementation idea is to load the texture as A8R8G8B8
12460 * texture, with width / 2. This way one read gives all 3 values, finding
12461 * U and V is easy in an unfiltered situation. Finding the luminance on
12462 * the other hand requires finding out if it is an odd or even pixel. The
12463 * real drawback of this approach is filtering. This would have to be
12464 * emulated completely in the shader, reading up two 2 packed pixels in up
12465 * to 2 rows and interpolating both horizontally and vertically. Beyond
12466 * that it would require adjustments to the texture handling code to deal
12467 * with the width scaling. */
12469 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12471 chroma = 'x';
12472 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12474 else
12476 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12477 luminance = 'x';
12480 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12482 /* First we have to read the chroma values. This means we need at least
12483 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12484 * unmodified chroma, we have to rid ourselves of the filtering when we
12485 * sample the texture. */
12486 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12487 /* We must not allow filtering between pixel x and x+1, this would mix U
12488 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12489 * center is at 0.5, so add 0.5. */
12490 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12491 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12493 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12494 * and 0.75 for the even and odd pixels respectively. */
12495 /* Put the value into either of the chroma values. */
12496 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12497 shader_addline(buffer, " if (even)\n");
12498 shader_addline(buffer, " chroma.y = luminance;\n");
12499 shader_addline(buffer, " else\n");
12500 shader_addline(buffer, " chroma.x = luminance;\n");
12502 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12503 * current one. Otherwise, sample the left pixel. */
12504 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12505 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12507 /* Put the value into the other chroma. */
12508 shader_addline(buffer, " if (even)\n");
12509 shader_addline(buffer, " chroma.x = luminance;\n");
12510 shader_addline(buffer, " else\n");
12511 shader_addline(buffer, " chroma.y = luminance;\n");
12513 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12514 * the current one and lerp the two U and V values. */
12516 /* This gives the correctly filtered luminance value. */
12517 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12520 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12521 const struct wined3d_gl_info *gl_info, const char *tex_type)
12523 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12524 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12526 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12527 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12528 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12529 * have only half the pitch of the luminance plane, the packing into the
12530 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12531 * luminance data it looks approximately like this:
12533 * +----------------------------------+----
12534 * | |
12535 * | |
12536 * | |
12537 * | |
12538 * | | 2
12539 * | LUMINANCE | -
12540 * | | 3
12541 * | |
12542 * | |
12543 * | |
12544 * | |
12545 * +----------------+-----------------+----
12546 * | | |
12547 * | V even rows | V odd rows |
12548 * | | | 1
12549 * +----------------+------------------ -
12550 * | | | 3
12551 * | U even rows | U odd rows |
12552 * | | |
12553 * +----------------+-----------------+----
12554 * | | |
12555 * | 0.5 | 0.5 |
12557 * So it appears as if there are 4 chroma images, but in fact the odd rows
12558 * in the chroma images are in the same row as the even ones. So it is
12559 * kinda tricky to read. */
12561 /* First sample the chroma values. */
12562 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12563 /* The chroma planes have only half the width. */
12564 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12566 /* The first value is between 2/3 and 5/6 of the texture's height, so
12567 * scale+bias the coordinate. Also read the right side of the image when
12568 * reading odd lines.
12570 * Don't forget to clamp the y values in into the range, otherwise we'll
12571 * get filtering bleeding. */
12573 /* Read odd lines from the right side (add 0.5 to the x coordinate). */
12574 shader_addline(buffer, " if (fract(floor(texcoord.y * size.y) * 0.5 + 1.0 / 6.0) >= 0.5)\n");
12575 shader_addline(buffer, " texcoord.x += 0.5;\n");
12577 /* Clamp, keep the half pixel origin in mind. */
12578 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12579 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12581 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12583 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12584 * 6/6th No need to clamp because we're just reusing the already clamped
12585 * value from above. */
12586 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12587 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12589 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12590 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12591 * values from bleeding into the sampled luminance values due to
12592 * filtering. */
12593 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12594 /* Multiply the y coordinate by 2/3 and clamp it. */
12595 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12596 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12599 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12600 const struct wined3d_gl_info *gl_info, const char *tex_type)
12602 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12603 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12605 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12606 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12607 * effective bitdepth is 12 bits per pixel. If the whole texture is
12608 * interpreted as luminance data it looks approximately like this:
12610 * +----------------------------------+----
12611 * | |
12612 * | |
12613 * | |
12614 * | |
12615 * | | 2
12616 * | LUMINANCE | -
12617 * | | 3
12618 * | |
12619 * | |
12620 * | |
12621 * | |
12622 * +----------------------------------+----
12623 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12624 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12625 * | | 1
12626 * | | -
12627 * | | 3
12628 * | |
12629 * | |
12630 * +----------------------------------+---- */
12632 /* First sample the chroma values. */
12633 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12634 /* We only have half the number of chroma pixels. */
12635 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12636 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12638 /* We must not allow filtering horizontally, this would mix U and V.
12639 * Vertical filtering is ok. However, bear in mind that the pixel center
12640 * is at 0.5, so add 0.5. */
12642 /* Convert to non-normalised coordinates so we can find the individual
12643 * pixel. */
12644 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12645 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12646 * 0.5 to hit the center of the pixel. Then convert back to normalised
12647 * coordinates. */
12648 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12649 /* Clamp, keep the half pixel origin in mind. */
12650 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12652 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12653 /* Add 1.0 / size.x to sample the adjacent texel. */
12654 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12655 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12657 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12658 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12659 * values from bleeding into the sampled luminance values due to
12660 * filtering. */
12661 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12662 /* Multiply the y coordinate by 2/3 and clamp it. */
12663 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12664 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12667 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12668 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12669 const char *output, const char *tex_type, const char *swizzle)
12671 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12673 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12674 shader_addline(buffer, "float luminance;\n");
12675 shader_addline(buffer, "vec2 texcoord;\n");
12676 shader_addline(buffer, "vec2 chroma;\n");
12677 shader_addline(buffer, "uniform vec2 size;\n");
12679 shader_addline(buffer, "\nvoid main()\n{\n");
12681 switch (complex_fixup)
12683 case COMPLEX_FIXUP_UYVY:
12684 case COMPLEX_FIXUP_YUY2:
12685 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12686 break;
12688 case COMPLEX_FIXUP_YV12:
12689 gen_yv12_read(buffer, gl_info, tex_type);
12690 break;
12692 case COMPLEX_FIXUP_NV12:
12693 gen_nv12_read(buffer, gl_info, tex_type);
12694 break;
12696 default:
12697 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12698 string_buffer_free(buffer);
12699 return;
12702 /* Calculate the final result. Formula is taken from
12703 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12704 * ranges from -0.5 to 0.5. */
12705 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12707 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12708 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12709 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12711 shader_addline(buffer, "}\n");
12714 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12715 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12716 const char *output, const char *tex_type, const char *swizzle)
12718 shader_addline(buffer, "\nvoid main()\n{\n");
12719 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12720 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12721 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12722 shader_addline(buffer, "}\n");
12725 /* Context activation is done by the caller. */
12726 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12727 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12729 static const struct
12731 GLenum texture_target;
12732 const char texture_type[7];
12733 const char texcoord_swizzle[4];
12735 texture_data[] =
12737 {GL_TEXTURE_2D, "2D", "xy"},
12738 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12739 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12741 static const char vshader_main[] =
12742 "\n"
12743 "void main()\n"
12744 "{\n"
12745 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12746 " out_texcoord = texcoord;\n"
12747 "}\n";
12748 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12749 struct wined3d_string_buffer *buffer, *output;
12750 GLuint program, vshader_id, fshader_id;
12751 const char *tex_type, *swizzle, *ptr;
12752 unsigned int i;
12753 GLint loc;
12755 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12757 if (args->texture_type == texture_data[i].texture_target)
12759 tex_type = texture_data[i].texture_type;
12760 swizzle = texture_data[i].texcoord_swizzle;
12761 break;
12764 if (i == ARRAY_SIZE(texture_data))
12766 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12767 return 0;
12770 program = GL_EXTCALL(glCreateProgram());
12772 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12774 buffer = string_buffer_get(&blitter->string_buffers);
12775 shader_glsl_add_version_declaration(buffer, gl_info);
12776 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12777 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12778 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12779 shader_addline(buffer, vshader_main);
12781 ptr = buffer->buffer;
12782 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12783 GL_EXTCALL(glAttachShader(program, vshader_id));
12784 GL_EXTCALL(glDeleteShader(vshader_id));
12786 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12788 string_buffer_clear(buffer);
12789 shader_glsl_add_version_declaration(buffer, gl_info);
12790 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12791 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12792 if (!needs_legacy_glsl_syntax(gl_info))
12793 shader_addline(buffer, "out vec4 ps_out[1];\n");
12795 output = string_buffer_get(&blitter->string_buffers);
12796 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12798 switch (complex_fixup)
12800 case COMPLEX_FIXUP_P8:
12801 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12802 break;
12803 case COMPLEX_FIXUP_YUY2:
12804 case COMPLEX_FIXUP_UYVY:
12805 case COMPLEX_FIXUP_YV12:
12806 case COMPLEX_FIXUP_NV12:
12807 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12808 break;
12809 case COMPLEX_FIXUP_NONE:
12810 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12813 string_buffer_release(&blitter->string_buffers, output);
12815 ptr = buffer->buffer;
12816 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12817 string_buffer_release(&blitter->string_buffers, buffer);
12818 GL_EXTCALL(glAttachShader(program, fshader_id));
12819 GL_EXTCALL(glDeleteShader(fshader_id));
12821 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12822 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12824 if (!needs_legacy_glsl_syntax(gl_info))
12825 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12827 GL_EXTCALL(glCompileShader(vshader_id));
12828 print_glsl_info_log(gl_info, vshader_id, FALSE);
12829 GL_EXTCALL(glCompileShader(fshader_id));
12830 print_glsl_info_log(gl_info, fshader_id, FALSE);
12831 GL_EXTCALL(glLinkProgram(program));
12832 shader_glsl_validate_link(gl_info, program);
12834 GL_EXTCALL(glUseProgram(program));
12835 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12836 GL_EXTCALL(glUniform1i(loc, 0));
12837 if (complex_fixup == COMPLEX_FIXUP_P8)
12839 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12840 GL_EXTCALL(glUniform1i(loc, 1));
12843 return program;
12846 /* Context activation is done by the caller. */
12847 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12848 struct wined3d_context *context, const struct wined3d_texture *texture)
12850 const struct wined3d_gl_info *gl_info = context->gl_info;
12851 const struct wined3d_palette *palette;
12853 palette = texture->swapchain ? texture->swapchain->palette : NULL;
12855 if (!blitter->palette_texture)
12856 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12858 context_active_texture(context, gl_info, 1);
12859 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12860 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12861 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12862 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12864 if (palette)
12866 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12867 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12869 else
12871 static const DWORD black;
12873 FIXME("P8 texture loaded without a palette.\n");
12874 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12875 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12878 context_active_texture(context, gl_info, 0);
12881 /* Context activation is done by the caller. */
12882 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12883 struct wined3d_context *context, const struct wined3d_texture *texture)
12885 const struct wined3d_gl_info *gl_info = context->gl_info;
12886 struct glsl_blitter_program *program;
12887 struct glsl_blitter_args args;
12888 struct wine_rb_entry *entry;
12890 memset(&args, 0, sizeof(args));
12891 args.texture_type = texture->target;
12892 args.fixup = texture->resource.format->color_fixup;
12894 if ((entry = wine_rb_get(&blitter->programs, &args)))
12895 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12897 if (!(program = heap_alloc(sizeof(*program))))
12899 ERR("Failed to allocate blitter program memory.\n");
12900 return NULL;
12903 program->args = args;
12904 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
12906 WARN("Failed to generate blitter program.\n");
12907 heap_free(program);
12908 return NULL;
12911 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
12913 ERR("Failed to store blitter program.\n");
12914 GL_EXTCALL(glDeleteProgram(program->id));
12915 heap_free(program);
12916 return NULL;
12919 return program;
12922 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
12923 const struct wined3d_texture *src_texture, DWORD src_location,
12924 const struct wined3d_texture *dst_texture, DWORD dst_location)
12926 const struct wined3d_resource *src_resource = &src_texture->resource;
12927 const struct wined3d_resource *dst_resource = &dst_texture->resource;
12928 const struct wined3d_format *src_format = src_resource->format;
12929 const struct wined3d_format *dst_format = dst_resource->format;
12930 BOOL decompress;
12932 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
12934 if (dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
12935 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
12936 else
12937 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
12940 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT)
12942 TRACE("Unsupported blit_op %#x.\n", blit_op);
12943 return FALSE;
12946 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
12947 return FALSE;
12949 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
12950 || dst_texture->target == GL_TEXTURE_2D_MULTISAMPLE
12951 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY
12952 || dst_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
12954 TRACE("Multi-sample textures not supported.\n");
12955 return FALSE;
12958 /* We don't necessarily want to blit from resources without
12959 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
12960 * compressed textures. */
12961 decompress = src_format && (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
12962 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
12963 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
12965 TRACE("Source or destination resource does not have GPU access.\n");
12966 return FALSE;
12969 if (!is_identity_fixup(dst_format->color_fixup)
12970 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
12972 TRACE("Destination fixups are not supported.\n");
12973 return FALSE;
12976 TRACE("Returning supported.\n");
12977 return TRUE;
12980 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
12981 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
12982 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
12983 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
12984 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
12986 struct wined3d_device *device = dst_texture->resource.device;
12987 const struct wined3d_gl_info *gl_info = context->gl_info;
12988 struct wined3d_texture *staging_texture = NULL;
12989 struct wined3d_glsl_blitter *glsl_blitter;
12990 struct glsl_blitter_program *program;
12991 struct wined3d_blitter *next;
12992 unsigned int src_level;
12993 GLint location;
12994 RECT s, d;
12996 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
12997 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
12998 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
12999 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13000 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
13002 if (!glsl_blitter_supported(op, context, src_texture, src_location, dst_texture, dst_location))
13004 if (!(next = blitter->next))
13006 ERR("No blitter to handle blit op %#x.\n", op);
13007 return dst_location;
13010 TRACE("Forwarding to blitter %p.\n", next);
13011 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13012 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
13015 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13017 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13019 struct wined3d_resource_desc desc;
13020 struct wined3d_box upload_box;
13021 HRESULT hr;
13023 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13025 src_level = src_sub_resource_idx % src_texture->level_count;
13026 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13027 desc.format = src_texture->resource.format->id;
13028 desc.multisample_type = src_texture->resource.multisample_type;
13029 desc.multisample_quality = src_texture->resource.multisample_quality;
13030 desc.usage = WINED3DUSAGE_PRIVATE;
13031 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13032 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13033 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13034 desc.depth = 1;
13035 desc.size = 0;
13037 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13038 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13040 ERR("Failed to create staging texture, hr %#x.\n", hr);
13041 return dst_location;
13044 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13045 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13046 src_texture, src_sub_resource_idx, &upload_box);
13048 src_texture = staging_texture;
13049 src_sub_resource_idx = 0;
13051 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13052 && (src_texture->sub_resources[src_sub_resource_idx].locations
13053 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13054 && !wined3d_resource_is_offscreen(&src_texture->resource))
13057 /* Without FBO blits transferring from the drawable to the texture is
13058 * expensive, because we have to flip the data in sysmem. Since we can
13059 * flip in the blitter, we don't actually need that flip anyway. So we
13060 * use the surface's texture as scratch texture, and flip the source
13061 * rectangle instead. */
13062 texture2d_load_fb_texture(src_texture, src_sub_resource_idx, FALSE, context);
13064 s = *src_rect;
13065 src_level = src_sub_resource_idx % src_texture->level_count;
13066 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13067 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13068 src_rect = &s;
13070 else
13072 wined3d_texture_load(src_texture, context, FALSE);
13075 context_apply_blit_state(context, device);
13077 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13079 d = *dst_rect;
13080 wined3d_texture_translate_drawable_coords(dst_texture, context->win_handle, &d);
13081 dst_rect = &d;
13084 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
13086 GLenum buffer;
13088 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13090 TRACE("Destination texture %p is onscreen.\n", dst_texture);
13091 buffer = wined3d_texture_get_gl_buffer(dst_texture);
13093 else
13095 TRACE("Destination texture %p is offscreen.\n", dst_texture);
13096 buffer = GL_COLOR_ATTACHMENT0;
13098 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER,
13099 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
13100 context_set_draw_buffer(context, buffer);
13101 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
13102 context_invalidate_state(context, STATE_FRAMEBUFFER);
13105 if (!(program = glsl_blitter_get_program(glsl_blitter, context, src_texture)))
13107 ERR("Failed to get blitter program.\n");
13108 return dst_location;
13110 GL_EXTCALL(glUseProgram(program->id));
13111 switch (get_complex_fixup(program->args.fixup))
13113 case COMPLEX_FIXUP_P8:
13114 glsl_blitter_upload_palette(glsl_blitter, context, src_texture);
13115 break;
13117 case COMPLEX_FIXUP_YUY2:
13118 case COMPLEX_FIXUP_UYVY:
13119 case COMPLEX_FIXUP_YV12:
13120 case COMPLEX_FIXUP_NV12:
13121 src_level = src_sub_resource_idx % src_texture->level_count;
13122 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13123 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13124 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13125 break;
13127 default:
13128 break;
13130 context_draw_shaded_quad(context, src_texture, src_sub_resource_idx, src_rect, dst_rect, filter);
13131 GL_EXTCALL(glUseProgram(0));
13133 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13134 gl_info->gl_ops.gl.p_glFlush();
13136 if (staging_texture)
13137 wined3d_texture_decref(staging_texture);
13139 return dst_location;
13142 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13143 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13144 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
13146 struct wined3d_blitter *next;
13148 if ((next = blitter->next))
13149 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13150 clear_rects, draw_rect, flags, color, depth, stencil);
13153 static const struct wined3d_blitter_ops glsl_blitter_ops =
13155 glsl_blitter_destroy,
13156 glsl_blitter_clear,
13157 glsl_blitter_blit,
13160 void wined3d_glsl_blitter_create(struct wined3d_blitter **next, const struct wined3d_device *device)
13162 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13163 struct wined3d_glsl_blitter *blitter;
13165 if (device->shader_backend != &glsl_shader_backend)
13166 return;
13168 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13169 return;
13171 if (!(blitter = heap_alloc(sizeof(*blitter))))
13173 ERR("Failed to allocate blitter.\n");
13174 return;
13177 TRACE("Created blitter %p.\n", blitter);
13179 blitter->blitter.ops = &glsl_blitter_ops;
13180 blitter->blitter.next = *next;
13181 string_buffer_list_init(&blitter->string_buffers);
13182 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13183 blitter->palette_texture = 0;
13184 *next = &blitter->blitter;