mstask: Implement ITask::DeleteTrigger().
[wine.git] / dlls / wined3d / glsl_shader.c
blobe29730cce24bddc0d62addda9bcccaf8f8b26447
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
32 #include "config.h"
33 #include "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
37 #ifdef HAVE_FLOAT_H
38 # include <float.h>
39 #endif
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
48 #define WINED3D_GLSL_SAMPLE_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
53 static const struct
55 unsigned int coord_size;
56 unsigned int resinfo_size;
57 const char *type_part;
59 resource_type_info[] =
61 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
62 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
63 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
64 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
66 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
67 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
68 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
69 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
70 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
71 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
74 struct glsl_dst_param
76 char reg_name[150];
77 char mask_str[6];
80 struct glsl_src_param
82 char reg_name[150];
83 char param_str[200];
86 struct glsl_sample_function
88 struct wined3d_string_buffer *name;
89 unsigned int coord_mask;
90 unsigned int deriv_mask;
91 enum wined3d_data_type data_type;
92 BOOL output_single_component;
93 unsigned int offset_size;
96 enum heap_node_op
98 HEAP_NODE_TRAVERSE_LEFT,
99 HEAP_NODE_TRAVERSE_RIGHT,
100 HEAP_NODE_POP,
103 struct constant_entry
105 unsigned int idx;
106 unsigned int version;
109 struct constant_heap
111 struct constant_entry *entries;
112 BOOL *contained;
113 unsigned int *positions;
114 unsigned int size;
117 /* GLSL shader private data */
118 struct shader_glsl_priv
120 struct wined3d_string_buffer shader_buffer;
121 struct wined3d_string_buffer_list string_buffers;
122 struct wine_rb_tree program_lookup;
123 struct constant_heap vconst_heap;
124 struct constant_heap pconst_heap;
125 unsigned char *stack;
126 UINT next_constant_version;
128 const struct wined3d_vertex_pipe_ops *vertex_pipe;
129 const struct fragment_pipeline *fragment_pipe;
130 struct wine_rb_tree ffp_vertex_shaders;
131 struct wine_rb_tree ffp_fragment_shaders;
132 BOOL ffp_proj_control;
133 BOOL legacy_lighting;
136 struct glsl_vs_program
138 struct list shader_entry;
139 GLuint id;
140 GLenum vertex_color_clamp;
141 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
142 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
143 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
144 GLint pos_fixup_location;
146 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
147 GLint projection_matrix_location;
148 GLint normal_matrix_location;
149 GLint texture_matrix_location[MAX_TEXTURES];
150 GLint material_ambient_location;
151 GLint material_diffuse_location;
152 GLint material_specular_location;
153 GLint material_emissive_location;
154 GLint material_shininess_location;
155 GLint light_ambient_location;
156 struct
158 GLint diffuse;
159 GLint specular;
160 GLint ambient;
161 GLint position;
162 GLint direction;
163 GLint range;
164 GLint falloff;
165 GLint c_att;
166 GLint l_att;
167 GLint q_att;
168 GLint cos_htheta;
169 GLint cos_hphi;
170 } light_location[MAX_ACTIVE_LIGHTS];
171 GLint pointsize_location;
172 GLint pointsize_min_location;
173 GLint pointsize_max_location;
174 GLint pointsize_c_att_location;
175 GLint pointsize_l_att_location;
176 GLint pointsize_q_att_location;
177 GLint clip_planes_location;
180 struct glsl_hs_program
182 struct list shader_entry;
183 GLuint id;
186 struct glsl_ds_program
188 struct list shader_entry;
189 GLuint id;
191 GLint pos_fixup_location;
194 struct glsl_gs_program
196 struct list shader_entry;
197 GLuint id;
199 GLint pos_fixup_location;
202 struct glsl_ps_program
204 struct list shader_entry;
205 GLuint id;
206 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
207 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
208 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
209 GLint bumpenv_mat_location[MAX_TEXTURES];
210 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
211 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
212 GLint tss_constant_location[MAX_TEXTURES];
213 GLint tex_factor_location;
214 GLint specular_enable_location;
215 GLint fog_color_location;
216 GLint fog_density_location;
217 GLint fog_end_location;
218 GLint fog_scale_location;
219 GLint alpha_test_ref_location;
220 GLint ycorrection_location;
221 GLint np2_fixup_location;
222 GLint color_key_location;
223 const struct ps_np2fixup_info *np2_fixup_info;
226 struct glsl_cs_program
228 struct list shader_entry;
229 GLuint id;
232 /* Struct to maintain data about a linked GLSL program */
233 struct glsl_shader_prog_link
235 struct wine_rb_entry program_lookup_entry;
236 struct glsl_vs_program vs;
237 struct glsl_hs_program hs;
238 struct glsl_ds_program ds;
239 struct glsl_gs_program gs;
240 struct glsl_ps_program ps;
241 struct glsl_cs_program cs;
242 GLuint id;
243 DWORD constant_update_mask;
244 unsigned int constant_version;
245 DWORD shader_controlled_clip_distances : 1;
246 DWORD clip_distance_mask : 8; /* MAX_CLIP_DISTANCES, 8 */
247 DWORD padding : 23;
250 struct glsl_program_key
252 GLuint vs_id;
253 GLuint hs_id;
254 GLuint ds_id;
255 GLuint gs_id;
256 GLuint ps_id;
257 GLuint cs_id;
260 struct shader_glsl_ctx_priv {
261 const struct vs_compile_args *cur_vs_args;
262 const struct ds_compile_args *cur_ds_args;
263 const struct ps_compile_args *cur_ps_args;
264 struct ps_np2fixup_info *cur_np2fixup_info;
265 struct wined3d_string_buffer_list *string_buffers;
268 struct glsl_context_data
270 struct glsl_shader_prog_link *glsl_program;
271 GLenum vertex_color_clamp;
272 BOOL rasterization_disabled;
275 struct glsl_ps_compiled_shader
277 struct ps_compile_args args;
278 struct ps_np2fixup_info np2fixup;
279 GLuint id;
282 struct glsl_vs_compiled_shader
284 struct vs_compile_args args;
285 GLuint id;
288 struct glsl_hs_compiled_shader
290 GLuint id;
293 struct glsl_ds_compiled_shader
295 struct ds_compile_args args;
296 GLuint id;
299 struct glsl_gs_compiled_shader
301 struct gs_compile_args args;
302 GLuint id;
305 struct glsl_cs_compiled_shader
307 GLuint id;
310 struct glsl_shader_private
312 union
314 struct glsl_vs_compiled_shader *vs;
315 struct glsl_hs_compiled_shader *hs;
316 struct glsl_ds_compiled_shader *ds;
317 struct glsl_gs_compiled_shader *gs;
318 struct glsl_ps_compiled_shader *ps;
319 struct glsl_cs_compiled_shader *cs;
320 } gl_shaders;
321 unsigned int num_gl_shaders, shader_array_size;
324 struct glsl_ffp_vertex_shader
326 struct wined3d_ffp_vs_desc desc;
327 GLuint id;
328 struct list linked_programs;
331 struct glsl_ffp_fragment_shader
333 struct ffp_frag_desc entry;
334 GLuint id;
335 struct list linked_programs;
338 struct glsl_ffp_destroy_ctx
340 struct shader_glsl_priv *priv;
341 const struct wined3d_gl_info *gl_info;
344 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
346 static const char *debug_gl_shader_type(GLenum type)
348 switch (type)
350 #define WINED3D_TO_STR(u) case u: return #u
351 WINED3D_TO_STR(GL_VERTEX_SHADER);
352 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
353 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
354 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
355 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
356 WINED3D_TO_STR(GL_COMPUTE_SHADER);
357 #undef WINED3D_TO_STR
358 default:
359 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
363 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
365 switch (type)
367 case WINED3D_SHADER_TYPE_VERTEX:
368 return "vs";
370 case WINED3D_SHADER_TYPE_HULL:
371 return "hs";
373 case WINED3D_SHADER_TYPE_DOMAIN:
374 return "ds";
376 case WINED3D_SHADER_TYPE_GEOMETRY:
377 return "gs";
379 case WINED3D_SHADER_TYPE_PIXEL:
380 return "ps";
382 case WINED3D_SHADER_TYPE_COMPUTE:
383 return "cs";
385 default:
386 FIXME("Unhandled shader type %#x.\n", type);
387 return "unknown";
391 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
393 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
394 return 440;
395 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
396 return 150;
397 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
398 return 130;
399 else
400 return 120;
403 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
404 const struct wined3d_gl_info *gl_info)
406 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
409 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
411 char str[4][17];
413 wined3d_ftoa(values[0], str[0]);
414 wined3d_ftoa(values[1], str[1]);
415 wined3d_ftoa(values[2], str[2]);
416 wined3d_ftoa(values[3], str[3]);
417 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
420 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
421 const int *values, unsigned int size)
423 int i;
425 if (!size || size > 4)
427 ERR("Invalid vector size %u.\n", size);
428 return;
431 if (size > 1)
432 shader_addline(buffer, "ivec%u(", size);
434 for (i = 0; i < size; ++i)
435 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
437 if (size > 1)
438 shader_addline(buffer, ")");
441 static const char *get_info_log_line(const char **ptr)
443 const char *p, *q;
445 p = *ptr;
446 if (!(q = strstr(p, "\n")))
448 if (!*p) return NULL;
449 *ptr += strlen(p);
450 return p;
452 *ptr = q + 1;
454 return p;
457 /* Context activation is done by the caller. */
458 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
460 int length = 0;
461 char *log;
463 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
464 return;
466 if (program)
467 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
468 else
469 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
471 /* A size of 1 is just a null-terminated string, so the log should be bigger than
472 * that if there are errors. */
473 if (length > 1)
475 const char *ptr, *line;
477 log = heap_alloc(length);
478 /* The info log is supposed to be zero-terminated, but at least some
479 * versions of fglrx don't terminate the string properly. The reported
480 * length does include the terminator, so explicitly set it to zero
481 * here. */
482 log[length - 1] = 0;
483 if (program)
484 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
485 else
486 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
488 ptr = log;
489 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
491 WARN("Info log received from GLSL shader #%u:\n", id);
492 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
494 else
496 FIXME("Info log received from GLSL shader #%u:\n", id);
497 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
499 heap_free(log);
503 /* Context activation is done by the caller. */
504 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
506 const char *ptr, *line;
508 TRACE("Compiling shader object %u.\n", shader);
510 if (TRACE_ON(d3d_shader))
512 ptr = src;
513 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
516 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
517 checkGLcall("glShaderSource");
518 GL_EXTCALL(glCompileShader(shader));
519 checkGLcall("glCompileShader");
520 print_glsl_info_log(gl_info, shader, FALSE);
523 /* Context activation is done by the caller. */
524 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
526 GLint i, shader_count, source_size = -1;
527 GLuint *shaders;
528 char *source = NULL;
530 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
531 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
533 ERR("Failed to allocate shader array memory.\n");
534 return;
537 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
538 for (i = 0; i < shader_count; ++i)
540 const char *ptr, *line;
541 GLint tmp;
543 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
545 if (source_size < tmp)
547 heap_free(source);
549 if (!(source = heap_alloc_zero(tmp)))
551 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
552 heap_free(shaders);
553 return;
555 source_size = tmp;
558 FIXME("Shader %u:\n", shaders[i]);
559 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
560 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
561 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
562 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
563 FIXME("\n");
565 ptr = source;
566 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
567 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
568 FIXME("\n");
571 heap_free(source);
572 heap_free(shaders);
575 /* Context activation is done by the caller. */
576 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
578 GLint tmp;
580 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
581 return;
583 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
584 if (!tmp)
586 FIXME("Program %u link status invalid.\n", program);
587 shader_glsl_dump_program_source(gl_info, program);
590 print_glsl_info_log(gl_info, program, TRUE);
593 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
595 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
596 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
597 * versions. */
598 return shader_glsl_get_version(gl_info) >= 140;
601 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
603 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
606 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
607 struct shader_glsl_priv *priv, GLuint program_id,
608 const struct wined3d_shader_reg_maps *reg_maps)
610 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
611 struct wined3d_string_buffer *name;
612 unsigned int i, base, count;
613 GLuint block_idx;
615 if (shader_glsl_use_layout_binding_qualifier(gl_info))
616 return;
618 name = string_buffer_get(&priv->string_buffers);
619 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
620 for (i = 0; i < count; ++i)
622 if (!reg_maps->cb_sizes[i])
623 continue;
625 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
626 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
627 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
629 checkGLcall("glUniformBlockBinding");
630 string_buffer_release(&priv->string_buffers, name);
633 /* Context activation is done by the caller. */
634 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
635 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
636 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
638 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
639 unsigned int i, mapped_unit;
640 GLint name_loc;
642 for (i = 0; i < count; ++i)
644 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
645 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
646 if (name_loc == -1)
647 continue;
649 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
650 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
652 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
653 continue;
656 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
657 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
659 checkGLcall("Load sampler bindings");
660 string_buffer_release(&priv->string_buffers, sampler_name);
663 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
664 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
666 const DWORD *tex_unit_map;
667 unsigned int base, count;
669 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
670 if (sampler_idx >= count)
671 return WINED3D_UNMAPPED_STAGE;
672 if (!tex_unit_map)
673 return base + sampler_idx;
674 return tex_unit_map[base + sampler_idx];
677 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
678 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
679 unsigned int sampler_idx)
681 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
682 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
683 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
684 else
685 ERR("Unmapped sampler %u.\n", sampler_idx);
688 /* Context activation is done by the caller. */
689 static void shader_glsl_load_samplers(const struct wined3d_context *context,
690 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
692 const struct wined3d_gl_info *gl_info = context->gl_info;
693 const struct wined3d_shader_version *shader_version;
694 const DWORD *tex_unit_map;
695 unsigned int base, count;
696 const char *prefix;
698 if (shader_glsl_use_layout_binding_qualifier(gl_info))
699 return;
701 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
702 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
703 tex_unit_map = context_get_tex_unit_mapping(context, shader_version, &base, &count);
704 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
707 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
708 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
710 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
712 if (icb)
714 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
715 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
716 GLint icb_location;
718 string_buffer_sprintf(icb_name, "%s_icb", prefix);
719 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
720 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
721 checkGLcall("Load immediate constant buffer");
723 string_buffer_release(&priv->string_buffers, icb_name);
727 /* Context activation is done by the caller. */
728 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
729 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
731 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
732 struct wined3d_string_buffer *name;
733 GLint location;
734 unsigned int i;
736 if (shader_glsl_use_layout_binding_qualifier(gl_info))
737 return;
739 name = string_buffer_get(&priv->string_buffers);
740 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
742 if (!reg_maps->uav_resource_info[i].type)
743 continue;
745 string_buffer_sprintf(name, "%s_image%u", prefix, i);
746 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
747 if (location == -1)
748 continue;
750 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
751 GL_EXTCALL(glUniform1i(location, i));
753 checkGLcall("Load image bindings");
754 string_buffer_release(&priv->string_buffers, name);
757 /* Context activation is done by the caller. */
758 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
759 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
761 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
763 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
764 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
765 /* Texture unit mapping is set up to be the same each time the shader
766 * program is used so we can hardcode the sampler uniform values. */
767 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
770 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
771 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
773 if (varyings && *strings)
775 char *ptr = *strings;
777 varyings[*varying_count] = ptr;
779 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
780 ptr += buffer->content_size + 1;
782 *strings = ptr;
785 *strings_length += buffer->content_size + 1;
786 ++(*varying_count);
789 static void append_transform_feedback_skip_components(const char **varyings,
790 unsigned int *varying_count, char **strings, unsigned int *strings_length,
791 struct wined3d_string_buffer *buffer, unsigned int component_count)
793 unsigned int j;
795 for (j = 0; j < component_count / 4; ++j)
797 string_buffer_sprintf(buffer, "gl_SkipComponents4");
798 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
800 if (component_count % 4)
802 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
803 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
807 static void shader_glsl_generate_transform_feedback_varyings(const struct wined3d_stream_output_desc *so_desc,
808 struct wined3d_string_buffer *buffer, const char **varyings, unsigned int *varying_count,
809 char *strings, unsigned int *strings_length, GLenum buffer_mode)
811 unsigned int i, buffer_idx, count, length, highest_output_slot, stride;
813 count = length = 0;
814 highest_output_slot = 0;
815 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
817 stride = 0;
819 for (i = 0; i < so_desc->element_count; ++i)
821 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
823 highest_output_slot = max(highest_output_slot, e->output_slot);
824 if (e->output_slot != buffer_idx)
825 continue;
827 if (e->stream_idx)
829 FIXME("Unhandled stream %u.\n", e->stream_idx);
830 continue;
833 stride += e->component_count;
835 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
837 append_transform_feedback_skip_components(varyings, &count,
838 &strings, &length, buffer, e->component_count);
839 continue;
842 if (e->component_idx || e->component_count != 4)
844 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
846 FIXME("Unsupported component range %u-%u.\n", e->component_idx, e->component_count);
847 append_transform_feedback_skip_components(varyings, &count,
848 &strings, &length, buffer, e->component_count);
849 continue;
852 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
853 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
854 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
856 else
858 string_buffer_sprintf(buffer, "shader_in_out.reg%u", e->register_idx);
859 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
863 if (buffer_idx < so_desc->buffer_stride_count
864 && stride < so_desc->buffer_strides[buffer_idx] / 4)
866 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
867 append_transform_feedback_skip_components(varyings, &count,
868 &strings, &length, buffer, component_count);
871 if (highest_output_slot <= buffer_idx)
872 break;
874 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
876 string_buffer_sprintf(buffer, "gl_NextBuffer");
877 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
881 if (varying_count)
882 *varying_count = count;
883 if (strings_length)
884 *strings_length = length;
887 static void shader_glsl_init_transform_feedback(const struct wined3d_context *context,
888 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
890 const struct wined3d_stream_output_desc *so_desc = &shader->u.gs.so_desc;
891 const struct wined3d_gl_info *gl_info = context->gl_info;
892 struct wined3d_string_buffer *buffer;
893 unsigned int i, count, length;
894 const char **varyings;
895 char *strings;
896 GLenum mode;
898 if (!so_desc->element_count)
899 return;
901 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
903 mode = GL_INTERLEAVED_ATTRIBS;
905 else
907 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
909 for (i = 0; i < so_desc->element_count; ++i)
911 if (so_desc->elements[i].register_idx == WINED3D_STREAM_OUTPUT_GAP)
913 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
914 return;
916 ++element_count[so_desc->elements[i].output_slot];
919 if (element_count[0] == so_desc->element_count)
921 mode = GL_INTERLEAVED_ATTRIBS;
923 else
925 mode = GL_SEPARATE_ATTRIBS;
926 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
928 if (element_count[i] != 1)
929 break;
931 for (; i < ARRAY_SIZE(element_count); ++i)
933 if (element_count[i])
935 FIXME("Only single element per buffer is allowed in separate mode.\n");
936 return;
942 buffer = string_buffer_get(&priv->string_buffers);
944 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, NULL, &count, NULL, &length, mode);
946 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
948 ERR("Out of memory.\n");
949 string_buffer_release(&priv->string_buffers, buffer);
950 return;
952 if (!(strings = heap_calloc(length, sizeof(*strings))))
954 ERR("Out of memory.\n");
955 heap_free(varyings);
956 string_buffer_release(&priv->string_buffers, buffer);
957 return;
960 shader_glsl_generate_transform_feedback_varyings(so_desc, buffer, varyings, NULL, strings, NULL, mode);
961 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
962 checkGLcall("glTransformFeedbackVaryings");
964 heap_free(varyings);
965 heap_free(strings);
966 string_buffer_release(&priv->string_buffers, buffer);
969 /* Context activation is done by the caller. */
970 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
971 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
973 unsigned int start = ~0U, end = 0;
974 int stack_idx = 0;
975 unsigned int heap_idx = 1;
976 unsigned int idx;
978 if (heap->entries[heap_idx].version <= version) return;
980 idx = heap->entries[heap_idx].idx;
981 if (constant_locations[idx] != -1)
982 start = end = idx;
983 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
985 while (stack_idx >= 0)
987 /* Note that we fall through to the next case statement. */
988 switch(stack[stack_idx])
990 case HEAP_NODE_TRAVERSE_LEFT:
992 unsigned int left_idx = heap_idx << 1;
993 if (left_idx < heap->size && heap->entries[left_idx].version > version)
995 heap_idx = left_idx;
996 idx = heap->entries[heap_idx].idx;
997 if (constant_locations[idx] != -1)
999 if (start > idx)
1000 start = idx;
1001 if (end < idx)
1002 end = idx;
1005 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1006 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1007 break;
1011 case HEAP_NODE_TRAVERSE_RIGHT:
1013 unsigned int right_idx = (heap_idx << 1) + 1;
1014 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1016 heap_idx = right_idx;
1017 idx = heap->entries[heap_idx].idx;
1018 if (constant_locations[idx] != -1)
1020 if (start > idx)
1021 start = idx;
1022 if (end < idx)
1023 end = idx;
1026 stack[stack_idx++] = HEAP_NODE_POP;
1027 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1028 break;
1032 case HEAP_NODE_POP:
1033 heap_idx >>= 1;
1034 --stack_idx;
1035 break;
1038 if (start <= end)
1039 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1040 checkGLcall("walk_constant_heap()");
1043 /* Context activation is done by the caller. */
1044 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1045 GLint location, const struct wined3d_vec4 *data)
1047 GLfloat clamped_constant[4];
1049 if (location == -1) return;
1051 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1052 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1053 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1054 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1056 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1059 /* Context activation is done by the caller. */
1060 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1061 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1062 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1064 int stack_idx = 0;
1065 unsigned int heap_idx = 1;
1066 unsigned int idx;
1068 if (heap->entries[heap_idx].version <= version) return;
1070 idx = heap->entries[heap_idx].idx;
1071 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1072 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1074 while (stack_idx >= 0)
1076 /* Note that we fall through to the next case statement. */
1077 switch(stack[stack_idx])
1079 case HEAP_NODE_TRAVERSE_LEFT:
1081 unsigned int left_idx = heap_idx << 1;
1082 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1084 heap_idx = left_idx;
1085 idx = heap->entries[heap_idx].idx;
1086 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1088 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1089 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1090 break;
1094 case HEAP_NODE_TRAVERSE_RIGHT:
1096 unsigned int right_idx = (heap_idx << 1) + 1;
1097 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1099 heap_idx = right_idx;
1100 idx = heap->entries[heap_idx].idx;
1101 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1103 stack[stack_idx++] = HEAP_NODE_POP;
1104 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1105 break;
1109 case HEAP_NODE_POP:
1110 heap_idx >>= 1;
1111 --stack_idx;
1112 break;
1115 checkGLcall("walk_constant_heap_clamped()");
1118 /* Context activation is done by the caller. */
1119 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1120 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1121 unsigned char *stack, unsigned int version)
1123 const struct wined3d_shader_lconst *lconst;
1125 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1126 if (shader->reg_maps.shader_version.major == 1
1127 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1128 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1129 else
1130 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1132 if (!shader->load_local_constsF)
1134 TRACE("No need to load local float constants for this shader.\n");
1135 return;
1138 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1139 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1141 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1143 checkGLcall("glUniform4fv()");
1146 /* Context activation is done by the caller. */
1147 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1148 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1150 unsigned int i;
1151 struct list* ptr;
1153 for (i = 0; constants_set; constants_set >>= 1, ++i)
1155 if (!(constants_set & 1)) continue;
1157 /* We found this uniform name in the program - go ahead and send the data */
1158 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1161 /* Load immediate constants */
1162 ptr = list_head(&shader->constantsI);
1163 while (ptr)
1165 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1166 unsigned int idx = lconst->idx;
1167 const GLint *values = (const GLint *)lconst->value;
1169 /* We found this uniform name in the program - go ahead and send the data */
1170 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1171 ptr = list_next(&shader->constantsI, ptr);
1173 checkGLcall("glUniform4iv()");
1176 /* Context activation is done by the caller. */
1177 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1178 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1180 unsigned int i;
1181 struct list* ptr;
1183 for (i = 0; constants_set; constants_set >>= 1, ++i)
1185 if (!(constants_set & 1)) continue;
1187 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1190 /* Load immediate constants */
1191 ptr = list_head(&shader->constantsB);
1192 while (ptr)
1194 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1195 unsigned int idx = lconst->idx;
1196 const GLint *values = (const GLint *)lconst->value;
1198 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1199 ptr = list_next(&shader->constantsB, ptr);
1201 checkGLcall("glUniform1iv()");
1204 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1206 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1209 /* Context activation is done by the caller (state handler). */
1210 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1211 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1213 struct
1215 float sx, sy;
1217 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
1218 UINT fixup = ps->np2_fixup_info->active;
1219 UINT i;
1221 for (i = 0; fixup; fixup >>= 1, ++i)
1223 const struct wined3d_texture *tex = state->textures[i];
1224 unsigned char idx = ps->np2_fixup_info->idx[i];
1226 if (!tex)
1228 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1229 continue;
1232 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1233 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1236 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1239 /* Taken and adapted from Mesa. */
1240 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
1242 float pos, neg, t, det;
1243 struct wined3d_matrix temp;
1245 /* Calculate the determinant of upper left 3x3 submatrix and
1246 * determine if the matrix is singular. */
1247 pos = neg = 0.0f;
1248 t = in->_11 * in->_22 * in->_33;
1249 if (t >= 0.0f)
1250 pos += t;
1251 else
1252 neg += t;
1254 t = in->_21 * in->_32 * in->_13;
1255 if (t >= 0.0f)
1256 pos += t;
1257 else
1258 neg += t;
1259 t = in->_31 * in->_12 * in->_23;
1260 if (t >= 0.0f)
1261 pos += t;
1262 else
1263 neg += t;
1265 t = -in->_31 * in->_22 * in->_13;
1266 if (t >= 0.0f)
1267 pos += t;
1268 else
1269 neg += t;
1270 t = -in->_21 * in->_12 * in->_33;
1271 if (t >= 0.0f)
1272 pos += t;
1273 else
1274 neg += t;
1276 t = -in->_11 * in->_32 * in->_23;
1277 if (t >= 0.0f)
1278 pos += t;
1279 else
1280 neg += t;
1282 det = pos + neg;
1284 if (fabsf(det) < 1e-25f)
1285 return FALSE;
1287 det = 1.0f / det;
1288 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1289 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1290 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1291 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1292 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1293 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1294 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1295 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1296 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1298 *out = temp;
1299 return TRUE;
1302 static void swap_rows(float **a, float **b)
1304 float *tmp = *a;
1306 *a = *b;
1307 *b = tmp;
1310 static BOOL invert_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1312 float wtmp[4][8];
1313 float m0, m1, m2, m3, s;
1314 float *r0, *r1, *r2, *r3;
1316 r0 = wtmp[0];
1317 r1 = wtmp[1];
1318 r2 = wtmp[2];
1319 r3 = wtmp[3];
1321 r0[0] = m->_11;
1322 r0[1] = m->_12;
1323 r0[2] = m->_13;
1324 r0[3] = m->_14;
1325 r0[4] = 1.0f;
1326 r0[5] = r0[6] = r0[7] = 0.0f;
1328 r1[0] = m->_21;
1329 r1[1] = m->_22;
1330 r1[2] = m->_23;
1331 r1[3] = m->_24;
1332 r1[5] = 1.0f;
1333 r1[4] = r1[6] = r1[7] = 0.0f;
1335 r2[0] = m->_31;
1336 r2[1] = m->_32;
1337 r2[2] = m->_33;
1338 r2[3] = m->_34;
1339 r2[6] = 1.0f;
1340 r2[4] = r2[5] = r2[7] = 0.0f;
1342 r3[0] = m->_41;
1343 r3[1] = m->_42;
1344 r3[2] = m->_43;
1345 r3[3] = m->_44;
1346 r3[7] = 1.0f;
1347 r3[4] = r3[5] = r3[6] = 0.0f;
1349 /* Choose pivot - or die. */
1350 if (fabsf(r3[0]) > fabsf(r2[0]))
1351 swap_rows(&r3, &r2);
1352 if (fabsf(r2[0]) > fabsf(r1[0]))
1353 swap_rows(&r2, &r1);
1354 if (fabsf(r1[0]) > fabsf(r0[0]))
1355 swap_rows(&r1, &r0);
1356 if (r0[0] == 0.0f)
1357 return FALSE;
1359 /* Eliminate first variable. */
1360 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1361 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1362 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1363 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1364 s = r0[4];
1365 if (s != 0.0f)
1367 r1[4] -= m1 * s;
1368 r2[4] -= m2 * s;
1369 r3[4] -= m3 * s;
1371 s = r0[5];
1372 if (s != 0.0f)
1374 r1[5] -= m1 * s;
1375 r2[5] -= m2 * s;
1376 r3[5] -= m3 * s;
1378 s = r0[6];
1379 if (s != 0.0f)
1381 r1[6] -= m1 * s;
1382 r2[6] -= m2 * s;
1383 r3[6] -= m3 * s;
1385 s = r0[7];
1386 if (s != 0.0f)
1388 r1[7] -= m1 * s;
1389 r2[7] -= m2 * s;
1390 r3[7] -= m3 * s;
1393 /* Choose pivot - or die. */
1394 if (fabsf(r3[1]) > fabsf(r2[1]))
1395 swap_rows(&r3, &r2);
1396 if (fabsf(r2[1]) > fabsf(r1[1]))
1397 swap_rows(&r2, &r1);
1398 if (r1[1] == 0.0f)
1399 return FALSE;
1401 /* Eliminate second variable. */
1402 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1403 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1404 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1405 s = r1[4];
1406 if (s != 0.0f)
1408 r2[4] -= m2 * s;
1409 r3[4] -= m3 * s;
1411 s = r1[5];
1412 if (s != 0.0f)
1414 r2[5] -= m2 * s;
1415 r3[5] -= m3 * s;
1417 s = r1[6];
1418 if (s != 0.0f)
1420 r2[6] -= m2 * s;
1421 r3[6] -= m3 * s;
1423 s = r1[7];
1424 if (s != 0.0f)
1426 r2[7] -= m2 * s;
1427 r3[7] -= m3 * s;
1430 /* Choose pivot - or die. */
1431 if (fabsf(r3[2]) > fabsf(r2[2]))
1432 swap_rows(&r3, &r2);
1433 if (r2[2] == 0.0f)
1434 return FALSE;
1436 /* Eliminate third variable. */
1437 m3 = r3[2] / r2[2];
1438 r3[3] -= m3 * r2[3];
1439 r3[4] -= m3 * r2[4];
1440 r3[5] -= m3 * r2[5];
1441 r3[6] -= m3 * r2[6];
1442 r3[7] -= m3 * r2[7];
1444 /* Last check. */
1445 if (r3[3] == 0.0f)
1446 return FALSE;
1448 /* Back substitute row 3. */
1449 s = 1.0f / r3[3];
1450 r3[4] *= s;
1451 r3[5] *= s;
1452 r3[6] *= s;
1453 r3[7] *= s;
1455 /* Back substitute row 2. */
1456 m2 = r2[3];
1457 s = 1.0f / r2[2];
1458 r2[4] = s * (r2[4] - r3[4] * m2);
1459 r2[5] = s * (r2[5] - r3[5] * m2);
1460 r2[6] = s * (r2[6] - r3[6] * m2);
1461 r2[7] = s * (r2[7] - r3[7] * m2);
1462 m1 = r1[3];
1463 r1[4] -= r3[4] * m1;
1464 r1[5] -= r3[5] * m1;
1465 r1[6] -= r3[6] * m1;
1466 r1[7] -= r3[7] * m1;
1467 m0 = r0[3];
1468 r0[4] -= r3[4] * m0;
1469 r0[5] -= r3[5] * m0;
1470 r0[6] -= r3[6] * m0;
1471 r0[7] -= r3[7] * m0;
1473 /* Back substitute row 1. */
1474 m1 = r1[2];
1475 s = 1.0f / r1[1];
1476 r1[4] = s * (r1[4] - r2[4] * m1);
1477 r1[5] = s * (r1[5] - r2[5] * m1);
1478 r1[6] = s * (r1[6] - r2[6] * m1);
1479 r1[7] = s * (r1[7] - r2[7] * m1);
1480 m0 = r0[2];
1481 r0[4] -= r2[4] * m0;
1482 r0[5] -= r2[5] * m0;
1483 r0[6] -= r2[6] * m0;
1484 r0[7] -= r2[7] * m0;
1486 /* Back substitute row 0. */
1487 m0 = r0[1];
1488 s = 1.0f / r0[0];
1489 r0[4] = s * (r0[4] - r1[4] * m0);
1490 r0[5] = s * (r0[5] - r1[5] * m0);
1491 r0[6] = s * (r0[6] - r1[6] * m0);
1492 r0[7] = s * (r0[7] - r1[7] * m0);
1494 out->_11 = r0[4];
1495 out->_12 = r0[5];
1496 out->_13 = r0[6];
1497 out->_14 = r0[7];
1498 out->_21 = r1[4];
1499 out->_22 = r1[5];
1500 out->_23 = r1[6];
1501 out->_24 = r1[7];
1502 out->_31 = r2[4];
1503 out->_32 = r2[5];
1504 out->_33 = r2[6];
1505 out->_34 = r2[7];
1506 out->_41 = r3[4];
1507 out->_42 = r3[5];
1508 out->_43 = r3[6];
1509 out->_44 = r3[7];
1511 return TRUE;
1514 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1516 struct wined3d_matrix temp;
1517 unsigned int i, j;
1519 for (i = 0; i < 4; ++i)
1520 for (j = 0; j < 4; ++j)
1521 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1523 *out = temp;
1526 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1527 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1529 const struct wined3d_gl_info *gl_info = context->gl_info;
1530 float mat[3 * 3];
1531 struct wined3d_matrix mv;
1532 unsigned int i, j;
1534 if (prog->vs.normal_matrix_location == -1)
1535 return;
1537 get_modelview_matrix(context, state, 0, &mv);
1538 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1539 invert_matrix_3d(&mv, &mv);
1540 else
1541 invert_matrix(&mv, &mv);
1542 /* Tests show that singular modelview matrices are used unchanged as normal
1543 * matrices on D3D3 and older. There seems to be no clearly consistent
1544 * behavior on newer D3D versions so always follow older ddraw behavior. */
1545 for (i = 0; i < 3; ++i)
1546 for (j = 0; j < 3; ++j)
1547 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1549 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1550 checkGLcall("glUniformMatrix3fv");
1553 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1554 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1556 const struct wined3d_gl_info *gl_info = context->gl_info;
1557 struct wined3d_matrix mat;
1559 if (tex >= MAX_TEXTURES)
1560 return;
1561 if (prog->vs.texture_matrix_location[tex] == -1)
1562 return;
1564 get_texture_matrix(context, state, tex, &mat);
1565 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1566 checkGLcall("glUniformMatrix4fv");
1569 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1570 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1572 const struct wined3d_gl_info *gl_info = context->gl_info;
1574 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1576 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1577 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1579 else
1581 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1583 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1585 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1586 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1587 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1588 checkGLcall("setting FFP material uniforms");
1591 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1592 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1594 const struct wined3d_gl_info *gl_info = context->gl_info;
1595 struct wined3d_color color;
1597 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1598 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1599 checkGLcall("glUniform3fv");
1602 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1603 const struct wined3d_matrix *src2)
1605 struct wined3d_vec4 temp;
1607 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1608 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1609 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1610 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1612 *dest = temp;
1615 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1616 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1617 struct glsl_shader_prog_link *prog)
1619 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1620 const struct wined3d_gl_info *gl_info = context->gl_info;
1621 struct wined3d_vec4 vec4;
1623 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1624 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1625 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1627 switch (light_info->OriginalParms.type)
1629 case WINED3D_LIGHT_POINT:
1630 multiply_vector_matrix(&vec4, &light_info->position, view);
1631 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1632 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1633 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1634 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1635 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1636 break;
1638 case WINED3D_LIGHT_SPOT:
1639 multiply_vector_matrix(&vec4, &light_info->position, view);
1640 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1642 multiply_vector_matrix(&vec4, &light_info->direction, view);
1643 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1645 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1646 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1647 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1648 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1649 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1650 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1651 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1652 break;
1654 case WINED3D_LIGHT_DIRECTIONAL:
1655 multiply_vector_matrix(&vec4, &light_info->direction, view);
1656 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1657 break;
1659 case WINED3D_LIGHT_PARALLELPOINT:
1660 multiply_vector_matrix(&vec4, &light_info->position, view);
1661 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1662 break;
1664 default:
1665 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1667 checkGLcall("setting FFP lights uniforms");
1670 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1671 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1673 const struct wined3d_gl_info *gl_info = context->gl_info;
1674 float min, max;
1675 float size, att[3];
1677 get_pointsize_minmax(context, state, &min, &max);
1679 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1680 checkGLcall("glUniform1f");
1681 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1682 checkGLcall("glUniform1f");
1684 get_pointsize(context, state, &size, att);
1686 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1687 checkGLcall("glUniform1f");
1688 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1689 checkGLcall("glUniform1f");
1690 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1691 checkGLcall("glUniform1f");
1692 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1693 checkGLcall("glUniform1f");
1696 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1697 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1699 const struct wined3d_gl_info *gl_info = context->gl_info;
1700 struct wined3d_color color;
1701 float start, end, scale;
1702 union
1704 DWORD d;
1705 float f;
1706 } tmpvalue;
1708 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1709 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1710 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1711 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1712 get_fog_start_end(context, state, &start, &end);
1713 scale = 1.0f / (end - start);
1714 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1715 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1716 checkGLcall("fog emulation uniforms");
1719 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1720 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1722 const struct wined3d_gl_info *gl_info = context->gl_info;
1723 struct wined3d_matrix matrix;
1724 struct wined3d_vec4 plane;
1726 plane = state->clip_planes[index];
1728 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1729 if (!use_vs(state))
1731 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1732 transpose_matrix(&matrix, &matrix);
1733 multiply_vector_matrix(&plane, &plane, &matrix);
1736 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1739 /* Context activation is done by the caller (state handler). */
1740 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1741 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1743 struct wined3d_color float_key[2];
1744 const struct wined3d_texture *texture = state->textures[0];
1746 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1747 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1750 /* Context activation is done by the caller (state handler). */
1751 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1752 const struct wined3d_state *state)
1754 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1755 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1756 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1757 const struct wined3d_gl_info *gl_info = context->gl_info;
1758 struct shader_glsl_priv *priv = shader_priv;
1759 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1760 DWORD update_mask;
1762 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1763 UINT constant_version;
1764 int i;
1766 if (!prog) {
1767 /* No GLSL program set - nothing to do. */
1768 return;
1770 constant_version = prog->constant_version;
1771 update_mask = context->constant_update_mask & prog->constant_update_mask;
1773 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1774 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1775 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1777 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1778 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1779 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1781 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1782 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1783 vshader->reg_maps.boolean_constants);
1785 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1787 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1788 shader_glsl_clip_plane_uniform(context, state, i, prog);
1791 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1792 shader_glsl_pointsize_uniform(context, state, prog);
1794 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1796 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1797 max(state->viewport_count, 1) : 1;
1798 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1799 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1800 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1801 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1802 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1803 else
1804 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1805 checkGLcall("glUniform4fv");
1808 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1810 struct wined3d_matrix mat;
1812 get_modelview_matrix(context, state, 0, &mat);
1813 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1814 checkGLcall("glUniformMatrix4fv");
1816 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1819 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1821 struct wined3d_matrix mat;
1823 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1825 if (prog->vs.modelview_matrix_location[i] == -1)
1826 break;
1828 get_modelview_matrix(context, state, i, &mat);
1829 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1830 checkGLcall("glUniformMatrix4fv");
1834 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1836 struct wined3d_matrix projection;
1838 get_projection_matrix(context, state, &projection);
1839 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1840 checkGLcall("glUniformMatrix4fv");
1843 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1845 for (i = 0; i < MAX_TEXTURES; ++i)
1846 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1849 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1850 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1852 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1854 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1855 DWORD point_count = 0;
1856 DWORD spot_count = 0;
1857 DWORD directional_count = 0;
1858 DWORD parallel_point_count = 0;
1860 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1862 if (!state->lights[i])
1863 continue;
1865 switch (state->lights[i]->OriginalParms.type)
1867 case WINED3D_LIGHT_POINT:
1868 ++point_count;
1869 break;
1870 case WINED3D_LIGHT_SPOT:
1871 ++spot_count;
1872 break;
1873 case WINED3D_LIGHT_DIRECTIONAL:
1874 ++directional_count;
1875 break;
1876 case WINED3D_LIGHT_PARALLELPOINT:
1877 ++parallel_point_count;
1878 break;
1879 default:
1880 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1881 break;
1884 point_idx = 0;
1885 spot_idx = point_idx + point_count;
1886 directional_idx = spot_idx + spot_count;
1887 parallel_point_idx = directional_idx + directional_count;
1889 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1890 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1892 const struct wined3d_light_info *light_info = state->lights[i];
1893 unsigned int idx;
1895 if (!light_info)
1896 continue;
1898 switch (light_info->OriginalParms.type)
1900 case WINED3D_LIGHT_POINT:
1901 idx = point_idx++;
1902 break;
1903 case WINED3D_LIGHT_SPOT:
1904 idx = spot_idx++;
1905 break;
1906 case WINED3D_LIGHT_DIRECTIONAL:
1907 idx = directional_idx++;
1908 break;
1909 case WINED3D_LIGHT_PARALLELPOINT:
1910 idx = parallel_point_idx++;
1911 break;
1912 default:
1913 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1914 continue;
1916 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1920 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1921 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1922 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1924 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1925 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1926 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1928 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1929 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1930 pshader->reg_maps.boolean_constants);
1932 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1934 for (i = 0; i < MAX_TEXTURES; ++i)
1936 if (prog->ps.bumpenv_mat_location[i] == -1)
1937 continue;
1939 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1940 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1942 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1944 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1945 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1946 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1947 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1951 checkGLcall("bump env uniforms");
1954 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1956 const struct wined3d_vec4 correction_params =
1958 /* Position is relative to the framebuffer, not the viewport. */
1959 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1960 context->render_offscreen ? 1.0f : -1.0f,
1961 0.0f,
1962 0.0f,
1965 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1968 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1969 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1970 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1971 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1973 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1975 struct wined3d_color color;
1977 if (prog->ps.tex_factor_location != -1)
1979 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1980 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1983 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1984 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1985 else
1986 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1988 for (i = 0; i < MAX_TEXTURES; ++i)
1990 if (prog->ps.tss_constant_location[i] == -1)
1991 continue;
1993 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1994 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1997 checkGLcall("fixed function uniforms");
2000 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
2001 shader_glsl_load_fog_uniform(context, state, prog);
2003 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
2005 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
2007 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
2008 checkGLcall("alpha test emulation uniform");
2011 if (priv->next_constant_version == UINT_MAX)
2013 TRACE("Max constant version reached, resetting to 0.\n");
2014 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
2015 priv->next_constant_version = 1;
2017 else
2019 prog->constant_version = priv->next_constant_version++;
2023 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
2025 struct constant_entry *entries = heap->entries;
2026 unsigned int *positions = heap->positions;
2027 unsigned int heap_idx, parent_idx;
2029 if (!heap->contained[idx])
2031 heap_idx = heap->size++;
2032 heap->contained[idx] = TRUE;
2034 else
2036 heap_idx = positions[idx];
2039 while (heap_idx > 1)
2041 parent_idx = heap_idx >> 1;
2043 if (new_version <= entries[parent_idx].version) break;
2045 entries[heap_idx] = entries[parent_idx];
2046 positions[entries[parent_idx].idx] = heap_idx;
2047 heap_idx = parent_idx;
2050 entries[heap_idx].version = new_version;
2051 entries[heap_idx].idx = idx;
2052 positions[idx] = heap_idx;
2055 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
2057 struct shader_glsl_priv *priv = device->shader_priv;
2058 struct constant_heap *heap = &priv->vconst_heap;
2059 UINT i;
2061 for (i = start; i < count + start; ++i)
2063 update_heap_entry(heap, i, priv->next_constant_version);
2067 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
2069 struct shader_glsl_priv *priv = device->shader_priv;
2070 struct constant_heap *heap = &priv->pconst_heap;
2071 UINT i;
2073 for (i = start; i < count + start; ++i)
2075 update_heap_entry(heap, i, priv->next_constant_version);
2079 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
2081 unsigned int ret = gl_info->limits.glsl_varyings / 4;
2082 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
2083 if(shader_major > 3) return ret;
2085 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
2086 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
2087 return ret;
2090 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
2092 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2095 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2097 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2098 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2101 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2103 return shader_glsl_get_version(gl_info) >= 150;
2106 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2108 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2111 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2112 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2114 va_list args;
2115 int ret;
2117 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2118 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2119 for (;;)
2121 va_start(args, format);
2122 ret = shader_vaddline(buffer, format, args);
2123 va_end(args);
2124 if (!ret)
2125 return;
2126 if (!string_buffer_resize(buffer, ret))
2127 return;
2131 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2132 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2134 va_list args;
2135 int ret;
2137 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2138 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2139 for (;;)
2141 va_start(args, format);
2142 ret = shader_vaddline(buffer, format, args);
2143 va_end(args);
2144 if (!ret)
2145 return;
2146 if (!string_buffer_resize(buffer, ret))
2147 return;
2151 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2153 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2156 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2158 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2161 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2163 switch (mode)
2165 case WINED3DSIM_CONSTANT:
2166 return "flat";
2167 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2168 return "noperspective";
2169 default:
2170 FIXME("Unhandled interpolation mode %#x.\n", mode);
2171 case WINED3DSIM_NONE:
2172 case WINED3DSIM_LINEAR:
2173 return "";
2177 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2178 const DWORD *packed_interpolation_mode, unsigned int register_idx)
2180 return wined3d_extract_bits(packed_interpolation_mode,
2181 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2184 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2185 struct wined3d_string_buffer *buffer, unsigned int element_count,
2186 const DWORD *interpolation_mode, BOOL unroll)
2188 enum wined3d_shader_interpolation_mode mode;
2189 unsigned int i;
2191 if (shader_glsl_use_interface_blocks(gl_info))
2193 if (unroll)
2195 shader_addline(buffer, "in shader_in_out {\n");
2196 for (i = 0; i < element_count; ++i)
2198 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2199 shader_addline(buffer, "%s vec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2201 shader_addline(buffer, "} shader_in;\n");
2203 else
2205 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2208 else
2210 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2214 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2215 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2216 const DWORD *interpolation_mode)
2218 enum wined3d_shader_interpolation_mode mode;
2219 unsigned int i;
2221 if (shader_glsl_use_interface_blocks(gl_info))
2223 if (rasterizer_setup)
2225 shader_addline(buffer, "out shader_in_out {\n");
2226 for (i = 0; i < element_count; ++i)
2228 const char *interpolation_qualifiers = "";
2229 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2231 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2232 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2234 shader_addline(buffer, "%s vec4 reg%u;\n", interpolation_qualifiers, i);
2236 shader_addline(buffer, "} shader_out;\n");
2238 else
2240 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2243 else
2245 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2249 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2251 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
2254 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2256 switch (primitive_type)
2258 case WINED3D_PT_POINTLIST:
2259 return "points";
2261 case WINED3D_PT_LINELIST:
2262 return "lines";
2264 case WINED3D_PT_LINESTRIP:
2265 return "line_strip";
2267 case WINED3D_PT_TRIANGLELIST:
2268 return "triangles";
2270 case WINED3D_PT_TRIANGLESTRIP:
2271 return "triangle_strip";
2273 case WINED3D_PT_LINELIST_ADJ:
2274 return "lines_adjacency";
2276 case WINED3D_PT_TRIANGLELIST_ADJ:
2277 return "triangles_adjacency";
2279 default:
2280 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2281 return "";
2285 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2287 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2288 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2289 DWORD input_reg_used = shader->u.ps.input_reg_used;
2290 unsigned int i;
2292 if (reg_maps->shader_version.major < 3)
2293 return input_reg_used & (1u << idx);
2295 for (i = 0; i < input_signature->element_count; ++i)
2297 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2299 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2300 continue;
2302 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2303 && input->semantic_idx == idx)
2304 return input_reg_used & (1u << input->register_idx);
2306 return FALSE;
2309 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2310 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2312 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2314 if (version->major >= 4)
2315 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2316 else
2317 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2320 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2321 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2322 unsigned int index)
2324 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2325 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2326 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2327 index, scalar_type, scalar_type, index);
2330 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2331 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2333 unsigned int index = e->register_idx;
2335 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2337 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
2338 index);
2339 return;
2341 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2343 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2344 index);
2345 return;
2347 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2348 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2350 if (shader_glsl_use_explicit_attrib_location(gl_info))
2351 shader_addline(buffer, "layout(location = %u) ", index);
2353 switch (e->component_type)
2355 case WINED3D_TYPE_UINT:
2356 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
2357 break;
2358 case WINED3D_TYPE_INT:
2359 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
2360 break;
2362 default:
2363 FIXME("Unhandled type %#x.\n", e->component_type);
2364 /* Fall through. */
2365 case WINED3D_TYPE_UNKNOWN:
2366 case WINED3D_TYPE_FLOAT:
2367 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2368 break;
2372 /** Generate the variable & register declarations for the GLSL output target */
2373 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
2374 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2375 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2377 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2378 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2379 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2380 const struct wined3d_gl_info *gl_info = context->gl_info;
2381 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2382 unsigned int uniform_block_base, uniform_block_count;
2383 const struct wined3d_shader_lconst *lconst;
2384 const char *prefix;
2385 unsigned int i;
2386 DWORD map;
2388 prefix = shader_glsl_get_prefix(version->type);
2390 /* Prototype the subroutines */
2391 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2393 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2396 /* Declare the constants (aka uniforms) */
2397 if (shader->limits->constant_float > 0)
2399 unsigned max_constantsF;
2401 /* Unless the shader uses indirect addressing, always declare the
2402 * maximum array size and ignore that we need some uniforms privately.
2403 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2404 * and immediate values, still declare VC[256]. If the shader needs
2405 * more uniforms than we have it won't work in any case. If it uses
2406 * less, the compiler will figure out which uniforms are really used
2407 * and strip them out. This allows a shader to use c255 on a dx9 card,
2408 * as long as it doesn't also use all the other constants.
2410 * If the shader uses indirect addressing the compiler must assume
2411 * that all declared uniforms are used. In this case, declare only the
2412 * amount that we're assured to have.
2414 * Thus we run into problems in these two cases:
2415 * 1) The shader really uses more uniforms than supported.
2416 * 2) The shader uses indirect addressing, less constants than
2417 * supported, but uses a constant index > #supported consts. */
2418 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2420 /* No indirect addressing here. */
2421 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2423 else
2425 if (reg_maps->usesrelconstF)
2427 /* Subtract the other potential uniforms from the max
2428 * available (bools, ints, and 1 row of projection matrix).
2429 * Subtract another uniform for immediate values, which have
2430 * to be loaded via uniform by the driver as well. The shader
2431 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2432 * shader code, so one vec4 should be enough. (Unfortunately
2433 * the Nvidia driver doesn't store 128 and -128 in one float).
2435 * Writing gl_ClipVertex requires one uniform for each
2436 * clipplane as well. */
2437 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2438 if (vs_args->clip_enabled)
2439 max_constantsF -= gl_info->limits.user_clip_distances;
2440 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2441 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2442 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2443 * for now take this into account when calculating the number of available constants
2445 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2446 /* Set by driver quirks in directx.c */
2447 max_constantsF -= gl_info->reserved_glsl_constants;
2449 if (max_constantsF < shader->limits->constant_float)
2451 static unsigned int once;
2453 if (!once++)
2454 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2455 " it may not render correctly.\n");
2456 else
2457 WARN("The hardware does not support enough uniform components to run this shader.\n");
2460 else
2462 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2465 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2466 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2469 /* Always declare the full set of constants, the compiler can remove the
2470 * unused ones because d3d doesn't (yet) support indirect int and bool
2471 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2472 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2473 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2475 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2476 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2478 /* Declare immediate constant buffer */
2479 if (reg_maps->icb)
2480 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2482 /* Declare constant buffers */
2483 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2484 &uniform_block_base, &uniform_block_count);
2485 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2487 if (reg_maps->cb_sizes[i])
2489 shader_addline(buffer, "layout(std140");
2490 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2491 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2492 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2493 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2497 /* Declare texture samplers */
2498 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2500 struct wined3d_shader_sampler_map_entry *entry;
2501 const char *sampler_type_prefix, *sampler_type;
2502 BOOL shadow_sampler, tex_rect;
2504 entry = &reg_maps->sampler_map.entries[i];
2506 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2508 ERR("Invalid resource index %u.\n", entry->resource_idx);
2509 continue;
2512 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2514 case WINED3D_DATA_FLOAT:
2515 case WINED3D_DATA_UNORM:
2516 case WINED3D_DATA_SNORM:
2517 sampler_type_prefix = "";
2518 break;
2520 case WINED3D_DATA_INT:
2521 sampler_type_prefix = "i";
2522 break;
2524 case WINED3D_DATA_UINT:
2525 sampler_type_prefix = "u";
2526 break;
2528 default:
2529 sampler_type_prefix = "";
2530 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2531 break;
2534 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2535 switch (reg_maps->resource_info[entry->resource_idx].type)
2537 case WINED3D_SHADER_RESOURCE_BUFFER:
2538 sampler_type = "samplerBuffer";
2539 break;
2541 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2542 if (shadow_sampler)
2543 sampler_type = "sampler1DShadow";
2544 else
2545 sampler_type = "sampler1D";
2546 break;
2548 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2549 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2550 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2551 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2552 if (shadow_sampler)
2554 if (tex_rect)
2555 sampler_type = "sampler2DRectShadow";
2556 else
2557 sampler_type = "sampler2DShadow";
2559 else
2561 if (tex_rect)
2562 sampler_type = "sampler2DRect";
2563 else
2564 sampler_type = "sampler2D";
2566 break;
2568 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2569 if (shadow_sampler)
2570 FIXME("Unsupported 3D shadow sampler.\n");
2571 sampler_type = "sampler3D";
2572 break;
2574 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2575 if (shadow_sampler)
2576 sampler_type = "samplerCubeShadow";
2577 else
2578 sampler_type = "samplerCube";
2579 break;
2581 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2582 if (shadow_sampler)
2583 sampler_type = "sampler1DArrayShadow";
2584 else
2585 sampler_type = "sampler1DArray";
2586 break;
2588 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2589 if (shadow_sampler)
2590 sampler_type = "sampler2DArrayShadow";
2591 else
2592 sampler_type = "sampler2DArray";
2593 break;
2595 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2596 if (shadow_sampler)
2597 sampler_type = "samplerCubeArrayShadow";
2598 else
2599 sampler_type = "samplerCubeArray";
2600 break;
2602 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2603 sampler_type = "sampler2DMS";
2604 break;
2606 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2607 sampler_type = "sampler2DMSArray";
2608 break;
2610 default:
2611 sampler_type = "unsupported_sampler";
2612 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2613 break;
2616 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2617 shader_glsl_append_sampler_binding_qualifier(buffer, context, version, entry->bind_idx);
2618 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2619 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2622 /* Declare images */
2623 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2625 const char *image_type_prefix, *image_type, *read_format;
2627 if (!reg_maps->uav_resource_info[i].type)
2628 continue;
2630 switch (reg_maps->uav_resource_info[i].data_type)
2632 case WINED3D_DATA_FLOAT:
2633 case WINED3D_DATA_UNORM:
2634 case WINED3D_DATA_SNORM:
2635 image_type_prefix = "";
2636 read_format = "r32f";
2637 break;
2639 case WINED3D_DATA_INT:
2640 image_type_prefix = "i";
2641 read_format = "r32i";
2642 break;
2644 case WINED3D_DATA_UINT:
2645 image_type_prefix = "u";
2646 read_format = "r32ui";
2647 break;
2649 default:
2650 image_type_prefix = "";
2651 read_format = "";
2652 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2653 break;
2656 switch (reg_maps->uav_resource_info[i].type)
2658 case WINED3D_SHADER_RESOURCE_BUFFER:
2659 image_type = "imageBuffer";
2660 break;
2662 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2663 image_type = "image2D";
2664 break;
2666 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2667 image_type = "image3D";
2668 break;
2670 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2671 image_type = "image2DArray";
2672 break;
2674 default:
2675 image_type = "unsupported_image";
2676 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2677 break;
2680 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2681 shader_addline(buffer, "layout(binding = %u)\n", i);
2682 if (reg_maps->uav_read_mask & (1u << i))
2683 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2684 read_format, image_type_prefix, image_type, prefix, i);
2685 else
2686 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2687 image_type_prefix, image_type, prefix, i);
2689 if (reg_maps->uav_counter_mask & (1u << i))
2690 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2691 i, prefix, i);
2694 /* Declare address variables */
2695 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2697 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2700 /* Declare output register temporaries */
2701 if (shader->limits->packed_output)
2702 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2704 /* Declare temporary variables */
2705 if (reg_maps->temporary_count)
2707 for (i = 0; i < reg_maps->temporary_count; ++i)
2708 shader_addline(buffer, "vec4 R%u;\n", i);
2710 else if (version->major < 4)
2712 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2714 if (map & 1)
2715 shader_addline(buffer, "vec4 R%u;\n", i);
2719 /* Declare indexable temporary variables */
2720 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2722 if (idx_temp_reg->component_count != 4)
2723 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2724 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2727 /* Declare loop registers aLx */
2728 if (version->major < 4)
2730 for (i = 0; i < reg_maps->loop_depth; ++i)
2732 shader_addline(buffer, "int aL%u;\n", i);
2733 shader_addline(buffer, "int tmpInt%u;\n", i);
2737 /* Temporary variables for matrix operations */
2738 shader_addline(buffer, "vec4 tmp0;\n");
2739 shader_addline(buffer, "vec4 tmp1;\n");
2741 if (!shader->load_local_constsF)
2743 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2745 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2746 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2747 shader_addline(buffer, ";\n");
2752 /* Prototypes */
2753 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2754 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2756 /** Used for opcode modifiers - They multiply the result by the specified amount */
2757 static const char * const shift_glsl_tab[] = {
2758 "", /* 0 (none) */
2759 "2.0 * ", /* 1 (x2) */
2760 "4.0 * ", /* 2 (x4) */
2761 "8.0 * ", /* 3 (x8) */
2762 "16.0 * ", /* 4 (x16) */
2763 "32.0 * ", /* 5 (x32) */
2764 "", /* 6 (x64) */
2765 "", /* 7 (x128) */
2766 "", /* 8 (d256) */
2767 "", /* 9 (d128) */
2768 "", /* 10 (d64) */
2769 "", /* 11 (d32) */
2770 "0.0625 * ", /* 12 (d16) */
2771 "0.125 * ", /* 13 (d8) */
2772 "0.25 * ", /* 14 (d4) */
2773 "0.5 * " /* 15 (d2) */
2776 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2777 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2778 const char *in_reg, const char *in_regswizzle, char *out_str)
2780 switch (src_modifier)
2782 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2783 case WINED3DSPSM_DW:
2784 case WINED3DSPSM_NONE:
2785 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2786 break;
2787 case WINED3DSPSM_NEG:
2788 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2789 break;
2790 case WINED3DSPSM_NOT:
2791 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2792 break;
2793 case WINED3DSPSM_BIAS:
2794 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2795 break;
2796 case WINED3DSPSM_BIASNEG:
2797 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2798 break;
2799 case WINED3DSPSM_SIGN:
2800 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2801 break;
2802 case WINED3DSPSM_SIGNNEG:
2803 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2804 break;
2805 case WINED3DSPSM_COMP:
2806 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2807 break;
2808 case WINED3DSPSM_X2:
2809 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2810 break;
2811 case WINED3DSPSM_X2NEG:
2812 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2813 break;
2814 case WINED3DSPSM_ABS:
2815 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2816 break;
2817 case WINED3DSPSM_ABSNEG:
2818 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2819 break;
2820 default:
2821 FIXME("Unhandled modifier %u\n", src_modifier);
2822 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2826 static void shader_glsl_fixup_scalar_register_variable(char *register_name,
2827 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2829 /* The ARB_shading_language_420pack extension allows swizzle operations on
2830 * scalars. */
2831 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2832 sprintf(register_name, "%s", glsl_variable);
2833 else
2834 sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2837 /** Writes the GLSL variable name that corresponds to the register that the
2838 * DX opcode parameter is trying to access */
2839 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2840 enum wined3d_data_type data_type, char *register_name, BOOL *is_color,
2841 const struct wined3d_shader_instruction *ins)
2843 /* oPos, oFog and oPts in D3D */
2844 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2846 const struct wined3d_shader *shader = ins->ctx->shader;
2847 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2848 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2849 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2850 const char *prefix = shader_glsl_get_prefix(version->type);
2851 struct glsl_src_param rel_param0, rel_param1;
2852 char imm_str[4][17];
2854 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2855 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2856 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2857 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2858 *is_color = FALSE;
2860 switch (reg->type)
2862 case WINED3DSPR_TEMP:
2863 sprintf(register_name, "R%u", reg->idx[0].offset);
2864 break;
2866 case WINED3DSPR_INPUT:
2867 case WINED3DSPR_INCONTROLPOINT:
2868 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2870 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2872 if (reg->idx[0].rel_addr)
2873 FIXME("VS3 input registers relative addressing.\n");
2874 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2875 *is_color = TRUE;
2876 if (reg->idx[0].rel_addr)
2878 sprintf(register_name, "%s_in[%s + %u]",
2879 prefix, rel_param0.param_str, reg->idx[0].offset);
2881 else
2883 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2885 break;
2888 if (version->type == WINED3D_SHADER_TYPE_HULL
2889 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2890 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2892 if (reg->idx[0].rel_addr)
2894 if (reg->idx[1].rel_addr)
2895 sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2896 rel_param0.param_str, reg->idx[0].offset,
2897 rel_param1.param_str, reg->idx[1].offset);
2898 else
2899 sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2900 rel_param0.param_str, reg->idx[0].offset,
2901 reg->idx[1].offset);
2903 else if (reg->idx[1].rel_addr)
2904 sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2905 rel_param1.param_str, reg->idx[1].offset);
2906 else
2907 sprintf(register_name, "shader_in[%u].reg[%u]",
2908 reg->idx[0].offset, reg->idx[1].offset);
2909 break;
2912 /* pixel shaders >= 3.0 */
2913 if (version->major >= 3)
2915 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2916 unsigned int in_count = vec4_varyings(version->major, gl_info);
2918 if (reg->idx[0].rel_addr)
2920 /* Removing a + 0 would be an obvious optimization, but
2921 * OS X doesn't see the NOP operation there. */
2922 if (idx)
2924 if (needs_legacy_glsl_syntax(gl_info)
2925 && shader->u.ps.declared_in_count > in_count)
2927 sprintf(register_name,
2928 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2929 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2930 prefix, rel_param0.param_str, idx);
2932 else
2934 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2937 else
2939 if (needs_legacy_glsl_syntax(gl_info)
2940 && shader->u.ps.declared_in_count > in_count)
2942 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2943 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2944 prefix, rel_param0.param_str);
2946 else
2948 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2952 else
2954 if (idx == in_count) sprintf(register_name, "gl_Color");
2955 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2956 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2959 else
2961 if (!reg->idx[0].offset)
2962 strcpy(register_name, "ffp_varying_diffuse");
2963 else
2964 strcpy(register_name, "ffp_varying_specular");
2965 break;
2967 break;
2969 case WINED3DSPR_CONST:
2971 /* Relative addressing */
2972 if (reg->idx[0].rel_addr)
2974 if (wined3d_settings.check_float_constants)
2975 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2976 rel_param0.param_str, reg->idx[0].offset,
2977 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2978 prefix, rel_param0.param_str, reg->idx[0].offset);
2979 else if (reg->idx[0].offset)
2980 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2981 else
2982 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2984 else
2986 if (shader_constant_is_local(shader, reg->idx[0].offset))
2987 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2988 else
2989 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2992 break;
2994 case WINED3DSPR_CONSTINT:
2995 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2996 break;
2998 case WINED3DSPR_CONSTBOOL:
2999 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
3000 break;
3002 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
3003 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
3004 sprintf(register_name, "T%u", reg->idx[0].offset);
3005 else
3006 sprintf(register_name, "A%u", reg->idx[0].offset);
3007 break;
3009 case WINED3DSPR_LOOP:
3010 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
3011 break;
3013 case WINED3DSPR_SAMPLER:
3014 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
3015 break;
3017 case WINED3DSPR_COLOROUT:
3018 if (reg->idx[0].offset >= gl_info->limits.buffers)
3019 WARN("Write to render target %u, only %d supported.\n",
3020 reg->idx[0].offset, gl_info->limits.buffers);
3022 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
3023 break;
3025 case WINED3DSPR_RASTOUT:
3026 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
3027 break;
3029 case WINED3DSPR_DEPTHOUT:
3030 case WINED3DSPR_DEPTHOUTGE:
3031 case WINED3DSPR_DEPTHOUTLE:
3032 sprintf(register_name, "gl_FragDepth");
3033 break;
3035 case WINED3DSPR_ATTROUT:
3036 if (!reg->idx[0].offset)
3037 sprintf(register_name, "%s_out[8]", prefix);
3038 else
3039 sprintf(register_name, "%s_out[9]", prefix);
3040 break;
3042 case WINED3DSPR_TEXCRDOUT:
3043 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
3044 if (reg->idx[0].rel_addr)
3045 sprintf(register_name, "%s_out[%s + %u]",
3046 prefix, rel_param0.param_str, reg->idx[0].offset);
3047 else
3048 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
3049 break;
3051 case WINED3DSPR_MISCTYPE:
3052 if (!reg->idx[0].offset)
3054 /* vPos */
3055 sprintf(register_name, "vpos");
3057 else if (reg->idx[0].offset == 1)
3059 /* Note that gl_FrontFacing is a bool, while vFace is
3060 * a float for which the sign determines front/back */
3061 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
3063 else
3065 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3066 sprintf(register_name, "unrecognized_register");
3068 break;
3070 case WINED3DSPR_IMMCONST:
3071 switch (reg->immconst_type)
3073 case WINED3D_IMMCONST_SCALAR:
3074 switch (data_type)
3076 case WINED3D_DATA_FLOAT:
3077 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3078 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
3079 else
3080 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
3081 break;
3082 case WINED3D_DATA_INT:
3083 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3084 break;
3085 case WINED3D_DATA_RESOURCE:
3086 case WINED3D_DATA_SAMPLER:
3087 case WINED3D_DATA_UINT:
3088 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3089 break;
3090 default:
3091 sprintf(register_name, "<unhandled data type %#x>", data_type);
3092 break;
3094 break;
3096 case WINED3D_IMMCONST_VEC4:
3097 switch (data_type)
3099 case WINED3D_DATA_FLOAT:
3100 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
3102 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
3103 reg->u.immconst_data[0], reg->u.immconst_data[1],
3104 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3106 else
3108 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
3109 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
3110 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
3111 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
3112 sprintf(register_name, "vec4(%s, %s, %s, %s)",
3113 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
3115 break;
3116 case WINED3D_DATA_INT:
3117 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3118 reg->u.immconst_data[0], reg->u.immconst_data[1],
3119 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3120 break;
3121 case WINED3D_DATA_RESOURCE:
3122 case WINED3D_DATA_SAMPLER:
3123 case WINED3D_DATA_UINT:
3124 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3125 reg->u.immconst_data[0], reg->u.immconst_data[1],
3126 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3127 break;
3128 default:
3129 sprintf(register_name, "<unhandled data type %#x>", data_type);
3130 break;
3132 break;
3134 default:
3135 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3136 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3138 break;
3140 case WINED3DSPR_CONSTBUFFER:
3141 if (reg->idx[1].rel_addr)
3142 sprintf(register_name, "%s_cb%u[%s + %u]",
3143 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3144 else
3145 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3146 break;
3148 case WINED3DSPR_IMMCONSTBUFFER:
3149 if (reg->idx[0].rel_addr)
3150 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
3151 else
3152 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3153 break;
3155 case WINED3DSPR_PRIMID:
3156 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3157 sprintf(register_name, "gl_PrimitiveIDIn");
3158 else
3159 sprintf(register_name, "gl_PrimitiveID");
3160 break;
3162 case WINED3DSPR_IDXTEMP:
3163 if (reg->idx[1].rel_addr)
3164 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3165 else
3166 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3167 break;
3169 case WINED3DSPR_LOCALTHREADINDEX:
3170 shader_glsl_fixup_scalar_register_variable(register_name,
3171 "int(gl_LocalInvocationIndex)", gl_info);
3172 break;
3174 case WINED3DSPR_GSINSTID:
3175 case WINED3DSPR_OUTPOINTID:
3176 shader_glsl_fixup_scalar_register_variable(register_name,
3177 "gl_InvocationID", gl_info);
3178 break;
3180 case WINED3DSPR_THREADID:
3181 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3182 break;
3184 case WINED3DSPR_THREADGROUPID:
3185 sprintf(register_name, "ivec3(gl_WorkGroupID)");
3186 break;
3188 case WINED3DSPR_LOCALTHREADID:
3189 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3190 break;
3192 case WINED3DSPR_FORKINSTID:
3193 case WINED3DSPR_JOININSTID:
3194 shader_glsl_fixup_scalar_register_variable(register_name,
3195 "phase_instance_id", gl_info);
3196 break;
3198 case WINED3DSPR_TESSCOORD:
3199 sprintf(register_name, "gl_TessCoord");
3200 break;
3202 case WINED3DSPR_OUTCONTROLPOINT:
3203 if (reg->idx[0].rel_addr)
3205 if (reg->idx[1].rel_addr)
3206 sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3207 rel_param0.param_str, reg->idx[0].offset,
3208 rel_param1.param_str, reg->idx[1].offset);
3209 else
3210 sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3211 rel_param0.param_str, reg->idx[0].offset,
3212 reg->idx[1].offset);
3214 else if (reg->idx[1].rel_addr)
3216 sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3217 reg->idx[0].offset, rel_param1.param_str,
3218 reg->idx[1].offset);
3220 else
3222 sprintf(register_name, "shader_out[%u].reg[%u]",
3223 reg->idx[0].offset, reg->idx[1].offset);
3225 break;
3227 case WINED3DSPR_PATCHCONST:
3228 if (version->type == WINED3D_SHADER_TYPE_HULL)
3229 sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3230 else
3231 sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3232 break;
3234 default:
3235 FIXME("Unhandled register type %#x.\n", reg->type);
3236 sprintf(register_name, "unrecognized_register");
3237 break;
3241 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3243 *str++ = '.';
3244 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3245 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3246 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3247 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3248 *str = '\0';
3251 /* Get the GLSL write mask for the destination register */
3252 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3254 DWORD mask = param->write_mask;
3256 if (shader_is_scalar(&param->reg))
3258 mask = WINED3DSP_WRITEMASK_0;
3259 *write_mask = '\0';
3261 else
3263 shader_glsl_write_mask_to_str(mask, write_mask);
3266 return mask;
3269 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3271 unsigned int size = 0;
3273 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3274 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3275 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3276 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3278 return size;
3281 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3282 unsigned int component_idx)
3284 /* swizzle bits fields: wwzzyyxx */
3285 return (swizzle >> (2 * component_idx)) & 0x3;
3288 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3290 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3291 * but addressed as "rgba". To fix this we need to swap the register's x
3292 * and z components. */
3293 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3294 unsigned int i;
3296 *str++ = '.';
3297 for (i = 0; i < 4; ++i)
3299 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3300 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3302 *str = '\0';
3305 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3306 BOOL fixup, DWORD mask, char *swizzle_str)
3308 if (shader_is_scalar(&param->reg))
3309 *swizzle_str = '\0';
3310 else
3311 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3314 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3315 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3317 if (dst_data_type == src_data_type)
3319 string_buffer_sprintf(dst_param, "%s", src_param);
3320 return;
3323 if (src_data_type == WINED3D_DATA_FLOAT)
3325 switch (dst_data_type)
3327 case WINED3D_DATA_INT:
3328 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3329 return;
3330 case WINED3D_DATA_RESOURCE:
3331 case WINED3D_DATA_SAMPLER:
3332 case WINED3D_DATA_UINT:
3333 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3334 return;
3335 default:
3336 break;
3340 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3342 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3343 return;
3346 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3348 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3349 return;
3352 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3353 string_buffer_sprintf(dst_param, "%s", src_param);
3356 /* From a given parameter token, generate the corresponding GLSL string.
3357 * Also, return the actual register name and swizzle in case the
3358 * caller needs this information as well. */
3359 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3360 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3361 enum wined3d_data_type data_type)
3363 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3364 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3365 enum wined3d_data_type param_data_type;
3366 BOOL is_color = FALSE;
3367 char swizzle_str[6];
3369 glsl_src->reg_name[0] = '\0';
3370 glsl_src->param_str[0] = '\0';
3371 swizzle_str[0] = '\0';
3373 shader_glsl_get_register_name(&wined3d_src->reg, data_type, glsl_src->reg_name, &is_color, ins);
3374 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3376 switch (wined3d_src->reg.type)
3378 case WINED3DSPR_IMMCONST:
3379 param_data_type = data_type;
3380 break;
3381 case WINED3DSPR_FORKINSTID:
3382 case WINED3DSPR_GSINSTID:
3383 case WINED3DSPR_JOININSTID:
3384 case WINED3DSPR_LOCALTHREADID:
3385 case WINED3DSPR_LOCALTHREADINDEX:
3386 case WINED3DSPR_OUTPOINTID:
3387 case WINED3DSPR_PRIMID:
3388 case WINED3DSPR_THREADGROUPID:
3389 case WINED3DSPR_THREADID:
3390 param_data_type = WINED3D_DATA_INT;
3391 break;
3392 default:
3393 param_data_type = WINED3D_DATA_FLOAT;
3394 break;
3397 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3398 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3400 string_buffer_release(priv->string_buffers, reg_name);
3403 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3404 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3406 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3409 /* From a given parameter token, generate the corresponding GLSL string.
3410 * Also, return the actual register name and swizzle in case the
3411 * caller needs this information as well. */
3412 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3413 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3415 BOOL is_color = FALSE;
3417 glsl_dst->mask_str[0] = '\0';
3418 glsl_dst->reg_name[0] = '\0';
3420 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type,
3421 glsl_dst->reg_name, &is_color, ins);
3422 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3425 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3426 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3427 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3428 enum wined3d_data_type data_type)
3430 struct glsl_dst_param glsl_dst;
3431 DWORD mask;
3433 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3435 switch (data_type)
3437 case WINED3D_DATA_FLOAT:
3438 shader_addline(buffer, "%s%s = %s(",
3439 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3440 break;
3441 case WINED3D_DATA_INT:
3442 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3443 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3444 break;
3445 case WINED3D_DATA_RESOURCE:
3446 case WINED3D_DATA_SAMPLER:
3447 case WINED3D_DATA_UINT:
3448 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3449 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3450 break;
3451 default:
3452 FIXME("Unhandled data type %#x.\n", data_type);
3453 shader_addline(buffer, "%s%s = %s(",
3454 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3455 break;
3459 return mask;
3462 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3463 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3465 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3468 /** Process GLSL instruction modifiers */
3469 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3471 struct glsl_dst_param dst_param;
3472 DWORD modifiers;
3474 if (!ins->dst_count) return;
3476 modifiers = ins->dst[0].modifiers;
3477 if (!modifiers) return;
3479 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3481 if (modifiers & WINED3DSPDM_SATURATE)
3483 /* _SAT means to clamp the value of the register to between 0 and 1 */
3484 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3485 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3488 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3490 FIXME("_centroid modifier not handled\n");
3493 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3495 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3499 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3501 switch (op)
3503 case WINED3D_SHADER_REL_OP_GT: return ">";
3504 case WINED3D_SHADER_REL_OP_EQ: return "==";
3505 case WINED3D_SHADER_REL_OP_GE: return ">=";
3506 case WINED3D_SHADER_REL_OP_LT: return "<";
3507 case WINED3D_SHADER_REL_OP_NE: return "!=";
3508 case WINED3D_SHADER_REL_OP_LE: return "<=";
3509 default:
3510 FIXME("Unrecognized operator %#x.\n", op);
3511 return "(\?\?)";
3515 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3517 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3520 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3521 unsigned int *coord_size, unsigned int *deriv_size)
3523 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3524 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3526 *coord_size = resource_type_info[resource_type].coord_size;
3527 *deriv_size = *coord_size;
3528 if (is_array)
3529 --(*deriv_size);
3532 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3533 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3535 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3536 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3537 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3538 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3539 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3540 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3541 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3542 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3543 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3544 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3545 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3546 const char *base = "texture", *type_part = "", *suffix = "";
3547 unsigned int coord_size, deriv_size;
3549 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3551 if (resource_type >= ARRAY_SIZE(resource_type_info))
3553 ERR("Unexpected resource type %#x.\n", resource_type);
3554 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3557 /* Note that there's no such thing as a projected cube texture. */
3558 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3559 projected = FALSE;
3561 if (needs_legacy_glsl_syntax(gl_info))
3563 if (shadow)
3564 base = "shadow";
3566 type_part = resource_type_info[resource_type].type_part;
3567 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3568 type_part = "2DRect";
3569 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3570 FIXME("Unhandled resource type %#x.\n", resource_type);
3572 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3574 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3575 suffix = "ARB";
3576 else
3577 FIXME("Unsupported grad function.\n");
3581 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3583 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3584 if (flags & ~texel_fetch_flags)
3585 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3587 base = "texelFetch";
3588 type_part = "";
3591 sample_function->name = string_buffer_get(priv->string_buffers);
3592 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3593 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3595 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3596 if (shadow)
3597 ++coord_size;
3598 sample_function->offset_size = offset ? deriv_size : 0;
3599 sample_function->coord_mask = (1u << coord_size) - 1;
3600 sample_function->deriv_mask = (1u << deriv_size) - 1;
3601 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3604 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3605 struct glsl_sample_function *sample_function)
3607 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3609 string_buffer_release(priv->string_buffers, sample_function->name);
3612 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3613 BOOL sign_fixup, enum fixup_channel_source channel_source)
3615 switch(channel_source)
3617 case CHANNEL_SOURCE_ZERO:
3618 strcat(arguments, "0.0");
3619 break;
3621 case CHANNEL_SOURCE_ONE:
3622 strcat(arguments, "1.0");
3623 break;
3625 case CHANNEL_SOURCE_X:
3626 strcat(arguments, reg_name);
3627 strcat(arguments, ".x");
3628 break;
3630 case CHANNEL_SOURCE_Y:
3631 strcat(arguments, reg_name);
3632 strcat(arguments, ".y");
3633 break;
3635 case CHANNEL_SOURCE_Z:
3636 strcat(arguments, reg_name);
3637 strcat(arguments, ".z");
3638 break;
3640 case CHANNEL_SOURCE_W:
3641 strcat(arguments, reg_name);
3642 strcat(arguments, ".w");
3643 break;
3645 default:
3646 FIXME("Unhandled channel source %#x\n", channel_source);
3647 strcat(arguments, "undefined");
3648 break;
3651 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3654 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3655 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3657 unsigned int mask_size, remaining;
3658 DWORD fixup_mask = 0;
3659 char arguments[256];
3660 char mask_str[6];
3662 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3663 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3664 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3665 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3666 if (!(mask &= fixup_mask))
3667 return;
3669 if (is_complex_fixup(fixup))
3671 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3672 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3673 return;
3676 shader_glsl_write_mask_to_str(mask, mask_str);
3677 mask_size = shader_glsl_get_write_mask_size(mask);
3679 arguments[0] = '\0';
3680 remaining = mask_size;
3681 if (mask & WINED3DSP_WRITEMASK_0)
3683 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3684 if (--remaining) strcat(arguments, ", ");
3686 if (mask & WINED3DSP_WRITEMASK_1)
3688 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3689 if (--remaining) strcat(arguments, ", ");
3691 if (mask & WINED3DSP_WRITEMASK_2)
3693 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3694 if (--remaining) strcat(arguments, ", ");
3696 if (mask & WINED3DSP_WRITEMASK_3)
3698 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3699 if (--remaining) strcat(arguments, ", ");
3702 if (mask_size > 1)
3703 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3704 else
3705 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3708 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3710 char reg_name[256];
3711 BOOL is_color;
3713 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, &is_color, ins);
3714 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3717 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3718 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3719 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3720 const char *coord_reg_fmt, ...)
3722 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3723 char dst_swizzle[6];
3724 struct color_fixup_desc fixup;
3725 BOOL np2_fixup = FALSE;
3726 va_list args;
3727 int ret;
3729 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3731 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3732 * We actually rely on it for vertex shaders and SM4+. */
3733 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3735 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3736 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3738 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3739 np2_fixup = TRUE;
3741 else
3743 fixup = COLOR_FIXUP_IDENTITY;
3746 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3748 if (sample_function->output_single_component)
3749 shader_addline(ins->ctx->buffer, "vec4(");
3751 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3752 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3754 for (;;)
3756 va_start(args, coord_reg_fmt);
3757 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3758 va_end(args);
3759 if (!ret)
3760 break;
3761 if (!string_buffer_resize(ins->ctx->buffer, ret))
3762 break;
3765 if (np2_fixup)
3767 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3768 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3770 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3772 case 1:
3773 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3774 idx >> 1, (idx % 2) ? "z" : "x");
3775 break;
3776 case 2:
3777 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3778 idx >> 1, (idx % 2) ? "zw" : "xy");
3779 break;
3780 case 3:
3781 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3782 idx >> 1, (idx % 2) ? "zw" : "xy");
3783 break;
3784 case 4:
3785 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3786 idx >> 1, (idx % 2) ? "zw" : "xy");
3787 break;
3790 if (dx && dy)
3791 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3792 else if (bias)
3793 shader_addline(ins->ctx->buffer, ", %s", bias);
3794 if (sample_function->offset_size)
3796 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3797 shader_addline(ins->ctx->buffer, ", ");
3798 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3800 shader_addline(ins->ctx->buffer, ")");
3802 if (sample_function->output_single_component)
3803 shader_addline(ins->ctx->buffer, ")");
3805 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3807 if (!is_identity_fixup(fixup))
3808 shader_glsl_color_correction(ins, fixup);
3811 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3813 /* Write the final position.
3815 * OpenGL coordinates specify the center of the pixel while D3D coords
3816 * specify the corner. The offsets are stored in z and w in
3817 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3818 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3819 * a MAD. */
3820 if (use_viewport_index)
3822 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3823 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3825 else
3827 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3828 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3831 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3832 * in utils.c
3834 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3835 * shaders are run before the homogeneous divide, so we have to take the w
3836 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3837 * z = z * 2 - w. */
3838 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3841 /*****************************************************************************
3842 * Begin processing individual instruction opcodes
3843 ****************************************************************************/
3845 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3847 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3848 struct glsl_src_param src0_param;
3849 struct glsl_src_param src1_param;
3850 DWORD write_mask;
3851 const char *op;
3853 /* Determine the GLSL operator to use based on the opcode */
3854 switch (ins->handler_idx)
3856 case WINED3DSIH_ADD: op = "+"; break;
3857 case WINED3DSIH_AND: op = "&"; break;
3858 case WINED3DSIH_DIV: op = "/"; break;
3859 case WINED3DSIH_IADD: op = "+"; break;
3860 case WINED3DSIH_ISHL: op = "<<"; break;
3861 case WINED3DSIH_ISHR: op = ">>"; break;
3862 case WINED3DSIH_MUL: op = "*"; break;
3863 case WINED3DSIH_OR: op = "|"; break;
3864 case WINED3DSIH_SUB: op = "-"; break;
3865 case WINED3DSIH_USHR: op = ">>"; break;
3866 case WINED3DSIH_XOR: op = "^"; break;
3867 default:
3868 op = "<unhandled operator>";
3869 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3870 break;
3873 write_mask = shader_glsl_append_dst(buffer, ins);
3874 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3875 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3876 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3879 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3881 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3882 struct glsl_src_param src0_param;
3883 struct glsl_src_param src1_param;
3884 unsigned int mask_size;
3885 DWORD write_mask;
3886 const char *op;
3888 write_mask = shader_glsl_append_dst(buffer, ins);
3889 mask_size = shader_glsl_get_write_mask_size(write_mask);
3890 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3891 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3893 if (mask_size > 1)
3895 switch (ins->handler_idx)
3897 case WINED3DSIH_EQ: op = "equal"; break;
3898 case WINED3DSIH_IEQ: op = "equal"; break;
3899 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3900 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3901 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3902 case WINED3DSIH_LT: op = "lessThan"; break;
3903 case WINED3DSIH_ILT: op = "lessThan"; break;
3904 case WINED3DSIH_ULT: op = "lessThan"; break;
3905 case WINED3DSIH_NE: op = "notEqual"; break;
3906 case WINED3DSIH_INE: op = "notEqual"; break;
3907 default:
3908 op = "<unhandled operator>";
3909 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3910 break;
3913 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3914 mask_size, op, src0_param.param_str, src1_param.param_str);
3916 else
3918 switch (ins->handler_idx)
3920 case WINED3DSIH_EQ: op = "=="; break;
3921 case WINED3DSIH_IEQ: op = "=="; break;
3922 case WINED3DSIH_GE: op = ">="; break;
3923 case WINED3DSIH_IGE: op = ">="; break;
3924 case WINED3DSIH_UGE: op = ">="; break;
3925 case WINED3DSIH_LT: op = "<"; break;
3926 case WINED3DSIH_ILT: op = "<"; break;
3927 case WINED3DSIH_ULT: op = "<"; break;
3928 case WINED3DSIH_NE: op = "!="; break;
3929 case WINED3DSIH_INE: op = "!="; break;
3930 default:
3931 op = "<unhandled operator>";
3932 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3933 break;
3936 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3937 src0_param.param_str, op, src1_param.param_str);
3941 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3943 struct glsl_src_param src_param;
3944 DWORD write_mask;
3945 const char *op;
3947 switch (ins->handler_idx)
3949 case WINED3DSIH_INEG: op = "-"; break;
3950 case WINED3DSIH_NOT: op = "~"; break;
3951 default:
3952 op = "<unhandled operator>";
3953 ERR("Unhandled opcode %s.\n",
3954 debug_d3dshaderinstructionhandler(ins->handler_idx));
3955 break;
3958 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3959 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3960 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3963 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3965 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3966 struct glsl_src_param src0_param;
3967 struct glsl_src_param src1_param;
3968 DWORD write_mask;
3970 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3971 * If not, we can emulate it. */
3972 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3973 FIXME("64-bit integer multiplies not implemented.\n");
3975 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3977 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3978 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3979 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3981 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3982 src0_param.param_str, src1_param.param_str);
3986 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3988 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3989 struct glsl_src_param src0_param, src1_param;
3990 DWORD write_mask;
3992 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3994 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3996 char dst_mask[6];
3998 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3999 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4000 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4001 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
4002 dst_mask, src0_param.param_str, src1_param.param_str);
4004 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4005 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4006 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4007 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4009 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4010 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4012 else
4014 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4015 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4016 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4017 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
4020 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4022 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4023 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4024 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4025 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
4029 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
4030 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
4032 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4033 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4034 struct glsl_src_param src0_param;
4035 DWORD write_mask;
4037 write_mask = shader_glsl_append_dst(buffer, ins);
4038 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4040 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
4041 * shader versions WINED3DSIO_MOVA is used for this. */
4042 if (ins->ctx->reg_maps->shader_version.major == 1
4043 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
4044 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
4046 /* This is a simple floor() */
4047 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4048 if (mask_size > 1) {
4049 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4050 } else {
4051 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4054 else if (ins->handler_idx == WINED3DSIH_MOVA)
4056 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4058 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4060 if (mask_size > 1)
4061 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4062 else
4063 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4065 else
4067 if (mask_size > 1)
4068 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4069 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4070 else
4071 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4072 src0_param.param_str, src0_param.param_str);
4075 else
4077 shader_addline(buffer, "%s);\n", src0_param.param_str);
4081 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4082 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4084 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4085 struct glsl_src_param src0_param;
4086 struct glsl_src_param src1_param;
4087 DWORD dst_write_mask, src_write_mask;
4088 unsigned int dst_size;
4090 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4091 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4093 /* dp4 works on vec4, dp3 on vec3, etc. */
4094 if (ins->handler_idx == WINED3DSIH_DP4)
4095 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4096 else if (ins->handler_idx == WINED3DSIH_DP3)
4097 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4098 else
4099 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4101 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4102 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4104 if (dst_size > 1) {
4105 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4106 } else {
4107 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4111 /* Note that this instruction has some restrictions. The destination write mask
4112 * can't contain the w component, and the source swizzles have to be .xyzw */
4113 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4115 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4116 struct glsl_src_param src0_param;
4117 struct glsl_src_param src1_param;
4118 char dst_mask[6];
4120 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4121 shader_glsl_append_dst(ins->ctx->buffer, ins);
4122 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4123 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4124 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4127 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4129 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4131 if (!stream)
4132 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4133 else
4134 FIXME("Unhandled primitive stream %u.\n", stream);
4137 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4138 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4139 * GLSL uses the value as-is. */
4140 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4142 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4143 struct glsl_src_param src0_param;
4144 struct glsl_src_param src1_param;
4145 DWORD dst_write_mask;
4146 unsigned int dst_size;
4148 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4149 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4151 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4152 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4154 if (dst_size > 1)
4156 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4157 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4159 else
4161 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4162 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4166 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4167 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4169 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4170 struct glsl_src_param src_param;
4171 const char *instruction;
4172 DWORD write_mask;
4173 unsigned i;
4175 /* Determine the GLSL function to use based on the opcode */
4176 /* TODO: Possibly make this a table for faster lookups */
4177 switch (ins->handler_idx)
4179 case WINED3DSIH_ABS: instruction = "abs"; break;
4180 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4181 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4182 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4183 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4184 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4185 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4186 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4187 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4188 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4189 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4190 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4191 case WINED3DSIH_FRC: instruction = "fract"; break;
4192 case WINED3DSIH_IMAX: instruction = "max"; break;
4193 case WINED3DSIH_IMIN: instruction = "min"; break;
4194 case WINED3DSIH_MAX: instruction = "max"; break;
4195 case WINED3DSIH_MIN: instruction = "min"; break;
4196 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4197 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4198 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4199 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4200 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4201 case WINED3DSIH_UMAX: instruction = "max"; break;
4202 case WINED3DSIH_UMIN: instruction = "min"; break;
4203 default: instruction = "";
4204 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4205 break;
4208 write_mask = shader_glsl_append_dst(buffer, ins);
4210 /* In D3D bits are numbered from the most significant bit. */
4211 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4212 shader_addline(buffer, "31 - ");
4213 shader_addline(buffer, "%s(", instruction);
4215 if (ins->src_count)
4217 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4218 shader_addline(buffer, "%s", src_param.param_str);
4219 for (i = 1; i < ins->src_count; ++i)
4221 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4222 shader_addline(buffer, ", %s", src_param.param_str);
4226 shader_addline(buffer, "));\n");
4229 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4231 struct wined3d_shader_dst_param dst;
4232 struct glsl_src_param src;
4233 DWORD write_mask;
4234 const char *fmt;
4235 unsigned int i;
4237 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4238 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4240 dst = ins->dst[0];
4241 for (i = 0; i < 4; ++i)
4243 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4244 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4245 &dst, dst.reg.data_type)))
4246 continue;
4248 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4249 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4253 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4255 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4256 struct wined3d_shader_dst_param dst;
4257 struct glsl_src_param src[4];
4258 const char *instruction;
4259 BOOL tmp_dst = FALSE;
4260 char mask_char[6];
4261 unsigned int i, j;
4262 DWORD write_mask;
4264 switch (ins->handler_idx)
4266 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4267 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4268 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4269 default:
4270 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4271 return;
4274 for (i = 0; i < ins->src_count; ++i)
4276 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4277 && ins->dst[0].reg.type == ins->src[i].reg.type)
4278 tmp_dst = TRUE;
4281 dst = ins->dst[0];
4282 for (i = 0; i < 4; ++i)
4284 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4285 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4286 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4287 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4288 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4289 continue;
4291 for (j = 0; j < ins->src_count; ++j)
4292 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4293 shader_addline(buffer, "%s(", instruction);
4294 for (j = 0; j < ins->src_count - 2; ++j)
4295 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4296 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4299 if (tmp_dst)
4301 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4302 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4303 shader_addline(buffer, "tmp0%s);\n", mask_char);
4307 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4309 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4311 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4312 struct glsl_src_param src_param;
4313 unsigned int mask_size;
4314 DWORD write_mask;
4315 char dst_mask[6];
4317 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4318 mask_size = shader_glsl_get_write_mask_size(write_mask);
4319 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4321 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4322 src_param.param_str, src_param.param_str);
4323 shader_glsl_append_dst(buffer, ins);
4325 if (mask_size > 1)
4327 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
4328 mask_size, src_param.param_str);
4330 else
4332 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
4333 src_param.param_str);
4337 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4339 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4340 ins->ctx->reg_maps->shader_version.minor);
4341 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4342 struct glsl_src_param src0_param;
4343 const char *prefix, *suffix;
4344 unsigned int dst_size;
4345 DWORD dst_write_mask;
4347 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4348 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4350 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4351 dst_write_mask = WINED3DSP_WRITEMASK_3;
4353 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4355 switch (ins->handler_idx)
4357 case WINED3DSIH_EXP:
4358 case WINED3DSIH_EXPP:
4359 prefix = "exp2(";
4360 suffix = ")";
4361 break;
4363 case WINED3DSIH_LOG:
4364 case WINED3DSIH_LOGP:
4365 prefix = "log2(abs(";
4366 suffix = "))";
4367 break;
4369 case WINED3DSIH_RCP:
4370 prefix = "1.0 / ";
4371 suffix = "";
4372 break;
4374 case WINED3DSIH_RSQ:
4375 prefix = "inversesqrt(abs(";
4376 suffix = "))";
4377 break;
4379 default:
4380 prefix = "";
4381 suffix = "";
4382 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4383 break;
4386 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4387 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4388 else
4389 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4392 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4393 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4394 * dst.x = 2^(floor(src))
4395 * dst.y = src - floor(src)
4396 * dst.z = 2^src (partial precision is allowed, but optional)
4397 * dst.w = 1.0;
4398 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4399 * dst = 2^src; (partial precision is allowed, but optional)
4401 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4403 if (ins->ctx->reg_maps->shader_version.major < 2)
4405 struct glsl_src_param src_param;
4406 char dst_mask[6];
4408 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4410 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4411 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4412 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4413 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4415 shader_glsl_append_dst(ins->ctx->buffer, ins);
4416 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4417 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4418 return;
4421 shader_glsl_scalar_op(ins);
4424 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4425 const char *vector_constructor, const char *scalar_constructor)
4427 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4428 struct glsl_src_param src_param;
4429 unsigned int mask_size;
4430 DWORD write_mask;
4432 write_mask = shader_glsl_append_dst(buffer, ins);
4433 mask_size = shader_glsl_get_write_mask_size(write_mask);
4434 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4436 if (mask_size > 1)
4437 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4438 else
4439 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4442 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4444 shader_glsl_cast(ins, "ivec", "int");
4447 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4449 shader_glsl_cast(ins, "uvec", "uint");
4452 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4454 shader_glsl_cast(ins, "vec", "float");
4457 /** Process signed comparison opcodes in GLSL. */
4458 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4460 struct glsl_src_param src0_param;
4461 struct glsl_src_param src1_param;
4462 DWORD write_mask;
4463 unsigned int mask_size;
4465 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4466 mask_size = shader_glsl_get_write_mask_size(write_mask);
4467 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4468 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4470 if (mask_size > 1) {
4471 const char *compare;
4473 switch(ins->handler_idx)
4475 case WINED3DSIH_SLT: compare = "lessThan"; break;
4476 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4477 default: compare = "";
4478 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4481 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4482 src0_param.param_str, src1_param.param_str);
4483 } else {
4484 switch(ins->handler_idx)
4486 case WINED3DSIH_SLT:
4487 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4488 * to return 0.0 but step returns 1.0 because step is not < x
4489 * An alternative is a bvec compare padded with an unused second component.
4490 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4491 * issue. Playing with not() is not possible either because not() does not accept
4492 * a scalar.
4494 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4495 src0_param.param_str, src1_param.param_str);
4496 break;
4497 case WINED3DSIH_SGE:
4498 /* Here we can use the step() function and safe a conditional */
4499 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4500 break;
4501 default:
4502 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4508 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4510 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4511 struct wined3d_shader_dst_param dst[2];
4512 struct glsl_src_param src[3];
4513 unsigned int i, j, k;
4514 char mask_char[6];
4515 DWORD write_mask;
4516 BOOL tmp_dst[2];
4518 for (i = 0; i < ins->dst_count; ++i)
4520 tmp_dst[i] = FALSE;
4521 for (j = 0; j < ins->src_count; ++j)
4523 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4524 && ins->dst[i].reg.type == ins->src[j].reg.type)
4525 tmp_dst[i] = TRUE;
4529 dst[0] = ins->dst[0];
4530 dst[1] = ins->dst[1];
4531 for (i = 0; i < 4; ++i)
4533 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4535 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4536 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4537 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4538 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4539 continue;
4541 for (k = 0; k < ARRAY_SIZE(src); ++k)
4542 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4544 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4545 src[0].param_str, src[1].param_str, src[2].param_str);
4549 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4551 if (tmp_dst[i])
4553 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4554 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4555 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4560 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4562 const char *condition_prefix, *condition_suffix;
4563 struct wined3d_shader_dst_param dst;
4564 struct glsl_src_param src0_param;
4565 struct glsl_src_param src1_param;
4566 struct glsl_src_param src2_param;
4567 BOOL temp_destination = FALSE;
4568 DWORD cmp_channel = 0;
4569 unsigned int i, j;
4570 char mask_char[6];
4571 DWORD write_mask;
4573 switch (ins->handler_idx)
4575 case WINED3DSIH_CMP:
4576 condition_prefix = "";
4577 condition_suffix = " >= 0.0";
4578 break;
4580 case WINED3DSIH_CND:
4581 condition_prefix = "";
4582 condition_suffix = " > 0.5";
4583 break;
4585 case WINED3DSIH_MOVC:
4586 condition_prefix = "bool(";
4587 condition_suffix = ")";
4588 break;
4590 default:
4591 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4592 condition_prefix = "<unhandled prefix>";
4593 condition_suffix = "<unhandled suffix>";
4594 break;
4597 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4599 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4600 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4601 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4602 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4604 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4605 condition_prefix, src0_param.param_str, condition_suffix,
4606 src1_param.param_str, src2_param.param_str);
4607 return;
4610 dst = ins->dst[0];
4612 /* Splitting the instruction up in multiple lines imposes a problem:
4613 * The first lines may overwrite source parameters of the following lines.
4614 * Deal with that by using a temporary destination register if needed. */
4615 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4616 && ins->src[0].reg.type == dst.reg.type)
4617 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4618 && ins->src[1].reg.type == dst.reg.type)
4619 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4620 && ins->src[2].reg.type == dst.reg.type))
4621 temp_destination = TRUE;
4623 /* Cycle through all source0 channels. */
4624 for (i = 0; i < 4; ++i)
4626 write_mask = 0;
4627 /* Find the destination channels which use the current source0 channel. */
4628 for (j = 0; j < 4; ++j)
4630 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4632 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4633 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4636 dst.write_mask = ins->dst[0].write_mask & write_mask;
4638 if (temp_destination)
4640 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4641 continue;
4642 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4644 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4645 continue;
4647 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4648 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4649 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4651 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4652 condition_prefix, src0_param.param_str, condition_suffix,
4653 src1_param.param_str, src2_param.param_str);
4656 if (temp_destination)
4658 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4659 shader_glsl_append_dst(ins->ctx->buffer, ins);
4660 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4664 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4665 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4666 * the compare is done per component of src0. */
4667 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4669 struct glsl_src_param src0_param;
4670 struct glsl_src_param src1_param;
4671 struct glsl_src_param src2_param;
4672 DWORD write_mask;
4673 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4674 ins->ctx->reg_maps->shader_version.minor);
4676 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4678 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4679 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4680 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4681 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4683 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4684 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4685 else
4686 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4687 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4688 return;
4691 shader_glsl_conditional_move(ins);
4694 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4695 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4697 struct glsl_src_param src0_param;
4698 struct glsl_src_param src1_param;
4699 struct glsl_src_param src2_param;
4700 DWORD write_mask;
4702 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4703 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4704 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4705 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4706 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4707 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4710 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4711 Vertex shaders to GLSL codes */
4712 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4714 int i;
4715 int nComponents = 0;
4716 struct wined3d_shader_dst_param tmp_dst = {{0}};
4717 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4718 struct wined3d_shader_instruction tmp_ins;
4720 memset(&tmp_ins, 0, sizeof(tmp_ins));
4722 /* Set constants for the temporary argument */
4723 tmp_ins.ctx = ins->ctx;
4724 tmp_ins.dst_count = 1;
4725 tmp_ins.dst = &tmp_dst;
4726 tmp_ins.src_count = 2;
4727 tmp_ins.src = tmp_src;
4729 switch(ins->handler_idx)
4731 case WINED3DSIH_M4x4:
4732 nComponents = 4;
4733 tmp_ins.handler_idx = WINED3DSIH_DP4;
4734 break;
4735 case WINED3DSIH_M4x3:
4736 nComponents = 3;
4737 tmp_ins.handler_idx = WINED3DSIH_DP4;
4738 break;
4739 case WINED3DSIH_M3x4:
4740 nComponents = 4;
4741 tmp_ins.handler_idx = WINED3DSIH_DP3;
4742 break;
4743 case WINED3DSIH_M3x3:
4744 nComponents = 3;
4745 tmp_ins.handler_idx = WINED3DSIH_DP3;
4746 break;
4747 case WINED3DSIH_M3x2:
4748 nComponents = 2;
4749 tmp_ins.handler_idx = WINED3DSIH_DP3;
4750 break;
4751 default:
4752 break;
4755 tmp_dst = ins->dst[0];
4756 tmp_src[0] = ins->src[0];
4757 tmp_src[1] = ins->src[1];
4758 for (i = 0; i < nComponents; ++i)
4760 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4761 shader_glsl_dot(&tmp_ins);
4762 ++tmp_src[1].reg.idx[0].offset;
4767 The LRP instruction performs a component-wise linear interpolation
4768 between the second and third operands using the first operand as the
4769 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4770 This is equivalent to mix(src2, src1, src0);
4772 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4774 struct glsl_src_param src0_param;
4775 struct glsl_src_param src1_param;
4776 struct glsl_src_param src2_param;
4777 DWORD write_mask;
4779 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4781 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4782 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4783 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4785 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4786 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4789 /** Process the WINED3DSIO_LIT instruction in GLSL:
4790 * dst.x = dst.w = 1.0
4791 * dst.y = (src0.x > 0) ? src0.x
4792 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4793 * where src.w is clamped at +- 128
4795 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4797 struct glsl_src_param src0_param;
4798 struct glsl_src_param src1_param;
4799 struct glsl_src_param src3_param;
4800 char dst_mask[6];
4802 shader_glsl_append_dst(ins->ctx->buffer, ins);
4803 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4805 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4806 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4807 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4809 /* The sdk specifies the instruction like this
4810 * dst.x = 1.0;
4811 * if(src.x > 0.0) dst.y = src.x
4812 * else dst.y = 0.0.
4813 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4814 * else dst.z = 0.0;
4815 * dst.w = 1.0;
4816 * (where power = src.w clamped between -128 and 128)
4818 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4819 * dst.x = 1.0 ... No further explanation needed
4820 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4821 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4822 * dst.w = 1.0. ... Nothing fancy.
4824 * So we still have one conditional in there. So do this:
4825 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4827 * 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),
4828 * 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.
4829 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4831 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4832 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4833 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4835 shader_addline(ins->ctx->buffer,
4836 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4837 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4838 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4839 src0_param.param_str, src3_param.param_str, dst_mask);
4842 /** Process the WINED3DSIO_DST instruction in GLSL:
4843 * dst.x = 1.0
4844 * dst.y = src0.x * src0.y
4845 * dst.z = src0.z
4846 * dst.w = src1.w
4848 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4850 struct glsl_src_param src0y_param;
4851 struct glsl_src_param src0z_param;
4852 struct glsl_src_param src1y_param;
4853 struct glsl_src_param src1w_param;
4854 char dst_mask[6];
4856 shader_glsl_append_dst(ins->ctx->buffer, ins);
4857 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4859 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4860 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4861 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4862 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4864 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4865 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4868 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4869 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4870 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4872 * dst.x = cos(src0.?)
4873 * dst.y = sin(src0.?)
4874 * dst.z = dst.z
4875 * dst.w = dst.w
4877 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4879 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4880 struct glsl_src_param src0_param;
4881 DWORD write_mask;
4883 if (ins->ctx->reg_maps->shader_version.major < 4)
4885 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4887 write_mask = shader_glsl_append_dst(buffer, ins);
4888 switch (write_mask)
4890 case WINED3DSP_WRITEMASK_0:
4891 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4892 break;
4894 case WINED3DSP_WRITEMASK_1:
4895 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4896 break;
4898 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4899 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4900 src0_param.param_str, src0_param.param_str);
4901 break;
4903 default:
4904 ERR("Write mask should be .x, .y or .xy\n");
4905 break;
4908 return;
4911 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4914 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4916 char dst_mask[6];
4918 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4919 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4920 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4922 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4923 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4924 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4926 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4927 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4929 else
4931 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4932 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4933 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4936 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4938 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4939 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4940 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4944 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4945 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4946 * generate invalid code
4948 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4950 struct glsl_src_param src0_param;
4951 DWORD write_mask;
4953 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4954 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4956 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4959 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4960 * Start a for() loop where src1.y is the initial value of aL,
4961 * increment aL by src1.z for a total of src1.x iterations.
4962 * Need to use a temporary variable for this operation.
4964 /* FIXME: I don't think nested loops will work correctly this way. */
4965 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4967 struct wined3d_shader_parser_state *state = ins->ctx->state;
4968 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4969 const struct wined3d_shader *shader = ins->ctx->shader;
4970 const struct wined3d_shader_lconst *constant;
4971 struct glsl_src_param src1_param;
4972 const DWORD *control_values = NULL;
4974 if (ins->ctx->reg_maps->shader_version.major < 4)
4976 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4978 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4979 * class hardware doesn't support real varying indexing, but Microsoft
4980 * designed this feature for Shader model 2.x+. If the loop control is
4981 * known at compile time, the GLSL compiler can unroll the loop, and
4982 * replace indirect addressing with direct addressing. */
4983 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4985 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4987 if (constant->idx == ins->src[1].reg.idx[0].offset)
4989 control_values = constant->value;
4990 break;
4995 if (control_values)
4997 struct wined3d_shader_loop_control loop_control;
4998 loop_control.count = control_values[0];
4999 loop_control.start = control_values[1];
5000 loop_control.step = (int)control_values[2];
5002 if (loop_control.step > 0)
5004 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
5005 state->current_loop_depth, loop_control.start,
5006 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
5007 state->current_loop_depth, loop_control.step);
5009 else if (loop_control.step < 0)
5011 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
5012 state->current_loop_depth, loop_control.start,
5013 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
5014 state->current_loop_depth, loop_control.step);
5016 else
5018 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
5019 state->current_loop_depth, loop_control.start, state->current_loop_depth,
5020 state->current_loop_depth, loop_control.count,
5021 state->current_loop_depth);
5024 else
5026 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
5027 state->current_loop_depth, state->current_loop_reg,
5028 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
5029 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
5032 ++state->current_loop_reg;
5034 else
5036 shader_addline(buffer, "for (;;)\n{\n");
5039 ++state->current_loop_depth;
5042 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
5044 struct wined3d_shader_parser_state *state = ins->ctx->state;
5046 shader_addline(ins->ctx->buffer, "}\n");
5048 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5050 --state->current_loop_depth;
5051 --state->current_loop_reg;
5054 if (ins->handler_idx == WINED3DSIH_ENDREP)
5056 --state->current_loop_depth;
5060 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5062 struct wined3d_shader_parser_state *state = ins->ctx->state;
5063 const struct wined3d_shader *shader = ins->ctx->shader;
5064 const struct wined3d_shader_lconst *constant;
5065 struct glsl_src_param src0_param;
5066 const DWORD *control_values = NULL;
5068 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5069 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5071 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5073 if (constant->idx == ins->src[0].reg.idx[0].offset)
5075 control_values = constant->value;
5076 break;
5081 if (control_values)
5083 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5084 state->current_loop_depth, state->current_loop_depth,
5085 control_values[0], state->current_loop_depth);
5087 else
5089 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5090 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5091 state->current_loop_depth, state->current_loop_depth,
5092 src0_param.param_str, state->current_loop_depth);
5095 ++state->current_loop_depth;
5098 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5100 struct glsl_src_param src0_param;
5102 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5103 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5106 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5108 struct glsl_src_param src0_param;
5110 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5111 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5114 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5116 shader_addline(ins->ctx->buffer, "default:\n");
5119 static void shader_glsl_generate_conditional_op(const struct wined3d_shader_instruction *ins,
5120 const char *op)
5122 struct glsl_src_param src_param;
5123 const char *condition;
5125 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5126 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5127 shader_addline(ins->ctx->buffer, "if (%s(%s)) %s\n", condition, src_param.param_str, op);
5130 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5132 shader_glsl_generate_conditional_op(ins, "{");
5135 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5137 struct glsl_src_param src0_param;
5138 struct glsl_src_param src1_param;
5140 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5141 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5143 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5144 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5147 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5149 shader_addline(ins->ctx->buffer, "} else {\n");
5152 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5154 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5155 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5157 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5158 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
5159 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5161 if (!stream)
5162 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5163 else
5164 FIXME("Unhandled primitive stream %u.\n", stream);
5167 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5169 shader_addline(ins->ctx->buffer, "break;\n");
5172 /* FIXME: According to MSDN the compare is done per component. */
5173 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5175 struct glsl_src_param src0_param;
5176 struct glsl_src_param src1_param;
5178 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5179 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5181 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5182 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5185 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5187 const char *op;
5189 switch (ins->handler_idx)
5191 case WINED3DSIH_BREAKP: op = "break;"; break;
5192 case WINED3DSIH_CONTINUEP: op = "continue;"; break;
5193 case WINED3DSIH_RETP: op = "return;"; break;
5194 default:
5195 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5196 return;
5199 shader_glsl_generate_conditional_op(ins, op);
5202 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5204 shader_addline(ins->ctx->buffer, "continue;\n");
5207 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5209 shader_addline(ins->ctx->buffer, "}\n");
5210 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5212 /* Subroutines appear at the end of the shader. */
5213 ins->ctx->state->in_subroutine = TRUE;
5216 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5218 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5221 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5223 struct glsl_src_param src1_param;
5225 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5226 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5227 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5230 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5232 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5234 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5236 shader_glsl_generate_shader_epilogue(ins->ctx);
5237 shader_addline(ins->ctx->buffer, "return;\n");
5241 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5243 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5244 ins->ctx->reg_maps->shader_version.minor);
5245 struct glsl_sample_function sample_function;
5246 DWORD sample_flags = 0;
5247 DWORD resource_idx;
5248 DWORD mask = 0, swizzle;
5249 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5251 /* 1.0-1.4: Use destination register as sampler source.
5252 * 2.0+: Use provided sampler source. */
5253 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5254 resource_idx = ins->dst[0].reg.idx[0].offset;
5255 else
5256 resource_idx = ins->src[1].reg.idx[0].offset;
5258 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5260 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5261 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5262 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5264 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5265 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5267 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5268 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5270 case WINED3D_TTFF_COUNT1:
5271 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5272 break;
5273 case WINED3D_TTFF_COUNT2:
5274 mask = WINED3DSP_WRITEMASK_1;
5275 break;
5276 case WINED3D_TTFF_COUNT3:
5277 mask = WINED3DSP_WRITEMASK_2;
5278 break;
5279 case WINED3D_TTFF_COUNT4:
5280 case WINED3D_TTFF_DISABLE:
5281 mask = WINED3DSP_WRITEMASK_3;
5282 break;
5286 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5288 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5290 if (src_mod == WINED3DSPSM_DZ) {
5291 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5292 mask = WINED3DSP_WRITEMASK_2;
5293 } else if (src_mod == WINED3DSPSM_DW) {
5294 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5295 mask = WINED3DSP_WRITEMASK_3;
5298 else
5300 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5301 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5303 /* ps 2.0 texldp instruction always divides by the fourth component. */
5304 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5305 mask = WINED3DSP_WRITEMASK_3;
5309 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5310 mask |= sample_function.coord_mask;
5311 sample_function.coord_mask = mask;
5313 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5314 else swizzle = ins->src[1].swizzle;
5316 /* 1.0-1.3: Use destination register as coordinate source.
5317 1.4+: Use provided coordinate source register. */
5318 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5320 char coord_mask[6];
5321 shader_glsl_write_mask_to_str(mask, coord_mask);
5322 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5323 "T%u%s", resource_idx, coord_mask);
5325 else
5327 struct glsl_src_param coord_param;
5328 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5329 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5331 struct glsl_src_param bias;
5332 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5333 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5334 NULL, "%s", coord_param.param_str);
5335 } else {
5336 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5337 "%s", coord_param.param_str);
5340 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5343 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5345 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5346 struct glsl_src_param coord_param, dx_param, dy_param;
5347 struct glsl_sample_function sample_function;
5348 DWORD sampler_idx;
5349 DWORD swizzle = ins->src[1].swizzle;
5351 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5353 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5354 shader_glsl_tex(ins);
5355 return;
5358 sampler_idx = ins->src[1].reg.idx[0].offset;
5360 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5361 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5362 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5363 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5365 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5366 NULL, NULL, "%s", coord_param.param_str);
5367 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5370 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5372 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5373 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5374 struct glsl_src_param coord_param, lod_param;
5375 struct glsl_sample_function sample_function;
5376 DWORD swizzle = ins->src[1].swizzle;
5377 DWORD sampler_idx;
5379 sampler_idx = ins->src[1].reg.idx[0].offset;
5381 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5382 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5384 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5386 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5387 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5389 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5390 * However, the NVIDIA drivers allow them in fragment shaders as well,
5391 * even without the appropriate extension. */
5392 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5394 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5395 "%s", coord_param.param_str);
5396 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5399 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5400 unsigned int resource_idx, unsigned int sampler_idx)
5402 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5403 unsigned int i;
5405 for (i = 0; i < sampler_map->count; ++i)
5407 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5408 return entries[i].bind_idx;
5411 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5413 return ~0u;
5416 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5418 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5419 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5420 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5421 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5422 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5423 struct glsl_src_param structure_idx, offset, data, data2;
5424 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5425 enum wined3d_shader_resource_type resource_type;
5426 struct wined3d_string_buffer *address;
5427 enum wined3d_data_type data_type;
5428 unsigned int resource_idx, stride;
5429 const char *op, *resource;
5430 DWORD coord_mask;
5431 BOOL is_tgsm;
5433 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5434 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5435 if (is_tgsm)
5437 if (resource_idx >= reg_maps->tgsm_count)
5439 ERR("Invalid TGSM index %u.\n", resource_idx);
5440 return;
5442 resource = "g";
5443 data_type = WINED3D_DATA_UINT;
5444 coord_mask = 1;
5445 stride = reg_maps->tgsm[resource_idx].stride;
5447 else
5449 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5451 ERR("Invalid UAV index %u.\n", resource_idx);
5452 return;
5454 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5455 if (resource_type >= ARRAY_SIZE(resource_type_info))
5457 ERR("Unexpected resource type %#x.\n", resource_type);
5458 return;
5460 resource = "image";
5461 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5462 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5463 stride = reg_maps->uav_resource_info[resource_idx].stride;
5466 switch (ins->handler_idx)
5468 case WINED3DSIH_ATOMIC_AND:
5469 case WINED3DSIH_IMM_ATOMIC_AND:
5470 if (is_tgsm)
5471 op = "atomicAnd";
5472 else
5473 op = "imageAtomicAnd";
5474 break;
5475 case WINED3DSIH_ATOMIC_CMP_STORE:
5476 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5477 if (is_tgsm)
5478 op = "atomicCompSwap";
5479 else
5480 op = "imageAtomicCompSwap";
5481 break;
5482 case WINED3DSIH_ATOMIC_IADD:
5483 case WINED3DSIH_IMM_ATOMIC_IADD:
5484 if (is_tgsm)
5485 op = "atomicAdd";
5486 else
5487 op = "imageAtomicAdd";
5488 break;
5489 case WINED3DSIH_ATOMIC_IMAX:
5490 case WINED3DSIH_IMM_ATOMIC_IMAX:
5491 if (is_tgsm)
5492 op = "atomicMax";
5493 else
5494 op = "imageAtomicMax";
5495 if (data_type != WINED3D_DATA_INT)
5497 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5498 return;
5500 break;
5501 case WINED3DSIH_ATOMIC_IMIN:
5502 case WINED3DSIH_IMM_ATOMIC_IMIN:
5503 if (is_tgsm)
5504 op = "atomicMin";
5505 else
5506 op = "imageAtomicMin";
5507 if (data_type != WINED3D_DATA_INT)
5509 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5510 return;
5512 break;
5513 case WINED3DSIH_ATOMIC_OR:
5514 case WINED3DSIH_IMM_ATOMIC_OR:
5515 if (is_tgsm)
5516 op = "atomicOr";
5517 else
5518 op = "imageAtomicOr";
5519 break;
5520 case WINED3DSIH_ATOMIC_UMAX:
5521 case WINED3DSIH_IMM_ATOMIC_UMAX:
5522 if (is_tgsm)
5523 op = "atomicMax";
5524 else
5525 op = "imageAtomicMax";
5526 if (data_type != WINED3D_DATA_UINT)
5528 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5529 return;
5531 break;
5532 case WINED3DSIH_ATOMIC_UMIN:
5533 case WINED3DSIH_IMM_ATOMIC_UMIN:
5534 if (is_tgsm)
5535 op = "atomicMin";
5536 else
5537 op = "imageAtomicMin";
5538 if (data_type != WINED3D_DATA_UINT)
5540 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5541 return;
5543 break;
5544 case WINED3DSIH_ATOMIC_XOR:
5545 case WINED3DSIH_IMM_ATOMIC_XOR:
5546 if (is_tgsm)
5547 op = "atomicXor";
5548 else
5549 op = "imageAtomicXor";
5550 break;
5551 case WINED3DSIH_IMM_ATOMIC_EXCH:
5552 if (is_tgsm)
5553 op = "atomicExchange";
5554 else
5555 op = "imageAtomicExchange";
5556 break;
5557 default:
5558 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5559 return;
5562 address = string_buffer_get(priv->string_buffers);
5563 if (stride)
5565 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5566 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5567 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5569 else
5571 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5572 string_buffer_sprintf(address, "%s", offset.param_str);
5573 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5574 shader_addline(address, "/ 4");
5577 if (is_imm_instruction)
5578 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5580 if (is_tgsm)
5581 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5582 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5583 else
5584 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5585 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5587 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5588 shader_addline(buffer, "%s", data.param_str);
5589 if (ins->src_count >= 3)
5591 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5592 shader_addline(buffer, ", %s", data2.param_str);
5595 if (is_imm_instruction)
5596 shader_addline(buffer, ")");
5597 shader_addline(buffer, ");\n");
5599 string_buffer_release(priv->string_buffers, address);
5602 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5604 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5605 const char *op;
5607 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5608 op = "atomicCounterIncrement";
5609 else
5610 op = "atomicCounterDecrement";
5612 shader_glsl_append_dst(ins->ctx->buffer, ins);
5613 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5616 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5618 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5619 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5620 enum wined3d_shader_resource_type resource_type;
5621 struct glsl_src_param image_coord_param;
5622 enum wined3d_data_type data_type;
5623 DWORD coord_mask, write_mask;
5624 unsigned int uav_idx;
5625 char dst_swizzle[6];
5627 uav_idx = ins->src[1].reg.idx[0].offset;
5628 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5630 ERR("Invalid UAV index %u.\n", uav_idx);
5631 return;
5633 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5634 if (resource_type >= ARRAY_SIZE(resource_type_info))
5636 ERR("Unexpected resource type %#x.\n", resource_type);
5637 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5639 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5640 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5642 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5643 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5645 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5646 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5647 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5650 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5652 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5653 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5654 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5655 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5656 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5657 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5658 struct glsl_src_param structure_idx, offset;
5659 struct wined3d_string_buffer *address;
5660 struct wined3d_shader_dst_param dst;
5661 const char *function, *resource;
5663 resource_idx = src->reg.idx[0].offset;
5664 if (src->reg.type == WINED3DSPR_RESOURCE)
5666 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5668 ERR("Invalid resource index %u.\n", resource_idx);
5669 return;
5671 stride = reg_maps->resource_info[resource_idx].stride;
5672 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5673 function = "texelFetch";
5674 resource = "sampler";
5676 else if (src->reg.type == WINED3DSPR_UAV)
5678 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5680 ERR("Invalid UAV index %u.\n", resource_idx);
5681 return;
5683 stride = reg_maps->uav_resource_info[resource_idx].stride;
5684 bind_idx = resource_idx;
5685 function = "imageLoad";
5686 resource = "image";
5688 else
5690 if (resource_idx >= reg_maps->tgsm_count)
5692 ERR("Invalid TGSM index %u.\n", resource_idx);
5693 return;
5695 stride = reg_maps->tgsm[resource_idx].stride;
5696 bind_idx = resource_idx;
5697 function = NULL;
5698 resource = "g";
5701 address = string_buffer_get(priv->string_buffers);
5702 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5704 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5705 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5707 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5708 shader_addline(address, "%s / 4", offset.param_str);
5710 dst = ins->dst[0];
5711 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5713 /* The instruction is split into multiple lines. The first lines may
5714 * overwrite source parameters of the following lines. */
5715 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5716 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5719 for (i = 0; i < 4; ++i)
5721 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5722 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5723 continue;
5725 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5726 if (function)
5727 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5728 function, prefix, resource, bind_idx, address->buffer, swizzle);
5729 else
5730 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5731 prefix, resource, bind_idx, address->buffer, swizzle);
5734 string_buffer_release(priv->string_buffers, address);
5737 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5739 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5740 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5741 struct glsl_src_param image_coord_param, image_data_param;
5742 enum wined3d_shader_resource_type resource_type;
5743 enum wined3d_data_type data_type;
5744 unsigned int uav_idx;
5745 DWORD coord_mask;
5747 uav_idx = ins->dst[0].reg.idx[0].offset;
5748 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5750 ERR("Invalid UAV index %u.\n", uav_idx);
5751 return;
5753 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5754 if (resource_type >= ARRAY_SIZE(resource_type_info))
5756 ERR("Unexpected resource type %#x.\n", resource_type);
5757 return;
5759 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5760 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5762 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5763 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5764 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5765 shader_glsl_get_prefix(version->type), uav_idx,
5766 image_coord_param.param_str, image_data_param.param_str);
5769 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5771 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5772 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5773 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5774 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5775 struct glsl_src_param structure_idx, offset, data;
5776 unsigned int i, resource_idx, stride, src_idx = 0;
5777 struct wined3d_string_buffer *address;
5778 DWORD write_mask;
5779 BOOL is_tgsm;
5781 resource_idx = ins->dst[0].reg.idx[0].offset;
5782 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5783 if (is_tgsm)
5785 if (resource_idx >= reg_maps->tgsm_count)
5787 ERR("Invalid TGSM index %u.\n", resource_idx);
5788 return;
5790 stride = reg_maps->tgsm[resource_idx].stride;
5792 else
5794 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5796 ERR("Invalid UAV index %u.\n", resource_idx);
5797 return;
5799 stride = reg_maps->uav_resource_info[resource_idx].stride;
5802 address = string_buffer_get(priv->string_buffers);
5803 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5805 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5806 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5808 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5809 shader_addline(address, "%s / 4", offset.param_str);
5811 for (i = 0; i < 4; ++i)
5813 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5814 continue;
5816 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5818 if (is_tgsm)
5819 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5820 prefix, resource_idx, address->buffer, i, data.param_str);
5821 else
5822 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5823 prefix, resource_idx, address->buffer, i, data.param_str);
5826 string_buffer_release(priv->string_buffers, address);
5829 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5831 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5832 unsigned int sync_flags = ins->flags;
5834 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5836 shader_addline(buffer, "barrier();\n");
5837 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5840 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5842 shader_addline(buffer, "memoryBarrierShared();\n");
5843 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5846 if (sync_flags)
5847 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5850 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5851 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5853 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5854 unsigned int idx = reg->idx[0].offset;
5856 if (reg->type == WINED3DSPR_RESOURCE)
5858 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5860 ERR("Invalid resource index %u.\n", idx);
5861 return NULL;
5863 return &reg_maps->resource_info[idx];
5866 if (reg->type == WINED3DSPR_UAV)
5868 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5870 ERR("Invalid UAV index %u.\n", idx);
5871 return NULL;
5873 return &reg_maps->uav_resource_info[idx];
5876 FIXME("Unhandled register type %#x.\n", reg->type);
5877 return NULL;
5880 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5882 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5883 const struct wined3d_shader_resource_info *resource_info;
5884 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5885 unsigned int resource_idx;
5886 char dst_swizzle[6];
5887 DWORD write_mask;
5889 write_mask = shader_glsl_append_dst(buffer, ins);
5890 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5892 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5893 return;
5894 resource_idx = ins->src[0].reg.idx[0].offset;
5896 shader_addline(buffer, "ivec2(");
5897 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5899 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5900 resource_idx, WINED3D_SAMPLER_DEFAULT);
5901 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5903 else
5905 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5907 if (resource_info->stride)
5908 shader_addline(buffer, " / %u", resource_info->stride);
5909 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5910 shader_addline(buffer, " * 4");
5911 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5914 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5916 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5917 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5920 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5922 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5925 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5927 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5928 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5929 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5930 enum wined3d_shader_resource_type resource_type;
5931 enum wined3d_shader_register_type reg_type;
5932 unsigned int resource_idx, bind_idx, i;
5933 enum wined3d_data_type dst_data_type;
5934 struct glsl_src_param lod_param;
5935 BOOL supports_mipmaps;
5936 char dst_swizzle[6];
5937 DWORD write_mask;
5939 dst_data_type = ins->dst[0].reg.data_type;
5940 if (ins->flags == WINED3DSI_RESINFO_UINT)
5941 dst_data_type = WINED3D_DATA_UINT;
5942 else if (ins->flags)
5943 FIXME("Unhandled flags %#x.\n", ins->flags);
5945 reg_type = ins->src[1].reg.type;
5946 resource_idx = ins->src[1].reg.idx[0].offset;
5947 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5948 if (reg_type == WINED3DSPR_RESOURCE)
5950 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5951 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5952 resource_idx, WINED3D_SAMPLER_DEFAULT);
5954 else
5956 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5957 bind_idx = resource_idx;
5960 if (resource_type >= ARRAY_SIZE(resource_type_info))
5962 ERR("Unexpected resource type %#x.\n", resource_type);
5963 return;
5966 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], dst_data_type);
5967 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5969 if (dst_data_type == WINED3D_DATA_UINT)
5970 shader_addline(buffer, "uvec4(");
5971 else
5972 shader_addline(buffer, "vec4(");
5974 if (reg_type == WINED3DSPR_RESOURCE)
5976 shader_addline(buffer, "textureSize(%s_sampler%u",
5977 shader_glsl_get_prefix(version->type), bind_idx);
5979 else
5981 shader_addline(buffer, "imageSize(%s_image%u",
5982 shader_glsl_get_prefix(version->type), bind_idx);
5985 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5986 if (supports_mipmaps)
5987 shader_addline(buffer, ", %s", lod_param.param_str);
5988 shader_addline(buffer, "), ");
5990 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5991 shader_addline(buffer, "0, ");
5993 if (supports_mipmaps)
5995 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5997 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5998 shader_glsl_get_prefix(version->type), bind_idx);
6000 else
6002 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
6003 shader_addline(buffer, "1");
6006 else
6008 shader_addline(buffer, "1");
6011 shader_addline(buffer, ")%s);\n", dst_swizzle);
6014 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6016 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6017 struct glsl_src_param coord_param, lod_param, sample_param;
6018 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6019 struct glsl_sample_function sample_function;
6020 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6021 BOOL has_lod_param;
6023 if (wined3d_shader_instruction_has_texel_offset(ins))
6024 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6026 resource_idx = ins->src[1].reg.idx[0].offset;
6027 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6029 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6031 ERR("Invalid resource index %u.\n", resource_idx);
6032 return;
6034 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6036 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6037 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6038 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6039 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6040 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6042 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6043 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6044 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6046 else
6048 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6049 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6050 "%s", coord_param.param_str);
6052 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6055 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6057 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6058 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6059 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6060 struct glsl_sample_function sample_function;
6061 DWORD flags = 0;
6063 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6064 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6065 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6066 flags |= WINED3D_GLSL_SAMPLE_LOD;
6067 if (wined3d_shader_instruction_has_texel_offset(ins))
6068 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6070 resource_idx = ins->src[1].reg.idx[0].offset;
6071 sampler_idx = ins->src[2].reg.idx[0].offset;
6073 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6074 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6076 switch (ins->handler_idx)
6078 case WINED3DSIH_SAMPLE:
6079 break;
6080 case WINED3DSIH_SAMPLE_B:
6081 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6082 lod_param_str = lod_param.param_str;
6083 break;
6084 case WINED3DSIH_SAMPLE_GRAD:
6085 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6086 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6087 dx_param_str = dx_param.param_str;
6088 dy_param_str = dy_param.param_str;
6089 break;
6090 case WINED3DSIH_SAMPLE_LOD:
6091 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6092 lod_param_str = lod_param.param_str;
6093 break;
6094 default:
6095 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6096 break;
6099 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6100 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6101 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6102 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6105 /* GLSL doesn't provide a function to sample from level zero with depth
6106 * comparison for array textures and cube textures. We use textureGrad*()
6107 * to implement sample_c_lz.
6109 static void shader_glsl_gen_sample_c_lz(const struct wined3d_shader_instruction *ins,
6110 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6111 unsigned int coord_size, const char *coord_param, const char *ref_param)
6113 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6114 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6115 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6116 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6117 char dst_swizzle[6];
6119 WARN("Emitting textureGrad() for sample_c_lz.\n");
6121 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6122 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6123 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6124 sample_function->offset_size ? "Offset" : "",
6125 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6126 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6127 if (sample_function->offset_size)
6129 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6130 shader_addline(buffer, ", ");
6131 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6133 shader_addline(buffer, "))%s);\n", dst_swizzle);
6136 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6138 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6139 const struct wined3d_shader_resource_info *resource_info;
6140 struct glsl_src_param coord_param, compare_param;
6141 struct glsl_sample_function sample_function;
6142 const char *lod_param = NULL;
6143 unsigned int coord_size;
6144 DWORD flags = 0;
6146 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6148 lod_param = "0";
6149 flags |= WINED3D_GLSL_SAMPLE_LOD;
6152 if (wined3d_shader_instruction_has_texel_offset(ins))
6153 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6155 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6156 return;
6157 resource_idx = ins->src[1].reg.idx[0].offset;
6158 sampler_idx = ins->src[2].reg.idx[0].offset;
6160 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6161 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6162 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6163 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6164 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6165 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6166 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6167 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6169 shader_glsl_gen_sample_c_lz(ins, sampler_bind_idx, &sample_function,
6170 coord_size, coord_param.param_str, compare_param.param_str);
6172 else
6174 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6175 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6176 coord_size, coord_param.param_str, compare_param.param_str);
6178 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6181 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6183 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6184 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6185 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6186 struct glsl_src_param coord_param, compare_param, offset_param;
6187 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
6188 const struct wined3d_shader_resource_info *resource_info;
6189 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6190 unsigned int coord_size, offset_size;
6191 char dst_swizzle[6];
6192 BOOL has_offset;
6194 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6196 FIXME("OpenGL implementation does not support textureGather.\n");
6197 return;
6200 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6201 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6202 || wined3d_shader_instruction_has_texel_offset(ins);
6204 resource_param_idx =
6205 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6206 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6207 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6208 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6209 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6211 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6212 return;
6214 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6216 ERR("Unexpected resource type %#x.\n", resource_info->type);
6217 return;
6219 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6221 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6222 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6224 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6226 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6227 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6228 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6230 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6231 shader_addline(buffer, ", %s", compare_param.param_str);
6233 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6235 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6236 shader_addline(buffer, ", %s", offset_param.param_str);
6238 else if (has_offset)
6240 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6241 shader_addline(buffer, ", ");
6242 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6244 if (component_idx)
6245 shader_addline(buffer, ", %u", component_idx);
6247 shader_addline(buffer, ")%s);\n", dst_swizzle);
6250 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6252 /* FIXME: Make this work for more than just 2D textures */
6253 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6254 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6256 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6258 char dst_mask[6];
6260 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6261 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6262 ins->dst[0].reg.idx[0].offset, dst_mask);
6264 else
6266 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6267 DWORD reg = ins->src[0].reg.idx[0].offset;
6268 char dst_swizzle[6];
6270 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6272 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6274 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6275 struct glsl_src_param div_param;
6276 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6278 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6280 if (mask_size > 1)
6281 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6282 else
6283 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6285 else
6287 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6292 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6293 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6294 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6295 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6297 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6298 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6299 struct glsl_sample_function sample_function;
6300 struct glsl_src_param src0_param;
6301 UINT mask_size;
6303 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6305 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6306 * scalar, and projected sampling would require 4.
6308 * It is a dependent read - not valid with conditional NP2 textures
6310 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6311 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6313 switch(mask_size)
6315 case 1:
6316 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6317 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6318 break;
6320 case 2:
6321 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6322 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6323 break;
6325 case 3:
6326 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6327 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6328 break;
6330 default:
6331 FIXME("Unexpected mask size %u\n", mask_size);
6332 break;
6334 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6337 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6338 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6339 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6341 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6342 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6343 struct glsl_src_param src0_param;
6344 DWORD dst_mask;
6345 unsigned int mask_size;
6347 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6348 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6349 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6351 if (mask_size > 1) {
6352 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6353 } else {
6354 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6358 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6359 * Calculate the depth as dst.x / dst.y */
6360 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6362 struct glsl_dst_param dst_param;
6364 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6366 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6367 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6368 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6369 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6370 * >= 1.0 or < 0.0
6372 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6373 dst_param.reg_name, dst_param.reg_name);
6376 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6377 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6378 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6379 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6381 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6383 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6384 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6385 struct glsl_src_param src0_param;
6387 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6389 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6390 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6393 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6394 * Calculate the 1st of a 2-row matrix multiplication. */
6395 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6397 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6398 DWORD reg = ins->dst[0].reg.idx[0].offset;
6399 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6400 struct glsl_src_param src0_param;
6402 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6403 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6406 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6407 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6408 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6410 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6411 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6412 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6413 DWORD reg = ins->dst[0].reg.idx[0].offset;
6414 struct glsl_src_param src0_param;
6416 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6417 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6418 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6421 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6423 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6424 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6425 struct glsl_sample_function sample_function;
6426 DWORD reg = ins->dst[0].reg.idx[0].offset;
6427 struct glsl_src_param src0_param;
6429 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6430 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6432 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6434 /* Sample the texture using the calculated coordinates */
6435 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6436 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6439 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6440 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6441 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6443 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6444 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6445 struct glsl_sample_function sample_function;
6446 DWORD reg = ins->dst[0].reg.idx[0].offset;
6447 struct glsl_src_param src0_param;
6449 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6450 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6452 /* Dependent read, not valid with conditional NP2 */
6453 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6455 /* Sample the texture using the calculated coordinates */
6456 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6457 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6459 tex_mx->current_row = 0;
6462 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6463 * Perform the 3rd row of a 3x3 matrix multiply */
6464 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6466 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6467 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6468 DWORD reg = ins->dst[0].reg.idx[0].offset;
6469 struct glsl_src_param src0_param;
6470 char dst_mask[6];
6472 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6474 shader_glsl_append_dst(ins->ctx->buffer, ins);
6475 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6476 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6478 tex_mx->current_row = 0;
6481 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6482 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6483 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6485 struct glsl_src_param src0_param;
6486 struct glsl_src_param src1_param;
6487 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6488 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6489 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6490 struct glsl_sample_function sample_function;
6491 DWORD reg = ins->dst[0].reg.idx[0].offset;
6492 char coord_mask[6];
6494 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6495 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6497 /* Perform the last matrix multiply operation */
6498 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6499 /* Reflection calculation */
6500 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6502 /* Dependent read, not valid with conditional NP2 */
6503 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6504 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6506 /* Sample the texture */
6507 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6508 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6509 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6511 tex_mx->current_row = 0;
6514 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6515 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6516 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6518 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6519 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6520 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6521 struct glsl_sample_function sample_function;
6522 DWORD reg = ins->dst[0].reg.idx[0].offset;
6523 struct glsl_src_param src0_param;
6524 char coord_mask[6];
6526 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6528 /* Perform the last matrix multiply operation */
6529 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6531 /* Construct the eye-ray vector from w coordinates */
6532 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6533 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6534 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6536 /* Dependent read, not valid with conditional NP2 */
6537 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6538 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6540 /* Sample the texture using the calculated coordinates */
6541 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6542 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6543 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6545 tex_mx->current_row = 0;
6548 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6549 * Apply a fake bump map transform.
6550 * texbem is pshader <= 1.3 only, this saves a few version checks
6552 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6554 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6555 struct glsl_sample_function sample_function;
6556 struct glsl_src_param coord_param;
6557 DWORD sampler_idx;
6558 DWORD mask;
6559 DWORD flags;
6560 char coord_mask[6];
6562 sampler_idx = ins->dst[0].reg.idx[0].offset;
6563 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6564 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6566 /* Dependent read, not valid with conditional NP2 */
6567 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6568 mask = sample_function.coord_mask;
6570 shader_glsl_write_mask_to_str(mask, coord_mask);
6572 /* With projected textures, texbem only divides the static texture coord,
6573 * not the displacement, so we can't let GL handle this. */
6574 if (flags & WINED3D_PSARGS_PROJECTED)
6576 DWORD div_mask=0;
6577 char coord_div_mask[3];
6578 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6580 case WINED3D_TTFF_COUNT1:
6581 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6582 break;
6583 case WINED3D_TTFF_COUNT2:
6584 div_mask = WINED3DSP_WRITEMASK_1;
6585 break;
6586 case WINED3D_TTFF_COUNT3:
6587 div_mask = WINED3DSP_WRITEMASK_2;
6588 break;
6589 case WINED3D_TTFF_COUNT4:
6590 case WINED3D_TTFF_DISABLE:
6591 div_mask = WINED3DSP_WRITEMASK_3;
6592 break;
6594 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6595 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6598 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6600 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6601 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6602 coord_param.param_str, coord_mask);
6604 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6606 struct glsl_src_param luminance_param;
6607 struct glsl_dst_param dst_param;
6609 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6610 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6612 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6613 dst_param.reg_name, dst_param.mask_str,
6614 luminance_param.param_str, sampler_idx, sampler_idx);
6616 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6619 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6621 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6622 struct glsl_src_param src0_param, src1_param;
6624 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6625 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6627 shader_glsl_append_dst(ins->ctx->buffer, ins);
6628 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6629 src0_param.param_str, sampler_idx, src1_param.param_str);
6632 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6633 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6634 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6636 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6637 struct glsl_sample_function sample_function;
6638 struct glsl_src_param src0_param;
6640 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6642 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6643 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6644 "%s.wx", src0_param.reg_name);
6645 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6648 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6649 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6650 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6652 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6653 struct glsl_sample_function sample_function;
6654 struct glsl_src_param src0_param;
6656 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6658 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6659 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6660 "%s.yz", src0_param.reg_name);
6661 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6664 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6665 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6666 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6668 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6669 struct glsl_sample_function sample_function;
6670 struct glsl_src_param src0_param;
6672 /* Dependent read, not valid with conditional NP2 */
6673 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6674 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6676 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6677 "%s", src0_param.param_str);
6678 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6681 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6682 * If any of the first 3 components are < 0, discard this pixel */
6683 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6685 if (ins->ctx->reg_maps->shader_version.major >= 4)
6687 shader_glsl_generate_conditional_op(ins, "discard;");
6689 else
6691 struct glsl_dst_param dst_param;
6693 /* The argument is a destination parameter, and no writemasks are allowed */
6694 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6696 /* 2.0 shaders compare all 4 components in texkill. */
6697 if (ins->ctx->reg_maps->shader_version.major >= 2)
6698 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6699 /* 1.x shaders only compare the first 3 components, probably due to
6700 * the nature of the texkill instruction as a tex* instruction, and
6701 * phase, which kills all .w components. Even if all 4 components are
6702 * defined, only the first 3 are used. */
6703 else
6704 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6708 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6709 * dst = dot2(src0, src1) + src2 */
6710 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6712 struct glsl_src_param src0_param;
6713 struct glsl_src_param src1_param;
6714 struct glsl_src_param src2_param;
6715 DWORD write_mask;
6716 unsigned int mask_size;
6718 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6719 mask_size = shader_glsl_get_write_mask_size(write_mask);
6721 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6722 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6723 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6725 if (mask_size > 1) {
6726 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6727 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6728 } else {
6729 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6730 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6734 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6735 const struct wined3d_shader_signature *input_signature,
6736 const struct wined3d_shader_reg_maps *reg_maps,
6737 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6739 unsigned int i;
6741 for (i = 0; i < input_signature->element_count; ++i)
6743 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6744 const char *semantic_name;
6745 UINT semantic_idx;
6746 char reg_mask[6];
6748 /* Unused */
6749 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6750 continue;
6752 semantic_name = input->semantic_name;
6753 semantic_idx = input->semantic_idx;
6754 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6756 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6758 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6760 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6761 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6763 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6765 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6767 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6769 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6770 input->register_idx, reg_mask);
6772 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6774 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6775 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6776 input->register_idx, reg_mask);
6777 else
6778 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6780 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6782 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6783 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6784 input->register_idx, reg_mask);
6785 else
6786 FIXME("ARB_viewport_array is not supported.\n");
6788 else
6790 if (input->sysval_semantic)
6791 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6792 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6793 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6794 shader_glsl_shader_input_name(gl_info),
6795 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6798 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6800 if (args->pointsprite)
6801 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6802 shader->u.ps.input_reg_map[input->register_idx]);
6803 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6804 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6805 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6806 needs_legacy_glsl_syntax(gl_info)
6807 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6808 else
6809 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6810 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6812 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6814 if (!semantic_idx)
6815 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6816 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6817 else if (semantic_idx == 1)
6818 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6819 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6820 else
6821 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6822 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6824 else
6826 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6827 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6832 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6834 struct glsl_program_key key;
6836 key.vs_id = entry->vs.id;
6837 key.hs_id = entry->hs.id;
6838 key.ds_id = entry->ds.id;
6839 key.gs_id = entry->gs.id;
6840 key.ps_id = entry->ps.id;
6841 key.cs_id = entry->cs.id;
6843 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6845 ERR("Failed to insert program entry.\n");
6849 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6850 const struct glsl_program_key *key)
6852 struct wine_rb_entry *entry;
6854 entry = wine_rb_get(&priv->program_lookup, key);
6855 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6858 /* Context activation is done by the caller. */
6859 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6860 struct glsl_shader_prog_link *entry)
6862 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6864 GL_EXTCALL(glDeleteProgram(entry->id));
6865 if (entry->vs.id)
6866 list_remove(&entry->vs.shader_entry);
6867 if (entry->hs.id)
6868 list_remove(&entry->hs.shader_entry);
6869 if (entry->ds.id)
6870 list_remove(&entry->ds.shader_entry);
6871 if (entry->gs.id)
6872 list_remove(&entry->gs.shader_entry);
6873 if (entry->ps.id)
6874 list_remove(&entry->ps.shader_entry);
6875 if (entry->cs.id)
6876 list_remove(&entry->cs.shader_entry);
6877 heap_free(entry);
6880 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6881 const struct wined3d_gl_info *gl_info, const DWORD *map,
6882 const struct wined3d_shader_signature *input_signature,
6883 const struct wined3d_shader_reg_maps *reg_maps_in,
6884 const struct wined3d_shader_signature *output_signature,
6885 const struct wined3d_shader_reg_maps *reg_maps_out)
6887 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6888 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6889 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6890 unsigned int in_count = vec4_varyings(3, gl_info);
6891 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6892 DWORD in_idx, *set = NULL;
6893 unsigned int i, j;
6894 char reg_mask[6];
6896 set = heap_calloc(max_varyings, sizeof(*set));
6898 for (i = 0; i < input_signature->element_count; ++i)
6900 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6902 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6903 continue;
6905 in_idx = map[input->register_idx];
6906 /* Declared, but not read register */
6907 if (in_idx == ~0u)
6908 continue;
6909 if (in_idx >= max_varyings)
6911 FIXME("More input varyings declared than supported, expect issues.\n");
6912 continue;
6915 if (in_idx == in_count)
6916 string_buffer_sprintf(destination, "gl_FrontColor");
6917 else if (in_idx == in_count + 1)
6918 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6919 else
6920 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6922 if (!set[in_idx])
6923 set[in_idx] = ~0u;
6925 for (j = 0; j < output_signature->element_count; ++j)
6927 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6928 DWORD mask;
6930 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6931 || input->semantic_idx != output->semantic_idx
6932 || strcmp(input->semantic_name, output->semantic_name)
6933 || !(mask = input->mask & output->mask))
6934 continue;
6936 if (set[in_idx] == ~0u)
6937 set[in_idx] = 0;
6938 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6939 shader_glsl_write_mask_to_str(mask, reg_mask);
6941 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6942 destination->buffer, reg_mask, output->register_idx, reg_mask);
6946 for (i = 0; i < max_varyings; ++i)
6948 unsigned int size;
6950 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6951 continue;
6953 if (set[i] == ~0u)
6954 set[i] = 0;
6956 size = 0;
6957 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6958 reg_mask[size++] = 'x';
6959 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6960 reg_mask[size++] = 'y';
6961 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6962 reg_mask[size++] = 'z';
6963 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6964 reg_mask[size++] = 'w';
6965 reg_mask[size] = '\0';
6967 if (i == in_count)
6968 string_buffer_sprintf(destination, "gl_FrontColor");
6969 else if (i == in_count + 1)
6970 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6971 else
6972 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6974 if (size == 1)
6975 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6976 else
6977 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6980 heap_free(set);
6981 string_buffer_release(&priv->string_buffers, destination);
6984 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6985 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6986 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6987 BOOL rasterizer_setup)
6989 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6990 char reg_mask[6];
6991 unsigned int i;
6993 for (i = 0; i < output_signature->element_count; ++i)
6995 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6997 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6998 continue;
7000 if (output->stream_idx)
7001 continue;
7003 if (output->register_idx >= input_count)
7004 continue;
7006 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7008 shader_addline(buffer,
7009 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7010 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7014 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7015 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7017 unsigned int i, clip_or_cull_index;
7018 const char *name;
7019 char reg_mask[6];
7021 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7022 /* Assign consecutive indices starting from 0. */
7023 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7024 for (i = 0; i < 4; ++i)
7026 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7027 continue;
7029 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7030 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7031 name, clip_or_cull_index, element->register_idx, reg_mask);
7032 ++clip_or_cull_index;
7036 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7037 const struct wined3d_gl_info *gl_info, const DWORD *map,
7038 const struct wined3d_shader_signature *input_signature,
7039 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7040 const struct wined3d_shader_signature *output_signature,
7041 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7043 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7044 const char *semantic_name;
7045 unsigned int semantic_idx;
7046 char reg_mask[6];
7047 unsigned int i;
7049 /* First, sort out position and point size system values. */
7050 for (i = 0; i < output_signature->element_count; ++i)
7052 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7054 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7055 continue;
7057 if (output->stream_idx)
7058 continue;
7060 semantic_name = output->semantic_name;
7061 semantic_idx = output->semantic_idx;
7062 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7064 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7066 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7067 reg_mask, output->register_idx, reg_mask);
7069 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7071 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7072 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7074 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7076 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7077 output->register_idx, reg_mask);
7079 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7081 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7082 output->register_idx, reg_mask);
7084 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7086 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7088 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7090 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7092 else if (output->sysval_semantic)
7094 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7098 /* Then, setup the pixel shader input. */
7099 if (reg_maps_out->shader_version.major < 4)
7100 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7101 output_signature, reg_maps_out);
7102 else
7103 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7106 /* Context activation is done by the caller. */
7107 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7108 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7109 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7111 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7112 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7113 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7114 const char *semantic_name;
7115 UINT semantic_idx;
7116 char reg_mask[6];
7117 unsigned int i;
7118 GLuint ret;
7120 string_buffer_clear(buffer);
7122 shader_glsl_add_version_declaration(buffer, gl_info);
7124 if (per_vertex_point_size)
7126 shader_addline(buffer, "uniform struct\n{\n");
7127 shader_addline(buffer, " float size_min;\n");
7128 shader_addline(buffer, " float size_max;\n");
7129 shader_addline(buffer, "} ffp_point;\n");
7132 if (ps_major < 3)
7134 DWORD colors_written_mask[2] = {0};
7135 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
7137 if (!legacy_syntax)
7139 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7140 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7141 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7142 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7145 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7147 for (i = 0; i < vs->output_signature.element_count; ++i)
7149 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7150 DWORD write_mask;
7152 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7153 continue;
7155 semantic_name = output->semantic_name;
7156 semantic_idx = output->semantic_idx;
7157 write_mask = output->mask;
7158 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7160 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7162 if (legacy_syntax)
7163 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7164 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7165 else
7166 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7167 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7169 colors_written_mask[semantic_idx] = write_mask;
7171 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7173 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7174 reg_mask, output->register_idx, reg_mask);
7176 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7178 if (semantic_idx < MAX_TEXTURES)
7180 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7181 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7182 semantic_idx, reg_mask, output->register_idx, reg_mask);
7183 texcoords_written_mask[semantic_idx] = write_mask;
7186 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7188 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7189 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7191 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7193 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7194 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7195 output->register_idx, reg_mask[1]);
7199 for (i = 0; i < 2; ++i)
7201 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7203 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7204 if (!i)
7205 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7206 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7207 reg_mask, reg_mask);
7208 else
7209 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7210 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7211 reg_mask, reg_mask);
7214 for (i = 0; i < MAX_TEXTURES; ++i)
7216 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7217 continue;
7219 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7221 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
7222 && !texcoords_written_mask[i])
7223 continue;
7225 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7226 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7227 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7231 else
7233 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7235 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7236 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7237 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7238 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7241 shader_addline(buffer, "}\n");
7243 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7244 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7245 shader_glsl_compile(gl_info, ret, buffer->buffer);
7247 return ret;
7250 static void shader_glsl_generate_stream_output_setup(struct shader_glsl_priv *priv,
7251 const struct wined3d_shader *shader, const struct wined3d_stream_output_desc *so_desc)
7253 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7254 unsigned int i;
7256 shader_addline(buffer, "out shader_in_out\n{\n");
7257 for (i = 0; i < so_desc->element_count; ++i)
7259 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7261 if (e->stream_idx)
7263 FIXME("Unhandled stream %u.\n", e->stream_idx);
7264 continue;
7266 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7267 continue;
7269 if (e->component_idx || e->component_count != 4)
7271 if (e->component_count == 1)
7272 shader_addline(buffer, "float");
7273 else
7274 shader_addline(buffer, "vec%u", e->component_count);
7275 shader_addline(buffer, " reg%u_%u_%u;\n",
7276 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1);
7278 else
7280 shader_addline(buffer, "vec4 reg%u;\n", e->register_idx);
7283 shader_addline(buffer, "} shader_out;\n");
7285 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7286 shader->limits->packed_output);
7287 for (i = 0; i < so_desc->element_count; ++i)
7289 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7291 if (e->stream_idx)
7293 FIXME("Unhandled stream %u.\n", e->stream_idx);
7294 continue;
7296 if (e->register_idx == WINED3D_STREAM_OUTPUT_GAP)
7297 continue;
7299 if (e->component_idx || e->component_count != 4)
7301 DWORD write_mask;
7302 char str_mask[6];
7304 write_mask = ((1u << e->component_count) - 1) << e->component_idx;
7305 shader_glsl_write_mask_to_str(write_mask, str_mask);
7306 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7307 e->register_idx, e->component_idx, e->component_idx + e->component_count - 1,
7308 e->register_idx, str_mask);
7310 else
7312 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n",
7313 e->register_idx, e->register_idx);
7316 shader_addline(buffer, "}\n");
7319 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7320 const struct wined3d_shader *shader, unsigned int input_count,
7321 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7323 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7324 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7326 if (rasterizer_setup)
7327 input_count = min(vec4_varyings(4, gl_info), input_count);
7329 if (input_count)
7330 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7332 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7333 prefix, shader->limits->packed_output);
7335 if (rasterizer_setup)
7336 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7337 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7338 else
7339 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7340 &shader->reg_maps, "shader_out", rasterizer_setup);
7342 shader_addline(buffer, "}\n");
7345 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7346 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7347 const char *reg_mask)
7349 if (!constant->sysval_semantic)
7351 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7352 return;
7355 switch (constant->sysval_semantic)
7357 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7358 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7359 case WINED3D_SV_TESS_FACTOR_LINEDET:
7360 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7361 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7362 break;
7363 case WINED3D_SV_TESS_FACTOR_QUADINT:
7364 case WINED3D_SV_TESS_FACTOR_TRIINT:
7365 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7366 break;
7367 default:
7368 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7369 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7373 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7374 const struct wined3d_shader_signature *signature, BOOL input_setup)
7376 unsigned int i, register_count, user_constant_index, user_constant_count;
7378 register_count = user_constant_count = 0;
7379 for (i = 0; i < signature->element_count; ++i)
7381 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7382 register_count = max(constant->register_idx + 1, register_count);
7383 if (!constant->sysval_semantic)
7384 ++user_constant_count;
7387 if (user_constant_count)
7388 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7389 input_setup ? "in" : "out", user_constant_count);
7390 if (input_setup)
7391 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7393 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7394 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7396 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7397 char reg_mask[6];
7399 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7401 if (input_setup)
7402 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7403 else
7404 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7406 shader_addline(buffer, " = ");
7408 if (input_setup)
7409 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7410 else
7411 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7413 shader_addline(buffer, ";\n");
7415 shader_addline(buffer, "}\n");
7418 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7419 const struct wined3d_gl_info *gl_info)
7421 const char *output = get_fragment_output(gl_info);
7423 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7424 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7425 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7426 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7427 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7428 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7431 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7432 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7434 const char *output = get_fragment_output(gl_info);
7436 switch (mode)
7438 case WINED3D_FFP_PS_FOG_OFF:
7439 return;
7441 case WINED3D_FFP_PS_FOG_LINEAR:
7442 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7443 break;
7445 case WINED3D_FFP_PS_FOG_EXP:
7446 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7447 break;
7449 case WINED3D_FFP_PS_FOG_EXP2:
7450 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7451 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7452 break;
7454 default:
7455 ERR("Invalid fog mode %#x.\n", mode);
7456 return;
7459 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7460 output, output);
7463 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7464 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7466 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7467 * flipping all the operators here, just negate the comparison below. */
7468 static const char * const comparison_operator[] =
7470 "", /* WINED3D_CMP_NEVER */
7471 "<", /* WINED3D_CMP_LESS */
7472 "==", /* WINED3D_CMP_EQUAL */
7473 "<=", /* WINED3D_CMP_LESSEQUAL */
7474 ">", /* WINED3D_CMP_GREATER */
7475 "!=", /* WINED3D_CMP_NOTEQUAL */
7476 ">=", /* WINED3D_CMP_GREATEREQUAL */
7477 "" /* WINED3D_CMP_ALWAYS */
7480 if (alpha_func == WINED3D_CMP_ALWAYS)
7481 return;
7483 if (alpha_func != WINED3D_CMP_NEVER)
7484 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7485 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7486 shader_addline(buffer, " discard;\n");
7489 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7490 const struct wined3d_gl_info *gl_info)
7492 if (gl_info->supported[ARB_CULL_DISTANCE])
7493 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7494 if (gl_info->supported[ARB_GPU_SHADER5])
7495 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7496 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7497 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7498 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7499 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7500 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7501 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7502 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7503 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7504 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7505 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7506 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7507 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7508 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7509 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7510 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7511 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7512 if (gl_info->supported[ARB_TEXTURE_GATHER])
7513 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7514 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7515 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7516 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7517 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7518 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7519 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7520 if (gl_info->supported[EXT_GPU_SHADER4])
7521 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7522 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7523 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7526 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7527 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7528 const struct ps_compile_args *args)
7530 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7532 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7533 if (reg_maps->shader_version.major < 2)
7534 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7536 if (args->srgb_correction)
7537 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7539 /* SM < 3 does not replace the fog stage. */
7540 if (reg_maps->shader_version.major < 3)
7541 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7543 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7546 /* Context activation is done by the caller. */
7547 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
7548 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7549 const struct wined3d_shader *shader,
7550 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
7552 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7553 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7554 const char *prefix = shader_glsl_get_prefix(version->type);
7555 const struct wined3d_gl_info *gl_info = context->gl_info;
7556 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7557 unsigned int i, extra_constants_needed = 0;
7558 struct shader_glsl_ctx_priv priv_ctx;
7559 GLuint shader_id;
7560 DWORD map;
7562 memset(&priv_ctx, 0, sizeof(priv_ctx));
7563 priv_ctx.cur_ps_args = args;
7564 priv_ctx.cur_np2fixup_info = np2fixup_info;
7565 priv_ctx.string_buffers = string_buffers;
7567 shader_glsl_add_version_declaration(buffer, gl_info);
7569 shader_glsl_enable_extensions(buffer, gl_info);
7570 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7571 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7572 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7573 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7574 if (shader_glsl_use_explicit_attrib_location(gl_info))
7575 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7576 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7577 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7578 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7579 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7580 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7581 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7582 /* The spec says that it doesn't have to be explicitly enabled, but the
7583 * nvidia drivers write a warning if we don't do so. */
7584 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7585 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7587 /* Base Declarations */
7588 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7590 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7592 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7593 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7594 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7595 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7598 /* Declare uniforms for NP2 texcoord fixup:
7599 * This is NOT done inside the loop that declares the texture samplers
7600 * since the NP2 fixup code is currently only used for the GeforceFX
7601 * series and when forcing the ARB_npot extension off. Modern cards just
7602 * skip the code anyway, so put it inside a separate loop. */
7603 if (args->np2_fixup)
7605 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7606 unsigned int cur = 0;
7608 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7609 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7610 * samplerNP2Fixup stores texture dimensions and is updated through
7611 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7613 for (i = 0; i < shader->limits->sampler; ++i)
7615 if (!reg_maps->resource_info[i].type || !(args->np2_fixup & (1u << i)))
7616 continue;
7618 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7620 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7621 continue;
7624 fixup->idx[i] = cur++;
7627 fixup->num_consts = (cur + 1) >> 1;
7628 fixup->active = args->np2_fixup;
7629 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7632 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7634 shader_addline(buffer, "uniform struct\n{\n");
7635 shader_addline(buffer, " vec4 color;\n");
7636 shader_addline(buffer, " float density;\n");
7637 shader_addline(buffer, " float end;\n");
7638 shader_addline(buffer, " float scale;\n");
7639 shader_addline(buffer, "} ffp_fog;\n");
7641 if (needs_legacy_glsl_syntax(gl_info))
7643 if (glsl_is_color_reg_read(shader, 0))
7644 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7645 if (glsl_is_color_reg_read(shader, 1))
7646 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7647 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7648 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7650 else
7652 if (glsl_is_color_reg_read(shader, 0))
7653 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7654 if (glsl_is_color_reg_read(shader, 1))
7655 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7656 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7657 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7658 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7662 if (version->major >= 3)
7664 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7666 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7667 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7668 shader->u.ps.interpolation_mode, version->major >= 4);
7669 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7672 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7674 if (!(map & 1))
7675 continue;
7677 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7679 if (reg_maps->luminanceparams & (1u << i))
7681 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7682 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7683 extra_constants_needed++;
7686 extra_constants_needed++;
7689 if (args->srgb_correction)
7691 shader_addline(buffer, "const vec4 srgb_const0 = ");
7692 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7693 shader_addline(buffer, ";\n");
7694 shader_addline(buffer, "const vec4 srgb_const1 = ");
7695 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7696 shader_addline(buffer, ";\n");
7698 if (reg_maps->vpos || reg_maps->usesdsy)
7700 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7702 ++extra_constants_needed;
7703 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7705 if (reg_maps->vpos)
7707 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7709 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7710 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7711 args->render_offscreen ? "" : "origin_upper_left, ");
7712 else if (!args->render_offscreen)
7713 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7715 shader_addline(buffer, "vec4 vpos;\n");
7719 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7720 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7722 if (!needs_legacy_glsl_syntax(gl_info))
7724 if (shader_glsl_use_explicit_attrib_location(gl_info))
7725 shader_addline(buffer, "layout(location = 0) ");
7726 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
7729 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7730 FIXME("Insufficient uniforms to run this shader.\n");
7732 if (shader->u.ps.force_early_depth_stencil)
7733 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7735 shader_addline(buffer, "void main()\n{\n");
7737 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7738 * add approximately 0.5. This causes off-by-one problems as spotted by
7739 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7740 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7741 * causes precision troubles when we just subtract 0.5.
7743 * To deal with that, just floor() the position. This will eliminate the
7744 * fraction on all cards.
7746 * TODO: Test how this behaves with multisampling.
7748 * An advantage of floor is that it works even if the driver doesn't add
7749 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7750 * to return in gl_FragCoord, even though coordinates specify the pixel
7751 * centers instead of the pixel corners. This code will behave correctly
7752 * on drivers that returns integer values. */
7753 if (reg_maps->vpos)
7755 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7756 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7757 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7758 shader_addline(buffer,
7759 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7760 else
7761 shader_addline(buffer,
7762 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7765 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7767 unsigned int i;
7768 WORD map = reg_maps->texcoord;
7770 if (legacy_syntax)
7772 if (glsl_is_color_reg_read(shader, 0))
7773 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7774 if (glsl_is_color_reg_read(shader, 1))
7775 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7778 for (i = 0; map; map >>= 1, ++i)
7780 if (map & 1)
7782 if (args->pointsprite)
7783 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7784 else if (args->texcoords_initialized & (1u << i))
7785 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7786 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7787 else
7788 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7789 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7793 if (legacy_syntax)
7794 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7797 /* Pack 3.0 inputs */
7798 if (reg_maps->shader_version.major >= 3)
7799 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7800 reg_maps->shader_version.major >= 4);
7802 /* Base Shader Body */
7803 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7804 return 0;
7806 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7807 if (reg_maps->shader_version.major < 4)
7808 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
7810 shader_addline(buffer, "}\n");
7812 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7813 TRACE("Compiling shader object %u.\n", shader_id);
7814 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7816 return shader_id;
7819 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7820 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7821 const struct vs_compile_args *args)
7823 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7824 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7825 unsigned int i;
7827 /* Unpack outputs. */
7828 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7830 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7831 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7832 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7833 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7835 if (reg_maps->shader_version.major < 3)
7837 if (args->fog_src == VS_FOG_Z)
7838 shader_addline(buffer, "%s = gl_Position.z;\n",
7839 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7840 else if (!reg_maps->fog)
7841 shader_addline(buffer, "%s = 0.0;\n",
7842 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7845 /* We always store the clipplanes without y inversion. */
7846 if (args->clip_enabled)
7848 if (legacy_syntax)
7849 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7850 else
7851 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7852 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7855 if (args->point_size && !args->per_vertex_point_size)
7856 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7858 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7859 shader_glsl_fixup_position(buffer, FALSE);
7862 /* Context activation is done by the caller. */
7863 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
7864 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7866 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7867 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7868 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7869 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7870 const struct wined3d_gl_info *gl_info = context->gl_info;
7871 struct shader_glsl_ctx_priv priv_ctx;
7872 GLuint shader_id;
7873 unsigned int i;
7875 memset(&priv_ctx, 0, sizeof(priv_ctx));
7876 priv_ctx.cur_vs_args = args;
7877 priv_ctx.string_buffers = string_buffers;
7879 shader_glsl_add_version_declaration(buffer, gl_info);
7881 shader_glsl_enable_extensions(buffer, gl_info);
7882 if (gl_info->supported[ARB_DRAW_INSTANCED])
7883 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7884 if (shader_glsl_use_explicit_attrib_location(gl_info))
7885 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7887 /* Base Declarations */
7888 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7890 for (i = 0; i < shader->input_signature.element_count; ++i)
7891 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7893 if (args->point_size && !args->per_vertex_point_size)
7895 shader_addline(buffer, "uniform struct\n{\n");
7896 shader_addline(buffer, " float size;\n");
7897 shader_addline(buffer, " float size_min;\n");
7898 shader_addline(buffer, " float size_max;\n");
7899 shader_addline(buffer, "} ffp_point;\n");
7902 if (!needs_legacy_glsl_syntax(gl_info))
7904 if (args->clip_enabled)
7905 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7907 if (version->major < 3)
7909 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7910 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7911 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7912 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7916 if (version->major < 4)
7917 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
7919 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7920 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7922 if (reg_maps->shader_version.major >= 4)
7923 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
7924 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
7926 shader_addline(buffer, "void main()\n{\n");
7928 if (reg_maps->input_rel_addressing)
7930 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
7931 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
7932 for (i = 0; i < shader->input_signature.element_count; ++i)
7934 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
7935 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
7939 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7940 return 0;
7942 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7943 if (reg_maps->shader_version.major < 4)
7944 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7946 shader_addline(buffer, "}\n");
7948 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7949 TRACE("Compiling shader object %u.\n", shader_id);
7950 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7952 return shader_id;
7955 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
7956 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
7958 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7959 char reg_mask[6];
7960 unsigned int i;
7962 for (i = 0; i < output_signature->element_count; ++i)
7964 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7966 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7967 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
7968 output->register_idx, reg_mask, output->register_idx, reg_mask);
7972 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
7973 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
7974 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
7975 const char *phase_name, unsigned phase_idx)
7977 unsigned int i;
7978 HRESULT hr;
7980 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
7981 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
7982 for (i = 0; i < phase->temporary_count; ++i)
7983 shader_addline(buffer, "vec4 R%u;\n", i);
7984 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
7985 shader_addline(buffer, "}\n");
7986 return hr;
7989 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
7990 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
7992 if (phase->instance_count)
7994 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
7995 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
7996 shader_addline(buffer, "}\n");
7998 else
8000 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8004 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context *context,
8005 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8007 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8008 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8009 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8010 const struct wined3d_gl_info *gl_info = context->gl_info;
8011 const struct wined3d_hull_shader *hs = &shader->u.hs;
8012 const struct wined3d_shader_phase *phase;
8013 struct shader_glsl_ctx_priv priv_ctx;
8014 GLuint shader_id;
8015 unsigned int i;
8017 memset(&priv_ctx, 0, sizeof(priv_ctx));
8018 priv_ctx.string_buffers = string_buffers;
8020 shader_glsl_add_version_declaration(buffer, gl_info);
8022 shader_glsl_enable_extensions(buffer, gl_info);
8023 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8025 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8027 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8029 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8030 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8032 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8034 if (hs->phases.control_point)
8036 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8037 shader->limits->packed_output);
8038 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8039 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8040 shader_addline(buffer, "}\n");
8043 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8044 if ((phase = hs->phases.control_point))
8046 for (i = 0; i < phase->temporary_count; ++i)
8047 shader_addline(buffer, "vec4 R%u;\n", i);
8048 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8049 return 0;
8050 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8052 else
8054 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8056 shader_addline(buffer, "}\n");
8058 for (i = 0; i < hs->phases.fork_count; ++i)
8060 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8061 &hs->phases.fork[i], "fork", i)))
8062 return 0;
8065 for (i = 0; i < hs->phases.join_count; ++i)
8067 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8068 &hs->phases.join[i], "join", i)))
8069 return 0;
8072 shader_addline(buffer, "void main()\n{\n");
8073 shader_addline(buffer, "hs_control_point_phase();\n");
8074 if (reg_maps->vocp)
8075 shader_addline(buffer, "barrier();\n");
8076 for (i = 0; i < hs->phases.fork_count; ++i)
8077 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8078 for (i = 0; i < hs->phases.join_count; ++i)
8079 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8080 shader_addline(buffer, "setup_patch_constant_output();\n");
8081 shader_addline(buffer, "}\n");
8083 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8084 TRACE("Compiling shader object %u.\n", shader_id);
8085 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8087 return shader_id;
8090 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8091 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8092 const struct ds_compile_args *args)
8094 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8096 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8097 shader_glsl_fixup_position(buffer, FALSE);
8100 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context *context,
8101 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8103 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8104 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8105 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8106 const struct wined3d_gl_info *gl_info = context->gl_info;
8107 struct shader_glsl_ctx_priv priv_ctx;
8108 GLuint shader_id;
8110 memset(&priv_ctx, 0, sizeof(priv_ctx));
8111 priv_ctx.cur_ds_args = args;
8112 priv_ctx.string_buffers = string_buffers;
8114 shader_glsl_add_version_declaration(buffer, gl_info);
8116 shader_glsl_enable_extensions(buffer, gl_info);
8117 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8119 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8121 shader_addline(buffer, "layout(");
8122 switch (shader->u.ds.tessellator_domain)
8124 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8125 shader_addline(buffer, "isolines");
8126 break;
8127 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8128 shader_addline(buffer, "quads");
8129 break;
8130 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8131 shader_addline(buffer, "triangles");
8132 break;
8134 switch (args->tessellator_output_primitive)
8136 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8137 if (args->render_offscreen)
8138 shader_addline(buffer, ", ccw");
8139 else
8140 shader_addline(buffer, ", cw");
8141 break;
8142 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8143 if (args->render_offscreen)
8144 shader_addline(buffer, ", cw");
8145 else
8146 shader_addline(buffer, ", ccw");
8147 break;
8148 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8149 shader_addline(buffer, ", point_mode");
8150 break;
8151 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8152 break;
8154 switch (args->tessellator_partitioning)
8156 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8157 shader_addline(buffer, ", fractional_odd_spacing");
8158 break;
8159 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8160 shader_addline(buffer, ", fractional_even_spacing");
8161 break;
8162 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8163 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8164 shader_addline(buffer, ", equal_spacing");
8165 break;
8167 shader_addline(buffer, ") in;\n");
8169 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8171 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8172 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8174 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8175 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8176 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8178 shader_addline(buffer, "void main()\n{\n");
8179 shader_addline(buffer, "setup_patch_constant_input();\n");
8181 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8182 return 0;
8184 shader_addline(buffer, "}\n");
8186 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8187 TRACE("Compiling shader object %u.\n", shader_id);
8188 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8190 return shader_id;
8193 /* Context activation is done by the caller. */
8194 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
8195 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8197 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8198 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8199 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8200 const struct wined3d_gl_info *gl_info = context->gl_info;
8201 const struct wined3d_shader_signature_element *output;
8202 enum wined3d_primitive_type primitive_type;
8203 struct shader_glsl_ctx_priv priv_ctx;
8204 unsigned int max_vertices;
8205 unsigned int i, j;
8206 GLuint shader_id;
8208 memset(&priv_ctx, 0, sizeof(priv_ctx));
8209 priv_ctx.string_buffers = string_buffers;
8211 shader_glsl_add_version_declaration(buffer, gl_info);
8213 shader_glsl_enable_extensions(buffer, gl_info);
8215 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8217 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8218 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8219 if (shader->u.gs.instance_count > 1)
8220 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8221 shader_addline(buffer, ") in;\n");
8223 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8224 if (!(max_vertices = shader->u.gs.vertices_out))
8226 switch (args->primitive_type)
8228 case WINED3D_PT_POINTLIST:
8229 max_vertices = 1;
8230 break;
8231 case WINED3D_PT_LINELIST:
8232 max_vertices = 2;
8233 break;
8234 case WINED3D_PT_TRIANGLELIST:
8235 max_vertices = 3;
8236 break;
8237 default:
8238 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8239 break;
8242 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8243 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8244 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8246 if (!gl_info->supported[ARB_CLIP_CONTROL])
8248 shader_addline(buffer, "uniform vec4 pos_fixup");
8249 if (reg_maps->viewport_array)
8250 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8251 shader_addline(buffer, ";\n");
8254 if (is_rasterization_disabled(shader))
8256 shader_glsl_generate_stream_output_setup(priv, shader, &shader->u.gs.so_desc);
8258 else
8260 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8261 gl_info, TRUE, args->interpolation_mode);
8264 shader_addline(buffer, "void main()\n{\n");
8265 if (shader->function)
8267 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8268 return 0;
8270 else
8272 for (i = 0; i < max_vertices; ++i)
8274 for (j = 0; j < shader->output_signature.element_count; ++j)
8276 output = &shader->output_signature.elements[j];
8277 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8278 output->register_idx, i, output->register_idx);
8280 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8281 if (!gl_info->supported[ARB_CLIP_CONTROL])
8282 shader_glsl_fixup_position(buffer, FALSE);
8283 shader_addline(buffer, "EmitVertex();\n");
8286 shader_addline(buffer, "}\n");
8288 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8289 TRACE("Compiling shader object %u.\n", shader_id);
8290 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8292 return shader_id;
8295 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8297 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8298 const struct wined3d_gl_info *gl_info = ctx->gl_info;
8299 struct wined3d_string_buffer *buffer = ctx->buffer;
8300 const struct wined3d_shader *shader = ctx->shader;
8302 switch (shader->reg_maps.shader_version.type)
8304 case WINED3D_SHADER_TYPE_PIXEL:
8305 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args);
8306 break;
8307 case WINED3D_SHADER_TYPE_VERTEX:
8308 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8309 break;
8310 case WINED3D_SHADER_TYPE_DOMAIN:
8311 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8312 break;
8313 case WINED3D_SHADER_TYPE_GEOMETRY:
8314 case WINED3D_SHADER_TYPE_COMPUTE:
8315 break;
8316 default:
8317 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8318 break;
8322 /* Context activation is done by the caller. */
8323 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
8324 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8325 const struct wined3d_shader *shader)
8327 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8328 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8329 const struct wined3d_gl_info *gl_info = context->gl_info;
8330 struct shader_glsl_ctx_priv priv_ctx;
8331 GLuint shader_id;
8332 unsigned int i;
8334 memset(&priv_ctx, 0, sizeof(priv_ctx));
8335 priv_ctx.string_buffers = string_buffers;
8337 shader_glsl_add_version_declaration(buffer, gl_info);
8339 shader_glsl_enable_extensions(buffer, gl_info);
8340 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8342 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
8344 for (i = 0; i < reg_maps->tgsm_count; ++i)
8346 if (reg_maps->tgsm[i].size)
8347 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8350 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8351 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8353 shader_addline(buffer, "void main()\n{\n");
8354 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8355 shader_addline(buffer, "}\n");
8357 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8358 TRACE("Compiling shader object %u.\n", shader_id);
8359 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8361 return shader_id;
8364 static GLuint find_glsl_pshader(const struct wined3d_context *context,
8365 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8366 struct wined3d_shader *shader,
8367 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8369 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8370 struct glsl_shader_private *shader_data;
8371 struct ps_np2fixup_info *np2fixup;
8372 UINT i;
8373 DWORD new_size;
8374 GLuint ret;
8376 if (!shader->backend_data)
8378 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8380 ERR("Failed to allocate backend data.\n");
8381 return 0;
8384 shader_data = shader->backend_data;
8385 gl_shaders = shader_data->gl_shaders.ps;
8387 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8388 * so a linear search is more performant than a hashmap or a binary search
8389 * (cache coherency etc)
8391 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8393 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8395 if (args->np2_fixup)
8396 *np2fixup_info = &gl_shaders[i].np2fixup;
8397 return gl_shaders[i].id;
8401 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8402 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8404 if (shader_data->num_gl_shaders)
8406 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8407 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8409 else
8411 new_array = heap_alloc(sizeof(*gl_shaders));
8412 new_size = 1;
8415 if(!new_array) {
8416 ERR("Out of memory\n");
8417 return 0;
8419 shader_data->gl_shaders.ps = new_array;
8420 shader_data->shader_array_size = new_size;
8421 gl_shaders = new_array;
8424 gl_shaders[shader_data->num_gl_shaders].args = *args;
8426 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8427 memset(np2fixup, 0, sizeof(*np2fixup));
8428 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8430 pixelshader_update_resource_types(shader, args->tex_types);
8432 string_buffer_clear(buffer);
8433 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
8434 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8436 return ret;
8439 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8440 const DWORD use_map)
8442 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
8443 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
8444 if (stored->point_size != new->point_size)
8445 return FALSE;
8446 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8447 return FALSE;
8448 if (stored->flatshading != new->flatshading)
8449 return FALSE;
8450 if (stored->next_shader_type != new->next_shader_type)
8451 return FALSE;
8452 if (stored->next_shader_input_count != new->next_shader_input_count)
8453 return FALSE;
8454 return stored->fog_src == new->fog_src;
8457 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
8458 struct wined3d_shader *shader, const struct vs_compile_args *args)
8460 UINT i;
8461 DWORD new_size;
8462 DWORD use_map = context->stream_info.use_map;
8463 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8464 struct glsl_shader_private *shader_data;
8465 GLuint ret;
8467 if (!shader->backend_data)
8469 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8471 ERR("Failed to allocate backend data.\n");
8472 return 0;
8475 shader_data = shader->backend_data;
8476 gl_shaders = shader_data->gl_shaders.vs;
8478 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8479 * so a linear search is more performant than a hashmap or a binary search
8480 * (cache coherency etc)
8482 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8484 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8485 return gl_shaders[i].id;
8488 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8490 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8492 if (shader_data->num_gl_shaders)
8494 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8495 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8497 else
8499 new_array = heap_alloc(sizeof(*gl_shaders));
8500 new_size = 1;
8503 if(!new_array) {
8504 ERR("Out of memory\n");
8505 return 0;
8507 shader_data->gl_shaders.vs = new_array;
8508 shader_data->shader_array_size = new_size;
8509 gl_shaders = new_array;
8512 gl_shaders[shader_data->num_gl_shaders].args = *args;
8514 string_buffer_clear(&priv->shader_buffer);
8515 ret = shader_glsl_generate_vshader(context, priv, shader, args);
8516 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8518 return ret;
8521 static GLuint find_glsl_hull_shader(const struct wined3d_context *context,
8522 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8524 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8525 struct glsl_shader_private *shader_data;
8526 unsigned int new_size;
8527 GLuint ret;
8529 if (!shader->backend_data)
8531 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8533 ERR("Failed to allocate backend data.\n");
8534 return 0;
8537 shader_data = shader->backend_data;
8538 gl_shaders = shader_data->gl_shaders.hs;
8540 if (shader_data->num_gl_shaders > 0)
8542 assert(shader_data->num_gl_shaders == 1);
8543 return gl_shaders[0].id;
8546 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8548 assert(!shader_data->gl_shaders.hs);
8549 new_size = 1;
8550 if (!(new_array = heap_alloc(sizeof(*new_array))))
8552 ERR("Failed to allocate GL shaders array.\n");
8553 return 0;
8555 shader_data->gl_shaders.hs = new_array;
8556 shader_data->shader_array_size = new_size;
8557 gl_shaders = new_array;
8559 string_buffer_clear(&priv->shader_buffer);
8560 ret = shader_glsl_generate_hull_shader(context, priv, shader);
8561 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8563 return ret;
8566 static GLuint find_glsl_domain_shader(const struct wined3d_context *context,
8567 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8569 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8570 struct glsl_shader_private *shader_data;
8571 unsigned int i, new_size;
8572 GLuint ret;
8574 if (!shader->backend_data)
8576 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8578 ERR("Failed to allocate backend data.\n");
8579 return 0;
8582 shader_data = shader->backend_data;
8583 gl_shaders = shader_data->gl_shaders.ds;
8585 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8587 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8588 return gl_shaders[i].id;
8591 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8593 if (shader_data->num_gl_shaders)
8595 new_size = shader_data->shader_array_size + 1;
8596 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8598 else
8600 new_array = heap_alloc(sizeof(*new_array));
8601 new_size = 1;
8604 if (!new_array)
8606 ERR("Failed to allocate GL shaders array.\n");
8607 return 0;
8609 shader_data->gl_shaders.ds = new_array;
8610 shader_data->shader_array_size = new_size;
8611 gl_shaders = new_array;
8613 string_buffer_clear(&priv->shader_buffer);
8614 ret = shader_glsl_generate_domain_shader(context, priv, shader, args);
8615 gl_shaders[shader_data->num_gl_shaders].args = *args;
8616 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8618 return ret;
8621 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
8622 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8624 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8625 struct glsl_shader_private *shader_data;
8626 unsigned int i, new_size;
8627 GLuint ret;
8629 if (!shader->backend_data)
8631 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8633 ERR("Failed to allocate backend data.\n");
8634 return 0;
8637 shader_data = shader->backend_data;
8638 gl_shaders = shader_data->gl_shaders.gs;
8640 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8642 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8643 return gl_shaders[i].id;
8646 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8648 if (shader_data->num_gl_shaders)
8650 new_size = shader_data->shader_array_size + 1;
8651 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8653 else
8655 new_array = heap_alloc(sizeof(*new_array));
8656 new_size = 1;
8659 if (!new_array)
8661 ERR("Failed to allocate GL shaders array.\n");
8662 return 0;
8664 shader_data->gl_shaders.gs = new_array;
8665 shader_data->shader_array_size = new_size;
8666 gl_shaders = new_array;
8668 string_buffer_clear(&priv->shader_buffer);
8669 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
8670 gl_shaders[shader_data->num_gl_shaders].args = *args;
8671 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8673 return ret;
8676 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8678 switch (mcs)
8680 case WINED3D_MCS_MATERIAL:
8681 return material;
8682 case WINED3D_MCS_COLOR1:
8683 return "ffp_attrib_diffuse";
8684 case WINED3D_MCS_COLOR2:
8685 return "ffp_attrib_specular";
8686 default:
8687 ERR("Invalid material color source %#x.\n", mcs);
8688 return "<invalid>";
8692 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8693 const struct wined3d_ffp_vs_settings *settings, unsigned int idx)
8695 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8696 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8697 if (settings->localviewer)
8698 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8699 else
8700 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8701 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0) specular +="
8702 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n", idx);
8705 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8706 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8708 const char *diffuse, *specular, *emissive, *ambient;
8709 unsigned int i, idx;
8711 if (!settings->lighting)
8713 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8714 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8715 return;
8718 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8719 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8720 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8721 shader_addline(buffer, "vec3 dir, dst;\n");
8722 shader_addline(buffer, "float att, t;\n");
8724 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8725 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8726 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8727 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8729 idx = 0;
8730 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8732 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8733 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8734 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8735 shader_addline(buffer, "dst.x = 1.0;\n");
8736 if (legacy_lighting)
8738 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8739 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8740 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8742 else
8744 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8746 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8747 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8748 if (!legacy_lighting)
8749 shader_addline(buffer, "att = 1.0 / att;\n");
8750 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8751 if (!settings->normal)
8753 shader_addline(buffer, "}\n");
8754 continue;
8756 shader_addline(buffer, "dir = normalize(dir);\n");
8757 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8758 shader_addline(buffer, "}\n");
8761 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8763 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8764 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8765 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8766 shader_addline(buffer, "dst.x = 1.0;\n");
8767 if (legacy_lighting)
8769 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8770 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8771 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8773 else
8775 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8777 shader_addline(buffer, "dir = normalize(dir);\n");
8778 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8779 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8780 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8781 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8782 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8783 idx, idx, idx, idx);
8784 if (legacy_lighting)
8785 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8786 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8787 idx, idx, idx);
8788 else
8789 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8790 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8791 idx, idx, idx);
8792 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8793 if (!settings->normal)
8795 shader_addline(buffer, "}\n");
8796 continue;
8798 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8799 shader_addline(buffer, "}\n");
8802 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8804 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8805 if (!settings->normal)
8806 continue;
8807 shader_addline(buffer, "att = 1.0;\n");
8808 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8809 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8812 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8814 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8815 if (!settings->normal)
8816 continue;
8817 shader_addline(buffer, "att = 1.0;\n");
8818 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8819 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx);
8822 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8823 ambient, diffuse, emissive);
8824 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8825 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8828 /* Context activation is done by the caller. */
8829 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8830 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8832 static const struct attrib_info
8834 const char type[6];
8835 const char name[24];
8837 attrib_info[] =
8839 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8840 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8841 /* TODO: Indexed vertex blending */
8842 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8843 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8844 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8845 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8846 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8848 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8849 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8850 BOOL output_legacy_fogcoord = legacy_syntax;
8851 BOOL legacy_lighting = priv->legacy_lighting;
8852 GLuint shader_obj;
8853 unsigned int i;
8855 string_buffer_clear(buffer);
8857 shader_glsl_add_version_declaration(buffer, gl_info);
8859 if (shader_glsl_use_explicit_attrib_location(gl_info))
8860 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8862 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8864 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8866 if (shader_glsl_use_explicit_attrib_location(gl_info))
8867 shader_addline(buffer, "layout(location = %u) ", i);
8868 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8870 shader_addline(buffer, "\n");
8872 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8873 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8874 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8875 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
8877 shader_addline(buffer, "uniform struct\n{\n");
8878 shader_addline(buffer, " vec4 emissive;\n");
8879 shader_addline(buffer, " vec4 ambient;\n");
8880 shader_addline(buffer, " vec4 diffuse;\n");
8881 shader_addline(buffer, " vec4 specular;\n");
8882 shader_addline(buffer, " float shininess;\n");
8883 shader_addline(buffer, "} ffp_material;\n");
8885 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8886 shader_addline(buffer, "uniform struct\n{\n");
8887 shader_addline(buffer, " vec4 diffuse;\n");
8888 shader_addline(buffer, " vec4 specular;\n");
8889 shader_addline(buffer, " vec4 ambient;\n");
8890 shader_addline(buffer, " vec4 position;\n");
8891 shader_addline(buffer, " vec3 direction;\n");
8892 shader_addline(buffer, " float range;\n");
8893 shader_addline(buffer, " float falloff;\n");
8894 shader_addline(buffer, " float c_att;\n");
8895 shader_addline(buffer, " float l_att;\n");
8896 shader_addline(buffer, " float q_att;\n");
8897 shader_addline(buffer, " float cos_htheta;\n");
8898 shader_addline(buffer, " float cos_hphi;\n");
8899 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
8901 if (settings->point_size)
8903 shader_addline(buffer, "uniform struct\n{\n");
8904 shader_addline(buffer, " float size;\n");
8905 shader_addline(buffer, " float size_min;\n");
8906 shader_addline(buffer, " float size_max;\n");
8907 shader_addline(buffer, " float c_att;\n");
8908 shader_addline(buffer, " float l_att;\n");
8909 shader_addline(buffer, " float q_att;\n");
8910 shader_addline(buffer, "} ffp_point;\n");
8913 if (legacy_syntax)
8915 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8916 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8917 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8918 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8920 else
8922 if (settings->clipping)
8923 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8925 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8926 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8927 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8928 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8931 shader_addline(buffer, "\nvoid main()\n{\n");
8932 shader_addline(buffer, "float m;\n");
8933 shader_addline(buffer, "vec3 r;\n");
8935 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
8937 if (attrib_info[i].name[0])
8938 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
8939 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
8941 for (i = 0; i < MAX_TEXTURES; ++i)
8943 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
8944 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
8945 && settings->texcoords & (1u << i))
8946 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
8949 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
8951 if (settings->transformed)
8953 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
8954 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8955 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
8957 else
8959 for (i = 0; i < settings->vertexblends; ++i)
8960 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
8962 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
8963 for (i = 0; i < settings->vertexblends + 1; ++i)
8964 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
8966 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
8967 if (settings->clipping)
8969 if (legacy_syntax)
8970 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
8971 else
8972 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8973 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
8975 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
8978 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
8979 if (settings->normal)
8981 if (!settings->vertexblends)
8983 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
8985 else
8987 for (i = 0; i < settings->vertexblends + 1; ++i)
8988 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
8991 if (settings->normalize)
8992 shader_addline(buffer, "normal = normalize(normal);\n");
8995 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
8996 if (legacy_syntax)
8998 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
8999 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9001 else
9003 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9004 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9007 for (i = 0; i < MAX_TEXTURES; ++i)
9009 BOOL output_legacy_texcoord = legacy_syntax;
9011 switch (settings->texgen[i] & 0xffff0000)
9013 case WINED3DTSS_TCI_PASSTHRU:
9014 if (settings->texcoords & (1u << i))
9015 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9016 i, i, i);
9017 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
9018 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9019 else
9020 output_legacy_texcoord = FALSE;
9021 break;
9023 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9024 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9025 break;
9027 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9028 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9029 break;
9031 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9032 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9033 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9034 break;
9036 case WINED3DTSS_TCI_SPHEREMAP:
9037 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9038 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9039 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9040 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9041 break;
9043 default:
9044 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9045 break;
9047 if (output_legacy_texcoord)
9048 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9051 switch (settings->fog_mode)
9053 case WINED3D_FFP_VS_FOG_OFF:
9054 output_legacy_fogcoord = FALSE;
9055 break;
9057 case WINED3D_FFP_VS_FOG_FOGCOORD:
9058 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9059 break;
9061 case WINED3D_FFP_VS_FOG_RANGE:
9062 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9063 break;
9065 case WINED3D_FFP_VS_FOG_DEPTH:
9066 if (settings->ortho_fog)
9068 if (gl_info->supported[ARB_CLIP_CONTROL])
9069 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9070 else
9071 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9072 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9074 else if (settings->transformed)
9076 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9078 else
9080 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9082 break;
9084 default:
9085 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9086 break;
9088 if (output_legacy_fogcoord)
9089 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9091 if (settings->point_size)
9093 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9094 " + ffp_point.l_att * length(ec_pos.xyz)"
9095 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9096 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9097 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9100 shader_addline(buffer, "}\n");
9102 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9103 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9105 return shader_obj;
9108 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9109 DWORD argnum, unsigned int stage, DWORD arg)
9111 const char *ret;
9113 if (arg == ARG_UNUSED)
9114 return "<unused arg>";
9116 switch (arg & WINED3DTA_SELECTMASK)
9118 case WINED3DTA_DIFFUSE:
9119 ret = "ffp_varying_diffuse";
9120 break;
9122 case WINED3DTA_CURRENT:
9123 ret = "ret";
9124 break;
9126 case WINED3DTA_TEXTURE:
9127 switch (stage)
9129 case 0: ret = "tex0"; break;
9130 case 1: ret = "tex1"; break;
9131 case 2: ret = "tex2"; break;
9132 case 3: ret = "tex3"; break;
9133 case 4: ret = "tex4"; break;
9134 case 5: ret = "tex5"; break;
9135 case 6: ret = "tex6"; break;
9136 case 7: ret = "tex7"; break;
9137 default:
9138 ret = "<invalid texture>";
9139 break;
9141 break;
9143 case WINED3DTA_TFACTOR:
9144 ret = "tex_factor";
9145 break;
9147 case WINED3DTA_SPECULAR:
9148 ret = "ffp_varying_specular";
9149 break;
9151 case WINED3DTA_TEMP:
9152 ret = "temp_reg";
9153 break;
9155 case WINED3DTA_CONSTANT:
9156 switch (stage)
9158 case 0: ret = "tss_const0"; break;
9159 case 1: ret = "tss_const1"; break;
9160 case 2: ret = "tss_const2"; break;
9161 case 3: ret = "tss_const3"; break;
9162 case 4: ret = "tss_const4"; break;
9163 case 5: ret = "tss_const5"; break;
9164 case 6: ret = "tss_const6"; break;
9165 case 7: ret = "tss_const7"; break;
9166 default:
9167 ret = "<invalid constant>";
9168 break;
9170 break;
9172 default:
9173 return "<unhandled arg>";
9176 if (arg & WINED3DTA_COMPLEMENT)
9178 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9179 if (argnum == 0)
9180 ret = "arg0";
9181 else if (argnum == 1)
9182 ret = "arg1";
9183 else if (argnum == 2)
9184 ret = "arg2";
9187 if (arg & WINED3DTA_ALPHAREPLICATE)
9189 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9190 if (argnum == 0)
9191 ret = "arg0";
9192 else if (argnum == 1)
9193 ret = "arg1";
9194 else if (argnum == 2)
9195 ret = "arg2";
9198 return ret;
9201 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9202 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9204 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9206 if (color && alpha)
9207 dstmask = "";
9208 else if (color)
9209 dstmask = ".xyz";
9210 else
9211 dstmask = ".w";
9213 dstreg = tmp_dst ? "temp_reg" : "ret";
9215 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9216 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9217 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9219 switch (op)
9221 case WINED3D_TOP_DISABLE:
9222 break;
9224 case WINED3D_TOP_SELECT_ARG1:
9225 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9226 break;
9228 case WINED3D_TOP_SELECT_ARG2:
9229 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9230 break;
9232 case WINED3D_TOP_MODULATE:
9233 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9234 break;
9236 case WINED3D_TOP_MODULATE_4X:
9237 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9238 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9239 break;
9241 case WINED3D_TOP_MODULATE_2X:
9242 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9243 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9244 break;
9246 case WINED3D_TOP_ADD:
9247 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9248 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9249 break;
9251 case WINED3D_TOP_ADD_SIGNED:
9252 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9253 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9254 break;
9256 case WINED3D_TOP_ADD_SIGNED_2X:
9257 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9258 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9259 break;
9261 case WINED3D_TOP_SUBTRACT:
9262 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9263 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9264 break;
9266 case WINED3D_TOP_ADD_SMOOTH:
9267 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9268 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9269 break;
9271 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9272 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9273 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9274 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9275 break;
9277 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9278 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9279 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9280 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9281 break;
9283 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9284 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9285 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9286 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9287 break;
9289 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9290 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9291 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9292 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9293 break;
9295 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9296 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9297 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9298 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9299 break;
9301 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9302 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9303 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9304 break;
9306 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9307 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9308 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9309 break;
9311 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9312 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9313 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9314 break;
9315 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9316 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9317 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9318 break;
9320 case WINED3D_TOP_BUMPENVMAP:
9321 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9322 /* These are handled in the first pass, nothing to do. */
9323 break;
9325 case WINED3D_TOP_DOTPRODUCT3:
9326 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9327 dstreg, dstmask, arg1, arg2, dstmask);
9328 break;
9330 case WINED3D_TOP_MULTIPLY_ADD:
9331 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9332 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9333 break;
9335 case WINED3D_TOP_LERP:
9336 /* MSDN isn't quite right here. */
9337 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9338 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9339 break;
9341 default:
9342 FIXME("Unhandled operation %#x.\n", op);
9343 break;
9347 /* Context activation is done by the caller. */
9348 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9349 const struct ffp_frag_settings *settings, const struct wined3d_context *context)
9351 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9352 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9353 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9354 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9355 const struct wined3d_gl_info *gl_info = context->gl_info;
9356 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9357 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9358 UINT lowest_disabled_stage;
9359 GLuint shader_id;
9360 DWORD arg0, arg1, arg2;
9361 unsigned int stage;
9363 string_buffer_clear(buffer);
9365 /* Find out which textures are read */
9366 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9368 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9369 break;
9371 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9372 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9373 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9375 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9376 || (stage == 0 && settings->color_key_enabled))
9377 tex_map |= 1u << stage;
9378 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9379 tfactor_used = TRUE;
9380 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9381 tempreg_used = TRUE;
9382 if (settings->op[stage].tmp_dst)
9383 tempreg_used = TRUE;
9384 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9385 tss_const_map |= 1u << stage;
9387 switch (settings->op[stage].cop)
9389 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9390 lum_map |= 1u << stage;
9391 /* fall through */
9392 case WINED3D_TOP_BUMPENVMAP:
9393 bump_map |= 1u << stage;
9394 /* fall through */
9395 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9396 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9397 tex_map |= 1u << stage;
9398 break;
9400 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9401 tfactor_used = TRUE;
9402 break;
9404 default:
9405 break;
9408 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9409 continue;
9411 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9412 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9413 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9415 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9416 tex_map |= 1u << stage;
9417 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9418 tfactor_used = TRUE;
9419 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9420 tempreg_used = TRUE;
9421 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9422 tss_const_map |= 1u << stage;
9424 lowest_disabled_stage = stage;
9426 shader_glsl_add_version_declaration(buffer, gl_info);
9428 if (shader_glsl_use_explicit_attrib_location(gl_info))
9429 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9430 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9431 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9432 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9433 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9435 if (!needs_legacy_glsl_syntax(gl_info))
9437 if (shader_glsl_use_explicit_attrib_location(gl_info))
9438 shader_addline(buffer, "layout(location = 0) ");
9439 shader_addline(buffer, "out vec4 ps_out[1];\n");
9442 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9443 shader_addline(buffer, "vec4 ret;\n");
9444 if (tempreg_used || settings->sRGB_write)
9445 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9446 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9448 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9450 const char *sampler_type;
9452 if (tss_const_map & (1u << stage))
9453 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9455 if (!(tex_map & (1u << stage)))
9456 continue;
9458 switch (settings->op[stage].tex_type)
9460 case WINED3D_GL_RES_TYPE_TEX_1D:
9461 sampler_type = "1D";
9462 break;
9463 case WINED3D_GL_RES_TYPE_TEX_2D:
9464 sampler_type = "2D";
9465 break;
9466 case WINED3D_GL_RES_TYPE_TEX_3D:
9467 sampler_type = "3D";
9468 break;
9469 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9470 sampler_type = "Cube";
9471 break;
9472 case WINED3D_GL_RES_TYPE_TEX_RECT:
9473 sampler_type = "2DRect";
9474 break;
9475 default:
9476 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9477 sampler_type = NULL;
9478 break;
9480 if (sampler_type)
9482 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9483 shader_glsl_append_sampler_binding_qualifier(buffer, context, NULL, stage);
9484 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9487 shader_addline(buffer, "vec4 tex%u;\n", stage);
9489 if (!(bump_map & (1u << stage)))
9490 continue;
9491 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9493 if (!(lum_map & (1u << stage)))
9494 continue;
9495 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9496 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9498 if (tfactor_used)
9499 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9500 if (settings->color_key_enabled)
9501 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9502 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9504 if (settings->sRGB_write)
9506 shader_addline(buffer, "const vec4 srgb_const0 = ");
9507 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
9508 shader_addline(buffer, ";\n");
9509 shader_addline(buffer, "const vec4 srgb_const1 = ");
9510 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
9511 shader_addline(buffer, ";\n");
9514 shader_addline(buffer, "uniform struct\n{\n");
9515 shader_addline(buffer, " vec4 color;\n");
9516 shader_addline(buffer, " float density;\n");
9517 shader_addline(buffer, " float end;\n");
9518 shader_addline(buffer, " float scale;\n");
9519 shader_addline(buffer, "} ffp_fog;\n");
9521 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9522 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9524 if (legacy_syntax)
9526 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9527 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9528 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9529 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9530 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9532 else
9534 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9535 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9536 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
9537 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
9538 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9541 shader_addline(buffer, "void main()\n{\n");
9543 if (legacy_syntax)
9545 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9546 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9549 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9551 if (tex_map & (1u << stage))
9553 if (settings->pointsprite)
9554 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9555 else if (settings->texcoords_initialized & (1u << stage))
9556 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9557 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9558 else
9559 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9563 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9564 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9566 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9567 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9569 /* Generate texture sampling instructions */
9570 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9572 const char *texture_function, *coord_mask;
9573 BOOL proj;
9575 if (!(tex_map & (1u << stage)))
9576 continue;
9578 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9580 proj = FALSE;
9582 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9583 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9585 proj = TRUE;
9587 else
9589 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9590 proj = TRUE;
9593 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9594 proj = FALSE;
9596 switch (settings->op[stage].tex_type)
9598 case WINED3D_GL_RES_TYPE_TEX_1D:
9599 if (proj)
9601 texture_function = "texture1DProj";
9602 coord_mask = "xw";
9604 else
9606 texture_function = "texture1D";
9607 coord_mask = "x";
9609 break;
9610 case WINED3D_GL_RES_TYPE_TEX_2D:
9611 if (proj)
9613 texture_function = "texture2DProj";
9614 coord_mask = "xyw";
9616 else
9618 texture_function = "texture2D";
9619 coord_mask = "xy";
9621 break;
9622 case WINED3D_GL_RES_TYPE_TEX_3D:
9623 if (proj)
9625 texture_function = "texture3DProj";
9626 coord_mask = "xyzw";
9628 else
9630 texture_function = "texture3D";
9631 coord_mask = "xyz";
9633 break;
9634 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9635 texture_function = "textureCube";
9636 coord_mask = "xyz";
9637 break;
9638 case WINED3D_GL_RES_TYPE_TEX_RECT:
9639 if (proj)
9641 texture_function = "texture2DRectProj";
9642 coord_mask = "xyw";
9644 else
9646 texture_function = "texture2DRect";
9647 coord_mask = "xy";
9649 break;
9650 default:
9651 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9652 texture_function = "";
9653 coord_mask = "xyzw";
9654 break;
9656 if (!legacy_syntax)
9657 texture_function = proj ? "textureProj" : "texture";
9659 if (stage > 0
9660 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9661 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9663 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9665 /* With projective textures, texbem only divides the static
9666 * texture coordinate, not the displacement, so multiply the
9667 * displacement with the dividing parameter before passing it to
9668 * TXP. */
9669 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9671 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9673 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9674 stage, stage);
9675 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9677 else
9679 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9680 stage, stage);
9681 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9684 else
9686 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9689 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
9690 stage, texture_function, stage, coord_mask);
9692 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9693 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9694 stage, stage - 1, stage - 1, stage - 1);
9696 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9698 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9699 stage, texture_function, stage, stage);
9701 else
9703 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
9704 stage, texture_function, stage, stage, coord_mask);
9707 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9708 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9709 settings->op[stage].color_fixup);
9712 if (settings->color_key_enabled)
9714 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
9715 shader_addline(buffer, " discard;\n");
9718 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9720 /* Generate the main shader */
9721 for (stage = 0; stage < MAX_TEXTURES; ++stage)
9723 BOOL op_equal;
9725 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9726 break;
9728 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9729 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9730 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9731 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9732 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9733 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9734 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9735 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9736 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9737 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9738 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9739 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9740 else
9741 op_equal = settings->op[stage].aop == settings->op[stage].cop
9742 && settings->op[stage].carg0 == settings->op[stage].aarg0
9743 && settings->op[stage].carg1 == settings->op[stage].aarg1
9744 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9746 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9748 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9749 settings->op[stage].cop, settings->op[stage].carg0,
9750 settings->op[stage].carg1, settings->op[stage].carg2);
9752 else if (op_equal)
9754 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9755 settings->op[stage].cop, settings->op[stage].carg0,
9756 settings->op[stage].carg1, settings->op[stage].carg2);
9758 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9759 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9761 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9762 settings->op[stage].cop, settings->op[stage].carg0,
9763 settings->op[stage].carg1, settings->op[stage].carg2);
9764 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9765 settings->op[stage].aop, settings->op[stage].aarg0,
9766 settings->op[stage].aarg1, settings->op[stage].aarg2);
9770 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9771 get_fragment_output(gl_info));
9773 if (settings->sRGB_write)
9774 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9776 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9778 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9780 shader_addline(buffer, "}\n");
9782 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9783 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9785 string_buffer_release(&priv->string_buffers, tex_reg_name);
9786 return shader_id;
9789 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9790 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9792 struct glsl_ffp_vertex_shader *shader;
9793 const struct wine_rb_entry *entry;
9795 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9796 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9798 if (!(shader = heap_alloc(sizeof(*shader))))
9799 return NULL;
9801 shader->desc.settings = *settings;
9802 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9803 list_init(&shader->linked_programs);
9804 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9805 ERR("Failed to insert ffp vertex shader.\n");
9807 return shader;
9810 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9811 const struct ffp_frag_settings *args, const struct wined3d_context *context)
9813 struct glsl_ffp_fragment_shader *glsl_desc;
9814 const struct ffp_frag_desc *desc;
9816 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9817 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9819 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9820 return NULL;
9822 glsl_desc->entry.settings = *args;
9823 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context);
9824 list_init(&glsl_desc->linked_programs);
9825 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9827 return glsl_desc;
9831 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9832 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9834 unsigned int i;
9835 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9837 for (i = 0; i < vs_c_count; ++i)
9839 string_buffer_sprintf(name, "vs_c[%u]", i);
9840 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9842 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9844 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9846 string_buffer_sprintf(name, "vs_i[%u]", i);
9847 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9850 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9852 string_buffer_sprintf(name, "vs_b[%u]", i);
9853 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9856 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9858 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9860 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9861 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9863 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9864 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9865 for (i = 0; i < MAX_TEXTURES; ++i)
9867 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9868 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9870 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9871 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9872 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9873 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9874 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9875 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9876 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
9878 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9879 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9880 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9881 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9882 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9883 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9884 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9885 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9886 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9887 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9888 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9889 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9890 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9891 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9892 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9893 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9894 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9895 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9896 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9897 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9898 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9899 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9900 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9901 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9903 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
9904 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
9905 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
9906 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
9907 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
9908 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
9909 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
9911 string_buffer_release(&priv->string_buffers, name);
9914 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
9915 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
9917 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9920 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
9921 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
9923 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9926 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
9927 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
9929 unsigned int i;
9930 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9932 for (i = 0; i < ps_c_count; ++i)
9934 string_buffer_sprintf(name, "ps_c[%u]", i);
9935 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9937 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
9939 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9941 string_buffer_sprintf(name, "ps_i[%u]", i);
9942 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9945 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9947 string_buffer_sprintf(name, "ps_b[%u]", i);
9948 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9951 for (i = 0; i < MAX_TEXTURES; ++i)
9953 string_buffer_sprintf(name, "bumpenv_mat%u", i);
9954 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9955 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
9956 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9957 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
9958 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9959 string_buffer_sprintf(name, "tss_const%u", i);
9960 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9963 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
9964 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
9966 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
9967 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
9968 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
9969 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
9971 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
9973 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
9974 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
9975 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
9977 string_buffer_release(&priv->string_buffers, name);
9980 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
9981 const struct wined3d_context *context, struct wined3d_shader *shader)
9983 struct glsl_context_data *ctx_data = context->shader_backend_data;
9984 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9985 const struct wined3d_gl_info *gl_info = context->gl_info;
9986 struct glsl_cs_compiled_shader *gl_shaders;
9987 struct glsl_shader_private *shader_data;
9988 struct glsl_shader_prog_link *entry;
9989 GLuint shader_id, program_id;
9991 if (!(entry = heap_alloc(sizeof(*entry))))
9993 ERR("Out of memory.\n");
9994 return E_OUTOFMEMORY;
9997 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
9999 ERR("Failed to allocate backend data.\n");
10000 heap_free(entry);
10001 return E_OUTOFMEMORY;
10003 shader_data = shader->backend_data;
10004 gl_shaders = shader_data->gl_shaders.cs;
10006 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10008 ERR("Failed to allocate GL shader array.\n");
10009 heap_free(entry);
10010 heap_free(shader->backend_data);
10011 shader->backend_data = NULL;
10012 return E_OUTOFMEMORY;
10014 shader_data->shader_array_size = 1;
10015 gl_shaders = shader_data->gl_shaders.cs;
10017 TRACE("Compiling compute shader %p.\n", shader);
10019 string_buffer_clear(buffer);
10020 shader_id = shader_glsl_generate_compute_shader(context, buffer, &priv->string_buffers, shader);
10021 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10023 program_id = GL_EXTCALL(glCreateProgram());
10024 TRACE("Created new GLSL shader program %u.\n", program_id);
10026 entry->id = program_id;
10027 entry->vs.id = 0;
10028 entry->hs.id = 0;
10029 entry->ds.id = 0;
10030 entry->gs.id = 0;
10031 entry->ps.id = 0;
10032 entry->cs.id = shader_id;
10033 entry->constant_version = 0;
10034 entry->shader_controlled_clip_distances = 0;
10035 entry->ps.np2_fixup_info = NULL;
10036 add_glsl_program_entry(priv, entry);
10038 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10039 GL_EXTCALL(glAttachShader(program_id, shader_id));
10040 checkGLcall("glAttachShader");
10042 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10044 TRACE("Linking GLSL shader program %u.\n", program_id);
10045 GL_EXTCALL(glLinkProgram(program_id));
10046 shader_glsl_validate_link(gl_info, program_id);
10048 GL_EXTCALL(glUseProgram(program_id));
10049 checkGLcall("glUseProgram");
10050 shader_glsl_load_program_resources(context, priv, program_id, shader);
10051 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10053 entry->constant_update_mask = 0;
10055 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10056 checkGLcall("glUseProgram");
10057 return WINED3D_OK;
10060 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
10061 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10063 struct glsl_shader_private *shader_data;
10065 if (!shader->backend_data)
10067 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10068 if (FAILED(shader_glsl_compile_compute_shader(priv, context, shader)))
10070 ERR("Failed to compile compute shader %p.\n", shader);
10071 return 0;
10074 shader_data = shader->backend_data;
10075 return shader_data->gl_shaders.cs[0].id;
10078 /* Context activation is done by the caller. */
10079 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
10080 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10082 struct glsl_shader_prog_link *entry;
10083 struct wined3d_shader *shader;
10084 struct glsl_program_key key;
10085 GLuint cs_id;
10087 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10088 return;
10090 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10092 WARN("Compute shader is NULL.\n");
10093 ctx_data->glsl_program = NULL;
10094 return;
10097 cs_id = find_glsl_compute_shader(context, priv, shader);
10098 memset(&key, 0, sizeof(key));
10099 key.cs_id = cs_id;
10100 if (!(entry = get_glsl_program_entry(priv, &key)))
10101 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10102 ctx_data->glsl_program = entry;
10105 /* Context activation is done by the caller. */
10106 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
10107 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10109 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
10110 const struct wined3d_gl_info *gl_info = context->gl_info;
10111 const struct wined3d_shader *pre_rasterization_shader;
10112 const struct ps_np2fixup_info *np2fixup_info = NULL;
10113 struct wined3d_shader *hshader, *dshader, *gshader;
10114 struct glsl_shader_prog_link *entry = NULL;
10115 struct wined3d_shader *vshader = NULL;
10116 struct wined3d_shader *pshader = NULL;
10117 GLuint reorder_shader_id = 0;
10118 struct glsl_program_key key;
10119 GLuint program_id;
10120 unsigned int i;
10121 GLuint vs_id = 0;
10122 GLuint hs_id = 0;
10123 GLuint ds_id = 0;
10124 GLuint gs_id = 0;
10125 GLuint ps_id = 0;
10126 struct list *ps_list, *vs_list;
10127 WORD attribs_map;
10128 struct wined3d_string_buffer *tmp_name;
10130 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10132 vs_id = ctx_data->glsl_program->vs.id;
10133 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10135 if (use_vs(state))
10136 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10138 else if (use_vs(state))
10140 struct vs_compile_args vs_compile_args;
10142 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10144 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, context);
10145 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
10146 vs_list = &vshader->linked_programs;
10148 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10150 struct glsl_ffp_vertex_shader *ffp_shader;
10151 struct wined3d_ffp_vs_settings settings;
10153 wined3d_ffp_get_vs_settings(context, state, &settings);
10154 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10155 vs_id = ffp_shader->id;
10156 vs_list = &ffp_shader->linked_programs;
10159 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10160 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10161 hs_id = ctx_data->glsl_program->hs.id;
10162 else if (hshader)
10163 hs_id = find_glsl_hull_shader(context, priv, hshader);
10165 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10166 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10168 ds_id = ctx_data->glsl_program->ds.id;
10170 else if (dshader)
10172 struct ds_compile_args args;
10174 find_ds_compile_args(state, dshader, &args, context);
10175 ds_id = find_glsl_domain_shader(context, priv, dshader, &args);
10178 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10179 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10181 gs_id = ctx_data->glsl_program->gs.id;
10183 else if (gshader)
10185 struct gs_compile_args args;
10187 find_gs_compile_args(state, gshader, &args, context);
10188 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
10191 /* A pixel shader is not used when rasterization is disabled. */
10192 if (is_rasterization_disabled(gshader))
10194 ps_id = 0;
10195 ps_list = NULL;
10197 else if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10199 ps_id = ctx_data->glsl_program->ps.id;
10200 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10202 if (use_ps(state))
10203 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10205 else if (use_ps(state))
10207 struct ps_compile_args ps_compile_args;
10208 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10209 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
10210 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
10211 pshader, &ps_compile_args, &np2fixup_info);
10212 ps_list = &pshader->linked_programs;
10214 else if (priv->fragment_pipe == &glsl_fragment_pipe
10215 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10217 struct glsl_ffp_fragment_shader *ffp_shader;
10218 struct ffp_frag_settings settings;
10220 gen_ffp_frag_op(context, state, &settings, FALSE);
10221 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context);
10222 ps_id = ffp_shader->id;
10223 ps_list = &ffp_shader->linked_programs;
10226 key.vs_id = vs_id;
10227 key.hs_id = hs_id;
10228 key.ds_id = ds_id;
10229 key.gs_id = gs_id;
10230 key.ps_id = ps_id;
10231 key.cs_id = 0;
10232 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10234 ctx_data->glsl_program = entry;
10235 return;
10238 /* If we get to this point, then no matching program exists, so we create one */
10239 program_id = GL_EXTCALL(glCreateProgram());
10240 TRACE("Created new GLSL shader program %u.\n", program_id);
10242 /* Create the entry */
10243 entry = heap_alloc(sizeof(*entry));
10244 entry->id = program_id;
10245 entry->vs.id = vs_id;
10246 entry->hs.id = hs_id;
10247 entry->ds.id = ds_id;
10248 entry->gs.id = gs_id;
10249 entry->ps.id = ps_id;
10250 entry->cs.id = 0;
10251 entry->constant_version = 0;
10252 entry->shader_controlled_clip_distances = 0;
10253 entry->ps.np2_fixup_info = np2fixup_info;
10254 /* Add the hash table entry */
10255 add_glsl_program_entry(priv, entry);
10257 /* Set the current program */
10258 ctx_data->glsl_program = entry;
10260 /* Attach GLSL vshader */
10261 if (vs_id)
10263 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10264 GL_EXTCALL(glAttachShader(program_id, vs_id));
10265 checkGLcall("glAttachShader");
10267 list_add_head(vs_list, &entry->vs.shader_entry);
10270 if (vshader)
10272 attribs_map = vshader->reg_maps.input_registers;
10273 if (vshader->reg_maps.shader_version.major < 4)
10275 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10276 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
10277 d3d_info->emulated_flatshading
10278 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10279 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10280 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10281 checkGLcall("glAttachShader");
10282 /* Flag the reorder function for deletion, it will be freed
10283 * automatically when the program is destroyed. */
10284 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10287 else
10289 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10292 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10294 /* Bind vertex attributes to a corresponding index number to match
10295 * the same index numbers as ARB_vertex_programs (makes loading
10296 * vertex attributes simpler). With this method, we can use the
10297 * exact same code to load the attributes later for both ARB and
10298 * GLSL shaders.
10300 * We have to do this here because we need to know the Program ID
10301 * in order to make the bindings work, and it has to be done prior
10302 * to linking the GLSL program. */
10303 tmp_name = string_buffer_get(&priv->string_buffers);
10304 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10306 if (!(attribs_map & 1))
10307 continue;
10309 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10310 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10311 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10313 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10314 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10315 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10316 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10319 checkGLcall("glBindAttribLocation");
10320 string_buffer_release(&priv->string_buffers, tmp_name);
10322 if (!needs_legacy_glsl_syntax(gl_info))
10324 GL_EXTCALL(glBindFragDataLocation(program_id, 0, "ps_out"));
10325 checkGLcall("glBindFragDataLocation");
10329 if (hshader)
10331 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10332 GL_EXTCALL(glAttachShader(program_id, hs_id));
10333 checkGLcall("glAttachShader");
10335 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10338 if (dshader)
10340 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10341 GL_EXTCALL(glAttachShader(program_id, ds_id));
10342 checkGLcall("glAttachShader");
10344 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10347 if (gshader)
10349 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10350 GL_EXTCALL(glAttachShader(program_id, gs_id));
10351 checkGLcall("glAttachShader");
10353 shader_glsl_init_transform_feedback(context, priv, program_id, gshader);
10355 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10358 /* Attach GLSL pshader */
10359 if (ps_id)
10361 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10362 GL_EXTCALL(glAttachShader(program_id, ps_id));
10363 checkGLcall("glAttachShader");
10365 list_add_head(ps_list, &entry->ps.shader_entry);
10368 /* Link the program */
10369 TRACE("Linking GLSL shader program %u.\n", program_id);
10370 GL_EXTCALL(glLinkProgram(program_id));
10371 shader_glsl_validate_link(gl_info, program_id);
10373 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10374 vshader ? vshader->limits->constant_float : 0);
10375 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10376 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10377 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10378 pshader ? pshader->limits->constant_float : 0);
10379 checkGLcall("find glsl program uniform locations");
10381 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10382 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10384 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10385 entry->shader_controlled_clip_distances = 1;
10386 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10389 if (needs_legacy_glsl_syntax(gl_info))
10391 if (pshader && pshader->reg_maps.shader_version.major >= 3
10392 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10394 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10395 entry->vs.vertex_color_clamp = GL_FALSE;
10397 else
10399 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10402 else
10404 /* With core profile we never change vertex_color_clamp from
10405 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10406 * glClampColorARB(). */
10407 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10410 /* Set the shader to allow uniform loading on it */
10411 GL_EXTCALL(glUseProgram(program_id));
10412 checkGLcall("glUseProgram");
10414 entry->constant_update_mask = 0;
10415 if (vshader)
10417 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10418 if (vshader->reg_maps.integer_constants)
10419 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10420 if (vshader->reg_maps.boolean_constants)
10421 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10422 if (entry->vs.pos_fixup_location != -1)
10423 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10425 shader_glsl_load_program_resources(context, priv, program_id, vshader);
10427 else
10429 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10430 | WINED3D_SHADER_CONST_FFP_PROJ;
10432 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10434 if (entry->vs.modelview_matrix_location[i] != -1)
10436 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10437 break;
10441 for (i = 0; i < MAX_TEXTURES; ++i)
10443 if (entry->vs.texture_matrix_location[i] != -1)
10445 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10446 break;
10449 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10450 || entry->vs.material_specular_location != -1
10451 || entry->vs.material_emissive_location != -1
10452 || entry->vs.material_shininess_location != -1)
10453 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10454 if (entry->vs.light_ambient_location != -1)
10455 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10457 if (entry->vs.clip_planes_location != -1)
10458 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10459 if (entry->vs.pointsize_min_location != -1)
10460 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10462 if (hshader)
10463 shader_glsl_load_program_resources(context, priv, program_id, hshader);
10465 if (dshader)
10467 if (entry->ds.pos_fixup_location != -1)
10468 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10470 shader_glsl_load_program_resources(context, priv, program_id, dshader);
10473 if (gshader)
10475 if (entry->gs.pos_fixup_location != -1)
10476 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10478 shader_glsl_load_program_resources(context, priv, program_id, gshader);
10481 if (ps_id)
10483 if (pshader)
10485 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10486 if (pshader->reg_maps.integer_constants)
10487 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10488 if (pshader->reg_maps.boolean_constants)
10489 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10490 if (entry->ps.ycorrection_location != -1)
10491 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10493 shader_glsl_load_program_resources(context, priv, program_id, pshader);
10494 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10496 else
10498 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10500 shader_glsl_load_samplers(context, priv, program_id, NULL);
10503 for (i = 0; i < MAX_TEXTURES; ++i)
10505 if (entry->ps.bumpenv_mat_location[i] != -1)
10507 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10508 break;
10512 if (entry->ps.fog_color_location != -1)
10513 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10514 if (entry->ps.alpha_test_ref_location != -1)
10515 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10516 if (entry->ps.np2_fixup_location != -1)
10517 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10518 if (entry->ps.color_key_location != -1)
10519 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10523 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10525 struct wined3d_device *device = shader->device;
10526 struct wined3d_context *context;
10528 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10530 context = context_acquire(device, NULL, 0);
10531 shader_glsl_compile_compute_shader(shader_priv, context, shader);
10532 context_release(context);
10536 /* Context activation is done by the caller. */
10537 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10538 const struct wined3d_state *state)
10540 struct glsl_context_data *ctx_data = context->shader_backend_data;
10541 const struct wined3d_gl_info *gl_info = context->gl_info;
10542 struct shader_glsl_priv *priv = shader_priv;
10543 struct glsl_shader_prog_link *glsl_program;
10544 GLenum current_vertex_color_clamp;
10545 GLuint program_id, prev_id;
10547 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
10548 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
10550 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10551 set_glsl_shader_program(context, state, priv, ctx_data);
10552 glsl_program = ctx_data->glsl_program;
10554 if (glsl_program)
10556 program_id = glsl_program->id;
10557 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10558 if (glsl_program->shader_controlled_clip_distances)
10559 context_enable_clip_distances(context, glsl_program->clip_distance_mask);
10561 else
10563 program_id = 0;
10564 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10567 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10569 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10570 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10572 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10573 checkGLcall("glClampColorARB");
10575 else
10577 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10581 TRACE("Using GLSL program %u.\n", program_id);
10583 if (prev_id != program_id)
10585 GL_EXTCALL(glUseProgram(program_id));
10586 checkGLcall("glUseProgram");
10588 if (glsl_program)
10589 context->constant_update_mask |= glsl_program->constant_update_mask;
10592 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10595 /* Context activation is done by the caller. */
10596 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10597 const struct wined3d_state *state)
10599 struct glsl_context_data *ctx_data = context->shader_backend_data;
10600 const struct wined3d_gl_info *gl_info = context->gl_info;
10601 struct shader_glsl_priv *priv = shader_priv;
10602 GLuint program_id, prev_id;
10604 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10605 set_glsl_compute_shader_program(context, state, priv, ctx_data);
10606 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10608 TRACE("Using GLSL program %u.\n", program_id);
10610 if (prev_id != program_id)
10612 GL_EXTCALL(glUseProgram(program_id));
10613 checkGLcall("glUseProgram");
10616 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10617 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10618 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10619 | (1u << WINED3D_SHADER_TYPE_HULL)
10620 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10623 /* "context" is not necessarily the currently active context. */
10624 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10626 struct glsl_context_data *ctx_data = context->shader_backend_data;
10628 ctx_data->glsl_program = NULL;
10629 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10630 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10631 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10632 | (1u << WINED3D_SHADER_TYPE_HULL)
10633 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10634 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10637 /* Context activation is done by the caller. */
10638 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10640 struct glsl_context_data *ctx_data = context->shader_backend_data;
10641 const struct wined3d_gl_info *gl_info = context->gl_info;
10642 struct shader_glsl_priv *priv = shader_priv;
10644 shader_glsl_invalidate_current_program(context);
10645 GL_EXTCALL(glUseProgram(0));
10646 checkGLcall("glUseProgram");
10648 priv->vertex_pipe->vp_enable(gl_info, FALSE);
10649 priv->fragment_pipe->enable_extension(gl_info, FALSE);
10651 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10653 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10654 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10655 checkGLcall("glClampColorARB");
10659 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10660 const struct glsl_shader_prog_link *program)
10662 const struct glsl_context_data *ctx_data;
10663 struct wined3d_context *context;
10664 unsigned int i;
10666 for (i = 0; i < device->context_count; ++i)
10668 context = device->contexts[i];
10669 ctx_data = context->shader_backend_data;
10671 if (ctx_data->glsl_program == program)
10672 shader_glsl_invalidate_current_program(context);
10676 static void shader_glsl_destroy(struct wined3d_shader *shader)
10678 struct glsl_shader_private *shader_data = shader->backend_data;
10679 struct wined3d_device *device = shader->device;
10680 struct shader_glsl_priv *priv = device->shader_priv;
10681 const struct wined3d_gl_info *gl_info;
10682 const struct list *linked_programs;
10683 struct wined3d_context *context;
10685 if (!shader_data || !shader_data->num_gl_shaders)
10687 heap_free(shader_data);
10688 shader->backend_data = NULL;
10689 return;
10692 context = context_acquire(device, NULL, 0);
10693 gl_info = context->gl_info;
10695 TRACE("Deleting linked programs.\n");
10696 linked_programs = &shader->linked_programs;
10697 if (linked_programs->next)
10699 struct glsl_shader_prog_link *entry, *entry2;
10700 UINT i;
10702 switch (shader->reg_maps.shader_version.type)
10704 case WINED3D_SHADER_TYPE_PIXEL:
10706 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10708 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10710 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10711 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10712 checkGLcall("glDeleteShader");
10714 heap_free(shader_data->gl_shaders.ps);
10716 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10717 struct glsl_shader_prog_link, ps.shader_entry)
10719 shader_glsl_invalidate_contexts_program(device, entry);
10720 delete_glsl_program_entry(priv, gl_info, entry);
10723 break;
10726 case WINED3D_SHADER_TYPE_VERTEX:
10728 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10730 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10732 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10733 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10734 checkGLcall("glDeleteShader");
10736 heap_free(shader_data->gl_shaders.vs);
10738 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10739 struct glsl_shader_prog_link, vs.shader_entry)
10741 shader_glsl_invalidate_contexts_program(device, entry);
10742 delete_glsl_program_entry(priv, gl_info, entry);
10745 break;
10748 case WINED3D_SHADER_TYPE_HULL:
10750 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10752 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10754 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10755 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10756 checkGLcall("glDeleteShader");
10758 heap_free(shader_data->gl_shaders.hs);
10760 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10761 struct glsl_shader_prog_link, hs.shader_entry)
10763 shader_glsl_invalidate_contexts_program(device, entry);
10764 delete_glsl_program_entry(priv, gl_info, entry);
10767 break;
10770 case WINED3D_SHADER_TYPE_DOMAIN:
10772 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10774 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10776 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10777 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10778 checkGLcall("glDeleteShader");
10780 heap_free(shader_data->gl_shaders.ds);
10782 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10783 struct glsl_shader_prog_link, ds.shader_entry)
10785 shader_glsl_invalidate_contexts_program(device, entry);
10786 delete_glsl_program_entry(priv, gl_info, entry);
10789 break;
10792 case WINED3D_SHADER_TYPE_GEOMETRY:
10794 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10796 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10798 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10799 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10800 checkGLcall("glDeleteShader");
10802 heap_free(shader_data->gl_shaders.gs);
10804 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10805 struct glsl_shader_prog_link, gs.shader_entry)
10807 shader_glsl_invalidate_contexts_program(device, entry);
10808 delete_glsl_program_entry(priv, gl_info, entry);
10811 break;
10814 case WINED3D_SHADER_TYPE_COMPUTE:
10816 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10818 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10820 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10821 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10822 checkGLcall("glDeleteShader");
10824 heap_free(shader_data->gl_shaders.cs);
10826 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10827 struct glsl_shader_prog_link, cs.shader_entry)
10829 shader_glsl_invalidate_contexts_program(device, entry);
10830 delete_glsl_program_entry(priv, gl_info, entry);
10833 break;
10836 default:
10837 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10838 break;
10842 heap_free(shader->backend_data);
10843 shader->backend_data = NULL;
10845 context_release(context);
10848 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10850 const struct glsl_program_key *k = key;
10851 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10852 const struct glsl_shader_prog_link, program_lookup_entry);
10854 if (k->vs_id > prog->vs.id) return 1;
10855 else if (k->vs_id < prog->vs.id) return -1;
10857 if (k->gs_id > prog->gs.id) return 1;
10858 else if (k->gs_id < prog->gs.id) return -1;
10860 if (k->ps_id > prog->ps.id) return 1;
10861 else if (k->ps_id < prog->ps.id) return -1;
10863 if (k->hs_id > prog->hs.id) return 1;
10864 else if (k->hs_id < prog->hs.id) return -1;
10866 if (k->ds_id > prog->ds.id) return 1;
10867 else if (k->ds_id < prog->ds.id) return -1;
10869 if (k->cs_id > prog->cs.id) return 1;
10870 else if (k->cs_id < prog->cs.id) return -1;
10872 return 0;
10875 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10877 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10878 + constant_count * sizeof(*heap->contained)
10879 + constant_count * sizeof(*heap->positions);
10880 void *mem;
10882 if (!(mem = heap_alloc(size)))
10884 ERR("Failed to allocate memory\n");
10885 return FALSE;
10888 heap->entries = mem;
10889 heap->entries[1].version = 0;
10890 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10891 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10892 heap->positions = (unsigned int *)(heap->contained + constant_count);
10893 heap->size = 1;
10895 return TRUE;
10898 static void constant_heap_free(struct constant_heap *heap)
10900 heap_free(heap->entries);
10903 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
10904 const struct fragment_pipeline *fragment_pipe)
10906 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
10907 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
10908 struct fragment_caps fragment_caps;
10909 void *vertex_priv, *fragment_priv;
10910 struct shader_glsl_priv *priv;
10912 if (!(priv = heap_alloc_zero(sizeof(*priv))))
10913 return E_OUTOFMEMORY;
10915 string_buffer_list_init(&priv->string_buffers);
10917 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
10919 ERR("Failed to initialize vertex pipe.\n");
10920 heap_free(priv);
10921 return E_FAIL;
10924 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
10926 ERR("Failed to initialize fragment pipe.\n");
10927 vertex_pipe->vp_free(device);
10928 heap_free(priv);
10929 return E_FAIL;
10932 if (!string_buffer_init(&priv->shader_buffer))
10934 ERR("Failed to initialize shader buffer.\n");
10935 goto fail;
10938 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
10940 ERR("Failed to allocate memory.\n");
10941 goto fail;
10944 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
10946 ERR("Failed to initialize vertex shader constant heap\n");
10947 goto fail;
10950 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
10952 ERR("Failed to initialize pixel shader constant heap\n");
10953 goto fail;
10956 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
10958 priv->next_constant_version = 1;
10959 priv->vertex_pipe = vertex_pipe;
10960 priv->fragment_pipe = fragment_pipe;
10961 fragment_pipe->get_caps(gl_info, &fragment_caps);
10962 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
10963 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
10965 device->vertex_priv = vertex_priv;
10966 device->fragment_priv = fragment_priv;
10967 device->shader_priv = priv;
10969 return WINED3D_OK;
10971 fail:
10972 constant_heap_free(&priv->pconst_heap);
10973 constant_heap_free(&priv->vconst_heap);
10974 heap_free(priv->stack);
10975 string_buffer_free(&priv->shader_buffer);
10976 fragment_pipe->free_private(device);
10977 vertex_pipe->vp_free(device);
10978 heap_free(priv);
10979 return E_OUTOFMEMORY;
10982 /* Context activation is done by the caller. */
10983 static void shader_glsl_free(struct wined3d_device *device)
10985 struct shader_glsl_priv *priv = device->shader_priv;
10987 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
10988 constant_heap_free(&priv->pconst_heap);
10989 constant_heap_free(&priv->vconst_heap);
10990 heap_free(priv->stack);
10991 string_buffer_list_cleanup(&priv->string_buffers);
10992 string_buffer_free(&priv->shader_buffer);
10993 priv->fragment_pipe->free_private(device);
10994 priv->vertex_pipe->vp_free(device);
10996 heap_free(device->shader_priv);
10997 device->shader_priv = NULL;
11000 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11002 struct glsl_context_data *ctx_data;
11004 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11005 return FALSE;
11006 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11007 context->shader_backend_data = ctx_data;
11008 return TRUE;
11011 static void shader_glsl_free_context_data(struct wined3d_context *context)
11013 heap_free(context->shader_backend_data);
11016 static void shader_glsl_init_context_state(struct wined3d_context *context)
11018 const struct wined3d_gl_info *gl_info = context->gl_info;
11020 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11021 checkGLcall("GL_PROGRAM_POINT_SIZE");
11024 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11026 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11027 && gl_info->supported[WINED3D_GL_VERSION_3_2]
11028 && gl_info->supported[ARB_SAMPLER_OBJECTS]
11029 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11030 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11032 if (shader_model_4
11033 && gl_info->supported[ARB_COMPUTE_SHADER]
11034 && gl_info->supported[ARB_CULL_DISTANCE]
11035 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11036 && gl_info->supported[ARB_DRAW_INDIRECT]
11037 && gl_info->supported[ARB_GPU_SHADER5]
11038 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11039 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11040 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11041 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11042 && gl_info->supported[ARB_TESSELLATION_SHADER]
11043 && gl_info->supported[ARB_TEXTURE_COMPRESSION_BPTC]
11044 && gl_info->supported[ARB_TEXTURE_GATHER]
11045 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11046 return 5;
11048 if (shader_model_4)
11049 return 4;
11051 /* Support for texldd and texldl instructions in pixel shaders is required
11052 * for SM3. */
11053 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11054 return 3;
11056 return 2;
11059 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
11061 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11063 TRACE("Shader model %u.\n", shader_model);
11065 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11066 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11067 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11068 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11069 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11070 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11072 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11073 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11075 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11076 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11077 caps->varying_count = gl_info->limits.glsl_varyings;
11079 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11080 * Direct3D minimum requirement.
11082 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11083 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11085 * The problem is that the refrast clamps temporary results in the shader to
11086 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11087 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11088 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11089 * offer a way to query this.
11091 if (shader_model >= 4)
11092 caps->ps_1x_max_value = FLT_MAX;
11093 else
11094 caps->ps_1x_max_value = 1024.0f;
11096 /* Ideally we'd only set caps like sRGB writes here if supported by both
11097 * the shader backend and the fragment pipe, but we can get called before
11098 * shader_glsl_alloc(). */
11099 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11100 | WINED3D_SHADER_CAP_SRGB_WRITE;
11103 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11105 /* We support everything except YUV conversions. */
11106 return !is_complex_fixup(fixup);
11109 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11111 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11112 /* WINED3DSIH_ADD */ shader_glsl_binop,
11113 /* WINED3DSIH_AND */ shader_glsl_binop,
11114 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11115 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11116 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11117 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11118 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11119 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11120 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11121 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11122 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11123 /* WINED3DSIH_BEM */ shader_glsl_bem,
11124 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11125 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11126 /* WINED3DSIH_BREAK */ shader_glsl_break,
11127 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11128 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11129 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11130 /* WINED3DSIH_CALL */ shader_glsl_call,
11131 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11132 /* WINED3DSIH_CASE */ shader_glsl_case,
11133 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11134 /* WINED3DSIH_CND */ shader_glsl_cnd,
11135 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11136 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11137 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11138 /* WINED3DSIH_CRS */ shader_glsl_cross,
11139 /* WINED3DSIH_CUT */ shader_glsl_cut,
11140 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11141 /* WINED3DSIH_DCL */ shader_glsl_nop,
11142 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11143 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11144 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11145 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11146 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11147 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11148 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11149 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11150 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11151 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11152 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11153 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11154 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11155 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11156 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11157 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
11158 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
11159 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11160 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11161 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11162 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11163 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11164 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11165 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11166 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11167 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11168 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11169 /* WINED3DSIH_DCL_STREAM */ NULL,
11170 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11171 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11172 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11173 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11174 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11175 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11176 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11177 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11178 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11179 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11180 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11181 /* WINED3DSIH_DEF */ shader_glsl_nop,
11182 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11183 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11184 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11185 /* WINED3DSIH_DIV */ shader_glsl_binop,
11186 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11187 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11188 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11189 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11190 /* WINED3DSIH_DST */ shader_glsl_dst,
11191 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11192 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11193 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11194 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11195 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11196 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11197 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ NULL,
11198 /* WINED3DSIH_ELSE */ shader_glsl_else,
11199 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11200 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11201 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11202 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11203 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11204 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11205 /* WINED3DSIH_EQ */ shader_glsl_relop,
11206 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11207 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11208 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11209 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11210 /* WINED3DSIH_FCALL */ NULL,
11211 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11212 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11213 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11214 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11215 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11216 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11217 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11218 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11219 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11220 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11221 /* WINED3DSIH_GE */ shader_glsl_relop,
11222 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11223 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11224 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11225 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11226 /* WINED3DSIH_IADD */ shader_glsl_binop,
11227 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11228 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11229 /* WINED3DSIH_IF */ shader_glsl_if,
11230 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11231 /* WINED3DSIH_IGE */ shader_glsl_relop,
11232 /* WINED3DSIH_ILT */ shader_glsl_relop,
11233 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11234 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11235 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11236 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11237 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11238 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11239 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11240 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11241 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11242 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11243 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11244 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11245 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11246 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11247 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11248 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11249 /* WINED3DSIH_INE */ shader_glsl_relop,
11250 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11251 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11252 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11253 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11254 /* WINED3DSIH_LABEL */ shader_glsl_label,
11255 /* WINED3DSIH_LD */ shader_glsl_ld,
11256 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11257 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11258 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11259 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11260 /* WINED3DSIH_LIT */ shader_glsl_lit,
11261 /* WINED3DSIH_LOD */ NULL,
11262 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11263 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11264 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11265 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11266 /* WINED3DSIH_LT */ shader_glsl_relop,
11267 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11268 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11269 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11270 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11271 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11272 /* WINED3DSIH_MAD */ shader_glsl_mad,
11273 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11274 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11275 /* WINED3DSIH_MOV */ shader_glsl_mov,
11276 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11277 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11278 /* WINED3DSIH_MUL */ shader_glsl_binop,
11279 /* WINED3DSIH_NE */ shader_glsl_relop,
11280 /* WINED3DSIH_NOP */ shader_glsl_nop,
11281 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11282 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11283 /* WINED3DSIH_OR */ shader_glsl_binop,
11284 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11285 /* WINED3DSIH_POW */ shader_glsl_pow,
11286 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11287 /* WINED3DSIH_REP */ shader_glsl_rep,
11288 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11289 /* WINED3DSIH_RET */ shader_glsl_ret,
11290 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11291 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11292 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11293 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11294 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11295 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11296 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11297 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11298 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11299 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11300 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11301 /* WINED3DSIH_SAMPLE_INFO */ NULL,
11302 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11303 /* WINED3DSIH_SAMPLE_POS */ NULL,
11304 /* WINED3DSIH_SETP */ NULL,
11305 /* WINED3DSIH_SGE */ shader_glsl_compare,
11306 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11307 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11308 /* WINED3DSIH_SLT */ shader_glsl_compare,
11309 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11310 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11311 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11312 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11313 /* WINED3DSIH_SUB */ shader_glsl_binop,
11314 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11315 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11316 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11317 /* WINED3DSIH_TEX */ shader_glsl_tex,
11318 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11319 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11320 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11321 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11322 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11323 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11324 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11325 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11326 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11327 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11328 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11329 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11330 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11331 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11332 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11333 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11334 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11335 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11336 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11337 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11338 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11339 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11340 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11341 /* WINED3DSIH_UGE */ shader_glsl_relop,
11342 /* WINED3DSIH_ULT */ shader_glsl_relop,
11343 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11344 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11345 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11346 /* WINED3DSIH_USHR */ shader_glsl_binop,
11347 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11348 /* WINED3DSIH_XOR */ shader_glsl_binop,
11351 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11352 SHADER_HANDLER hw_fct;
11354 /* Select handler */
11355 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11357 /* Unhandled opcode */
11358 if (!hw_fct)
11360 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11361 return;
11363 hw_fct(ins);
11365 shader_glsl_add_instruction_modifiers(ins);
11368 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11370 struct shader_glsl_priv *priv = shader_priv;
11372 return priv->ffp_proj_control;
11375 const struct wined3d_shader_backend_ops glsl_shader_backend =
11377 shader_glsl_handle_instruction,
11378 shader_glsl_precompile,
11379 shader_glsl_select,
11380 shader_glsl_select_compute,
11381 shader_glsl_disable,
11382 shader_glsl_update_float_vertex_constants,
11383 shader_glsl_update_float_pixel_constants,
11384 shader_glsl_load_constants,
11385 shader_glsl_destroy,
11386 shader_glsl_alloc,
11387 shader_glsl_free,
11388 shader_glsl_allocate_context_data,
11389 shader_glsl_free_context_data,
11390 shader_glsl_init_context_state,
11391 shader_glsl_get_caps,
11392 shader_glsl_color_fixup_supported,
11393 shader_glsl_has_ffp_proj_control,
11396 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
11398 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
11400 caps->xyzrhw = TRUE;
11401 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11402 caps->ffp_generic_attributes = TRUE;
11403 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
11404 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11405 caps->max_vertex_blend_matrix_index = 0;
11406 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11407 | WINED3DVTXPCAPS_MATERIALSOURCE7
11408 | WINED3DVTXPCAPS_VERTEXFOG
11409 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11410 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11411 | WINED3DVTXPCAPS_LOCALVIEWER
11412 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11413 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11414 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11415 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11418 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11420 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11421 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11422 return 0;
11425 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11427 struct shader_glsl_priv *priv;
11429 if (shader_backend == &glsl_shader_backend)
11431 priv = shader_priv;
11432 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11433 return priv;
11436 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11438 return NULL;
11441 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
11443 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11444 struct glsl_ffp_vertex_shader, desc.entry);
11445 struct glsl_shader_prog_link *program, *program2;
11446 struct glsl_ffp_destroy_ctx *ctx = context;
11448 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11449 struct glsl_shader_prog_link, vs.shader_entry)
11451 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11453 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11454 heap_free(shader);
11457 /* Context activation is done by the caller. */
11458 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
11460 struct shader_glsl_priv *priv = device->vertex_priv;
11461 struct glsl_ffp_destroy_ctx ctx;
11463 ctx.priv = priv;
11464 ctx.gl_info = &device->adapter->gl_info;
11465 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11468 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11469 const struct wined3d_state *state, DWORD state_id) {}
11471 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11472 const struct wined3d_state *state, DWORD state_id)
11474 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11477 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11478 const struct wined3d_state *state, DWORD state_id)
11480 const struct wined3d_gl_info *gl_info = context->gl_info;
11481 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11482 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11483 BOOL transformed = context->stream_info.position_transformed;
11484 BOOL wasrhw = context->last_was_rhw;
11485 unsigned int i;
11487 context->last_was_rhw = transformed;
11489 /* If the vertex declaration contains a transformed position attribute,
11490 * the draw uses the fixed function vertex pipeline regardless of any
11491 * vertex shader set by the application. */
11492 if (transformed != wasrhw
11493 || context->stream_info.swizzle_map != context->last_swizzle_map)
11494 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11496 context->last_swizzle_map = context->stream_info.swizzle_map;
11498 if (!use_vs(state))
11500 if (context->last_was_vshader)
11502 if (legacy_clip_planes)
11503 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11504 clipplane(context, state, STATE_CLIPPLANE(i));
11505 else
11506 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11509 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11511 /* Because of settings->texcoords, we have to regenerate the vertex
11512 * shader on a vdecl change if there aren't enough varyings to just
11513 * always output all the texture coordinates. */
11514 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
11515 || normal != context->last_was_normal)
11516 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11518 if (use_ps(state)
11519 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11520 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11521 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11523 else
11525 if (!context->last_was_vshader)
11527 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11528 if (legacy_clip_planes)
11529 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11530 clipplane(context, state, STATE_CLIPPLANE(i));
11531 else
11532 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11536 context->last_was_vshader = use_vs(state);
11537 context->last_was_normal = normal;
11540 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11541 const struct wined3d_state *state, DWORD state_id)
11543 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11544 /* Different vertex shaders potentially require a different vertex attributes setup. */
11545 if (!isStateDirty(context, STATE_VDECL))
11546 context_apply_state(context, state, STATE_VDECL);
11549 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11550 const struct wined3d_state *state, DWORD state_id)
11552 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11553 * winding) are defined in Hull Shaders, while in GLSL those are
11554 * specified in Tessellation Evaluation Shaders. */
11555 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11557 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11558 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11561 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11562 const struct wined3d_state *state, DWORD state_id)
11564 struct glsl_context_data *ctx_data = context->shader_backend_data;
11565 BOOL rasterization_disabled;
11567 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11568 if (ctx_data->rasterization_disabled != rasterization_disabled)
11569 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11570 ctx_data->rasterization_disabled = rasterization_disabled;
11572 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11573 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11574 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11575 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11576 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11579 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11580 const struct wined3d_state *state, DWORD state_id)
11582 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11583 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11584 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11585 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11586 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11587 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11588 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11591 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11592 const struct wined3d_state *state, DWORD state_id)
11594 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11597 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11598 const struct wined3d_state *state, DWORD state_id)
11600 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11603 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11605 const struct wined3d_gl_info *gl_info = context->gl_info;
11606 unsigned int k;
11608 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11609 | WINED3D_SHADER_CONST_FFP_LIGHTS
11610 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11612 if (needs_legacy_glsl_syntax(gl_info))
11614 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11616 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11617 clipplane(context, state, STATE_CLIPPLANE(k));
11620 else
11622 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11626 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11627 const struct wined3d_state *state, DWORD state_id)
11629 /* Table fog behavior depends on the projection matrix. */
11630 if (state->render_states[WINED3D_RS_FOGENABLE]
11631 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11632 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11633 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11636 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11637 const struct wined3d_state *state, DWORD state_id)
11639 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11640 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11641 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11642 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11643 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11644 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11647 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11648 const struct wined3d_state *state, DWORD state_id)
11650 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11653 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11654 const struct wined3d_state *state, DWORD state_id)
11656 DWORD sampler = state_id - STATE_SAMPLER(0);
11657 const struct wined3d_texture *texture = state->textures[sampler];
11658 BOOL np2;
11660 if (!texture)
11661 return;
11663 if (sampler >= MAX_TEXTURES)
11664 return;
11666 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11667 || context->lastWasPow2Texture & (1u << sampler))
11669 if (np2)
11670 context->lastWasPow2Texture |= 1u << sampler;
11671 else
11672 context->lastWasPow2Texture &= ~(1u << sampler);
11674 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11678 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11679 const struct wined3d_state *state, DWORD state_id)
11681 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11684 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11685 const struct wined3d_state *state, DWORD state_id)
11687 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11690 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11691 const struct wined3d_state *state, DWORD state_id)
11693 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11696 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11697 const struct wined3d_state *state, DWORD state_id)
11699 if (!use_vs(state))
11700 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11703 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11704 const struct wined3d_state *state, DWORD state_id)
11706 static unsigned int once;
11708 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11709 FIXME("Non-point sprite points not supported in core profile.\n");
11712 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11713 const struct wined3d_state *state, DWORD state_id)
11715 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11718 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11719 const struct wined3d_state *state, DWORD state_id)
11721 const struct wined3d_gl_info *gl_info = context->gl_info;
11722 UINT index = state_id - STATE_CLIPPLANE(0);
11724 if (index >= gl_info->limits.user_clip_distances)
11725 return;
11727 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11730 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
11732 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11733 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11734 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11735 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11736 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11737 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11738 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11739 /* Clip planes */
11740 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11741 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11742 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11743 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11744 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11745 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11746 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11747 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11748 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11749 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11750 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11751 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11752 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11753 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11754 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11755 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11756 /* Lights */
11757 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11758 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11759 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11760 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11761 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11762 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11763 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11764 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11765 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11766 /* Viewport */
11767 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11768 /* Transform states */
11769 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11770 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11771 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11772 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11773 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11774 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11775 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11776 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11777 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11778 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11779 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11780 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11781 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11782 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11783 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11784 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11785 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11786 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11787 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11788 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11789 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11790 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11791 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11792 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11793 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11794 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11795 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11796 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11797 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11798 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11799 /* Fog */
11800 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11801 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11802 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11803 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11804 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11805 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11806 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11807 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11808 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11809 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11810 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11811 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11812 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11813 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11814 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11815 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11816 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11817 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11818 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11819 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11820 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11821 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11822 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11823 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11824 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11825 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11826 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11827 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11828 /* NP2 texture matrix fixups. They are not needed if
11829 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11830 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11831 * matrix. */
11832 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11833 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11834 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11835 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11836 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11837 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11838 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11839 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11840 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11841 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11842 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11843 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11844 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11845 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11846 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11847 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11848 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11849 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11850 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11851 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11852 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11853 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11854 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11855 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11856 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11857 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11858 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11859 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11862 /* TODO:
11863 * - Implement vertex tweening. */
11864 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11866 glsl_vertex_pipe_vp_enable,
11867 glsl_vertex_pipe_vp_get_caps,
11868 glsl_vertex_pipe_vp_get_emul_mask,
11869 glsl_vertex_pipe_vp_alloc,
11870 glsl_vertex_pipe_vp_free,
11871 glsl_vertex_pipe_vp_states,
11874 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
11876 /* Nothing to do. */
11879 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
11881 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11882 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11883 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11884 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11885 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11886 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11887 | WINED3DTEXOPCAPS_SELECTARG1
11888 | WINED3DTEXOPCAPS_SELECTARG2
11889 | WINED3DTEXOPCAPS_MODULATE4X
11890 | WINED3DTEXOPCAPS_MODULATE2X
11891 | WINED3DTEXOPCAPS_MODULATE
11892 | WINED3DTEXOPCAPS_ADDSIGNED2X
11893 | WINED3DTEXOPCAPS_ADDSIGNED
11894 | WINED3DTEXOPCAPS_ADD
11895 | WINED3DTEXOPCAPS_SUBTRACT
11896 | WINED3DTEXOPCAPS_ADDSMOOTH
11897 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
11898 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
11899 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
11900 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
11901 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
11902 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
11903 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
11904 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
11905 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
11906 | WINED3DTEXOPCAPS_DOTPRODUCT3
11907 | WINED3DTEXOPCAPS_MULTIPLYADD
11908 | WINED3DTEXOPCAPS_LERP
11909 | WINED3DTEXOPCAPS_BUMPENVMAP
11910 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
11911 caps->MaxTextureBlendStages = MAX_TEXTURES;
11912 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], MAX_TEXTURES);
11915 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
11917 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11918 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11919 return 0;
11922 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11924 struct shader_glsl_priv *priv;
11926 if (shader_backend == &glsl_shader_backend)
11928 priv = shader_priv;
11929 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
11930 return priv;
11933 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
11935 return NULL;
11938 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
11940 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11941 struct glsl_ffp_fragment_shader, entry.entry);
11942 struct glsl_shader_prog_link *program, *program2;
11943 struct glsl_ffp_destroy_ctx *ctx = context;
11945 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11946 struct glsl_shader_prog_link, ps.shader_entry)
11948 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
11950 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
11951 heap_free(shader);
11954 /* Context activation is done by the caller. */
11955 static void glsl_fragment_pipe_free(struct wined3d_device *device)
11957 struct shader_glsl_priv *priv = device->fragment_priv;
11958 struct glsl_ffp_destroy_ctx ctx;
11960 ctx.priv = priv;
11961 ctx.gl_info = &device->adapter->gl_info;
11962 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
11965 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
11966 const struct wined3d_state *state, DWORD state_id)
11968 context->last_was_pshader = use_ps(state);
11970 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11973 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
11974 const struct wined3d_state *state, DWORD state_id)
11976 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
11979 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
11980 const struct wined3d_state *state, DWORD state_id)
11982 BOOL use_vshader = use_vs(state);
11983 enum fogsource new_source;
11984 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
11985 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
11987 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11989 if (!state->render_states[WINED3D_RS_FOGENABLE])
11990 return;
11992 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
11994 if (use_vshader)
11995 new_source = FOGSOURCE_VS;
11996 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
11997 new_source = FOGSOURCE_COORD;
11998 else
11999 new_source = FOGSOURCE_FFP;
12001 else
12003 new_source = FOGSOURCE_FFP;
12006 if (new_source != context->fog_source || fogstart == fogend)
12008 context->fog_source = new_source;
12009 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12013 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12014 const struct wined3d_state *state, DWORD state_id)
12016 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12017 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
12018 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12020 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12021 glsl_fragment_pipe_fog(context, state, state_id);
12024 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12025 const struct wined3d_state *state, DWORD state_id)
12027 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12028 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
12029 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12032 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12033 const struct wined3d_state *state, DWORD state_id)
12035 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12038 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12039 const struct wined3d_state *state, DWORD state_id)
12041 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12044 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12045 const struct wined3d_state *state, DWORD state_id)
12047 const struct wined3d_gl_info *gl_info = context->gl_info;
12048 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12049 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
12051 if (func)
12053 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12054 checkGLcall("glAlphaFunc");
12058 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12059 const struct wined3d_state *state, DWORD state_id)
12061 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12064 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12065 const struct wined3d_state *state, DWORD state_id)
12067 const struct wined3d_gl_info *gl_info = context->gl_info;
12069 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12071 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12072 checkGLcall("glEnable(GL_ALPHA_TEST)");
12074 else
12076 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12077 checkGLcall("glDisable(GL_ALPHA_TEST)");
12081 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12082 const struct wined3d_state *state, DWORD state_id)
12084 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12087 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12088 const struct wined3d_state *state, DWORD state_id)
12090 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12093 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12094 const struct wined3d_state *state, DWORD state_id)
12096 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12099 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
12101 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12102 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12103 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12104 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12105 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12106 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12107 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12108 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12109 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12110 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12111 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12112 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12113 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12114 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12115 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12116 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12117 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12118 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12119 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12120 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12121 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12122 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12123 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12124 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12125 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12126 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12127 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12128 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12129 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12130 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12131 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12132 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12133 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12134 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12135 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12136 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12137 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12138 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12139 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12140 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12141 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12142 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12143 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12144 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12145 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12146 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12147 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12148 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12149 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12150 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12151 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12152 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12153 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12154 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12155 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12156 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12157 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12158 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12159 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12160 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12161 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12162 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12163 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12164 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12165 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12166 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12167 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12168 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12169 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12170 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12171 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12172 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12173 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12174 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12175 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12176 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12177 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12178 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12179 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12180 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12181 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12182 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12183 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12184 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12185 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12186 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12187 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12188 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12189 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12190 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12191 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12192 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12193 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12194 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12195 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12196 {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 },
12197 {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 },
12198 {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 },
12199 {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 },
12200 {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 },
12201 {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 },
12202 {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 },
12203 {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 },
12204 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12205 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12206 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12207 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12208 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12209 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12210 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12211 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12212 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12213 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12214 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12215 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12216 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12219 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12221 return TRUE;
12224 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12228 const struct fragment_pipeline glsl_fragment_pipe =
12230 glsl_fragment_pipe_enable,
12231 glsl_fragment_pipe_get_caps,
12232 glsl_fragment_pipe_get_emul_mask,
12233 glsl_fragment_pipe_alloc,
12234 glsl_fragment_pipe_free,
12235 glsl_fragment_pipe_alloc_context_data,
12236 glsl_fragment_pipe_free_context_data,
12237 shader_glsl_color_fixup_supported,
12238 glsl_fragment_pipe_state_template,
12241 struct glsl_blitter_args
12243 GLenum texture_type;
12244 struct color_fixup_desc fixup;
12245 unsigned short padding;
12248 struct glsl_blitter_program
12250 struct wine_rb_entry entry;
12251 struct glsl_blitter_args args;
12252 GLuint id;
12255 struct wined3d_glsl_blitter
12257 struct wined3d_blitter blitter;
12258 struct wined3d_string_buffer_list string_buffers;
12259 struct wine_rb_tree programs;
12262 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12264 const struct glsl_blitter_args *a = key;
12265 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12267 return memcmp(a, b, sizeof(*a));
12270 /* Context activation is done by the caller. */
12271 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12273 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12274 struct wined3d_context *context = ctx;
12275 const struct wined3d_gl_info *gl_info = context->gl_info;
12277 GL_EXTCALL(glDeleteProgram(program->id));
12278 checkGLcall("glDeleteProgram()");
12279 heap_free(program);
12282 /* Context activation is done by the caller. */
12283 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12285 struct wined3d_glsl_blitter *glsl_blitter;
12286 struct wined3d_blitter *next;
12288 if ((next = blitter->next))
12289 next->ops->blitter_destroy(next, context);
12291 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12293 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context);
12294 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12296 heap_free(glsl_blitter);
12299 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12300 const struct wined3d_gl_info *gl_info, struct glsl_blitter_args *args)
12302 static const struct
12304 GLenum texture_target;
12305 const char texture_type[7];
12306 const char texcoord_swizzle[4];
12308 texture_data[] =
12310 {GL_TEXTURE_2D, "2D", "xy"},
12311 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12312 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12314 static const char vshader_main[] =
12315 "\n"
12316 "void main()\n"
12317 "{\n"
12318 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12319 " out_texcoord = texcoord;\n"
12320 "}\n";
12321 static const char fshader_header[] =
12322 "\n"
12323 "void main()\n"
12324 "{\n";
12325 struct wined3d_string_buffer *buffer, *string;
12326 GLuint program, vshader_id, fshader_id;
12327 const char *tex_type, *swizzle, *ptr;
12328 unsigned int i;
12330 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12332 if (args->texture_type == texture_data[i].texture_target)
12334 tex_type = texture_data[i].texture_type;
12335 swizzle = texture_data[i].texcoord_swizzle;
12336 break;
12339 if (i == ARRAY_SIZE(texture_data))
12341 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12342 return 0;
12345 program = GL_EXTCALL(glCreateProgram());
12347 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12349 buffer = string_buffer_get(&blitter->string_buffers);
12350 shader_glsl_add_version_declaration(buffer, gl_info);
12351 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12352 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12353 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12354 shader_addline(buffer, vshader_main);
12356 ptr = buffer->buffer;
12357 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12358 GL_EXTCALL(glAttachShader(program, vshader_id));
12359 GL_EXTCALL(glDeleteShader(vshader_id));
12361 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12363 string_buffer_clear(buffer);
12364 shader_glsl_add_version_declaration(buffer, gl_info);
12365 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12366 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12367 if (!needs_legacy_glsl_syntax(gl_info))
12368 shader_addline(buffer, "out vec4 ps_out[1];\n");
12370 shader_addline(buffer, fshader_header);
12371 string = string_buffer_get(&blitter->string_buffers);
12372 string_buffer_sprintf(string, "%s[0]", get_fragment_output(gl_info));
12373 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12374 string->buffer, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12375 shader_glsl_color_correction_ext(buffer, string->buffer, WINED3DSP_WRITEMASK_ALL, args->fixup);
12376 string_buffer_release(&blitter->string_buffers, string);
12377 shader_addline(buffer, "}\n");
12379 ptr = buffer->buffer;
12380 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12381 string_buffer_release(&blitter->string_buffers, buffer);
12382 GL_EXTCALL(glAttachShader(program, fshader_id));
12383 GL_EXTCALL(glDeleteShader(fshader_id));
12385 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12386 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12388 if (!needs_legacy_glsl_syntax(gl_info))
12389 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12391 GL_EXTCALL(glCompileShader(vshader_id));
12392 print_glsl_info_log(gl_info, vshader_id, FALSE);
12393 GL_EXTCALL(glCompileShader(fshader_id));
12394 print_glsl_info_log(gl_info, fshader_id, FALSE);
12395 GL_EXTCALL(glLinkProgram(program));
12396 shader_glsl_validate_link(gl_info, program);
12397 return program;
12400 /* Context activation is done by the caller. */
12401 static GLuint glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12402 struct wined3d_context *context, const struct wined3d_texture *texture)
12404 const struct wined3d_gl_info *gl_info = context->gl_info;
12405 struct glsl_blitter_program *program;
12406 struct glsl_blitter_args args;
12407 struct wine_rb_entry *entry;
12409 memset(&args, 0, sizeof(args));
12410 args.texture_type = texture->target;
12411 args.fixup = texture->resource.format->color_fixup;
12413 if (is_complex_fixup(args.fixup))
12415 FIXME("Complex fixups not supported.\n");
12416 return 0;
12419 if ((entry = wine_rb_get(&blitter->programs, &args)))
12421 program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12422 return program->id;
12425 if (!(program = heap_alloc(sizeof(*program))))
12427 ERR("Failed to allocate blitter program memory.\n");
12428 return 0;
12431 program->args = args;
12432 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
12434 WARN("Failed to generate blitter program.\n");
12435 heap_free(program);
12436 return 0;
12439 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
12441 ERR("Failed to store blitter program.\n");
12442 GL_EXTCALL(glDeleteProgram(program->id));
12443 heap_free(program);
12444 return 0;
12447 return program->id;
12450 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
12451 const struct wined3d_texture *src_texture, DWORD src_location,
12452 const struct wined3d_texture *dst_texture, DWORD dst_location)
12454 const struct wined3d_resource *src_resource = &src_texture->resource;
12455 const struct wined3d_resource *dst_resource = &dst_texture->resource;
12456 const struct wined3d_format *src_format = src_resource->format;
12457 const struct wined3d_format *dst_format = dst_resource->format;
12458 BOOL decompress;
12460 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
12462 if (dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
12463 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
12464 else
12465 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
12468 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT)
12470 TRACE("Unsupported blit_op %#x.\n", blit_op);
12471 return FALSE;
12474 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
12475 || dst_texture->target == GL_TEXTURE_2D_MULTISAMPLE
12476 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY
12477 || dst_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
12479 TRACE("Multi-sample textures not supported.\n");
12480 return FALSE;
12483 /* We don't necessarily want to blit from resources without
12484 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
12485 * compressed textures. */
12486 decompress = src_format && (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
12487 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
12488 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
12490 TRACE("Source or destination resource does not have GPU access.\n");
12491 return FALSE;
12494 if (is_complex_fixup(src_format->color_fixup))
12496 TRACE("Complex source fixups are not supported.\n");
12497 return FALSE;
12500 if (!is_identity_fixup(dst_format->color_fixup))
12502 TRACE("Destination fixups are not supported.\n");
12503 return FALSE;
12506 TRACE("Returning supported.\n");
12507 return TRUE;
12510 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
12511 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
12512 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
12513 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
12514 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
12516 struct wined3d_device *device = dst_texture->resource.device;
12517 const struct wined3d_gl_info *gl_info = context->gl_info;
12518 struct wined3d_texture *staging_texture = NULL;
12519 struct wined3d_glsl_blitter *glsl_blitter;
12520 struct wined3d_blitter *next;
12521 unsigned int src_level;
12522 GLuint program_id;
12523 RECT s, d;
12525 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
12526 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
12527 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
12528 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
12529 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
12531 if (!glsl_blitter_supported(op, context, src_texture, src_location, dst_texture, dst_location))
12533 if (!(next = blitter->next))
12535 ERR("No blitter to handle blit op %#x.\n", op);
12536 return dst_location;
12539 TRACE("Forwarding to blitter %p.\n", next);
12540 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
12541 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
12544 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12546 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
12548 struct wined3d_resource_desc desc;
12549 struct wined3d_box upload_box;
12550 HRESULT hr;
12552 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
12554 src_level = src_sub_resource_idx % src_texture->level_count;
12555 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
12556 desc.format = src_texture->resource.format->id;
12557 desc.multisample_type = src_texture->resource.multisample_type;
12558 desc.multisample_quality = src_texture->resource.multisample_quality;
12559 desc.usage = WINED3DUSAGE_PRIVATE;
12560 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
12561 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
12562 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
12563 desc.depth = 1;
12564 desc.size = 0;
12566 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
12567 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
12569 ERR("Failed to create staging texture, hr %#x.\n", hr);
12570 return dst_location;
12573 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
12574 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
12575 src_texture, src_sub_resource_idx, &upload_box);
12577 src_texture = staging_texture;
12578 src_sub_resource_idx = 0;
12580 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
12581 && (src_texture->sub_resources[src_sub_resource_idx].locations
12582 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
12583 && !wined3d_resource_is_offscreen(&src_texture->resource))
12586 /* Without FBO blits transferring from the drawable to the texture is
12587 * expensive, because we have to flip the data in sysmem. Since we can
12588 * flip in the blitter, we don't actually need that flip anyway. So we
12589 * use the surface's texture as scratch texture, and flip the source
12590 * rectangle instead. */
12591 texture2d_load_fb_texture(src_texture, src_sub_resource_idx, FALSE, context);
12593 s = *src_rect;
12594 src_level = src_sub_resource_idx % src_texture->level_count;
12595 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
12596 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
12597 src_rect = &s;
12599 else
12601 wined3d_texture_load(src_texture, context, FALSE);
12604 context_apply_blit_state(context, device);
12606 if (dst_location == WINED3D_LOCATION_DRAWABLE)
12608 d = *dst_rect;
12609 wined3d_texture_translate_drawable_coords(dst_texture, context->win_handle, &d);
12610 dst_rect = &d;
12613 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
12615 GLenum buffer;
12617 if (dst_location == WINED3D_LOCATION_DRAWABLE)
12619 TRACE("Destination texture %p is onscreen.\n", dst_texture);
12620 buffer = wined3d_texture_get_gl_buffer(dst_texture);
12622 else
12624 TRACE("Destination texture %p is offscreen.\n", dst_texture);
12625 buffer = GL_COLOR_ATTACHMENT0;
12627 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER,
12628 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
12629 context_set_draw_buffer(context, buffer);
12630 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
12631 context_invalidate_state(context, STATE_FRAMEBUFFER);
12634 if (!(program_id = glsl_blitter_get_program(glsl_blitter, context, src_texture)))
12636 ERR("Failed to get blitter program.\n");
12637 return dst_location;
12639 GL_EXTCALL(glUseProgram(program_id));
12640 context_draw_shaded_quad(context, src_texture, src_sub_resource_idx, src_rect, dst_rect, filter);
12641 GL_EXTCALL(glUseProgram(0));
12643 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
12644 gl_info->gl_ops.gl.p_glFlush();
12646 if (staging_texture)
12647 wined3d_texture_decref(staging_texture);
12649 return dst_location;
12652 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
12653 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
12654 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
12656 struct wined3d_blitter *next;
12658 if ((next = blitter->next))
12659 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
12660 clear_rects, draw_rect, flags, color, depth, stencil);
12663 static const struct wined3d_blitter_ops glsl_blitter_ops =
12665 glsl_blitter_destroy,
12666 glsl_blitter_clear,
12667 glsl_blitter_blit,
12670 void wined3d_glsl_blitter_create(struct wined3d_blitter **next, const struct wined3d_device *device)
12672 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
12673 struct wined3d_glsl_blitter *blitter;
12675 if (device->shader_backend != &glsl_shader_backend)
12676 return;
12678 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
12679 return;
12681 if (!(blitter = heap_alloc(sizeof(*blitter))))
12683 ERR("Failed to allocate blitter.\n");
12684 return;
12687 TRACE("Created blitter %p.\n", blitter);
12689 blitter->blitter.ops = &glsl_blitter_ops;
12690 blitter->blitter.next = *next;
12691 string_buffer_list_init(&blitter->string_buffers);
12692 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
12693 *next = &blitter->blitter;