wined3d: Do not use texture unit mapping for SM4+ shaders.
[wine.git] / dlls / wined3d / glsl_shader.c
blobf7f6ca55a29b07be20be2b034af2b2af245142f5
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 {
119 struct wined3d_string_buffer shader_buffer;
120 struct wined3d_string_buffer_list string_buffers;
121 struct wine_rb_tree program_lookup;
122 struct constant_heap vconst_heap;
123 struct constant_heap pconst_heap;
124 unsigned char *stack;
125 UINT next_constant_version;
127 const struct wined3d_vertex_pipe_ops *vertex_pipe;
128 const struct fragment_pipeline *fragment_pipe;
129 struct wine_rb_tree ffp_vertex_shaders;
130 struct wine_rb_tree ffp_fragment_shaders;
131 BOOL ffp_proj_control;
132 BOOL legacy_lighting;
135 struct glsl_vs_program
137 struct list shader_entry;
138 GLuint id;
139 GLenum vertex_color_clamp;
140 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
141 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
142 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
143 GLint pos_fixup_location;
145 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
146 GLint projection_matrix_location;
147 GLint normal_matrix_location;
148 GLint texture_matrix_location[MAX_TEXTURES];
149 GLint material_ambient_location;
150 GLint material_diffuse_location;
151 GLint material_specular_location;
152 GLint material_emissive_location;
153 GLint material_shininess_location;
154 GLint light_ambient_location;
155 struct
157 GLint diffuse;
158 GLint specular;
159 GLint ambient;
160 GLint position;
161 GLint direction;
162 GLint range;
163 GLint falloff;
164 GLint c_att;
165 GLint l_att;
166 GLint q_att;
167 GLint cos_htheta;
168 GLint cos_hphi;
169 } light_location[MAX_ACTIVE_LIGHTS];
170 GLint pointsize_location;
171 GLint pointsize_min_location;
172 GLint pointsize_max_location;
173 GLint pointsize_c_att_location;
174 GLint pointsize_l_att_location;
175 GLint pointsize_q_att_location;
176 GLint clip_planes_location;
179 struct glsl_gs_program
181 struct list shader_entry;
182 GLuint id;
184 GLint pos_fixup_location;
187 struct glsl_ps_program
189 struct list shader_entry;
190 GLuint id;
191 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
192 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
193 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
194 GLint bumpenv_mat_location[MAX_TEXTURES];
195 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
196 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
197 GLint tss_constant_location[MAX_TEXTURES];
198 GLint tex_factor_location;
199 GLint specular_enable_location;
200 GLint fog_color_location;
201 GLint fog_density_location;
202 GLint fog_end_location;
203 GLint fog_scale_location;
204 GLint alpha_test_ref_location;
205 GLint ycorrection_location;
206 GLint np2_fixup_location;
207 GLint color_key_location;
208 const struct ps_np2fixup_info *np2_fixup_info;
211 struct glsl_cs_program
213 struct list shader_entry;
214 GLuint id;
217 /* Struct to maintain data about a linked GLSL program */
218 struct glsl_shader_prog_link
220 struct wine_rb_entry program_lookup_entry;
221 struct glsl_vs_program vs;
222 struct glsl_gs_program gs;
223 struct glsl_ps_program ps;
224 struct glsl_cs_program cs;
225 GLuint id;
226 DWORD constant_update_mask;
227 UINT constant_version;
230 struct glsl_program_key
232 GLuint vs_id;
233 GLuint gs_id;
234 GLuint ps_id;
235 GLuint cs_id;
238 struct shader_glsl_ctx_priv {
239 const struct vs_compile_args *cur_vs_args;
240 const struct ps_compile_args *cur_ps_args;
241 struct ps_np2fixup_info *cur_np2fixup_info;
242 struct wined3d_string_buffer_list *string_buffers;
245 struct glsl_context_data
247 struct glsl_shader_prog_link *glsl_program;
248 GLenum vertex_color_clamp;
251 struct glsl_ps_compiled_shader
253 struct ps_compile_args args;
254 struct ps_np2fixup_info np2fixup;
255 GLuint id;
258 struct glsl_vs_compiled_shader
260 struct vs_compile_args args;
261 GLuint id;
264 struct glsl_gs_compiled_shader
266 struct gs_compile_args args;
267 GLuint id;
270 struct glsl_cs_compiled_shader
272 GLuint id;
275 struct glsl_shader_private
277 union
279 struct glsl_vs_compiled_shader *vs;
280 struct glsl_gs_compiled_shader *gs;
281 struct glsl_ps_compiled_shader *ps;
282 struct glsl_cs_compiled_shader *cs;
283 } gl_shaders;
284 UINT num_gl_shaders, shader_array_size;
287 struct glsl_ffp_vertex_shader
289 struct wined3d_ffp_vs_desc desc;
290 GLuint id;
291 struct list linked_programs;
294 struct glsl_ffp_fragment_shader
296 struct ffp_frag_desc entry;
297 GLuint id;
298 struct list linked_programs;
301 struct glsl_ffp_destroy_ctx
303 struct shader_glsl_priv *priv;
304 const struct wined3d_gl_info *gl_info;
307 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
309 static const char *debug_gl_shader_type(GLenum type)
311 switch (type)
313 #define WINED3D_TO_STR(u) case u: return #u
314 WINED3D_TO_STR(GL_VERTEX_SHADER);
315 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
316 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
317 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
318 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
319 WINED3D_TO_STR(GL_COMPUTE_SHADER);
320 #undef WINED3D_TO_STR
321 default:
322 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
326 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
328 switch (type)
330 case WINED3D_SHADER_TYPE_VERTEX:
331 return "vs";
333 case WINED3D_SHADER_TYPE_HULL:
334 return "hs";
336 case WINED3D_SHADER_TYPE_DOMAIN:
337 return "ds";
339 case WINED3D_SHADER_TYPE_GEOMETRY:
340 return "gs";
342 case WINED3D_SHADER_TYPE_PIXEL:
343 return "ps";
345 case WINED3D_SHADER_TYPE_COMPUTE:
346 return "cs";
348 default:
349 FIXME("Unhandled shader type %#x.\n", type);
350 return "unknown";
354 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
355 const struct wined3d_shader_version *version)
357 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
358 || (version && version->type == WINED3D_SHADER_TYPE_COMPUTE))
359 return 150;
360 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
361 return 130;
362 else
363 return 120;
366 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
367 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_version *version)
369 unsigned int glsl_version = shader_glsl_get_version(gl_info, version);
370 if (glsl_version >= 150 && gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
371 shader_addline(buffer, "#version %u compatibility\n", glsl_version);
372 else
373 shader_addline(buffer, "#version %u\n", glsl_version);
376 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
378 char str[4][17];
380 wined3d_ftoa(values[0], str[0]);
381 wined3d_ftoa(values[1], str[1]);
382 wined3d_ftoa(values[2], str[2]);
383 wined3d_ftoa(values[3], str[3]);
384 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
387 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
388 const int *values, unsigned int size)
390 int i;
392 if (!size || size > 4)
394 ERR("Invalid vector size %u.\n", size);
395 return;
398 if (size > 1)
399 shader_addline(buffer, "ivec%u(", size);
401 for (i = 0; i < size; ++i)
402 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
404 if (size > 1)
405 shader_addline(buffer, ")");
408 static const char *get_info_log_line(const char **ptr)
410 const char *p, *q;
412 p = *ptr;
413 if (!(q = strstr(p, "\n")))
415 if (!*p) return NULL;
416 *ptr += strlen(p);
417 return p;
419 *ptr = q + 1;
421 return p;
424 /* Context activation is done by the caller. */
425 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
427 int length = 0;
428 char *log;
430 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
431 return;
433 if (program)
434 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
435 else
436 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
438 /* A size of 1 is just a null-terminated string, so the log should be bigger than
439 * that if there are errors. */
440 if (length > 1)
442 const char *ptr, *line;
444 log = HeapAlloc(GetProcessHeap(), 0, length);
445 /* The info log is supposed to be zero-terminated, but at least some
446 * versions of fglrx don't terminate the string properly. The reported
447 * length does include the terminator, so explicitly set it to zero
448 * here. */
449 log[length - 1] = 0;
450 if (program)
451 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
452 else
453 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
455 ptr = log;
456 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
458 WARN("Info log received from GLSL shader #%u:\n", id);
459 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
461 else
463 FIXME("Info log received from GLSL shader #%u:\n", id);
464 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
466 HeapFree(GetProcessHeap(), 0, log);
470 /* Context activation is done by the caller. */
471 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
473 const char *ptr, *line;
475 TRACE("Compiling shader object %u.\n", shader);
477 if (TRACE_ON(d3d_shader))
479 ptr = src;
480 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
483 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
484 checkGLcall("glShaderSource");
485 GL_EXTCALL(glCompileShader(shader));
486 checkGLcall("glCompileShader");
487 print_glsl_info_log(gl_info, shader, FALSE);
490 /* Context activation is done by the caller. */
491 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
493 GLint i, shader_count, source_size = -1;
494 GLuint *shaders;
495 char *source = NULL;
497 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
498 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
500 ERR("Failed to allocate shader array memory.\n");
501 return;
504 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
505 for (i = 0; i < shader_count; ++i)
507 const char *ptr, *line;
508 GLint tmp;
510 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
512 if (source_size < tmp)
514 HeapFree(GetProcessHeap(), 0, source);
516 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
517 if (!source)
519 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
520 HeapFree(GetProcessHeap(), 0, shaders);
521 return;
523 source_size = tmp;
526 FIXME("Shader %u:\n", shaders[i]);
527 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
528 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
529 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
530 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
531 FIXME("\n");
533 ptr = source;
534 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
535 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
536 FIXME("\n");
539 HeapFree(GetProcessHeap(), 0, source);
540 HeapFree(GetProcessHeap(), 0, shaders);
543 /* Context activation is done by the caller. */
544 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
546 GLint tmp;
548 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
549 return;
551 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
552 if (!tmp)
554 FIXME("Program %u link status invalid.\n", program);
555 shader_glsl_dump_program_source(gl_info, program);
558 print_glsl_info_log(gl_info, program, TRUE);
561 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
563 return !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_SHADING_LANGUAGE_420PACK];
566 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
567 struct shader_glsl_priv *priv, GLuint program_id,
568 const struct wined3d_shader_reg_maps *reg_maps)
570 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
571 struct wined3d_string_buffer *name;
572 unsigned int i, base, count;
573 GLuint block_idx;
575 if (shader_glsl_use_layout_binding_qualifier(gl_info))
576 return;
578 name = string_buffer_get(&priv->string_buffers);
579 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
580 for (i = 0; i < count; ++i)
582 if (!reg_maps->cb_sizes[i])
583 continue;
585 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
586 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
587 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
589 checkGLcall("glUniformBlockBinding");
590 string_buffer_release(&priv->string_buffers, name);
593 /* Context activation is done by the caller. */
594 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
595 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
596 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
598 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
599 unsigned int i, mapped_unit;
600 GLint name_loc;
602 for (i = 0; i < count; ++i)
604 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
605 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
606 if (name_loc == -1)
607 continue;
609 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
610 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
612 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
613 continue;
616 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
617 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
619 checkGLcall("Load sampler bindings");
620 string_buffer_release(&priv->string_buffers, sampler_name);
623 /* Context activation is done by the caller. */
624 static void shader_glsl_load_samplers(const struct wined3d_context *context,
625 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
627 const struct wined3d_shader_version *version = &reg_maps->shader_version;
628 const char *prefix = shader_glsl_get_prefix(version->type);
629 const struct wined3d_gl_info *gl_info = context->gl_info;
630 const DWORD *tex_unit_map;
631 unsigned int base, count;
633 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, version->type, &base, &count);
634 tex_unit_map = version->major >= 4 ? NULL : context->tex_unit_map;
635 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
638 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
639 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
641 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
643 if (icb)
645 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
646 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
647 GLint icb_location;
649 string_buffer_sprintf(icb_name, "%s_icb", prefix);
650 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
651 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
652 checkGLcall("Load immediate constant buffer");
654 string_buffer_release(&priv->string_buffers, icb_name);
658 /* Context activation is done by the caller. */
659 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
660 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
662 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
663 struct wined3d_string_buffer *name;
664 GLint location;
665 unsigned int i;
667 if (shader_glsl_use_layout_binding_qualifier(gl_info))
668 return;
670 name = string_buffer_get(&priv->string_buffers);
671 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
673 if (!reg_maps->uav_resource_info[i].type)
674 continue;
676 string_buffer_sprintf(name, "%s_image%u", prefix, i);
677 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
678 if (location == -1)
679 continue;
681 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
682 GL_EXTCALL(glUniform1i(location, i));
684 checkGLcall("Load image bindings");
685 string_buffer_release(&priv->string_buffers, name);
688 /* Context activation is done by the caller. */
689 static void shader_glsl_load_program_resources(const struct wined3d_context *context,
690 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
692 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
694 shader_glsl_init_uniform_block_bindings(context->gl_info, priv, program_id, reg_maps);
695 shader_glsl_load_icb(context->gl_info, priv, program_id, reg_maps);
696 /* Texture unit mapping is set up to be the same each time the shader
697 * program is used so we can hardcode the sampler uniform values. */
698 shader_glsl_load_samplers(context, priv, program_id, reg_maps);
701 /* Context activation is done by the caller. */
702 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
703 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
705 unsigned int start = ~0U, end = 0;
706 int stack_idx = 0;
707 unsigned int heap_idx = 1;
708 unsigned int idx;
710 if (heap->entries[heap_idx].version <= version) return;
712 idx = heap->entries[heap_idx].idx;
713 if (constant_locations[idx] != -1)
714 start = end = idx;
715 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
717 while (stack_idx >= 0)
719 /* Note that we fall through to the next case statement. */
720 switch(stack[stack_idx])
722 case HEAP_NODE_TRAVERSE_LEFT:
724 unsigned int left_idx = heap_idx << 1;
725 if (left_idx < heap->size && heap->entries[left_idx].version > version)
727 heap_idx = left_idx;
728 idx = heap->entries[heap_idx].idx;
729 if (constant_locations[idx] != -1)
731 if (start > idx)
732 start = idx;
733 if (end < idx)
734 end = idx;
737 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
738 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
739 break;
743 case HEAP_NODE_TRAVERSE_RIGHT:
745 unsigned int right_idx = (heap_idx << 1) + 1;
746 if (right_idx < heap->size && heap->entries[right_idx].version > version)
748 heap_idx = right_idx;
749 idx = heap->entries[heap_idx].idx;
750 if (constant_locations[idx] != -1)
752 if (start > idx)
753 start = idx;
754 if (end < idx)
755 end = idx;
758 stack[stack_idx++] = HEAP_NODE_POP;
759 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
760 break;
764 case HEAP_NODE_POP:
765 heap_idx >>= 1;
766 --stack_idx;
767 break;
770 if (start <= end)
771 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
772 checkGLcall("walk_constant_heap()");
775 /* Context activation is done by the caller. */
776 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
777 GLint location, const struct wined3d_vec4 *data)
779 GLfloat clamped_constant[4];
781 if (location == -1) return;
783 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
784 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
785 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
786 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
788 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
791 /* Context activation is done by the caller. */
792 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
793 const struct wined3d_vec4 *constants, const GLint *constant_locations,
794 const struct constant_heap *heap, unsigned char *stack, DWORD version)
796 int stack_idx = 0;
797 unsigned int heap_idx = 1;
798 unsigned int idx;
800 if (heap->entries[heap_idx].version <= version) return;
802 idx = heap->entries[heap_idx].idx;
803 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
804 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
806 while (stack_idx >= 0)
808 /* Note that we fall through to the next case statement. */
809 switch(stack[stack_idx])
811 case HEAP_NODE_TRAVERSE_LEFT:
813 unsigned int left_idx = heap_idx << 1;
814 if (left_idx < heap->size && heap->entries[left_idx].version > version)
816 heap_idx = left_idx;
817 idx = heap->entries[heap_idx].idx;
818 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
820 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
821 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
822 break;
826 case HEAP_NODE_TRAVERSE_RIGHT:
828 unsigned int right_idx = (heap_idx << 1) + 1;
829 if (right_idx < heap->size && heap->entries[right_idx].version > version)
831 heap_idx = right_idx;
832 idx = heap->entries[heap_idx].idx;
833 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
835 stack[stack_idx++] = HEAP_NODE_POP;
836 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
837 break;
841 case HEAP_NODE_POP:
842 heap_idx >>= 1;
843 --stack_idx;
844 break;
847 checkGLcall("walk_constant_heap_clamped()");
850 /* Context activation is done by the caller. */
851 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
852 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
853 unsigned char *stack, unsigned int version)
855 const struct wined3d_shader_lconst *lconst;
857 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
858 if (shader->reg_maps.shader_version.major == 1
859 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
860 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
861 else
862 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
864 if (!shader->load_local_constsF)
866 TRACE("No need to load local float constants for this shader.\n");
867 return;
870 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
871 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
873 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
875 checkGLcall("glUniform4fv()");
878 /* Context activation is done by the caller. */
879 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
880 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
882 unsigned int i;
883 struct list* ptr;
885 for (i = 0; constants_set; constants_set >>= 1, ++i)
887 if (!(constants_set & 1)) continue;
889 /* We found this uniform name in the program - go ahead and send the data */
890 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
893 /* Load immediate constants */
894 ptr = list_head(&shader->constantsI);
895 while (ptr)
897 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
898 unsigned int idx = lconst->idx;
899 const GLint *values = (const GLint *)lconst->value;
901 /* We found this uniform name in the program - go ahead and send the data */
902 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
903 ptr = list_next(&shader->constantsI, ptr);
905 checkGLcall("glUniform4iv()");
908 /* Context activation is done by the caller. */
909 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
910 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
912 unsigned int i;
913 struct list* ptr;
915 for (i = 0; constants_set; constants_set >>= 1, ++i)
917 if (!(constants_set & 1)) continue;
919 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
922 /* Load immediate constants */
923 ptr = list_head(&shader->constantsB);
924 while (ptr)
926 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
927 unsigned int idx = lconst->idx;
928 const GLint *values = (const GLint *)lconst->value;
930 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
931 ptr = list_next(&shader->constantsB, ptr);
933 checkGLcall("glUniform1iv()");
936 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
938 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
941 /* Context activation is done by the caller (state handler). */
942 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
943 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
945 struct
947 float sx, sy;
949 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
950 UINT fixup = ps->np2_fixup_info->active;
951 UINT i;
953 for (i = 0; fixup; fixup >>= 1, ++i)
955 const struct wined3d_texture *tex = state->textures[i];
956 unsigned char idx = ps->np2_fixup_info->idx[i];
958 if (!tex)
960 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
961 continue;
964 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
965 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
968 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
971 /* Taken and adapted from Mesa. */
972 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
974 float pos, neg, t, det;
975 struct wined3d_matrix temp;
977 /* Calculate the determinant of upper left 3x3 submatrix and
978 * determine if the matrix is singular. */
979 pos = neg = 0.0f;
980 t = in->_11 * in->_22 * in->_33;
981 if (t >= 0.0f)
982 pos += t;
983 else
984 neg += t;
986 t = in->_21 * in->_32 * in->_13;
987 if (t >= 0.0f)
988 pos += t;
989 else
990 neg += t;
991 t = in->_31 * in->_12 * in->_23;
992 if (t >= 0.0f)
993 pos += t;
994 else
995 neg += t;
997 t = -in->_31 * in->_22 * in->_13;
998 if (t >= 0.0f)
999 pos += t;
1000 else
1001 neg += t;
1002 t = -in->_21 * in->_12 * in->_33;
1003 if (t >= 0.0f)
1004 pos += t;
1005 else
1006 neg += t;
1008 t = -in->_11 * in->_32 * in->_23;
1009 if (t >= 0.0f)
1010 pos += t;
1011 else
1012 neg += t;
1014 det = pos + neg;
1016 if (fabsf(det) < 1e-25f)
1017 return FALSE;
1019 det = 1.0f / det;
1020 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
1021 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
1022 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
1023 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
1024 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
1025 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
1026 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
1027 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
1028 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
1030 *out = temp;
1031 return TRUE;
1034 static void swap_rows(float **a, float **b)
1036 float *tmp = *a;
1038 *a = *b;
1039 *b = tmp;
1042 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
1044 float wtmp[4][8];
1045 float m0, m1, m2, m3, s;
1046 float *r0, *r1, *r2, *r3;
1048 r0 = wtmp[0];
1049 r1 = wtmp[1];
1050 r2 = wtmp[2];
1051 r3 = wtmp[3];
1053 r0[0] = m->_11;
1054 r0[1] = m->_12;
1055 r0[2] = m->_13;
1056 r0[3] = m->_14;
1057 r0[4] = 1.0f;
1058 r0[5] = r0[6] = r0[7] = 0.0f;
1060 r1[0] = m->_21;
1061 r1[1] = m->_22;
1062 r1[2] = m->_23;
1063 r1[3] = m->_24;
1064 r1[5] = 1.0f;
1065 r1[4] = r1[6] = r1[7] = 0.0f;
1067 r2[0] = m->_31;
1068 r2[1] = m->_32;
1069 r2[2] = m->_33;
1070 r2[3] = m->_34;
1071 r2[6] = 1.0f;
1072 r2[4] = r2[5] = r2[7] = 0.0f;
1074 r3[0] = m->_41;
1075 r3[1] = m->_42;
1076 r3[2] = m->_43;
1077 r3[3] = m->_44;
1078 r3[7] = 1.0f;
1079 r3[4] = r3[5] = r3[6] = 0.0f;
1081 /* Choose pivot - or die. */
1082 if (fabsf(r3[0]) > fabsf(r2[0]))
1083 swap_rows(&r3, &r2);
1084 if (fabsf(r2[0]) > fabsf(r1[0]))
1085 swap_rows(&r2, &r1);
1086 if (fabsf(r1[0]) > fabsf(r0[0]))
1087 swap_rows(&r1, &r0);
1088 if (r0[0] == 0.0f)
1089 return FALSE;
1091 /* Eliminate first variable. */
1092 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1093 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1094 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1095 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1096 s = r0[4];
1097 if (s != 0.0f)
1099 r1[4] -= m1 * s;
1100 r2[4] -= m2 * s;
1101 r3[4] -= m3 * s;
1103 s = r0[5];
1104 if (s != 0.0f)
1106 r1[5] -= m1 * s;
1107 r2[5] -= m2 * s;
1108 r3[5] -= m3 * s;
1110 s = r0[6];
1111 if (s != 0.0f)
1113 r1[6] -= m1 * s;
1114 r2[6] -= m2 * s;
1115 r3[6] -= m3 * s;
1117 s = r0[7];
1118 if (s != 0.0f)
1120 r1[7] -= m1 * s;
1121 r2[7] -= m2 * s;
1122 r3[7] -= m3 * s;
1125 /* Choose pivot - or die. */
1126 if (fabsf(r3[1]) > fabsf(r2[1]))
1127 swap_rows(&r3, &r2);
1128 if (fabsf(r2[1]) > fabsf(r1[1]))
1129 swap_rows(&r2, &r1);
1130 if (r1[1] == 0.0f)
1131 return FALSE;
1133 /* Eliminate second variable. */
1134 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1135 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1136 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1137 s = r1[4];
1138 if (s != 0.0f)
1140 r2[4] -= m2 * s;
1141 r3[4] -= m3 * s;
1143 s = r1[5];
1144 if (s != 0.0f)
1146 r2[5] -= m2 * s;
1147 r3[5] -= m3 * s;
1149 s = r1[6];
1150 if (s != 0.0f)
1152 r2[6] -= m2 * s;
1153 r3[6] -= m3 * s;
1155 s = r1[7];
1156 if (s != 0.0f)
1158 r2[7] -= m2 * s;
1159 r3[7] -= m3 * s;
1162 /* Choose pivot - or die. */
1163 if (fabsf(r3[2]) > fabsf(r2[2]))
1164 swap_rows(&r3, &r2);
1165 if (r2[2] == 0.0f)
1166 return FALSE;
1168 /* Eliminate third variable. */
1169 m3 = r3[2] / r2[2];
1170 r3[3] -= m3 * r2[3];
1171 r3[4] -= m3 * r2[4];
1172 r3[5] -= m3 * r2[5];
1173 r3[6] -= m3 * r2[6];
1174 r3[7] -= m3 * r2[7];
1176 /* Last check. */
1177 if (r3[3] == 0.0f)
1178 return FALSE;
1180 /* Back substitute row 3. */
1181 s = 1.0f / r3[3];
1182 r3[4] *= s;
1183 r3[5] *= s;
1184 r3[6] *= s;
1185 r3[7] *= s;
1187 /* Back substitute row 2. */
1188 m2 = r2[3];
1189 s = 1.0f / r2[2];
1190 r2[4] = s * (r2[4] - r3[4] * m2);
1191 r2[5] = s * (r2[5] - r3[5] * m2);
1192 r2[6] = s * (r2[6] - r3[6] * m2);
1193 r2[7] = s * (r2[7] - r3[7] * m2);
1194 m1 = r1[3];
1195 r1[4] -= r3[4] * m1;
1196 r1[5] -= r3[5] * m1;
1197 r1[6] -= r3[6] * m1;
1198 r1[7] -= r3[7] * m1;
1199 m0 = r0[3];
1200 r0[4] -= r3[4] * m0;
1201 r0[5] -= r3[5] * m0;
1202 r0[6] -= r3[6] * m0;
1203 r0[7] -= r3[7] * m0;
1205 /* Back substitute row 1. */
1206 m1 = r1[2];
1207 s = 1.0f / r1[1];
1208 r1[4] = s * (r1[4] - r2[4] * m1);
1209 r1[5] = s * (r1[5] - r2[5] * m1);
1210 r1[6] = s * (r1[6] - r2[6] * m1);
1211 r1[7] = s * (r1[7] - r2[7] * m1);
1212 m0 = r0[2];
1213 r0[4] -= r2[4] * m0;
1214 r0[5] -= r2[5] * m0;
1215 r0[6] -= r2[6] * m0;
1216 r0[7] -= r2[7] * m0;
1218 /* Back substitute row 0. */
1219 m0 = r0[1];
1220 s = 1.0f / r0[0];
1221 r0[4] = s * (r0[4] - r1[4] * m0);
1222 r0[5] = s * (r0[5] - r1[5] * m0);
1223 r0[6] = s * (r0[6] - r1[6] * m0);
1224 r0[7] = s * (r0[7] - r1[7] * m0);
1226 out->_11 = r0[4];
1227 out->_12 = r0[5];
1228 out->_13 = r0[6];
1229 out->_14 = r0[7];
1230 out->_21 = r1[4];
1231 out->_22 = r1[5];
1232 out->_23 = r1[6];
1233 out->_24 = r1[7];
1234 out->_31 = r2[4];
1235 out->_32 = r2[5];
1236 out->_33 = r2[6];
1237 out->_34 = r2[7];
1238 out->_41 = r3[4];
1239 out->_42 = r3[5];
1240 out->_43 = r3[6];
1241 out->_44 = r3[7];
1243 return TRUE;
1246 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1247 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1249 const struct wined3d_gl_info *gl_info = context->gl_info;
1250 float mat[3 * 3];
1251 struct wined3d_matrix mv;
1252 unsigned int i, j;
1254 if (prog->vs.normal_matrix_location == -1)
1255 return;
1257 get_modelview_matrix(context, state, 0, &mv);
1258 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1259 invert_matrix_3d(&mv, &mv);
1260 else
1261 invert_matrix(&mv, &mv);
1262 /* Tests show that singular modelview matrices are used unchanged as normal
1263 * matrices on D3D3 and older. There seems to be no clearly consistent
1264 * behavior on newer D3D versions so always follow older ddraw behavior. */
1265 for (i = 0; i < 3; ++i)
1266 for (j = 0; j < 3; ++j)
1267 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1269 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1270 checkGLcall("glUniformMatrix3fv");
1273 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1274 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1276 const struct wined3d_gl_info *gl_info = context->gl_info;
1277 struct wined3d_matrix mat;
1279 if (tex >= MAX_TEXTURES)
1280 return;
1281 if (prog->vs.texture_matrix_location[tex] == -1)
1282 return;
1284 get_texture_matrix(context, state, tex, &mat);
1285 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1286 checkGLcall("glUniformMatrix4fv");
1289 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1290 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1292 const struct wined3d_gl_info *gl_info = context->gl_info;
1294 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1296 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1297 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1299 else
1301 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1303 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1305 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1306 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1307 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1308 checkGLcall("setting FFP material uniforms");
1311 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1312 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1314 const struct wined3d_gl_info *gl_info = context->gl_info;
1315 struct wined3d_color color;
1317 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1318 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1319 checkGLcall("glUniform3fv");
1322 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1323 const struct wined3d_matrix *src2)
1325 struct wined3d_vec4 temp;
1327 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1328 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1329 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1330 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1332 *dest = temp;
1335 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1336 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1337 struct glsl_shader_prog_link *prog)
1339 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1340 const struct wined3d_gl_info *gl_info = context->gl_info;
1341 struct wined3d_vec4 vec4;
1343 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1344 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1345 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1347 switch (light_info->OriginalParms.type)
1349 case WINED3D_LIGHT_POINT:
1350 multiply_vector_matrix(&vec4, &light_info->position, view);
1351 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1352 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1353 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1354 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1355 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1356 break;
1358 case WINED3D_LIGHT_SPOT:
1359 multiply_vector_matrix(&vec4, &light_info->position, view);
1360 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1362 multiply_vector_matrix(&vec4, &light_info->direction, view);
1363 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1365 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1366 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1367 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1368 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1369 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1370 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1371 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1372 break;
1374 case WINED3D_LIGHT_DIRECTIONAL:
1375 multiply_vector_matrix(&vec4, &light_info->direction, view);
1376 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1377 break;
1379 case WINED3D_LIGHT_PARALLELPOINT:
1380 multiply_vector_matrix(&vec4, &light_info->position, view);
1381 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1382 break;
1384 default:
1385 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1387 checkGLcall("setting FFP lights uniforms");
1390 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1391 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1393 const struct wined3d_gl_info *gl_info = context->gl_info;
1394 float min, max;
1395 float size, att[3];
1397 get_pointsize_minmax(context, state, &min, &max);
1399 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1400 checkGLcall("glUniform1f");
1401 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1402 checkGLcall("glUniform1f");
1404 get_pointsize(context, state, &size, att);
1406 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1407 checkGLcall("glUniform1f");
1408 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1409 checkGLcall("glUniform1f");
1410 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1411 checkGLcall("glUniform1f");
1412 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1413 checkGLcall("glUniform1f");
1416 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1417 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1419 const struct wined3d_gl_info *gl_info = context->gl_info;
1420 struct wined3d_color color;
1421 float start, end, scale;
1422 union
1424 DWORD d;
1425 float f;
1426 } tmpvalue;
1428 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1429 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1430 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1431 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1432 get_fog_start_end(context, state, &start, &end);
1433 scale = 1.0f / (end - start);
1434 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1435 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1436 checkGLcall("fog emulation uniforms");
1439 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1440 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1442 const struct wined3d_gl_info *gl_info = context->gl_info;
1443 struct wined3d_vec4 plane;
1445 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1446 if (!use_vs(state))
1447 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1448 else
1449 plane = state->clip_planes[index];
1451 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1454 /* Context activation is done by the caller (state handler). */
1455 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1456 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1458 struct wined3d_color float_key[2];
1459 const struct wined3d_texture *texture = state->textures[0];
1461 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1462 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1465 /* Context activation is done by the caller (state handler). */
1466 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1467 const struct wined3d_state *state)
1469 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1470 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1471 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1472 const struct wined3d_gl_info *gl_info = context->gl_info;
1473 struct shader_glsl_priv *priv = shader_priv;
1474 float position_fixup[4];
1475 DWORD update_mask;
1477 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1478 UINT constant_version;
1479 int i;
1481 if (!prog) {
1482 /* No GLSL program set - nothing to do. */
1483 return;
1485 constant_version = prog->constant_version;
1486 update_mask = context->constant_update_mask & prog->constant_update_mask;
1488 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1489 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1490 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1492 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1493 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1494 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1496 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1497 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1498 vshader->reg_maps.boolean_constants);
1500 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1502 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1503 shader_glsl_clip_plane_uniform(context, state, i, prog);
1506 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1507 shader_glsl_pointsize_uniform(context, state, prog);
1509 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1511 shader_get_position_fixup(context, state, position_fixup);
1512 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1513 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1514 else
1515 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1516 checkGLcall("glUniform4fv");
1519 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1521 struct wined3d_matrix mat;
1523 get_modelview_matrix(context, state, 0, &mat);
1524 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1525 checkGLcall("glUniformMatrix4fv");
1527 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1530 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1532 struct wined3d_matrix mat;
1534 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1536 if (prog->vs.modelview_matrix_location[i] == -1)
1537 break;
1539 get_modelview_matrix(context, state, i, &mat);
1540 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1541 checkGLcall("glUniformMatrix4fv");
1545 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1547 struct wined3d_matrix projection;
1549 get_projection_matrix(context, state, &projection);
1550 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1551 checkGLcall("glUniformMatrix4fv");
1554 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1556 for (i = 0; i < MAX_TEXTURES; ++i)
1557 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1560 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1561 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1563 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1565 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1566 DWORD point_count = 0;
1567 DWORD spot_count = 0;
1568 DWORD directional_count = 0;
1569 DWORD parallel_point_count = 0;
1571 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1573 if (!state->lights[i])
1574 continue;
1576 switch (state->lights[i]->OriginalParms.type)
1578 case WINED3D_LIGHT_POINT:
1579 ++point_count;
1580 break;
1581 case WINED3D_LIGHT_SPOT:
1582 ++spot_count;
1583 break;
1584 case WINED3D_LIGHT_DIRECTIONAL:
1585 ++directional_count;
1586 break;
1587 case WINED3D_LIGHT_PARALLELPOINT:
1588 ++parallel_point_count;
1589 break;
1590 default:
1591 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1592 break;
1595 point_idx = 0;
1596 spot_idx = point_idx + point_count;
1597 directional_idx = spot_idx + spot_count;
1598 parallel_point_idx = directional_idx + directional_count;
1600 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1601 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1603 const struct wined3d_light_info *light_info = state->lights[i];
1604 unsigned int idx;
1606 if (!light_info)
1607 continue;
1609 switch (light_info->OriginalParms.type)
1611 case WINED3D_LIGHT_POINT:
1612 idx = point_idx++;
1613 break;
1614 case WINED3D_LIGHT_SPOT:
1615 idx = spot_idx++;
1616 break;
1617 case WINED3D_LIGHT_DIRECTIONAL:
1618 idx = directional_idx++;
1619 break;
1620 case WINED3D_LIGHT_PARALLELPOINT:
1621 idx = parallel_point_idx++;
1622 break;
1623 default:
1624 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1625 continue;
1627 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1631 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1632 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1633 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1635 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1636 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1637 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1639 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1640 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1641 pshader->reg_maps.boolean_constants);
1643 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1645 for (i = 0; i < MAX_TEXTURES; ++i)
1647 if (prog->ps.bumpenv_mat_location[i] == -1)
1648 continue;
1650 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1651 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1653 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1655 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1656 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1657 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1658 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1662 checkGLcall("bump env uniforms");
1665 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1667 const struct wined3d_vec4 correction_params =
1669 /* Position is relative to the framebuffer, not the viewport. */
1670 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1671 context->render_offscreen ? 1.0f : -1.0f,
1672 0.0f,
1673 0.0f,
1676 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1679 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1680 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1681 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1682 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1684 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1686 struct wined3d_color color;
1688 if (prog->ps.tex_factor_location != -1)
1690 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1691 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1694 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1695 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1696 else
1697 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1699 for (i = 0; i < MAX_TEXTURES; ++i)
1701 if (prog->ps.tss_constant_location[i] == -1)
1702 continue;
1704 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1705 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1708 checkGLcall("fixed function uniforms");
1711 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1712 shader_glsl_load_fog_uniform(context, state, prog);
1714 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1716 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1718 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1719 checkGLcall("alpha test emulation uniform");
1722 if (priv->next_constant_version == UINT_MAX)
1724 TRACE("Max constant version reached, resetting to 0.\n");
1725 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1726 priv->next_constant_version = 1;
1728 else
1730 prog->constant_version = priv->next_constant_version++;
1734 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1736 struct constant_entry *entries = heap->entries;
1737 unsigned int *positions = heap->positions;
1738 unsigned int heap_idx, parent_idx;
1740 if (!heap->contained[idx])
1742 heap_idx = heap->size++;
1743 heap->contained[idx] = TRUE;
1745 else
1747 heap_idx = positions[idx];
1750 while (heap_idx > 1)
1752 parent_idx = heap_idx >> 1;
1754 if (new_version <= entries[parent_idx].version) break;
1756 entries[heap_idx] = entries[parent_idx];
1757 positions[entries[parent_idx].idx] = heap_idx;
1758 heap_idx = parent_idx;
1761 entries[heap_idx].version = new_version;
1762 entries[heap_idx].idx = idx;
1763 positions[idx] = heap_idx;
1766 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1768 struct shader_glsl_priv *priv = device->shader_priv;
1769 struct constant_heap *heap = &priv->vconst_heap;
1770 UINT i;
1772 for (i = start; i < count + start; ++i)
1774 update_heap_entry(heap, i, priv->next_constant_version);
1778 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1780 struct shader_glsl_priv *priv = device->shader_priv;
1781 struct constant_heap *heap = &priv->pconst_heap;
1782 UINT i;
1784 for (i = start; i < count + start; ++i)
1786 update_heap_entry(heap, i, priv->next_constant_version);
1790 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1792 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1793 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1794 if(shader_major > 3) return ret;
1796 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1797 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1798 return ret;
1801 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1803 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1806 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1808 return !needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION];
1811 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1813 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1816 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1817 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1819 va_list args;
1820 int ret;
1822 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1823 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1824 for (;;)
1826 va_start(args, format);
1827 ret = shader_vaddline(buffer, format, args);
1828 va_end(args);
1829 if (!ret)
1830 return;
1831 if (!string_buffer_resize(buffer, ret))
1832 return;
1836 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1837 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1839 va_list args;
1840 int ret;
1842 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1843 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1844 for (;;)
1846 va_start(args, format);
1847 ret = shader_vaddline(buffer, format, args);
1848 va_end(args);
1849 if (!ret)
1850 return;
1851 if (!string_buffer_resize(buffer, ret))
1852 return;
1856 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1858 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1861 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1863 switch (primitive_type)
1865 case WINED3D_PT_POINTLIST:
1866 return "points";
1868 case WINED3D_PT_LINELIST:
1869 return "lines";
1871 case WINED3D_PT_LINESTRIP:
1872 return "line_strip";
1874 case WINED3D_PT_TRIANGLELIST:
1875 return "triangles";
1877 case WINED3D_PT_TRIANGLESTRIP:
1878 return "triangle_strip";
1880 case WINED3D_PT_LINELIST_ADJ:
1881 return "lines_adjacency";
1883 case WINED3D_PT_TRIANGLELIST_ADJ:
1884 return "triangles_adjacency";
1886 default:
1887 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1888 return "";
1892 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1894 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1895 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1896 DWORD input_reg_used = shader->u.ps.input_reg_used;
1897 unsigned int i;
1899 if (reg_maps->shader_version.major < 3)
1900 return input_reg_used & (1u << idx);
1902 for (i = 0; i < input_signature->element_count; ++i)
1904 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1906 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1907 continue;
1909 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1910 && input->semantic_idx == idx)
1911 return input_reg_used & (1u << input->register_idx);
1913 return FALSE;
1916 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1917 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1919 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1921 if (version->major >= 4)
1922 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1923 else
1924 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1927 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1928 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1929 unsigned int index)
1931 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1932 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1933 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1934 index, scalar_type, scalar_type, index);
1937 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1938 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1940 unsigned int index = e->register_idx;
1942 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1944 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1945 index);
1946 return;
1948 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1950 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1951 index);
1952 return;
1954 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1955 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1957 if (shader_glsl_use_explicit_attrib_location(gl_info))
1958 shader_addline(buffer, "layout(location = %u) ", index);
1960 switch (e->component_type)
1962 case WINED3D_TYPE_UINT:
1963 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1964 break;
1965 case WINED3D_TYPE_INT:
1966 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1967 break;
1969 default:
1970 FIXME("Unhandled type %#x.\n", e->component_type);
1971 /* Fall through. */
1972 case WINED3D_TYPE_UNKNOWN:
1973 case WINED3D_TYPE_FLOAT:
1974 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1975 break;
1979 /** Generate the variable & register declarations for the GLSL output target */
1980 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1981 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1982 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1984 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1985 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1986 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1987 const struct wined3d_gl_info *gl_info = context->gl_info;
1988 const struct wined3d_shader_indexable_temp *idx_temp_reg;
1989 unsigned int uniform_block_base, uniform_block_count;
1990 unsigned int i, extra_constants_needed = 0;
1991 const struct wined3d_shader_lconst *lconst;
1992 const char *prefix;
1993 DWORD map;
1995 prefix = shader_glsl_get_prefix(version->type);
1997 /* Prototype the subroutines */
1998 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2000 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2003 /* Declare the constants (aka uniforms) */
2004 if (shader->limits->constant_float > 0)
2006 unsigned max_constantsF;
2008 /* Unless the shader uses indirect addressing, always declare the
2009 * maximum array size and ignore that we need some uniforms privately.
2010 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2011 * and immediate values, still declare VC[256]. If the shader needs
2012 * more uniforms than we have it won't work in any case. If it uses
2013 * less, the compiler will figure out which uniforms are really used
2014 * and strip them out. This allows a shader to use c255 on a dx9 card,
2015 * as long as it doesn't also use all the other constants.
2017 * If the shader uses indirect addressing the compiler must assume
2018 * that all declared uniforms are used. In this case, declare only the
2019 * amount that we're assured to have.
2021 * Thus we run into problems in these two cases:
2022 * 1) The shader really uses more uniforms than supported.
2023 * 2) The shader uses indirect addressing, less constants than
2024 * supported, but uses a constant index > #supported consts. */
2025 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2027 /* No indirect addressing here. */
2028 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2030 else
2032 if (reg_maps->usesrelconstF)
2034 /* Subtract the other potential uniforms from the max
2035 * available (bools, ints, and 1 row of projection matrix).
2036 * Subtract another uniform for immediate values, which have
2037 * to be loaded via uniform by the driver as well. The shader
2038 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2039 * shader code, so one vec4 should be enough. (Unfortunately
2040 * the Nvidia driver doesn't store 128 and -128 in one float).
2042 * Writing gl_ClipVertex requires one uniform for each
2043 * clipplane as well. */
2044 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2045 if (vs_args->clip_enabled)
2046 max_constantsF -= gl_info->limits.user_clip_distances;
2047 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2048 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2049 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2050 * for now take this into account when calculating the number of available constants
2052 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2053 /* Set by driver quirks in directx.c */
2054 max_constantsF -= gl_info->reserved_glsl_constants;
2056 if (max_constantsF < shader->limits->constant_float)
2058 static unsigned int once;
2060 if (!once++)
2061 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2062 " it may not render correctly.\n");
2063 else
2064 WARN("The hardware does not support enough uniform components to run this shader.\n");
2067 else
2069 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2072 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2073 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2076 /* Always declare the full set of constants, the compiler can remove the
2077 * unused ones because d3d doesn't (yet) support indirect int and bool
2078 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2079 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2080 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2082 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2083 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2085 /* Declare immediate constant buffer */
2086 if (reg_maps->icb)
2087 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2089 /* Declare constant buffers */
2090 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2091 &uniform_block_base, &uniform_block_count);
2092 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2094 if (reg_maps->cb_sizes[i])
2096 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2097 shader_addline(buffer, "layout(binding = %u)\n", uniform_block_base + i);
2098 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2099 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2103 /* Declare texture samplers */
2104 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2106 struct wined3d_shader_sampler_map_entry *entry;
2107 const char *sampler_type_prefix, *sampler_type;
2108 BOOL shadow_sampler, tex_rect;
2110 entry = &reg_maps->sampler_map.entries[i];
2112 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2114 ERR("Invalid resource index %u.\n", entry->resource_idx);
2115 continue;
2118 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2120 case WINED3D_DATA_FLOAT:
2121 case WINED3D_DATA_UNORM:
2122 case WINED3D_DATA_SNORM:
2123 sampler_type_prefix = "";
2124 break;
2126 case WINED3D_DATA_INT:
2127 sampler_type_prefix = "i";
2128 break;
2130 case WINED3D_DATA_UINT:
2131 sampler_type_prefix = "u";
2132 break;
2134 default:
2135 sampler_type_prefix = "";
2136 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2137 break;
2140 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2141 switch (reg_maps->resource_info[entry->resource_idx].type)
2143 case WINED3D_SHADER_RESOURCE_BUFFER:
2144 sampler_type = "samplerBuffer";
2145 break;
2147 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2148 if (shadow_sampler)
2149 sampler_type = "sampler1DShadow";
2150 else
2151 sampler_type = "sampler1D";
2152 break;
2154 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2155 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2156 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2157 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2158 if (shadow_sampler)
2160 if (tex_rect)
2161 sampler_type = "sampler2DRectShadow";
2162 else
2163 sampler_type = "sampler2DShadow";
2165 else
2167 if (tex_rect)
2168 sampler_type = "sampler2DRect";
2169 else
2170 sampler_type = "sampler2D";
2172 break;
2174 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2175 if (shadow_sampler)
2176 FIXME("Unsupported 3D shadow sampler.\n");
2177 sampler_type = "sampler3D";
2178 break;
2180 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2181 if (shadow_sampler)
2182 FIXME("Unsupported Cube shadow sampler.\n");
2183 sampler_type = "samplerCube";
2184 break;
2186 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2187 if (shadow_sampler)
2188 sampler_type = "sampler2DArrayShadow";
2189 else
2190 sampler_type = "sampler2DArray";
2191 break;
2193 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2194 if (shadow_sampler)
2195 FIXME("Unsupported Cube array shadow sampler.\n");
2196 sampler_type = "samplerCubeArray";
2197 break;
2199 default:
2200 sampler_type = "unsupported_sampler";
2201 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2202 break;
2204 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2205 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2208 /* Declare images */
2209 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2211 const char *image_type_prefix, *image_type, *read_format;
2213 if (!reg_maps->uav_resource_info[i].type)
2214 continue;
2216 switch (reg_maps->uav_resource_info[i].data_type)
2218 case WINED3D_DATA_FLOAT:
2219 case WINED3D_DATA_UNORM:
2220 case WINED3D_DATA_SNORM:
2221 image_type_prefix = "";
2222 read_format = "r32f";
2223 break;
2225 case WINED3D_DATA_INT:
2226 image_type_prefix = "i";
2227 read_format = "r32i";
2228 break;
2230 case WINED3D_DATA_UINT:
2231 image_type_prefix = "u";
2232 read_format = "r32ui";
2233 break;
2235 default:
2236 image_type_prefix = "";
2237 read_format = "";
2238 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2239 break;
2242 switch (reg_maps->uav_resource_info[i].type)
2244 case WINED3D_SHADER_RESOURCE_BUFFER:
2245 image_type = "imageBuffer";
2246 break;
2248 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2249 image_type = "image2D";
2250 break;
2252 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2253 image_type = "image3D";
2254 break;
2256 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2257 image_type = "image2DArray";
2258 break;
2260 default:
2261 image_type = "unsupported_image";
2262 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2263 break;
2266 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2267 shader_addline(buffer, "layout(binding = %u)\n", i);
2268 if (reg_maps->uav_read_mask & (1u << i))
2269 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2270 read_format, image_type_prefix, image_type, prefix, i);
2271 else
2272 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2273 image_type_prefix, image_type, prefix, i);
2275 if (reg_maps->uav_counter_mask & (1u << i))
2276 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2277 i, prefix, i);
2280 /* Declare uniforms for NP2 texcoord fixup:
2281 * This is NOT done inside the loop that declares the texture samplers
2282 * since the NP2 fixup code is currently only used for the GeforceFX
2283 * series and when forcing the ARB_npot extension off. Modern cards just
2284 * skip the code anyway, so put it inside a separate loop. */
2285 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2287 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2288 UINT cur = 0;
2290 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2291 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2292 * samplerNP2Fixup stores texture dimensions and is updated through
2293 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2295 for (i = 0; i < shader->limits->sampler; ++i)
2297 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2298 continue;
2300 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2302 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2303 continue;
2306 fixup->idx[i] = cur++;
2309 fixup->num_consts = (cur + 1) >> 1;
2310 fixup->active = ps_args->np2_fixup;
2311 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2314 /* Declare address variables */
2315 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2317 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2320 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2322 for (i = 0; i < shader->input_signature.element_count; ++i)
2323 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2325 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2327 shader_addline(buffer, "uniform struct\n{\n");
2328 shader_addline(buffer, " float size;\n");
2329 shader_addline(buffer, " float size_min;\n");
2330 shader_addline(buffer, " float size_max;\n");
2331 shader_addline(buffer, "} ffp_point;\n");
2334 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2336 if (vs_args->clip_enabled)
2337 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
2339 if (version->major < 3)
2341 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2342 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2343 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2344 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2348 if (version->major < 4)
2349 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2351 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2353 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2355 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2357 else
2359 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2360 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2361 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2362 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2365 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2367 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2369 shader_addline(buffer, "uniform struct\n{\n");
2370 shader_addline(buffer, " vec4 color;\n");
2371 shader_addline(buffer, " float density;\n");
2372 shader_addline(buffer, " float end;\n");
2373 shader_addline(buffer, " float scale;\n");
2374 shader_addline(buffer, "} ffp_fog;\n");
2376 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2378 if (glsl_is_color_reg_read(shader, 0))
2379 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2380 if (glsl_is_color_reg_read(shader, 1))
2381 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2382 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2383 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2385 else
2387 if (glsl_is_color_reg_read(shader, 0))
2388 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2389 if (glsl_is_color_reg_read(shader, 1))
2390 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2391 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2392 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2393 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2397 if (version->major >= 3)
2399 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2401 if (ps_args->vp_mode == vertexshader)
2402 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2403 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2406 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2408 if (!(map & 1))
2409 continue;
2411 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2413 if (reg_maps->luminanceparams & (1u << i))
2415 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2416 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2417 extra_constants_needed++;
2420 extra_constants_needed++;
2423 if (ps_args->srgb_correction)
2425 shader_addline(buffer, "const vec4 srgb_const0 = ");
2426 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2427 shader_addline(buffer, ";\n");
2428 shader_addline(buffer, "const vec4 srgb_const1 = ");
2429 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2430 shader_addline(buffer, ";\n");
2432 if (reg_maps->vpos || reg_maps->usesdsy)
2434 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2436 ++extra_constants_needed;
2437 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2439 if (reg_maps->vpos)
2441 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2443 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
2444 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2445 ps_args->render_offscreen ? "" : "origin_upper_left, ");
2446 else if (!ps_args->render_offscreen)
2447 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2449 shader_addline(buffer, "vec4 vpos;\n");
2453 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2454 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2456 if (!needs_legacy_glsl_syntax(gl_info))
2457 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2459 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2460 FIXME("Insufficient uniforms to run this shader.\n");
2463 /* Declare output register temporaries */
2464 if (shader->limits->packed_output)
2465 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2467 /* Declare temporary variables */
2468 if (reg_maps->temporary_count)
2470 for (i = 0; i < reg_maps->temporary_count; ++i)
2471 shader_addline(buffer, "vec4 R%u;\n", i);
2473 else
2475 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2477 if (map & 1)
2478 shader_addline(buffer, "vec4 R%u;\n", i);
2482 /* Declare indexable temporary variables */
2483 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2485 if (idx_temp_reg->component_count != 4)
2486 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2487 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2490 /* Declare loop registers aLx */
2491 if (version->major < 4)
2493 for (i = 0; i < reg_maps->loop_depth; ++i)
2495 shader_addline(buffer, "int aL%u;\n", i);
2496 shader_addline(buffer, "int tmpInt%u;\n", i);
2500 /* Temporary variables for matrix operations */
2501 shader_addline(buffer, "vec4 tmp0;\n");
2502 shader_addline(buffer, "vec4 tmp1;\n");
2504 if (!shader->load_local_constsF)
2506 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2508 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2509 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2510 shader_addline(buffer, ";\n");
2515 /*****************************************************************************
2516 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2518 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2519 ****************************************************************************/
2521 /* Prototypes */
2522 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2523 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2525 /** Used for opcode modifiers - They multiply the result by the specified amount */
2526 static const char * const shift_glsl_tab[] = {
2527 "", /* 0 (none) */
2528 "2.0 * ", /* 1 (x2) */
2529 "4.0 * ", /* 2 (x4) */
2530 "8.0 * ", /* 3 (x8) */
2531 "16.0 * ", /* 4 (x16) */
2532 "32.0 * ", /* 5 (x32) */
2533 "", /* 6 (x64) */
2534 "", /* 7 (x128) */
2535 "", /* 8 (d256) */
2536 "", /* 9 (d128) */
2537 "", /* 10 (d64) */
2538 "", /* 11 (d32) */
2539 "0.0625 * ", /* 12 (d16) */
2540 "0.125 * ", /* 13 (d8) */
2541 "0.25 * ", /* 14 (d4) */
2542 "0.5 * " /* 15 (d2) */
2545 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2546 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2547 const char *in_reg, const char *in_regswizzle, char *out_str)
2549 switch (src_modifier)
2551 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2552 case WINED3DSPSM_DW:
2553 case WINED3DSPSM_NONE:
2554 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2555 break;
2556 case WINED3DSPSM_NEG:
2557 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2558 break;
2559 case WINED3DSPSM_NOT:
2560 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2561 break;
2562 case WINED3DSPSM_BIAS:
2563 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2564 break;
2565 case WINED3DSPSM_BIASNEG:
2566 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2567 break;
2568 case WINED3DSPSM_SIGN:
2569 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2570 break;
2571 case WINED3DSPSM_SIGNNEG:
2572 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2573 break;
2574 case WINED3DSPSM_COMP:
2575 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2576 break;
2577 case WINED3DSPSM_X2:
2578 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2579 break;
2580 case WINED3DSPSM_X2NEG:
2581 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2582 break;
2583 case WINED3DSPSM_ABS:
2584 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2585 break;
2586 case WINED3DSPSM_ABSNEG:
2587 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2588 break;
2589 default:
2590 FIXME("Unhandled modifier %u\n", src_modifier);
2591 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2595 /** Writes the GLSL variable name that corresponds to the register that the
2596 * DX opcode parameter is trying to access */
2597 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2598 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2600 /* oPos, oFog and oPts in D3D */
2601 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2603 const struct wined3d_shader *shader = ins->ctx->shader;
2604 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2605 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2606 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2607 const char *prefix = shader_glsl_get_prefix(version->type);
2608 struct glsl_src_param rel_param0, rel_param1;
2609 char imm_str[4][17];
2611 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2612 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2613 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2614 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2615 *is_color = FALSE;
2617 switch (reg->type)
2619 case WINED3DSPR_TEMP:
2620 sprintf(register_name, "R%u", reg->idx[0].offset);
2621 break;
2623 case WINED3DSPR_INPUT:
2624 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2626 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2628 if (reg->idx[0].rel_addr)
2629 FIXME("VS3+ input registers relative addressing.\n");
2630 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2631 *is_color = TRUE;
2632 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2633 break;
2636 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2638 if (reg->idx[0].rel_addr)
2640 if (reg->idx[1].rel_addr)
2641 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2642 rel_param0.param_str, reg->idx[0].offset,
2643 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2644 rel_param1.param_str, reg->idx[1].offset);
2645 else
2646 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2647 rel_param0.param_str, reg->idx[0].offset,
2648 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2649 reg->idx[1].offset);
2651 else if (reg->idx[1].rel_addr)
2652 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2653 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2654 rel_param1.param_str, reg->idx[1].offset);
2655 else
2656 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2657 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2658 reg->idx[1].offset);
2659 break;
2662 /* pixel shaders >= 3.0 */
2663 if (version->major >= 3)
2665 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2666 unsigned int in_count = vec4_varyings(version->major, gl_info);
2668 if (reg->idx[0].rel_addr)
2670 /* Removing a + 0 would be an obvious optimization, but
2671 * OS X doesn't see the NOP operation there. */
2672 if (idx)
2674 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2675 && shader->u.ps.declared_in_count > in_count)
2677 sprintf(register_name,
2678 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2679 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2680 prefix, rel_param0.param_str, idx);
2682 else
2684 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2687 else
2689 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2690 && shader->u.ps.declared_in_count > in_count)
2692 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2693 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2694 prefix, rel_param0.param_str);
2696 else
2698 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2702 else
2704 if (idx == in_count) sprintf(register_name, "gl_Color");
2705 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2706 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2709 else
2711 if (!reg->idx[0].offset)
2712 strcpy(register_name, "ffp_varying_diffuse");
2713 else
2714 strcpy(register_name, "ffp_varying_specular");
2715 break;
2717 break;
2719 case WINED3DSPR_CONST:
2721 /* Relative addressing */
2722 if (reg->idx[0].rel_addr)
2724 if (wined3d_settings.check_float_constants)
2725 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2726 rel_param0.param_str, reg->idx[0].offset,
2727 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2728 prefix, rel_param0.param_str, reg->idx[0].offset);
2729 else if (reg->idx[0].offset)
2730 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2731 else
2732 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2734 else
2736 if (shader_constant_is_local(shader, reg->idx[0].offset))
2737 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2738 else
2739 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2742 break;
2744 case WINED3DSPR_CONSTINT:
2745 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2746 break;
2748 case WINED3DSPR_CONSTBOOL:
2749 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2750 break;
2752 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2753 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2754 sprintf(register_name, "T%u", reg->idx[0].offset);
2755 else
2756 sprintf(register_name, "A%u", reg->idx[0].offset);
2757 break;
2759 case WINED3DSPR_LOOP:
2760 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2761 break;
2763 case WINED3DSPR_SAMPLER:
2764 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2765 break;
2767 case WINED3DSPR_COLOROUT:
2768 if (reg->idx[0].offset >= gl_info->limits.buffers)
2769 WARN("Write to render target %u, only %d supported.\n",
2770 reg->idx[0].offset, gl_info->limits.buffers);
2772 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2773 break;
2775 case WINED3DSPR_RASTOUT:
2776 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2777 break;
2779 case WINED3DSPR_DEPTHOUT:
2780 sprintf(register_name, "gl_FragDepth");
2781 break;
2783 case WINED3DSPR_ATTROUT:
2784 if (!reg->idx[0].offset)
2785 sprintf(register_name, "%s_out[8]", prefix);
2786 else
2787 sprintf(register_name, "%s_out[9]", prefix);
2788 break;
2790 case WINED3DSPR_TEXCRDOUT:
2791 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2792 if (reg->idx[0].rel_addr)
2793 FIXME("VS3 output registers relative addressing.\n");
2794 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2795 break;
2797 case WINED3DSPR_MISCTYPE:
2798 if (!reg->idx[0].offset)
2800 /* vPos */
2801 sprintf(register_name, "vpos");
2803 else if (reg->idx[0].offset == 1)
2805 /* Note that gl_FrontFacing is a bool, while vFace is
2806 * a float for which the sign determines front/back */
2807 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2809 else
2811 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2812 sprintf(register_name, "unrecognized_register");
2814 break;
2816 case WINED3DSPR_IMMCONST:
2817 switch (reg->immconst_type)
2819 case WINED3D_IMMCONST_SCALAR:
2820 switch (reg->data_type)
2822 case WINED3D_DATA_FLOAT:
2823 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2824 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
2825 else
2826 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
2827 break;
2828 case WINED3D_DATA_INT:
2829 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2830 break;
2831 case WINED3D_DATA_RESOURCE:
2832 case WINED3D_DATA_SAMPLER:
2833 case WINED3D_DATA_UINT:
2834 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2835 break;
2836 default:
2837 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2838 break;
2840 break;
2842 case WINED3D_IMMCONST_VEC4:
2843 switch (reg->data_type)
2845 case WINED3D_DATA_FLOAT:
2846 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2848 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2849 reg->u.immconst_data[0], reg->u.immconst_data[1],
2850 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2852 else
2854 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
2855 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
2856 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
2857 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
2858 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2859 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2861 break;
2862 case WINED3D_DATA_INT:
2863 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2864 reg->u.immconst_data[0], reg->u.immconst_data[1],
2865 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2866 break;
2867 case WINED3D_DATA_RESOURCE:
2868 case WINED3D_DATA_SAMPLER:
2869 case WINED3D_DATA_UINT:
2870 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2871 reg->u.immconst_data[0], reg->u.immconst_data[1],
2872 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2873 break;
2874 default:
2875 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2876 break;
2878 break;
2880 default:
2881 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2882 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2884 break;
2886 case WINED3DSPR_CONSTBUFFER:
2887 if (reg->idx[1].rel_addr)
2888 sprintf(register_name, "%s_cb%u[%s + %u]",
2889 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2890 else
2891 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2892 break;
2894 case WINED3DSPR_IMMCONSTBUFFER:
2895 if (reg->idx[0].rel_addr)
2896 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2897 else
2898 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2899 break;
2901 case WINED3DSPR_PRIMID:
2902 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2903 break;
2905 case WINED3DSPR_IDXTEMP:
2906 if (reg->idx[1].rel_addr)
2907 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2908 else
2909 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2910 break;
2912 case WINED3DSPR_LOCALTHREADINDEX:
2913 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2914 sprintf(register_name, "int(gl_LocalInvocationIndex)");
2915 else
2916 sprintf(register_name, "ivec2(gl_LocalInvocationIndex, 0)");
2917 break;
2919 case WINED3DSPR_THREADID:
2920 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2921 break;
2923 case WINED3DSPR_THREADGROUPID:
2924 sprintf(register_name, "ivec3(gl_WorkGroupID)");
2925 break;
2927 case WINED3DSPR_LOCALTHREADID:
2928 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
2929 break;
2931 default:
2932 FIXME("Unhandled register type %#x.\n", reg->type);
2933 sprintf(register_name, "unrecognized_register");
2934 break;
2938 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2940 *str++ = '.';
2941 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2942 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2943 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2944 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2945 *str = '\0';
2948 /* Get the GLSL write mask for the destination register */
2949 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2951 DWORD mask = param->write_mask;
2953 if (shader_is_scalar(&param->reg))
2955 mask = WINED3DSP_WRITEMASK_0;
2956 *write_mask = '\0';
2958 else
2960 shader_glsl_write_mask_to_str(mask, write_mask);
2963 return mask;
2966 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
2968 unsigned int size = 0;
2970 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2971 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2972 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2973 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2975 return size;
2978 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
2979 unsigned int component_idx)
2981 /* swizzle bits fields: wwzzyyxx */
2982 return (swizzle >> (2 * component_idx)) & 0x3;
2985 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2987 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2988 * but addressed as "rgba". To fix this we need to swap the register's x
2989 * and z components. */
2990 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2991 unsigned int i;
2993 *str++ = '.';
2994 for (i = 0; i < 4; ++i)
2996 if (mask & (WINED3DSP_WRITEMASK_0 << i))
2997 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
2999 *str = '\0';
3002 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3003 BOOL fixup, DWORD mask, char *swizzle_str)
3005 if (shader_is_scalar(&param->reg))
3006 *swizzle_str = '\0';
3007 else
3008 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3011 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3012 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
3014 if (dst_data_type == src_data_type)
3016 string_buffer_sprintf(dst_param, "%s", src_param);
3017 return;
3020 if (src_data_type == WINED3D_DATA_FLOAT)
3022 switch (dst_data_type)
3024 case WINED3D_DATA_INT:
3025 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3026 return;
3027 case WINED3D_DATA_RESOURCE:
3028 case WINED3D_DATA_SAMPLER:
3029 case WINED3D_DATA_UINT:
3030 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3031 return;
3032 default:
3033 break;
3037 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3039 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3040 return;
3043 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
3045 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3046 return;
3049 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3050 string_buffer_sprintf(dst_param, "%s", src_param);
3053 /* From a given parameter token, generate the corresponding GLSL string.
3054 * Also, return the actual register name and swizzle in case the
3055 * caller needs this information as well. */
3056 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3057 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3058 enum wined3d_data_type data_type)
3060 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3061 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3062 enum wined3d_data_type param_data_type;
3063 BOOL is_color = FALSE;
3064 char swizzle_str[6];
3066 glsl_src->reg_name[0] = '\0';
3067 glsl_src->param_str[0] = '\0';
3068 swizzle_str[0] = '\0';
3070 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
3071 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3073 switch (wined3d_src->reg.type)
3075 case WINED3DSPR_IMMCONST:
3076 param_data_type = data_type;
3077 break;
3078 case WINED3DSPR_PRIMID:
3079 param_data_type = WINED3D_DATA_UINT;
3080 break;
3081 case WINED3DSPR_LOCALTHREADINDEX:
3082 case WINED3DSPR_THREADID:
3083 case WINED3DSPR_THREADGROUPID:
3084 case WINED3DSPR_LOCALTHREADID:
3085 param_data_type = WINED3D_DATA_INT;
3086 break;
3087 default:
3088 param_data_type = WINED3D_DATA_FLOAT;
3089 break;
3092 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3093 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3095 string_buffer_release(priv->string_buffers, reg_name);
3098 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3099 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3101 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3104 /* From a given parameter token, generate the corresponding GLSL string.
3105 * Also, return the actual register name and swizzle in case the
3106 * caller needs this information as well. */
3107 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3108 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3110 BOOL is_color = FALSE;
3112 glsl_dst->mask_str[0] = '\0';
3113 glsl_dst->reg_name[0] = '\0';
3115 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
3116 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3119 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3120 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3121 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3122 enum wined3d_data_type data_type)
3124 struct glsl_dst_param glsl_dst;
3125 DWORD mask;
3127 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3129 switch (data_type)
3131 case WINED3D_DATA_FLOAT:
3132 shader_addline(buffer, "%s%s = %s(",
3133 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3134 break;
3135 case WINED3D_DATA_INT:
3136 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3137 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3138 break;
3139 case WINED3D_DATA_RESOURCE:
3140 case WINED3D_DATA_SAMPLER:
3141 case WINED3D_DATA_UINT:
3142 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3143 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3144 break;
3145 default:
3146 FIXME("Unhandled data type %#x.\n", data_type);
3147 shader_addline(buffer, "%s%s = %s(",
3148 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3149 break;
3153 return mask;
3156 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3157 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3159 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3162 /** Process GLSL instruction modifiers */
3163 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3165 struct glsl_dst_param dst_param;
3166 DWORD modifiers;
3168 if (!ins->dst_count) return;
3170 modifiers = ins->dst[0].modifiers;
3171 if (!modifiers) return;
3173 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3175 if (modifiers & WINED3DSPDM_SATURATE)
3177 /* _SAT means to clamp the value of the register to between 0 and 1 */
3178 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3179 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3182 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3184 FIXME("_centroid modifier not handled\n");
3187 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3189 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3193 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3195 switch (op)
3197 case WINED3D_SHADER_REL_OP_GT: return ">";
3198 case WINED3D_SHADER_REL_OP_EQ: return "==";
3199 case WINED3D_SHADER_REL_OP_GE: return ">=";
3200 case WINED3D_SHADER_REL_OP_LT: return "<";
3201 case WINED3D_SHADER_REL_OP_NE: return "!=";
3202 case WINED3D_SHADER_REL_OP_LE: return "<=";
3203 default:
3204 FIXME("Unrecognized operator %#x.\n", op);
3205 return "(\?\?)";
3209 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info,
3210 const struct wined3d_shader_version *version)
3212 return shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3215 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3216 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3218 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3219 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3220 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3221 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3222 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3223 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3224 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3225 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3226 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3227 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3228 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3229 const char *base = "texture", *type_part = "", *suffix = "";
3230 unsigned int coord_size, deriv_size;
3231 BOOL array;
3233 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3235 if (resource_type >= ARRAY_SIZE(resource_type_info))
3237 ERR("Unexpected resource type %#x.\n", resource_type);
3238 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3240 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3241 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3243 /* Note that there's no such thing as a projected cube texture. */
3244 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3245 projected = FALSE;
3247 if (needs_legacy_glsl_syntax(gl_info))
3249 if (shadow)
3250 base = "shadow";
3252 type_part = resource_type_info[resource_type].type_part;
3253 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3254 type_part = "2DRect";
3255 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3256 FIXME("Unhandled resource type %#x.\n", resource_type);
3258 if (!lod && grad && !shader_glsl_has_core_grad(gl_info, &ctx->shader->reg_maps.shader_version))
3260 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3261 suffix = "ARB";
3262 else
3263 FIXME("Unsupported grad function.\n");
3267 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3269 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3270 if (flags & ~texel_fetch_flags)
3271 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3273 base = "texelFetch";
3274 type_part = "";
3277 sample_function->name = string_buffer_get(priv->string_buffers);
3278 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3279 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3281 coord_size = resource_type_info[resource_type].coord_size;
3282 deriv_size = coord_size;
3283 if (shadow)
3284 ++coord_size;
3285 if (array)
3286 --deriv_size;
3287 sample_function->offset_size = offset ? deriv_size : 0;
3288 sample_function->coord_mask = (1u << coord_size) - 1;
3289 sample_function->deriv_mask = (1u << deriv_size) - 1;
3290 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3293 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3294 struct glsl_sample_function *sample_function)
3296 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3298 string_buffer_release(priv->string_buffers, sample_function->name);
3301 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3302 BOOL sign_fixup, enum fixup_channel_source channel_source)
3304 switch(channel_source)
3306 case CHANNEL_SOURCE_ZERO:
3307 strcat(arguments, "0.0");
3308 break;
3310 case CHANNEL_SOURCE_ONE:
3311 strcat(arguments, "1.0");
3312 break;
3314 case CHANNEL_SOURCE_X:
3315 strcat(arguments, reg_name);
3316 strcat(arguments, ".x");
3317 break;
3319 case CHANNEL_SOURCE_Y:
3320 strcat(arguments, reg_name);
3321 strcat(arguments, ".y");
3322 break;
3324 case CHANNEL_SOURCE_Z:
3325 strcat(arguments, reg_name);
3326 strcat(arguments, ".z");
3327 break;
3329 case CHANNEL_SOURCE_W:
3330 strcat(arguments, reg_name);
3331 strcat(arguments, ".w");
3332 break;
3334 default:
3335 FIXME("Unhandled channel source %#x\n", channel_source);
3336 strcat(arguments, "undefined");
3337 break;
3340 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3343 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3344 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3346 unsigned int mask_size, remaining;
3347 DWORD fixup_mask = 0;
3348 char arguments[256];
3349 char mask_str[6];
3351 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3352 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3353 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3354 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3355 if (!(mask &= fixup_mask))
3356 return;
3358 if (is_complex_fixup(fixup))
3360 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3361 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3362 return;
3365 shader_glsl_write_mask_to_str(mask, mask_str);
3366 mask_size = shader_glsl_get_write_mask_size(mask);
3368 arguments[0] = '\0';
3369 remaining = mask_size;
3370 if (mask & WINED3DSP_WRITEMASK_0)
3372 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3373 if (--remaining) strcat(arguments, ", ");
3375 if (mask & WINED3DSP_WRITEMASK_1)
3377 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3378 if (--remaining) strcat(arguments, ", ");
3380 if (mask & WINED3DSP_WRITEMASK_2)
3382 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3383 if (--remaining) strcat(arguments, ", ");
3385 if (mask & WINED3DSP_WRITEMASK_3)
3387 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3388 if (--remaining) strcat(arguments, ", ");
3391 if (mask_size > 1)
3392 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3393 else
3394 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3397 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3399 char reg_name[256];
3400 BOOL is_color;
3402 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3403 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3406 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3407 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3408 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3409 const char *coord_reg_fmt, ...)
3411 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3412 char dst_swizzle[6];
3413 struct color_fixup_desc fixup;
3414 BOOL np2_fixup = FALSE;
3415 va_list args;
3416 int ret;
3418 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3420 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3421 * We actually rely on it for vertex shaders and SM4+. */
3422 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3424 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3425 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3427 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3428 np2_fixup = TRUE;
3430 else
3432 fixup = COLOR_FIXUP_IDENTITY;
3435 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3437 if (sample_function->output_single_component)
3438 shader_addline(ins->ctx->buffer, "vec4(");
3440 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3441 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3443 for (;;)
3445 va_start(args, coord_reg_fmt);
3446 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3447 va_end(args);
3448 if (!ret)
3449 break;
3450 if (!string_buffer_resize(ins->ctx->buffer, ret))
3451 break;
3454 if (np2_fixup)
3456 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3457 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3459 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3461 case 1:
3462 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3463 idx >> 1, (idx % 2) ? "z" : "x");
3464 break;
3465 case 2:
3466 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3467 idx >> 1, (idx % 2) ? "zw" : "xy");
3468 break;
3469 case 3:
3470 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3471 idx >> 1, (idx % 2) ? "zw" : "xy");
3472 break;
3473 case 4:
3474 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3475 idx >> 1, (idx % 2) ? "zw" : "xy");
3476 break;
3479 if (dx && dy)
3480 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3481 else if (bias)
3482 shader_addline(ins->ctx->buffer, ", %s", bias);
3483 if (sample_function->offset_size)
3485 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3486 shader_addline(ins->ctx->buffer, ", ");
3487 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3489 shader_addline(ins->ctx->buffer, ")");
3491 if (sample_function->output_single_component)
3492 shader_addline(ins->ctx->buffer, ")");
3494 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3496 if (!is_identity_fixup(fixup))
3497 shader_glsl_color_correction(ins, fixup);
3500 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3502 /* Write the final position.
3504 * OpenGL coordinates specify the center of the pixel while D3D coords
3505 * specify the corner. The offsets are stored in z and w in
3506 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3507 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3508 * a MAD. */
3509 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3510 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3512 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3513 * in utils.c
3515 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3516 * shaders are run before the homogeneous divide, so we have to take the w
3517 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3518 * z = z * 2 - w. */
3519 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3522 /*****************************************************************************
3523 * Begin processing individual instruction opcodes
3524 ****************************************************************************/
3526 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3528 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3529 struct glsl_src_param src0_param;
3530 struct glsl_src_param src1_param;
3531 DWORD write_mask;
3532 const char *op;
3534 /* Determine the GLSL operator to use based on the opcode */
3535 switch (ins->handler_idx)
3537 case WINED3DSIH_ADD: op = "+"; break;
3538 case WINED3DSIH_AND: op = "&"; break;
3539 case WINED3DSIH_DIV: op = "/"; break;
3540 case WINED3DSIH_IADD: op = "+"; break;
3541 case WINED3DSIH_ISHL: op = "<<"; break;
3542 case WINED3DSIH_ISHR: op = ">>"; break;
3543 case WINED3DSIH_MUL: op = "*"; break;
3544 case WINED3DSIH_OR: op = "|"; break;
3545 case WINED3DSIH_SUB: op = "-"; break;
3546 case WINED3DSIH_USHR: op = ">>"; break;
3547 case WINED3DSIH_XOR: op = "^"; break;
3548 default:
3549 op = "<unhandled operator>";
3550 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3551 break;
3554 write_mask = shader_glsl_append_dst(buffer, ins);
3555 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3556 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3557 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3560 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3562 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3563 struct glsl_src_param src0_param;
3564 struct glsl_src_param src1_param;
3565 unsigned int mask_size;
3566 DWORD write_mask;
3567 const char *op;
3569 write_mask = shader_glsl_append_dst(buffer, ins);
3570 mask_size = shader_glsl_get_write_mask_size(write_mask);
3571 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3572 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3574 if (mask_size > 1)
3576 switch (ins->handler_idx)
3578 case WINED3DSIH_EQ: op = "equal"; break;
3579 case WINED3DSIH_IEQ: op = "equal"; break;
3580 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3581 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3582 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3583 case WINED3DSIH_LT: op = "lessThan"; break;
3584 case WINED3DSIH_ILT: op = "lessThan"; break;
3585 case WINED3DSIH_ULT: op = "lessThan"; break;
3586 case WINED3DSIH_NE: op = "notEqual"; break;
3587 case WINED3DSIH_INE: op = "notEqual"; break;
3588 default:
3589 op = "<unhandled operator>";
3590 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3591 break;
3594 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3595 mask_size, op, src0_param.param_str, src1_param.param_str);
3597 else
3599 switch (ins->handler_idx)
3601 case WINED3DSIH_EQ: op = "=="; break;
3602 case WINED3DSIH_IEQ: op = "=="; break;
3603 case WINED3DSIH_GE: op = ">="; break;
3604 case WINED3DSIH_IGE: op = ">="; break;
3605 case WINED3DSIH_UGE: op = ">="; break;
3606 case WINED3DSIH_LT: op = "<"; break;
3607 case WINED3DSIH_ILT: op = "<"; break;
3608 case WINED3DSIH_ULT: op = "<"; break;
3609 case WINED3DSIH_NE: op = "!="; break;
3610 case WINED3DSIH_INE: op = "!="; break;
3611 default:
3612 op = "<unhandled operator>";
3613 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3614 break;
3617 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3618 src0_param.param_str, op, src1_param.param_str);
3622 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3624 struct glsl_src_param src_param;
3625 DWORD write_mask;
3626 const char *op;
3628 switch (ins->handler_idx)
3630 case WINED3DSIH_INEG: op = "-"; break;
3631 case WINED3DSIH_NOT: op = "~"; break;
3632 default:
3633 op = "<unhandled operator>";
3634 ERR("Unhandled opcode %s.\n",
3635 debug_d3dshaderinstructionhandler(ins->handler_idx));
3636 break;
3639 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3640 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3641 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3644 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3646 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3647 struct glsl_src_param src0_param;
3648 struct glsl_src_param src1_param;
3649 DWORD write_mask;
3651 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3652 * If not, we can emulate it. */
3653 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3654 FIXME("64-bit integer multiplies not implemented.\n");
3656 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3658 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3659 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3660 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3662 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3663 src0_param.param_str, src1_param.param_str);
3667 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3669 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3670 struct glsl_src_param src0_param, src1_param;
3671 DWORD write_mask;
3673 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3675 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3677 char dst_mask[6];
3679 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3680 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3681 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3682 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3683 dst_mask, src0_param.param_str, src1_param.param_str);
3685 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3686 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3687 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3688 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3690 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3691 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3693 else
3695 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3696 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3697 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3698 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3701 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3703 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3704 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3705 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3706 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3710 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3711 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3713 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3714 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3715 struct glsl_src_param src0_param;
3716 DWORD write_mask;
3718 write_mask = shader_glsl_append_dst(buffer, ins);
3719 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3721 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3722 * shader versions WINED3DSIO_MOVA is used for this. */
3723 if (ins->ctx->reg_maps->shader_version.major == 1
3724 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3725 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3727 /* This is a simple floor() */
3728 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3729 if (mask_size > 1) {
3730 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3731 } else {
3732 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3735 else if (ins->handler_idx == WINED3DSIH_MOVA)
3737 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
3738 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3740 if (shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3742 if (mask_size > 1)
3743 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3744 else
3745 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3747 else
3749 if (mask_size > 1)
3750 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3751 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3752 else
3753 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3754 src0_param.param_str, src0_param.param_str);
3757 else
3759 shader_addline(buffer, "%s);\n", src0_param.param_str);
3763 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3764 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3766 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3767 struct glsl_src_param src0_param;
3768 struct glsl_src_param src1_param;
3769 DWORD dst_write_mask, src_write_mask;
3770 unsigned int dst_size;
3772 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3773 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3775 /* dp4 works on vec4, dp3 on vec3, etc. */
3776 if (ins->handler_idx == WINED3DSIH_DP4)
3777 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3778 else if (ins->handler_idx == WINED3DSIH_DP3)
3779 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3780 else
3781 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3783 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3784 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3786 if (dst_size > 1) {
3787 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3788 } else {
3789 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3793 /* Note that this instruction has some restrictions. The destination write mask
3794 * can't contain the w component, and the source swizzles have to be .xyzw */
3795 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3797 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3798 struct glsl_src_param src0_param;
3799 struct glsl_src_param src1_param;
3800 char dst_mask[6];
3802 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3803 shader_glsl_append_dst(ins->ctx->buffer, ins);
3804 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3805 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3806 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3809 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3811 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3813 if (!stream)
3814 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3815 else
3816 FIXME("Unhandled primitive stream %u.\n", stream);
3819 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3820 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3821 * GLSL uses the value as-is. */
3822 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3824 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3825 struct glsl_src_param src0_param;
3826 struct glsl_src_param src1_param;
3827 DWORD dst_write_mask;
3828 unsigned int dst_size;
3830 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3831 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3833 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3834 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3836 if (dst_size > 1)
3838 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3839 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3841 else
3843 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3844 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3848 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3849 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3851 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3852 struct glsl_src_param src_param;
3853 const char *instruction;
3854 DWORD write_mask;
3855 unsigned i;
3857 /* Determine the GLSL function to use based on the opcode */
3858 /* TODO: Possibly make this a table for faster lookups */
3859 switch (ins->handler_idx)
3861 case WINED3DSIH_ABS: instruction = "abs"; break;
3862 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
3863 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
3864 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3865 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3866 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3867 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3868 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3869 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3870 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
3871 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
3872 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
3873 case WINED3DSIH_FRC: instruction = "fract"; break;
3874 case WINED3DSIH_IMAX: instruction = "max"; break;
3875 case WINED3DSIH_IMIN: instruction = "min"; break;
3876 case WINED3DSIH_MAX: instruction = "max"; break;
3877 case WINED3DSIH_MIN: instruction = "min"; break;
3878 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3879 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3880 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3881 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3882 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3883 case WINED3DSIH_UMAX: instruction = "max"; break;
3884 case WINED3DSIH_UMIN: instruction = "min"; break;
3885 default: instruction = "";
3886 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3887 break;
3890 write_mask = shader_glsl_append_dst(buffer, ins);
3892 /* In D3D bits are numbered from the most significant bit. */
3893 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
3894 shader_addline(buffer, "31 - ");
3895 shader_addline(buffer, "%s(", instruction);
3897 if (ins->src_count)
3899 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3900 shader_addline(buffer, "%s", src_param.param_str);
3901 for (i = 1; i < ins->src_count; ++i)
3903 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3904 shader_addline(buffer, ", %s", src_param.param_str);
3908 shader_addline(buffer, "));\n");
3911 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
3913 struct wined3d_shader_dst_param dst;
3914 struct glsl_src_param src;
3915 DWORD write_mask;
3916 const char *fmt;
3917 unsigned int i;
3919 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
3920 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3922 dst = ins->dst[0];
3923 for (i = 0; i < 4; ++i)
3925 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3926 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3927 &dst, dst.reg.data_type)))
3928 continue;
3930 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
3931 shader_addline(ins->ctx->buffer, fmt, src.param_str);
3935 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
3937 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3938 struct wined3d_shader_dst_param dst;
3939 struct glsl_src_param src[4];
3940 const char *instruction;
3941 unsigned int i, j;
3942 DWORD write_mask;
3944 switch (ins->handler_idx)
3946 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
3947 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
3948 default:
3949 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3950 return;
3953 dst = ins->dst[0];
3954 for (i = 0; i < 4; ++i)
3956 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3957 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3958 &dst, dst.reg.data_type)))
3959 continue;
3961 for (j = 0; j < ins->src_count; ++j)
3962 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
3963 shader_addline(buffer, "%s(", instruction);
3964 for (j = 0; j < ins->src_count - 2; ++j)
3965 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
3966 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
3970 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3972 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3974 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3975 struct glsl_src_param src_param;
3976 unsigned int mask_size;
3977 DWORD write_mask;
3978 char dst_mask[6];
3980 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3981 mask_size = shader_glsl_get_write_mask_size(write_mask);
3982 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3984 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3985 src_param.param_str, src_param.param_str);
3986 shader_glsl_append_dst(buffer, ins);
3988 if (mask_size > 1)
3990 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3991 mask_size, src_param.param_str);
3993 else
3995 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3996 src_param.param_str);
4000 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4002 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4003 ins->ctx->reg_maps->shader_version.minor);
4004 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4005 struct glsl_src_param src0_param;
4006 const char *prefix, *suffix;
4007 unsigned int dst_size;
4008 DWORD dst_write_mask;
4010 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4011 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4013 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4014 dst_write_mask = WINED3DSP_WRITEMASK_3;
4016 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4018 switch (ins->handler_idx)
4020 case WINED3DSIH_EXP:
4021 case WINED3DSIH_EXPP:
4022 prefix = "exp2(";
4023 suffix = ")";
4024 break;
4026 case WINED3DSIH_LOG:
4027 case WINED3DSIH_LOGP:
4028 prefix = "log2(abs(";
4029 suffix = "))";
4030 break;
4032 case WINED3DSIH_RCP:
4033 prefix = "1.0 / ";
4034 suffix = "";
4035 break;
4037 case WINED3DSIH_RSQ:
4038 prefix = "inversesqrt(abs(";
4039 suffix = "))";
4040 break;
4042 default:
4043 prefix = "";
4044 suffix = "";
4045 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4046 break;
4049 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4050 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4051 else
4052 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4055 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4056 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4057 * dst.x = 2^(floor(src))
4058 * dst.y = src - floor(src)
4059 * dst.z = 2^src (partial precision is allowed, but optional)
4060 * dst.w = 1.0;
4061 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4062 * dst = 2^src; (partial precision is allowed, but optional)
4064 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4066 if (ins->ctx->reg_maps->shader_version.major < 2)
4068 struct glsl_src_param src_param;
4069 char dst_mask[6];
4071 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4073 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4074 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4075 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4076 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4078 shader_glsl_append_dst(ins->ctx->buffer, ins);
4079 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4080 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4081 return;
4084 shader_glsl_scalar_op(ins);
4087 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4088 const char *vector_constructor, const char *scalar_constructor)
4090 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4091 struct glsl_src_param src_param;
4092 unsigned int mask_size;
4093 DWORD write_mask;
4095 write_mask = shader_glsl_append_dst(buffer, ins);
4096 mask_size = shader_glsl_get_write_mask_size(write_mask);
4097 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4099 if (mask_size > 1)
4100 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4101 else
4102 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4105 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4107 shader_glsl_cast(ins, "ivec", "int");
4110 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4112 shader_glsl_cast(ins, "uvec", "uint");
4115 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4117 shader_glsl_cast(ins, "vec", "float");
4120 /** Process signed comparison opcodes in GLSL. */
4121 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4123 struct glsl_src_param src0_param;
4124 struct glsl_src_param src1_param;
4125 DWORD write_mask;
4126 unsigned int mask_size;
4128 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4129 mask_size = shader_glsl_get_write_mask_size(write_mask);
4130 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4131 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4133 if (mask_size > 1) {
4134 const char *compare;
4136 switch(ins->handler_idx)
4138 case WINED3DSIH_SLT: compare = "lessThan"; break;
4139 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4140 default: compare = "";
4141 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4144 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4145 src0_param.param_str, src1_param.param_str);
4146 } else {
4147 switch(ins->handler_idx)
4149 case WINED3DSIH_SLT:
4150 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4151 * to return 0.0 but step returns 1.0 because step is not < x
4152 * An alternative is a bvec compare padded with an unused second component.
4153 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4154 * issue. Playing with not() is not possible either because not() does not accept
4155 * a scalar.
4157 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4158 src0_param.param_str, src1_param.param_str);
4159 break;
4160 case WINED3DSIH_SGE:
4161 /* Here we can use the step() function and safe a conditional */
4162 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4163 break;
4164 default:
4165 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4171 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4173 const char *condition_prefix, *condition_suffix;
4174 struct wined3d_shader_dst_param dst;
4175 struct glsl_src_param src0_param;
4176 struct glsl_src_param src1_param;
4177 struct glsl_src_param src2_param;
4178 BOOL temp_destination = FALSE;
4179 DWORD cmp_channel = 0;
4180 unsigned int i, j;
4181 char mask_char[6];
4182 DWORD write_mask;
4184 switch (ins->handler_idx)
4186 case WINED3DSIH_CMP:
4187 condition_prefix = "";
4188 condition_suffix = " >= 0.0";
4189 break;
4191 case WINED3DSIH_CND:
4192 condition_prefix = "";
4193 condition_suffix = " > 0.5";
4194 break;
4196 case WINED3DSIH_MOVC:
4197 condition_prefix = "bool(";
4198 condition_suffix = ")";
4199 break;
4201 default:
4202 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4203 condition_prefix = "<unhandled prefix>";
4204 condition_suffix = "<unhandled suffix>";
4205 break;
4208 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4210 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4211 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4212 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4213 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4215 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4216 condition_prefix, src0_param.param_str, condition_suffix,
4217 src1_param.param_str, src2_param.param_str);
4218 return;
4221 dst = ins->dst[0];
4223 /* Splitting the instruction up in multiple lines imposes a problem:
4224 * The first lines may overwrite source parameters of the following lines.
4225 * Deal with that by using a temporary destination register if needed. */
4226 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4227 && ins->src[0].reg.type == dst.reg.type)
4228 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4229 && ins->src[1].reg.type == dst.reg.type)
4230 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4231 && ins->src[2].reg.type == dst.reg.type))
4232 temp_destination = TRUE;
4234 /* Cycle through all source0 channels. */
4235 for (i = 0; i < 4; ++i)
4237 write_mask = 0;
4238 /* Find the destination channels which use the current source0 channel. */
4239 for (j = 0; j < 4; ++j)
4241 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4243 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4244 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4247 dst.write_mask = ins->dst[0].write_mask & write_mask;
4249 if (temp_destination)
4251 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4252 continue;
4253 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4255 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4256 continue;
4258 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4259 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4260 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4262 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4263 condition_prefix, src0_param.param_str, condition_suffix,
4264 src1_param.param_str, src2_param.param_str);
4267 if (temp_destination)
4269 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4270 shader_glsl_append_dst(ins->ctx->buffer, ins);
4271 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4275 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4276 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4277 * the compare is done per component of src0. */
4278 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4280 struct glsl_src_param src0_param;
4281 struct glsl_src_param src1_param;
4282 struct glsl_src_param src2_param;
4283 DWORD write_mask;
4284 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4285 ins->ctx->reg_maps->shader_version.minor);
4287 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4289 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4290 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4291 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4292 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4294 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4295 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4296 else
4297 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4298 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4299 return;
4302 shader_glsl_conditional_move(ins);
4305 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4306 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4308 struct glsl_src_param src0_param;
4309 struct glsl_src_param src1_param;
4310 struct glsl_src_param src2_param;
4311 DWORD write_mask;
4313 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4314 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4315 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4316 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4317 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4318 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4321 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4322 Vertex shaders to GLSL codes */
4323 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4325 int i;
4326 int nComponents = 0;
4327 struct wined3d_shader_dst_param tmp_dst = {{0}};
4328 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4329 struct wined3d_shader_instruction tmp_ins;
4331 memset(&tmp_ins, 0, sizeof(tmp_ins));
4333 /* Set constants for the temporary argument */
4334 tmp_ins.ctx = ins->ctx;
4335 tmp_ins.dst_count = 1;
4336 tmp_ins.dst = &tmp_dst;
4337 tmp_ins.src_count = 2;
4338 tmp_ins.src = tmp_src;
4340 switch(ins->handler_idx)
4342 case WINED3DSIH_M4x4:
4343 nComponents = 4;
4344 tmp_ins.handler_idx = WINED3DSIH_DP4;
4345 break;
4346 case WINED3DSIH_M4x3:
4347 nComponents = 3;
4348 tmp_ins.handler_idx = WINED3DSIH_DP4;
4349 break;
4350 case WINED3DSIH_M3x4:
4351 nComponents = 4;
4352 tmp_ins.handler_idx = WINED3DSIH_DP3;
4353 break;
4354 case WINED3DSIH_M3x3:
4355 nComponents = 3;
4356 tmp_ins.handler_idx = WINED3DSIH_DP3;
4357 break;
4358 case WINED3DSIH_M3x2:
4359 nComponents = 2;
4360 tmp_ins.handler_idx = WINED3DSIH_DP3;
4361 break;
4362 default:
4363 break;
4366 tmp_dst = ins->dst[0];
4367 tmp_src[0] = ins->src[0];
4368 tmp_src[1] = ins->src[1];
4369 for (i = 0; i < nComponents; ++i)
4371 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4372 shader_glsl_dot(&tmp_ins);
4373 ++tmp_src[1].reg.idx[0].offset;
4378 The LRP instruction performs a component-wise linear interpolation
4379 between the second and third operands using the first operand as the
4380 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4381 This is equivalent to mix(src2, src1, src0);
4383 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4385 struct glsl_src_param src0_param;
4386 struct glsl_src_param src1_param;
4387 struct glsl_src_param src2_param;
4388 DWORD write_mask;
4390 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4392 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4393 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4394 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4396 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4397 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4400 /** Process the WINED3DSIO_LIT instruction in GLSL:
4401 * dst.x = dst.w = 1.0
4402 * dst.y = (src0.x > 0) ? src0.x
4403 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4404 * where src.w is clamped at +- 128
4406 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4408 struct glsl_src_param src0_param;
4409 struct glsl_src_param src1_param;
4410 struct glsl_src_param src3_param;
4411 char dst_mask[6];
4413 shader_glsl_append_dst(ins->ctx->buffer, ins);
4414 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4416 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4417 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4418 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4420 /* The sdk specifies the instruction like this
4421 * dst.x = 1.0;
4422 * if(src.x > 0.0) dst.y = src.x
4423 * else dst.y = 0.0.
4424 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4425 * else dst.z = 0.0;
4426 * dst.w = 1.0;
4427 * (where power = src.w clamped between -128 and 128)
4429 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4430 * dst.x = 1.0 ... No further explanation needed
4431 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4432 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4433 * dst.w = 1.0. ... Nothing fancy.
4435 * So we still have one conditional in there. So do this:
4436 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4438 * 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),
4439 * 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.
4440 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4442 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4443 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4444 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4446 shader_addline(ins->ctx->buffer,
4447 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4448 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4449 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4450 src0_param.param_str, src3_param.param_str, dst_mask);
4453 /** Process the WINED3DSIO_DST instruction in GLSL:
4454 * dst.x = 1.0
4455 * dst.y = src0.x * src0.y
4456 * dst.z = src0.z
4457 * dst.w = src1.w
4459 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4461 struct glsl_src_param src0y_param;
4462 struct glsl_src_param src0z_param;
4463 struct glsl_src_param src1y_param;
4464 struct glsl_src_param src1w_param;
4465 char dst_mask[6];
4467 shader_glsl_append_dst(ins->ctx->buffer, ins);
4468 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4470 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4471 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4472 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4473 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4475 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4476 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4479 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4480 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4481 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4483 * dst.x = cos(src0.?)
4484 * dst.y = sin(src0.?)
4485 * dst.z = dst.z
4486 * dst.w = dst.w
4488 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4490 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4491 struct glsl_src_param src0_param;
4492 DWORD write_mask;
4494 if (ins->ctx->reg_maps->shader_version.major < 4)
4496 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4498 write_mask = shader_glsl_append_dst(buffer, ins);
4499 switch (write_mask)
4501 case WINED3DSP_WRITEMASK_0:
4502 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4503 break;
4505 case WINED3DSP_WRITEMASK_1:
4506 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4507 break;
4509 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4510 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4511 src0_param.param_str, src0_param.param_str);
4512 break;
4514 default:
4515 ERR("Write mask should be .x, .y or .xy\n");
4516 break;
4519 return;
4522 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4525 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4527 char dst_mask[6];
4529 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4530 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4531 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4533 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4534 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4535 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4537 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4538 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4540 else
4542 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4543 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4544 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4547 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4549 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4550 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4551 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4555 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4556 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4557 * generate invalid code
4559 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4561 struct glsl_src_param src0_param;
4562 DWORD write_mask;
4564 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4565 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4567 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4570 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4571 * Start a for() loop where src1.y is the initial value of aL,
4572 * increment aL by src1.z for a total of src1.x iterations.
4573 * Need to use a temporary variable for this operation.
4575 /* FIXME: I don't think nested loops will work correctly this way. */
4576 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4578 struct wined3d_shader_parser_state *state = ins->ctx->state;
4579 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4580 const struct wined3d_shader *shader = ins->ctx->shader;
4581 const struct wined3d_shader_lconst *constant;
4582 struct glsl_src_param src1_param;
4583 const DWORD *control_values = NULL;
4585 if (ins->ctx->reg_maps->shader_version.major < 4)
4587 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4589 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4590 * class hardware doesn't support real varying indexing, but Microsoft
4591 * designed this feature for Shader model 2.x+. If the loop control is
4592 * known at compile time, the GLSL compiler can unroll the loop, and
4593 * replace indirect addressing with direct addressing. */
4594 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4596 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4598 if (constant->idx == ins->src[1].reg.idx[0].offset)
4600 control_values = constant->value;
4601 break;
4606 if (control_values)
4608 struct wined3d_shader_loop_control loop_control;
4609 loop_control.count = control_values[0];
4610 loop_control.start = control_values[1];
4611 loop_control.step = (int)control_values[2];
4613 if (loop_control.step > 0)
4615 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4616 state->current_loop_depth, loop_control.start,
4617 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4618 state->current_loop_depth, loop_control.step);
4620 else if (loop_control.step < 0)
4622 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4623 state->current_loop_depth, loop_control.start,
4624 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4625 state->current_loop_depth, loop_control.step);
4627 else
4629 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4630 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4631 state->current_loop_depth, loop_control.count,
4632 state->current_loop_depth);
4635 else
4637 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4638 state->current_loop_depth, state->current_loop_reg,
4639 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4640 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4643 ++state->current_loop_reg;
4645 else
4647 shader_addline(buffer, "for (;;)\n{\n");
4650 ++state->current_loop_depth;
4653 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4655 struct wined3d_shader_parser_state *state = ins->ctx->state;
4657 shader_addline(ins->ctx->buffer, "}\n");
4659 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4661 --state->current_loop_depth;
4662 --state->current_loop_reg;
4665 if (ins->handler_idx == WINED3DSIH_ENDREP)
4667 --state->current_loop_depth;
4671 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4673 struct wined3d_shader_parser_state *state = ins->ctx->state;
4674 const struct wined3d_shader *shader = ins->ctx->shader;
4675 const struct wined3d_shader_lconst *constant;
4676 struct glsl_src_param src0_param;
4677 const DWORD *control_values = NULL;
4679 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4680 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4682 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4684 if (constant->idx == ins->src[0].reg.idx[0].offset)
4686 control_values = constant->value;
4687 break;
4692 if (control_values)
4694 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4695 state->current_loop_depth, state->current_loop_depth,
4696 control_values[0], state->current_loop_depth);
4698 else
4700 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4701 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4702 state->current_loop_depth, state->current_loop_depth,
4703 src0_param.param_str, state->current_loop_depth);
4706 ++state->current_loop_depth;
4709 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4711 struct glsl_src_param src0_param;
4713 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4714 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4717 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4719 struct glsl_src_param src0_param;
4721 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4722 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4725 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4727 shader_addline(ins->ctx->buffer, "default:\n");
4730 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4732 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4733 struct glsl_src_param src0_param;
4735 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4736 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4739 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4741 struct glsl_src_param src0_param;
4742 struct glsl_src_param src1_param;
4744 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4745 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4747 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4748 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4751 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4753 shader_addline(ins->ctx->buffer, "} else {\n");
4756 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4758 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4760 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4761 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
4762 shader_glsl_fixup_position(ins->ctx->buffer);
4764 if (!stream)
4765 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4766 else
4767 FIXME("Unhandled primitive stream %u.\n", stream);
4770 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4772 shader_addline(ins->ctx->buffer, "break;\n");
4775 /* FIXME: According to MSDN the compare is done per component. */
4776 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4778 struct glsl_src_param src0_param;
4779 struct glsl_src_param src1_param;
4781 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4782 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4784 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4785 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4788 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4790 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4791 struct glsl_src_param src_param;
4793 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4794 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4797 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4799 shader_addline(ins->ctx->buffer, "continue;\n");
4802 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4804 shader_addline(ins->ctx->buffer, "}\n");
4805 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4807 /* Subroutines appear at the end of the shader. */
4808 ins->ctx->state->in_subroutine = TRUE;
4811 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4813 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4816 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4818 struct glsl_src_param src1_param;
4820 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4821 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4822 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4825 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4827 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
4829 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
4831 shader_glsl_generate_shader_epilogue(ins->ctx);
4832 shader_addline(ins->ctx->buffer, "return;\n");
4836 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4838 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4839 ins->ctx->reg_maps->shader_version.minor);
4840 struct glsl_sample_function sample_function;
4841 DWORD sample_flags = 0;
4842 DWORD resource_idx;
4843 DWORD mask = 0, swizzle;
4844 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4846 /* 1.0-1.4: Use destination register as sampler source.
4847 * 2.0+: Use provided sampler source. */
4848 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4849 resource_idx = ins->dst[0].reg.idx[0].offset;
4850 else
4851 resource_idx = ins->src[1].reg.idx[0].offset;
4853 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4855 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4856 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4857 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4859 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4860 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4862 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4863 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4865 case WINED3D_TTFF_COUNT1:
4866 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4867 break;
4868 case WINED3D_TTFF_COUNT2:
4869 mask = WINED3DSP_WRITEMASK_1;
4870 break;
4871 case WINED3D_TTFF_COUNT3:
4872 mask = WINED3DSP_WRITEMASK_2;
4873 break;
4874 case WINED3D_TTFF_COUNT4:
4875 case WINED3D_TTFF_DISABLE:
4876 mask = WINED3DSP_WRITEMASK_3;
4877 break;
4881 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4883 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4885 if (src_mod == WINED3DSPSM_DZ) {
4886 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4887 mask = WINED3DSP_WRITEMASK_2;
4888 } else if (src_mod == WINED3DSPSM_DW) {
4889 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4890 mask = WINED3DSP_WRITEMASK_3;
4893 else
4895 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4896 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4898 /* ps 2.0 texldp instruction always divides by the fourth component. */
4899 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4900 mask = WINED3DSP_WRITEMASK_3;
4904 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4905 mask |= sample_function.coord_mask;
4906 sample_function.coord_mask = mask;
4908 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4909 else swizzle = ins->src[1].swizzle;
4911 /* 1.0-1.3: Use destination register as coordinate source.
4912 1.4+: Use provided coordinate source register. */
4913 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4915 char coord_mask[6];
4916 shader_glsl_write_mask_to_str(mask, coord_mask);
4917 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4918 "T%u%s", resource_idx, coord_mask);
4920 else
4922 struct glsl_src_param coord_param;
4923 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4924 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4926 struct glsl_src_param bias;
4927 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4928 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4929 NULL, "%s", coord_param.param_str);
4930 } else {
4931 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4932 "%s", coord_param.param_str);
4935 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4938 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4940 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4941 struct glsl_src_param coord_param, dx_param, dy_param;
4942 struct glsl_sample_function sample_function;
4943 DWORD sampler_idx;
4944 DWORD swizzle = ins->src[1].swizzle;
4946 if (!shader_glsl_has_core_grad(gl_info, &ins->ctx->shader->reg_maps.shader_version)
4947 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4949 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4950 shader_glsl_tex(ins);
4951 return;
4954 sampler_idx = ins->src[1].reg.idx[0].offset;
4956 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4957 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4958 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4959 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4961 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4962 NULL, NULL, "%s", coord_param.param_str);
4963 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4966 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4968 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
4969 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4970 struct glsl_src_param coord_param, lod_param;
4971 struct glsl_sample_function sample_function;
4972 DWORD swizzle = ins->src[1].swizzle;
4973 DWORD sampler_idx;
4975 sampler_idx = ins->src[1].reg.idx[0].offset;
4977 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4978 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4980 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4982 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info, shader_version)
4983 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4985 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4986 * However, the NVIDIA drivers allow them in fragment shaders as well,
4987 * even without the appropriate extension. */
4988 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4990 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4991 "%s", coord_param.param_str);
4992 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4995 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4996 unsigned int resource_idx, unsigned int sampler_idx)
4998 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4999 unsigned int i;
5001 for (i = 0; i < sampler_map->count; ++i)
5003 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5004 return entries[i].bind_idx;
5007 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5009 return ~0u;
5012 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5014 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5015 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5016 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5017 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5018 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5019 struct glsl_src_param structure_idx, offset, data, data2;
5020 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5021 enum wined3d_shader_resource_type resource_type;
5022 struct wined3d_string_buffer *address;
5023 enum wined3d_data_type data_type;
5024 unsigned int resource_idx, stride;
5025 const char *op, *resource;
5026 DWORD coord_mask;
5027 BOOL is_tgsm;
5029 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5030 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5031 if (is_tgsm)
5033 if (resource_idx >= reg_maps->tgsm_count)
5035 ERR("Invalid TGSM index %u.\n", resource_idx);
5036 return;
5038 resource = "g";
5039 data_type = WINED3D_DATA_UINT;
5040 coord_mask = 1;
5041 stride = reg_maps->tgsm[resource_idx].stride;
5043 else
5045 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5047 ERR("Invalid UAV index %u.\n", resource_idx);
5048 return;
5050 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5051 if (resource_type >= ARRAY_SIZE(resource_type_info))
5053 ERR("Unexpected resource type %#x.\n", resource_type);
5054 return;
5056 resource = "image";
5057 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5058 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5059 stride = reg_maps->uav_resource_info[resource_idx].stride;
5062 switch (ins->handler_idx)
5064 case WINED3DSIH_ATOMIC_AND:
5065 case WINED3DSIH_IMM_ATOMIC_AND:
5066 if (is_tgsm)
5067 op = "atomicAnd";
5068 else
5069 op = "imageAtomicAnd";
5070 break;
5071 case WINED3DSIH_ATOMIC_CMP_STORE:
5072 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5073 if (is_tgsm)
5074 op = "atomicCompSwap";
5075 else
5076 op = "imageAtomicCompSwap";
5077 break;
5078 case WINED3DSIH_ATOMIC_IADD:
5079 case WINED3DSIH_IMM_ATOMIC_IADD:
5080 if (is_tgsm)
5081 op = "atomicAdd";
5082 else
5083 op = "imageAtomicAdd";
5084 break;
5085 case WINED3DSIH_ATOMIC_IMAX:
5086 case WINED3DSIH_IMM_ATOMIC_IMAX:
5087 if (is_tgsm)
5088 op = "atomicMax";
5089 else
5090 op = "imageAtomicMax";
5091 if (data_type != WINED3D_DATA_INT)
5093 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5094 return;
5096 break;
5097 case WINED3DSIH_ATOMIC_IMIN:
5098 case WINED3DSIH_IMM_ATOMIC_IMIN:
5099 if (is_tgsm)
5100 op = "atomicMin";
5101 else
5102 op = "imageAtomicMin";
5103 if (data_type != WINED3D_DATA_INT)
5105 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5106 return;
5108 break;
5109 case WINED3DSIH_ATOMIC_OR:
5110 case WINED3DSIH_IMM_ATOMIC_OR:
5111 if (is_tgsm)
5112 op = "atomicOr";
5113 else
5114 op = "imageAtomicOr";
5115 break;
5116 case WINED3DSIH_ATOMIC_UMAX:
5117 case WINED3DSIH_IMM_ATOMIC_UMAX:
5118 if (is_tgsm)
5119 op = "atomicMax";
5120 else
5121 op = "imageAtomicMax";
5122 if (data_type != WINED3D_DATA_UINT)
5124 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5125 return;
5127 break;
5128 case WINED3DSIH_ATOMIC_UMIN:
5129 case WINED3DSIH_IMM_ATOMIC_UMIN:
5130 if (is_tgsm)
5131 op = "atomicMin";
5132 else
5133 op = "imageAtomicMin";
5134 if (data_type != WINED3D_DATA_UINT)
5136 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5137 return;
5139 break;
5140 case WINED3DSIH_ATOMIC_XOR:
5141 case WINED3DSIH_IMM_ATOMIC_XOR:
5142 if (is_tgsm)
5143 op = "atomicXor";
5144 else
5145 op = "imageAtomicXor";
5146 break;
5147 case WINED3DSIH_IMM_ATOMIC_EXCH:
5148 if (is_tgsm)
5149 op = "atomicExchange";
5150 else
5151 op = "imageAtomicExchange";
5152 break;
5153 default:
5154 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5155 return;
5158 address = string_buffer_get(priv->string_buffers);
5159 if (stride)
5161 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5162 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5163 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5165 else
5167 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5168 string_buffer_sprintf(address, "%s", offset.param_str);
5169 if (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW)
5170 shader_addline(address, "/ 4");
5173 if (is_imm_instruction)
5174 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5176 if (is_tgsm)
5177 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5178 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5179 else
5180 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5181 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5183 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5184 shader_addline(buffer, "%s", data.param_str);
5185 if (ins->src_count >= 3)
5187 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5188 shader_addline(buffer, ", %s", data2.param_str);
5191 if (is_imm_instruction)
5192 shader_addline(buffer, ")");
5193 shader_addline(buffer, ");\n");
5195 string_buffer_release(priv->string_buffers, address);
5198 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5200 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5201 const char *op;
5203 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5204 op = "atomicCounterIncrement";
5205 else
5206 op = "atomicCounterDecrement";
5208 shader_glsl_append_dst(ins->ctx->buffer, ins);
5209 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5212 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5214 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5215 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5216 enum wined3d_shader_resource_type resource_type;
5217 struct glsl_src_param image_coord_param;
5218 enum wined3d_data_type data_type;
5219 DWORD coord_mask, write_mask;
5220 unsigned int uav_idx;
5221 char dst_swizzle[6];
5223 uav_idx = ins->src[1].reg.idx[0].offset;
5224 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5226 ERR("Invalid UAV index %u.\n", uav_idx);
5227 return;
5229 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5230 if (resource_type >= ARRAY_SIZE(resource_type_info))
5232 ERR("Unexpected resource type %#x.\n", resource_type);
5233 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5235 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5236 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5238 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5239 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5241 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5242 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5243 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5246 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5248 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5249 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5250 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5251 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5252 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5253 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5254 struct glsl_src_param structure_idx, offset;
5255 struct wined3d_string_buffer *address;
5256 struct wined3d_shader_dst_param dst;
5257 const char *function, *resource;
5259 resource_idx = src->reg.idx[0].offset;
5260 if (src->reg.type == WINED3DSPR_RESOURCE)
5262 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5264 ERR("Invalid resource index %u.\n", resource_idx);
5265 return;
5267 stride = reg_maps->resource_info[resource_idx].stride;
5268 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5269 function = "texelFetch";
5270 resource = "sampler";
5272 else if (src->reg.type == WINED3DSPR_UAV)
5274 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5276 ERR("Invalid UAV index %u.\n", resource_idx);
5277 return;
5279 stride = reg_maps->uav_resource_info[resource_idx].stride;
5280 bind_idx = resource_idx;
5281 function = "imageLoad";
5282 resource = "image";
5284 else
5286 if (resource_idx >= reg_maps->tgsm_count)
5288 ERR("Invalid TGSM index %u.\n", resource_idx);
5289 return;
5291 stride = reg_maps->tgsm[resource_idx].stride;
5292 bind_idx = resource_idx;
5293 function = NULL;
5294 resource = "g";
5297 address = string_buffer_get(priv->string_buffers);
5298 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5300 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5301 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5303 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5304 shader_addline(address, "%s / 4", offset.param_str);
5306 dst = ins->dst[0];
5307 for (i = 0; i < 4; ++i)
5309 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5310 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5311 continue;
5313 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5314 if (function)
5315 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5316 function, prefix, resource, bind_idx, address->buffer, swizzle);
5317 else
5318 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5319 prefix, resource, bind_idx, address->buffer, swizzle);
5322 string_buffer_release(priv->string_buffers, address);
5325 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5327 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5328 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5329 struct glsl_src_param image_coord_param, image_data_param;
5330 enum wined3d_shader_resource_type resource_type;
5331 enum wined3d_data_type data_type;
5332 unsigned int uav_idx;
5333 DWORD coord_mask;
5335 uav_idx = ins->dst[0].reg.idx[0].offset;
5336 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5338 ERR("Invalid UAV index %u.\n", uav_idx);
5339 return;
5341 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5342 if (resource_type >= ARRAY_SIZE(resource_type_info))
5344 ERR("Unexpected resource type %#x.\n", resource_type);
5345 return;
5347 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5348 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5350 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5351 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5352 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5353 shader_glsl_get_prefix(version->type), uav_idx,
5354 image_coord_param.param_str, image_data_param.param_str);
5357 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5359 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5360 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5361 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5362 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5363 struct glsl_src_param structure_idx, offset, data;
5364 unsigned int i, resource_idx, stride, src_idx = 0;
5365 struct wined3d_string_buffer *address;
5366 DWORD write_mask;
5367 BOOL is_tgsm;
5369 resource_idx = ins->dst[0].reg.idx[0].offset;
5370 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5371 if (is_tgsm)
5373 if (resource_idx >= reg_maps->tgsm_count)
5375 ERR("Invalid TGSM index %u.\n", resource_idx);
5376 return;
5378 stride = reg_maps->tgsm[resource_idx].stride;
5380 else
5382 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5384 ERR("Invalid UAV index %u.\n", resource_idx);
5385 return;
5387 stride = reg_maps->uav_resource_info[resource_idx].stride;
5390 address = string_buffer_get(priv->string_buffers);
5391 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5393 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5394 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5396 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5397 shader_addline(address, "%s / 4", offset.param_str);
5399 for (i = 0; i < 4; ++i)
5401 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5402 continue;
5404 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5406 if (is_tgsm)
5407 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5408 prefix, resource_idx, address->buffer, i, data.param_str);
5409 else
5410 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5411 prefix, resource_idx, address->buffer, i, data.param_str);
5414 string_buffer_release(priv->string_buffers, address);
5417 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5419 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5420 unsigned int sync_flags = ins->flags;
5422 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5424 shader_addline(buffer, "barrier();\n");
5425 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5428 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5430 shader_addline(buffer, "memoryBarrierShared();\n");
5431 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5434 if (sync_flags)
5435 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5438 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5439 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5441 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5442 unsigned int idx = reg->idx[0].offset;
5444 if (reg->type == WINED3DSPR_RESOURCE)
5446 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5448 ERR("Invalid resource index %u.\n", idx);
5449 return NULL;
5451 return &reg_maps->resource_info[idx];
5454 if (reg->type == WINED3DSPR_UAV)
5456 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5458 ERR("Invalid UAV index %u.\n", idx);
5459 return NULL;
5461 return &reg_maps->uav_resource_info[idx];
5464 FIXME("Unhandled register type %#x.\n", reg->type);
5465 return NULL;
5468 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5470 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5471 const struct wined3d_shader_resource_info *resource_info;
5472 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5473 unsigned int resource_idx;
5474 char dst_swizzle[6];
5475 DWORD write_mask;
5477 write_mask = shader_glsl_append_dst(buffer, ins);
5478 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5480 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5481 return;
5482 resource_idx = ins->src[0].reg.idx[0].offset;
5484 shader_addline(buffer, "ivec2(");
5485 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5487 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5488 resource_idx, WINED3D_SAMPLER_DEFAULT);
5489 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5491 else
5493 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5495 if (resource_info->stride)
5496 shader_addline(buffer, " / %u", resource_info->stride);
5497 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5498 shader_addline(buffer, " * 4");
5499 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5502 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5504 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5505 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5506 enum wined3d_shader_resource_type resource_type;
5507 enum wined3d_shader_register_type reg_type;
5508 unsigned int resource_idx, bind_idx, i;
5509 enum wined3d_data_type dst_data_type;
5510 struct glsl_src_param lod_param;
5511 char dst_swizzle[6];
5512 DWORD write_mask;
5514 dst_data_type = ins->dst[0].reg.data_type;
5515 if (ins->flags == WINED3DSI_RESINFO_UINT)
5516 dst_data_type = WINED3D_DATA_UINT;
5517 else if (ins->flags)
5518 FIXME("Unhandled flags %#x.\n", ins->flags);
5520 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5521 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5523 reg_type = ins->src[1].reg.type;
5524 resource_idx = ins->src[1].reg.idx[0].offset;
5525 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5526 if (reg_type == WINED3DSPR_RESOURCE)
5528 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5529 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5530 resource_idx, WINED3D_SAMPLER_DEFAULT);
5532 else
5534 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5535 bind_idx = resource_idx;
5538 if (resource_type >= ARRAY_SIZE(resource_type_info))
5540 ERR("Unexpected resource type %#x.\n", resource_type);
5541 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5544 if (dst_data_type == WINED3D_DATA_UINT)
5545 shader_addline(ins->ctx->buffer, "uvec4(");
5546 else
5547 shader_addline(ins->ctx->buffer, "vec4(");
5549 if (reg_type == WINED3DSPR_RESOURCE)
5551 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5552 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5554 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5555 shader_addline(ins->ctx->buffer, "0, ");
5557 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5559 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5560 shader_glsl_get_prefix(version->type), bind_idx);
5562 else
5564 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5565 shader_addline(ins->ctx->buffer, "1");
5568 else
5570 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5571 shader_glsl_get_prefix(version->type), bind_idx);
5573 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5574 shader_addline(ins->ctx->buffer, "0, ");
5576 /* For UAVs the returned miplevel count is always 1. */
5577 shader_addline(ins->ctx->buffer, "1");
5580 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5583 /* FIXME: The current implementation does not handle multisample textures correctly. */
5584 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5586 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5587 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5588 struct glsl_src_param coord_param, lod_param;
5589 struct glsl_sample_function sample_function;
5590 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5591 BOOL has_lod_param;
5593 if (wined3d_shader_instruction_has_texel_offset(ins))
5594 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5596 resource_idx = ins->src[1].reg.idx[0].offset;
5597 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5599 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5601 ERR("Invalid resource index %u.\n", resource_idx);
5602 return;
5604 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5606 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5607 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5608 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5609 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5610 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5611 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5612 "%s", coord_param.param_str);
5613 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5616 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5618 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5619 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5620 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5621 struct glsl_sample_function sample_function;
5622 DWORD flags = 0;
5624 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5625 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5626 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5627 flags |= WINED3D_GLSL_SAMPLE_LOD;
5628 if (wined3d_shader_instruction_has_texel_offset(ins))
5629 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5631 resource_idx = ins->src[1].reg.idx[0].offset;
5632 sampler_idx = ins->src[2].reg.idx[0].offset;
5634 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5635 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5637 switch (ins->handler_idx)
5639 case WINED3DSIH_SAMPLE:
5640 break;
5641 case WINED3DSIH_SAMPLE_B:
5642 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5643 lod_param_str = lod_param.param_str;
5644 break;
5645 case WINED3DSIH_SAMPLE_GRAD:
5646 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5647 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5648 dx_param_str = dx_param.param_str;
5649 dy_param_str = dy_param.param_str;
5650 break;
5651 case WINED3DSIH_SAMPLE_LOD:
5652 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5653 lod_param_str = lod_param.param_str;
5654 break;
5655 default:
5656 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5657 break;
5660 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5661 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5662 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5663 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5666 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5668 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5669 struct glsl_src_param coord_param, compare_param;
5670 struct glsl_sample_function sample_function;
5671 const char *lod_param = NULL;
5672 DWORD flags = 0;
5673 UINT coord_size;
5675 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5677 lod_param = "0";
5678 flags |= WINED3D_GLSL_SAMPLE_LOD;
5681 if (wined3d_shader_instruction_has_texel_offset(ins))
5682 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5684 resource_idx = ins->src[1].reg.idx[0].offset;
5685 sampler_idx = ins->src[2].reg.idx[0].offset;
5687 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5688 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5689 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5690 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5691 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5692 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5693 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5694 coord_size, coord_param.param_str, compare_param.param_str);
5695 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5698 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
5700 /* FIXME: Make this work for more than just 2D textures */
5701 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5702 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5704 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
5706 char dst_mask[6];
5708 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5709 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5710 ins->dst[0].reg.idx[0].offset, dst_mask);
5712 else
5714 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5715 DWORD reg = ins->src[0].reg.idx[0].offset;
5716 char dst_swizzle[6];
5718 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5720 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
5722 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
5723 struct glsl_src_param div_param;
5724 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
5726 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
5728 if (mask_size > 1)
5729 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
5730 else
5731 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
5733 else
5735 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
5740 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5741 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5742 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5743 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
5745 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5746 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5747 struct glsl_sample_function sample_function;
5748 struct glsl_src_param src0_param;
5749 UINT mask_size;
5751 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5753 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5754 * scalar, and projected sampling would require 4.
5756 * It is a dependent read - not valid with conditional NP2 textures
5758 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5759 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5761 switch(mask_size)
5763 case 1:
5764 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5765 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
5766 break;
5768 case 2:
5769 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5770 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
5771 break;
5773 case 3:
5774 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5775 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
5776 break;
5778 default:
5779 FIXME("Unexpected mask size %u\n", mask_size);
5780 break;
5782 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5785 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5786 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5787 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
5789 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5790 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5791 struct glsl_src_param src0_param;
5792 DWORD dst_mask;
5793 unsigned int mask_size;
5795 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5796 mask_size = shader_glsl_get_write_mask_size(dst_mask);
5797 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5799 if (mask_size > 1) {
5800 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
5801 } else {
5802 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
5806 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5807 * Calculate the depth as dst.x / dst.y */
5808 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
5810 struct glsl_dst_param dst_param;
5812 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5814 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5815 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5816 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5817 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5818 * >= 1.0 or < 0.0
5820 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5821 dst_param.reg_name, dst_param.reg_name);
5824 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5825 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5826 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5827 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5829 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
5831 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5832 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5833 struct glsl_src_param src0_param;
5835 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5837 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
5838 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5841 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5842 * Calculate the 1st of a 2-row matrix multiplication. */
5843 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
5845 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5846 DWORD reg = ins->dst[0].reg.idx[0].offset;
5847 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5848 struct glsl_src_param src0_param;
5850 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5851 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5854 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5855 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5856 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
5858 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5859 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5860 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5861 DWORD reg = ins->dst[0].reg.idx[0].offset;
5862 struct glsl_src_param src0_param;
5864 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5865 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
5866 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
5869 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
5871 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5872 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5873 struct glsl_sample_function sample_function;
5874 DWORD reg = ins->dst[0].reg.idx[0].offset;
5875 struct glsl_src_param src0_param;
5877 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5878 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5880 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5882 /* Sample the texture using the calculated coordinates */
5883 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5884 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5887 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5888 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5889 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5891 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5892 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5893 struct glsl_sample_function sample_function;
5894 DWORD reg = ins->dst[0].reg.idx[0].offset;
5895 struct glsl_src_param src0_param;
5897 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5898 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5900 /* Dependent read, not valid with conditional NP2 */
5901 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5903 /* Sample the texture using the calculated coordinates */
5904 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5905 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5907 tex_mx->current_row = 0;
5910 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5911 * Perform the 3rd row of a 3x3 matrix multiply */
5912 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5914 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5915 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5916 DWORD reg = ins->dst[0].reg.idx[0].offset;
5917 struct glsl_src_param src0_param;
5918 char dst_mask[6];
5920 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5922 shader_glsl_append_dst(ins->ctx->buffer, ins);
5923 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5924 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5926 tex_mx->current_row = 0;
5929 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5930 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5931 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5933 struct glsl_src_param src0_param;
5934 struct glsl_src_param src1_param;
5935 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5936 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5937 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5938 struct glsl_sample_function sample_function;
5939 DWORD reg = ins->dst[0].reg.idx[0].offset;
5940 char coord_mask[6];
5942 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5943 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5945 /* Perform the last matrix multiply operation */
5946 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5947 /* Reflection calculation */
5948 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5950 /* Dependent read, not valid with conditional NP2 */
5951 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5952 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5954 /* Sample the texture */
5955 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5956 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5957 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5959 tex_mx->current_row = 0;
5962 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5963 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5964 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5966 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5967 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5968 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5969 struct glsl_sample_function sample_function;
5970 DWORD reg = ins->dst[0].reg.idx[0].offset;
5971 struct glsl_src_param src0_param;
5972 char coord_mask[6];
5974 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5976 /* Perform the last matrix multiply operation */
5977 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5979 /* Construct the eye-ray vector from w coordinates */
5980 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5981 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5982 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5984 /* Dependent read, not valid with conditional NP2 */
5985 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5986 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5988 /* Sample the texture using the calculated coordinates */
5989 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5990 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5991 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5993 tex_mx->current_row = 0;
5996 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5997 * Apply a fake bump map transform.
5998 * texbem is pshader <= 1.3 only, this saves a few version checks
6000 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6002 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6003 struct glsl_sample_function sample_function;
6004 struct glsl_src_param coord_param;
6005 DWORD sampler_idx;
6006 DWORD mask;
6007 DWORD flags;
6008 char coord_mask[6];
6010 sampler_idx = ins->dst[0].reg.idx[0].offset;
6011 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6012 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6014 /* Dependent read, not valid with conditional NP2 */
6015 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6016 mask = sample_function.coord_mask;
6018 shader_glsl_write_mask_to_str(mask, coord_mask);
6020 /* With projected textures, texbem only divides the static texture coord,
6021 * not the displacement, so we can't let GL handle this. */
6022 if (flags & WINED3D_PSARGS_PROJECTED)
6024 DWORD div_mask=0;
6025 char coord_div_mask[3];
6026 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6028 case WINED3D_TTFF_COUNT1:
6029 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6030 break;
6031 case WINED3D_TTFF_COUNT2:
6032 div_mask = WINED3DSP_WRITEMASK_1;
6033 break;
6034 case WINED3D_TTFF_COUNT3:
6035 div_mask = WINED3DSP_WRITEMASK_2;
6036 break;
6037 case WINED3D_TTFF_COUNT4:
6038 case WINED3D_TTFF_DISABLE:
6039 div_mask = WINED3DSP_WRITEMASK_3;
6040 break;
6042 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6043 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6046 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6048 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6049 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6050 coord_param.param_str, coord_mask);
6052 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6054 struct glsl_src_param luminance_param;
6055 struct glsl_dst_param dst_param;
6057 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6058 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6060 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6061 dst_param.reg_name, dst_param.mask_str,
6062 luminance_param.param_str, sampler_idx, sampler_idx);
6064 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6067 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6069 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6070 struct glsl_src_param src0_param, src1_param;
6072 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6073 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6075 shader_glsl_append_dst(ins->ctx->buffer, ins);
6076 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6077 src0_param.param_str, sampler_idx, src1_param.param_str);
6080 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6081 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6082 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6084 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6085 struct glsl_sample_function sample_function;
6086 struct glsl_src_param src0_param;
6088 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6090 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6091 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6092 "%s.wx", src0_param.reg_name);
6093 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6096 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6097 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6098 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6100 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6101 struct glsl_sample_function sample_function;
6102 struct glsl_src_param src0_param;
6104 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6106 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6107 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6108 "%s.yz", src0_param.reg_name);
6109 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6112 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6113 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6114 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6116 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6117 struct glsl_sample_function sample_function;
6118 struct glsl_src_param src0_param;
6120 /* Dependent read, not valid with conditional NP2 */
6121 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6122 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6124 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6125 "%s", src0_param.param_str);
6126 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6129 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6130 * If any of the first 3 components are < 0, discard this pixel */
6131 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6133 if (ins->ctx->reg_maps->shader_version.major >= 4)
6135 struct glsl_src_param src_param;
6137 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
6138 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
6140 else
6142 struct glsl_dst_param dst_param;
6144 /* The argument is a destination parameter, and no writemasks are allowed */
6145 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6147 /* 2.0 shaders compare all 4 components in texkill. */
6148 if (ins->ctx->reg_maps->shader_version.major >= 2)
6149 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6150 /* 1.x shaders only compare the first 3 components, probably due to
6151 * the nature of the texkill instruction as a tex* instruction, and
6152 * phase, which kills all .w components. Even if all 4 components are
6153 * defined, only the first 3 are used. */
6154 else
6155 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6159 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6160 * dst = dot2(src0, src1) + src2 */
6161 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6163 struct glsl_src_param src0_param;
6164 struct glsl_src_param src1_param;
6165 struct glsl_src_param src2_param;
6166 DWORD write_mask;
6167 unsigned int mask_size;
6169 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6170 mask_size = shader_glsl_get_write_mask_size(write_mask);
6172 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6173 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6174 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6176 if (mask_size > 1) {
6177 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6178 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6179 } else {
6180 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6181 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6185 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6186 const struct wined3d_shader_signature *input_signature,
6187 const struct wined3d_shader_reg_maps *reg_maps,
6188 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
6190 unsigned int i;
6192 for (i = 0; i < input_signature->element_count; ++i)
6194 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6195 const char *semantic_name;
6196 UINT semantic_idx;
6197 char reg_mask[6];
6199 /* Unused */
6200 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6201 continue;
6203 semantic_name = input->semantic_name;
6204 semantic_idx = input->semantic_idx;
6205 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6207 if (args->vp_mode == vertexshader)
6209 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6211 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6212 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6214 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6216 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6218 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6220 shader_addline(buffer, "ps_in[%u] = vec4("
6221 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
6222 input->register_idx);
6224 else
6226 if (input->sysval_semantic)
6227 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6228 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
6229 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6230 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6233 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6235 if (args->pointsprite)
6236 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6237 shader->u.ps.input_reg_map[input->register_idx]);
6238 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6239 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6240 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6241 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
6242 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6243 else
6244 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6245 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6247 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6249 if (!semantic_idx)
6250 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6251 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6252 else if (semantic_idx == 1)
6253 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6254 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6255 else
6256 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6257 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6259 else
6261 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6262 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6267 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6269 struct glsl_program_key key;
6271 key.vs_id = entry->vs.id;
6272 key.gs_id = entry->gs.id;
6273 key.ps_id = entry->ps.id;
6274 key.cs_id = entry->cs.id;
6276 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6278 ERR("Failed to insert program entry.\n");
6282 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6283 const struct glsl_program_key *key)
6285 struct wine_rb_entry *entry;
6287 entry = wine_rb_get(&priv->program_lookup, key);
6288 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6291 /* Context activation is done by the caller. */
6292 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6293 struct glsl_shader_prog_link *entry)
6295 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6297 GL_EXTCALL(glDeleteProgram(entry->id));
6298 if (entry->vs.id)
6299 list_remove(&entry->vs.shader_entry);
6300 if (entry->gs.id)
6301 list_remove(&entry->gs.shader_entry);
6302 if (entry->ps.id)
6303 list_remove(&entry->ps.shader_entry);
6304 if (entry->cs.id)
6305 list_remove(&entry->cs.shader_entry);
6306 HeapFree(GetProcessHeap(), 0, entry);
6309 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6310 const struct wined3d_gl_info *gl_info, const DWORD *map,
6311 const struct wined3d_shader_signature *input_signature,
6312 const struct wined3d_shader_reg_maps *reg_maps_in,
6313 const struct wined3d_shader_signature *output_signature,
6314 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6316 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6317 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6318 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6319 unsigned int in_count = vec4_varyings(3, gl_info);
6320 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
6321 DWORD in_idx, *set = NULL;
6322 unsigned int i, j;
6323 char reg_mask[6];
6325 set = wined3d_calloc(max_varyings, sizeof(*set));
6327 for (i = 0; i < input_signature->element_count; ++i)
6329 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6331 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6332 continue;
6334 in_idx = map[input->register_idx];
6335 /* Declared, but not read register */
6336 if (in_idx == ~0u)
6337 continue;
6338 if (in_idx >= max_varyings)
6340 FIXME("More input varyings declared than supported, expect issues.\n");
6341 continue;
6344 if (in_idx == in_count)
6345 string_buffer_sprintf(destination, "gl_FrontColor");
6346 else if (in_idx == in_count + 1)
6347 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6348 else
6349 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6351 if (!set[in_idx])
6352 set[in_idx] = ~0u;
6354 for (j = 0; j < output_signature->element_count; ++j)
6356 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6357 DWORD mask;
6359 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6360 || input->semantic_idx != output->semantic_idx
6361 || strcmp(input->semantic_name, output->semantic_name)
6362 || !(mask = input->mask & output->mask))
6363 continue;
6365 if (set[in_idx] == ~0u)
6366 set[in_idx] = 0;
6367 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6368 shader_glsl_write_mask_to_str(mask, reg_mask);
6370 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6371 destination->buffer, reg_mask, output->register_idx, reg_mask);
6375 for (i = 0; i < max_varyings; ++i)
6377 unsigned int size;
6379 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6380 continue;
6382 if (set[i] == ~0u)
6383 set[i] = 0;
6385 size = 0;
6386 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6387 reg_mask[size++] = 'x';
6388 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6389 reg_mask[size++] = 'y';
6390 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6391 reg_mask[size++] = 'z';
6392 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6393 reg_mask[size++] = 'w';
6394 reg_mask[size] = '\0';
6396 if (i == in_count)
6397 string_buffer_sprintf(destination, "gl_FrontColor");
6398 else if (i == in_count + 1)
6399 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6400 else
6401 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6403 if (size == 1)
6404 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6405 else
6406 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6409 HeapFree(GetProcessHeap(), 0, set);
6410 string_buffer_release(&priv->string_buffers, destination);
6413 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6414 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6415 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6417 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6418 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6419 char reg_mask[6];
6420 unsigned int i;
6422 for (i = 0; i < output_signature->element_count; ++i)
6424 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6426 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6427 continue;
6429 if (output->register_idx >= input_count)
6430 continue;
6432 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
6434 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6436 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6437 destination->buffer, reg_mask, output->register_idx, reg_mask);
6440 string_buffer_release(&priv->string_buffers, destination);
6443 /* Context activation is done by the caller. */
6444 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
6445 const struct wined3d_shader *vs, unsigned int input_count,
6446 const struct wined3d_gl_info *gl_info)
6448 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6449 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6451 if (legacy_context)
6452 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
6453 else
6454 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
6455 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6457 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
6458 legacy_context ? "gs_in" : "gs_in.gs_in");
6460 shader_addline(buffer, "}\n");
6463 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6464 const struct wined3d_gl_info *gl_info, const DWORD *map,
6465 const struct wined3d_shader_signature *input_signature,
6466 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6467 const struct wined3d_shader_signature *output_signature,
6468 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6470 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6471 const char *semantic_name;
6472 UINT semantic_idx;
6473 char reg_mask[6];
6474 unsigned int i;
6476 /* First, sort out position and point size system values. */
6477 for (i = 0; i < output_signature->element_count; ++i)
6479 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6481 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6482 continue;
6484 semantic_name = output->semantic_name;
6485 semantic_idx = output->semantic_idx;
6486 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6488 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6490 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6491 reg_mask, output->register_idx, reg_mask);
6493 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6495 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6496 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6498 else if (output->sysval_semantic)
6500 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6504 /* Then, setup the pixel shader input. */
6505 if (reg_maps_out->shader_version.major < 4)
6506 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6507 output_signature, reg_maps_out, "ps_link");
6508 else
6509 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
6512 /* Context activation is done by the caller. */
6513 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6514 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6515 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6517 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6518 GLuint ret;
6519 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6520 unsigned int i;
6521 const char *semantic_name;
6522 UINT semantic_idx;
6523 char reg_mask[6];
6524 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6526 string_buffer_clear(buffer);
6528 shader_glsl_add_version_declaration(buffer, gl_info, &vs->reg_maps.shader_version);
6530 if (per_vertex_point_size)
6532 shader_addline(buffer, "uniform struct\n{\n");
6533 shader_addline(buffer, " float size_min;\n");
6534 shader_addline(buffer, " float size_max;\n");
6535 shader_addline(buffer, "} ffp_point;\n");
6538 if (ps_major < 3)
6540 DWORD colors_written_mask[2] = {0};
6541 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
6543 if (!legacy_context)
6545 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
6546 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
6547 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6548 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6551 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6553 for (i = 0; i < vs->output_signature.element_count; ++i)
6555 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
6556 DWORD write_mask;
6558 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
6559 continue;
6561 semantic_name = output->semantic_name;
6562 semantic_idx = output->semantic_idx;
6563 write_mask = output->mask;
6564 shader_glsl_write_mask_to_str(write_mask, reg_mask);
6566 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
6568 if (legacy_context)
6569 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6570 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
6571 else
6572 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6573 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
6575 colors_written_mask[semantic_idx] = write_mask;
6577 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
6579 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6580 reg_mask, output->register_idx, reg_mask);
6582 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6584 if (semantic_idx < MAX_TEXTURES)
6586 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
6587 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
6588 semantic_idx, reg_mask, output->register_idx, reg_mask);
6589 texcoords_written_mask[semantic_idx] = write_mask;
6592 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6594 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6595 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6597 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6599 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6600 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6601 output->register_idx, reg_mask[1]);
6605 for (i = 0; i < 2; ++i)
6607 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6609 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6610 if (!i)
6611 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6612 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
6613 reg_mask, reg_mask);
6614 else
6615 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6616 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6617 reg_mask, reg_mask);
6620 for (i = 0; i < MAX_TEXTURES; ++i)
6622 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6623 continue;
6625 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6627 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6628 && !texcoords_written_mask[i])
6629 continue;
6631 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6632 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6633 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6637 else
6639 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6641 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
6642 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6643 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6644 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6647 shader_addline(buffer, "}\n");
6649 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6650 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6651 shader_glsl_compile(gl_info, ret, buffer->buffer);
6653 return ret;
6656 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
6657 const struct wined3d_shader *shader, unsigned int input_count,
6658 const struct wined3d_gl_info *gl_info)
6660 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6662 if (input_count)
6663 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
6665 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6666 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
6668 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
6669 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
6671 shader_addline(buffer, "}\n");
6674 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
6675 const struct wined3d_gl_info *gl_info)
6677 const char *output = get_fragment_output(gl_info);
6679 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
6680 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6681 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
6682 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
6683 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
6684 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
6687 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
6688 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
6690 const char *output = get_fragment_output(gl_info);
6692 switch (mode)
6694 case WINED3D_FFP_PS_FOG_OFF:
6695 return;
6697 case WINED3D_FFP_PS_FOG_LINEAR:
6698 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6699 break;
6701 case WINED3D_FFP_PS_FOG_EXP:
6702 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6703 break;
6705 case WINED3D_FFP_PS_FOG_EXP2:
6706 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6707 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6708 break;
6710 default:
6711 ERR("Invalid fog mode %#x.\n", mode);
6712 return;
6715 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6716 output, output);
6719 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
6720 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
6722 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6723 * flipping all the operators here, just negate the comparison below. */
6724 static const char * const comparison_operator[] =
6726 "", /* WINED3D_CMP_NEVER */
6727 "<", /* WINED3D_CMP_LESS */
6728 "==", /* WINED3D_CMP_EQUAL */
6729 "<=", /* WINED3D_CMP_LESSEQUAL */
6730 ">", /* WINED3D_CMP_GREATER */
6731 "!=", /* WINED3D_CMP_NOTEQUAL */
6732 ">=", /* WINED3D_CMP_GREATEREQUAL */
6733 "" /* WINED3D_CMP_ALWAYS */
6736 if (alpha_func == WINED3D_CMP_ALWAYS)
6737 return;
6739 if (alpha_func != WINED3D_CMP_NEVER)
6740 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
6741 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
6742 shader_addline(buffer, " discard;\n");
6745 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
6746 const struct wined3d_gl_info *gl_info)
6748 if (gl_info->supported[ARB_GPU_SHADER5])
6749 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
6750 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
6751 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
6752 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
6753 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
6754 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
6755 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
6756 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
6757 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
6758 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
6759 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
6760 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
6761 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
6762 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
6763 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
6764 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
6765 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
6766 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6767 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
6768 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
6769 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
6770 if (gl_info->supported[EXT_GPU_SHADER4])
6771 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
6772 if (gl_info->supported[EXT_TEXTURE_ARRAY])
6773 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
6776 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
6777 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6778 const struct ps_compile_args *args)
6780 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6782 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6783 if (reg_maps->shader_version.major < 2)
6784 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6786 if (args->srgb_correction)
6787 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6789 /* SM < 3 does not replace the fog stage. */
6790 if (reg_maps->shader_version.major < 3)
6791 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6793 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6796 /* Context activation is done by the caller. */
6797 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
6798 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6799 const struct wined3d_shader *shader,
6800 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
6802 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6803 const struct wined3d_gl_info *gl_info = context->gl_info;
6804 struct shader_glsl_ctx_priv priv_ctx;
6805 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6806 GLuint shader_id;
6808 memset(&priv_ctx, 0, sizeof(priv_ctx));
6809 priv_ctx.cur_ps_args = args;
6810 priv_ctx.cur_np2fixup_info = np2fixup_info;
6811 priv_ctx.string_buffers = string_buffers;
6813 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6815 shader_glsl_enable_extensions(buffer, gl_info);
6816 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
6817 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
6818 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6819 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6820 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
6821 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
6822 /* The spec says that it doesn't have to be explicitly enabled, but the
6823 * nvidia drivers write a warning if we don't do so. */
6824 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6825 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6827 /* Base Declarations */
6828 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6830 shader_addline(buffer, "void main()\n{\n");
6832 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6833 * add approximately 0.5. This causes off-by-one problems as spotted by
6834 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6835 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6836 * causes precision troubles when we just subtract 0.5.
6838 * To deal with that, just floor() the position. This will eliminate the
6839 * fraction on all cards.
6841 * TODO: Test how this behaves with multisampling.
6843 * An advantage of floor is that it works even if the driver doesn't add
6844 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6845 * to return in gl_FragCoord, even though coordinates specify the pixel
6846 * centers instead of the pixel corners. This code will behave correctly
6847 * on drivers that returns integer values. */
6848 if (reg_maps->vpos)
6850 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6851 shader_addline(buffer, "vpos = gl_FragCoord;\n");
6852 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
6853 shader_addline(buffer,
6854 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6855 else
6856 shader_addline(buffer,
6857 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6860 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
6862 unsigned int i;
6863 WORD map = reg_maps->texcoord;
6865 if (legacy_context)
6867 if (glsl_is_color_reg_read(shader, 0))
6868 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6869 if (glsl_is_color_reg_read(shader, 1))
6870 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6873 for (i = 0; map; map >>= 1, ++i)
6875 if (map & 1)
6877 if (args->pointsprite)
6878 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
6879 else if (args->texcoords_initialized & (1u << i))
6880 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
6881 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
6882 else
6883 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
6884 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
6888 if (legacy_context)
6889 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6892 /* Pack 3.0 inputs */
6893 if (reg_maps->shader_version.major >= 3)
6894 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
6896 /* Base Shader Body */
6897 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
6898 return 0;
6900 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6901 if (reg_maps->shader_version.major < 4)
6902 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
6904 shader_addline(buffer, "}\n");
6906 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6907 TRACE("Compiling shader object %u.\n", shader_id);
6908 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6910 return shader_id;
6913 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
6914 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6915 const struct vs_compile_args *args)
6917 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6918 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6919 unsigned int i;
6921 /* Unpack outputs. */
6922 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6924 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6925 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6926 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6927 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6929 if (reg_maps->shader_version.major < 3)
6931 if (args->fog_src == VS_FOG_Z)
6932 shader_addline(buffer, "%s = gl_Position.z;\n",
6933 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6934 else if (!reg_maps->fog)
6935 shader_addline(buffer, "%s = 0.0;\n",
6936 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6939 /* We always store the clipplanes without y inversion. */
6940 if (args->clip_enabled)
6942 if (legacy_context)
6943 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6944 else
6945 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6946 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6949 if (args->point_size && !args->per_vertex_point_size)
6950 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6952 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6953 shader_glsl_fixup_position(buffer);
6956 /* Context activation is done by the caller. */
6957 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6958 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6960 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6961 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6962 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6963 const struct wined3d_gl_info *gl_info = context->gl_info;
6964 struct shader_glsl_ctx_priv priv_ctx;
6965 GLuint shader_id;
6967 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6969 shader_glsl_enable_extensions(buffer, gl_info);
6970 if (gl_info->supported[ARB_DRAW_INSTANCED])
6971 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6972 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6973 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6975 memset(&priv_ctx, 0, sizeof(priv_ctx));
6976 priv_ctx.cur_vs_args = args;
6977 priv_ctx.string_buffers = string_buffers;
6979 /* Base Declarations */
6980 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6982 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6983 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6985 if (reg_maps->shader_version.major >= 4)
6987 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6988 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6989 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6990 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6993 shader_addline(buffer, "void main()\n{\n");
6995 /* Base Shader Body */
6996 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
6997 return 0;
6999 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7000 if (reg_maps->shader_version.major < 4)
7001 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
7003 shader_addline(buffer, "}\n");
7005 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7006 TRACE("Compiling shader object %u.\n", shader_id);
7007 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7009 return shader_id;
7012 /* Context activation is done by the caller. */
7013 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
7014 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
7016 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7017 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7018 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7019 const struct wined3d_gl_info *gl_info = context->gl_info;
7020 struct shader_glsl_ctx_priv priv_ctx;
7021 GLuint shader_id;
7023 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
7025 shader_glsl_enable_extensions(buffer, gl_info);
7026 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
7027 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
7029 memset(&priv_ctx, 0, sizeof(priv_ctx));
7030 priv_ctx.string_buffers = string_buffers;
7031 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7032 if (!gl_info->supported[ARB_CLIP_CONTROL])
7033 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
7034 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
7035 shader_addline(buffer, "void main()\n{\n");
7036 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
7037 return 0;
7038 shader_addline(buffer, "}\n");
7040 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
7041 TRACE("Compiling shader object %u.\n", shader_id);
7042 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7044 return shader_id;
7047 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
7049 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
7050 const struct wined3d_gl_info *gl_info = ctx->gl_info;
7051 const struct wined3d_shader *shader = ctx->shader;
7053 switch (shader->reg_maps.shader_version.type)
7055 case WINED3D_SHADER_TYPE_PIXEL:
7056 shader_glsl_generate_ps_epilogue(gl_info, ctx->buffer, shader, priv->cur_ps_args);
7057 break;
7058 case WINED3D_SHADER_TYPE_VERTEX:
7059 shader_glsl_generate_vs_epilogue(gl_info, ctx->buffer, shader, priv->cur_vs_args);
7060 break;
7061 case WINED3D_SHADER_TYPE_GEOMETRY:
7062 case WINED3D_SHADER_TYPE_COMPUTE:
7063 break;
7064 default:
7065 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7066 break;
7070 /* Context activation is done by the caller. */
7071 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
7072 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7073 const struct wined3d_shader *shader)
7075 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
7076 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7077 const struct wined3d_gl_info *gl_info = context->gl_info;
7078 struct shader_glsl_ctx_priv priv_ctx;
7079 GLuint shader_id;
7080 unsigned int i;
7082 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
7084 shader_glsl_enable_extensions(buffer, gl_info);
7085 if (gl_info->supported[ARB_COMPUTE_SHADER])
7086 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
7088 memset(&priv_ctx, 0, sizeof(priv_ctx));
7089 priv_ctx.string_buffers = string_buffers;
7090 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
7092 for (i = 0; i < reg_maps->tgsm_count; ++i)
7094 if (reg_maps->tgsm[i].size)
7095 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
7098 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
7099 thread_group_size->x, thread_group_size->y, thread_group_size->z);
7101 shader_addline(buffer, "void main()\n{\n");
7102 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
7103 shader_addline(buffer, "}\n");
7105 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
7106 TRACE("Compiling shader object %u.\n", shader_id);
7107 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7109 return shader_id;
7112 static GLuint find_glsl_pshader(const struct wined3d_context *context,
7113 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7114 struct wined3d_shader *shader,
7115 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
7117 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
7118 struct glsl_shader_private *shader_data;
7119 struct ps_np2fixup_info *np2fixup;
7120 UINT i;
7121 DWORD new_size;
7122 GLuint ret;
7124 if (!shader->backend_data)
7126 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
7127 if (!shader->backend_data)
7129 ERR("Failed to allocate backend data.\n");
7130 return 0;
7133 shader_data = shader->backend_data;
7134 gl_shaders = shader_data->gl_shaders.ps;
7136 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7137 * so a linear search is more performant than a hashmap or a binary search
7138 * (cache coherency etc)
7140 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7142 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
7144 if (args->np2_fixup)
7145 *np2fixup_info = &gl_shaders[i].np2fixup;
7146 return gl_shaders[i].id;
7150 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7151 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
7152 if (shader_data->num_gl_shaders)
7154 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
7155 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
7156 new_size * sizeof(*gl_shaders));
7158 else
7160 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
7161 new_size = 1;
7164 if(!new_array) {
7165 ERR("Out of memory\n");
7166 return 0;
7168 shader_data->gl_shaders.ps = new_array;
7169 shader_data->shader_array_size = new_size;
7170 gl_shaders = new_array;
7173 gl_shaders[shader_data->num_gl_shaders].args = *args;
7175 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
7176 memset(np2fixup, 0, sizeof(*np2fixup));
7177 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
7179 pixelshader_update_resource_types(shader, args->tex_types);
7181 string_buffer_clear(buffer);
7182 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
7183 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7185 return ret;
7188 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
7189 const DWORD use_map)
7191 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
7192 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
7193 if (stored->point_size != new->point_size)
7194 return FALSE;
7195 if (stored->per_vertex_point_size != new->per_vertex_point_size)
7196 return FALSE;
7197 if (stored->flatshading != new->flatshading)
7198 return FALSE;
7199 if (stored->next_shader_type != new->next_shader_type)
7200 return FALSE;
7201 if (stored->next_shader_input_count != new->next_shader_input_count)
7202 return FALSE;
7203 return stored->fog_src == new->fog_src;
7206 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
7207 struct wined3d_shader *shader, const struct vs_compile_args *args)
7209 UINT i;
7210 DWORD new_size;
7211 DWORD use_map = context->stream_info.use_map;
7212 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
7213 struct glsl_shader_private *shader_data;
7214 GLuint ret;
7216 if (!shader->backend_data)
7218 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
7219 if (!shader->backend_data)
7221 ERR("Failed to allocate backend data.\n");
7222 return 0;
7225 shader_data = shader->backend_data;
7226 gl_shaders = shader_data->gl_shaders.vs;
7228 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7229 * so a linear search is more performant than a hashmap or a binary search
7230 * (cache coherency etc)
7232 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7234 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
7235 return gl_shaders[i].id;
7238 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7240 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
7241 if (shader_data->num_gl_shaders)
7243 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
7244 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
7245 new_size * sizeof(*gl_shaders));
7247 else
7249 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
7250 new_size = 1;
7253 if(!new_array) {
7254 ERR("Out of memory\n");
7255 return 0;
7257 shader_data->gl_shaders.vs = new_array;
7258 shader_data->shader_array_size = new_size;
7259 gl_shaders = new_array;
7262 gl_shaders[shader_data->num_gl_shaders].args = *args;
7264 string_buffer_clear(&priv->shader_buffer);
7265 ret = shader_glsl_generate_vshader(context, priv, shader, args);
7266 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7268 return ret;
7271 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
7272 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
7274 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
7275 struct glsl_shader_private *shader_data;
7276 unsigned int i, new_size;
7277 GLuint ret;
7279 if (!shader->backend_data)
7281 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7283 ERR("Failed to allocate backend data.\n");
7284 return 0;
7287 shader_data = shader->backend_data;
7288 gl_shaders = shader_data->gl_shaders.gs;
7290 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7292 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
7293 return gl_shaders[i].id;
7296 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7298 if (shader_data->num_gl_shaders)
7300 new_size = shader_data->shader_array_size + 1;
7301 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
7302 new_size * sizeof(*new_array));
7304 else
7306 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
7307 new_size = 1;
7310 if (!new_array)
7312 ERR("Failed to allocate GL shaders array.\n");
7313 return 0;
7315 shader_data->gl_shaders.gs = new_array;
7316 shader_data->shader_array_size = new_size;
7317 gl_shaders = new_array;
7319 string_buffer_clear(&priv->shader_buffer);
7320 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
7321 gl_shaders[shader_data->num_gl_shaders].args = *args;
7322 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7324 return ret;
7327 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
7328 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7329 struct wined3d_shader *shader)
7331 struct glsl_cs_compiled_shader *gl_shaders;
7332 struct glsl_shader_private *shader_data;
7333 GLuint ret;
7335 if (!shader->backend_data)
7337 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7339 ERR("Failed to allocate backend data.\n");
7340 return 0;
7343 shader_data = shader->backend_data;
7344 gl_shaders = shader_data->gl_shaders.cs;
7346 /* No shader variants are used for compute shaders. */
7347 if (shader_data->num_gl_shaders)
7348 return gl_shaders[0].id;
7350 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7352 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
7354 ERR("Failed to allocate GL shader array.\n");
7355 return 0;
7357 shader_data->shader_array_size = 1;
7358 gl_shaders = shader_data->gl_shaders.cs;
7360 string_buffer_clear(buffer);
7361 ret = shader_glsl_generate_compute_shader(context, buffer, string_buffers, shader);
7362 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7364 return ret;
7367 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
7369 switch (mcs)
7371 case WINED3D_MCS_MATERIAL:
7372 return material;
7373 case WINED3D_MCS_COLOR1:
7374 return "ffp_attrib_diffuse";
7375 case WINED3D_MCS_COLOR2:
7376 return "ffp_attrib_specular";
7377 default:
7378 ERR("Invalid material color source %#x.\n", mcs);
7379 return "<invalid>";
7383 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
7384 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
7386 const char *diffuse, *specular, *emissive, *ambient;
7387 unsigned int i, idx;
7389 if (!settings->lighting)
7391 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
7392 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
7393 return;
7396 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
7397 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
7398 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
7399 shader_addline(buffer, "vec3 dir, dst;\n");
7400 shader_addline(buffer, "float att, t;\n");
7402 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
7403 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
7404 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
7405 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
7407 idx = 0;
7408 for (i = 0; i < settings->point_light_count; ++i, ++idx)
7410 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7411 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7412 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7413 shader_addline(buffer, "dst.x = 1.0;\n");
7414 if (legacy_lighting)
7416 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7417 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7419 else
7421 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7423 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7424 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
7425 if (!legacy_lighting)
7426 shader_addline(buffer, "att = 1.0 / att;\n");
7427 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7428 if (!settings->normal)
7430 if (!legacy_lighting)
7431 shader_addline(buffer, "}\n");
7432 continue;
7434 shader_addline(buffer, "dir = normalize(dir);\n");
7435 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7436 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7437 if (settings->localviewer)
7438 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7439 else
7440 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7441 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7442 " * ffp_light[%u].specular) * att;\n", idx);
7443 if (!legacy_lighting)
7444 shader_addline(buffer, "}\n");
7447 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
7449 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7450 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7451 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7452 shader_addline(buffer, "dst.x = 1.0;\n");
7453 if (legacy_lighting)
7455 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7456 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7458 else
7460 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7462 shader_addline(buffer, "dir = normalize(dir);\n");
7463 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
7464 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
7465 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
7466 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
7467 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
7468 idx, idx, idx, idx);
7469 if (legacy_lighting)
7470 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7471 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7472 idx, idx, idx);
7473 else
7474 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7475 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7476 idx, idx, idx);
7477 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7478 if (!settings->normal)
7480 if (!legacy_lighting)
7481 shader_addline(buffer, "}\n");
7482 continue;
7484 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7485 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7486 if (settings->localviewer)
7487 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7488 else
7489 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7490 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7491 " * ffp_light[%u].specular) * att;\n", idx);
7492 if (!legacy_lighting)
7493 shader_addline(buffer, "}\n");
7496 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
7498 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7499 if (!settings->normal)
7500 continue;
7501 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
7502 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7503 " * ffp_light[%u].diffuse.xyz;\n", idx);
7504 /* TODO: In the non-local viewer case the halfvector is constant
7505 * and could be precomputed and stored in a uniform. */
7506 if (settings->localviewer)
7507 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7508 else
7509 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7510 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7511 " * ffp_light[%u].specular;\n", idx);
7514 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
7516 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7517 if (!settings->normal)
7518 continue;
7519 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
7520 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7521 " * ffp_light[%u].diffuse.xyz;\n", idx);
7522 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7523 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7524 " * ffp_light[%u].specular;\n", idx);
7527 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
7528 ambient, diffuse, emissive);
7529 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
7530 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
7533 /* Context activation is done by the caller. */
7534 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
7535 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
7537 static const struct attrib_info
7539 const char type[6];
7540 const char name[24];
7542 attrib_info[] =
7544 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
7545 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
7546 /* TODO: Indexed vertex blending */
7547 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
7548 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
7549 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
7550 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
7551 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
7553 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7554 BOOL legacy_lighting = priv->legacy_lighting;
7555 GLuint shader_obj;
7556 unsigned int i;
7557 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7558 BOOL output_legacy_fogcoord = legacy_context;
7560 string_buffer_clear(buffer);
7562 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
7564 if (shader_glsl_use_explicit_attrib_location(gl_info))
7565 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7567 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
7569 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
7571 if (shader_glsl_use_explicit_attrib_location(gl_info))
7572 shader_addline(buffer, "layout(location = %u) ", i);
7573 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
7575 shader_addline(buffer, "\n");
7577 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
7578 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
7579 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
7580 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
7582 shader_addline(buffer, "uniform struct\n{\n");
7583 shader_addline(buffer, " vec4 emissive;\n");
7584 shader_addline(buffer, " vec4 ambient;\n");
7585 shader_addline(buffer, " vec4 diffuse;\n");
7586 shader_addline(buffer, " vec4 specular;\n");
7587 shader_addline(buffer, " float shininess;\n");
7588 shader_addline(buffer, "} ffp_material;\n");
7590 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
7591 shader_addline(buffer, "uniform struct\n{\n");
7592 shader_addline(buffer, " vec4 diffuse;\n");
7593 shader_addline(buffer, " vec4 specular;\n");
7594 shader_addline(buffer, " vec4 ambient;\n");
7595 shader_addline(buffer, " vec4 position;\n");
7596 shader_addline(buffer, " vec3 direction;\n");
7597 shader_addline(buffer, " float range;\n");
7598 shader_addline(buffer, " float falloff;\n");
7599 shader_addline(buffer, " float c_att;\n");
7600 shader_addline(buffer, " float l_att;\n");
7601 shader_addline(buffer, " float q_att;\n");
7602 shader_addline(buffer, " float cos_htheta;\n");
7603 shader_addline(buffer, " float cos_hphi;\n");
7604 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
7606 if (settings->point_size)
7608 shader_addline(buffer, "uniform struct\n{\n");
7609 shader_addline(buffer, " float size;\n");
7610 shader_addline(buffer, " float size_min;\n");
7611 shader_addline(buffer, " float size_max;\n");
7612 shader_addline(buffer, " float c_att;\n");
7613 shader_addline(buffer, " float l_att;\n");
7614 shader_addline(buffer, " float q_att;\n");
7615 shader_addline(buffer, "} ffp_point;\n");
7618 if (legacy_context)
7620 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7621 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7622 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7623 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7625 else
7627 if (settings->clipping)
7628 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7630 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7631 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7632 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7633 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7636 shader_addline(buffer, "\nvoid main()\n{\n");
7637 shader_addline(buffer, "float m;\n");
7638 shader_addline(buffer, "vec3 r;\n");
7640 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
7642 if (attrib_info[i].name[0])
7643 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
7644 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
7646 for (i = 0; i < MAX_TEXTURES; ++i)
7648 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
7649 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7650 && settings->texcoords & (1u << i))
7651 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
7654 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
7656 if (settings->transformed)
7658 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7659 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7660 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7662 else
7664 for (i = 0; i < settings->vertexblends; ++i)
7665 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
7667 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
7668 for (i = 0; i < settings->vertexblends + 1; ++i)
7669 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
7671 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7672 if (settings->clipping)
7674 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7675 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
7676 else
7677 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7678 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
7680 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
7683 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
7684 if (settings->normal)
7686 if (!settings->vertexblends)
7688 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7690 else
7692 for (i = 0; i < settings->vertexblends + 1; ++i)
7693 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
7696 if (settings->normalize)
7697 shader_addline(buffer, "normal = normalize(normal);\n");
7700 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
7701 if (legacy_context)
7703 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
7704 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7706 else
7708 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7709 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7712 for (i = 0; i < MAX_TEXTURES; ++i)
7714 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7716 switch (settings->texgen[i] & 0xffff0000)
7718 case WINED3DTSS_TCI_PASSTHRU:
7719 if (settings->texcoords & (1u << i))
7720 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7721 i, i, i);
7722 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
7723 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
7724 else
7725 output_legacy_texcoord = FALSE;
7726 break;
7728 case WINED3DTSS_TCI_CAMERASPACENORMAL:
7729 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
7730 break;
7732 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
7733 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
7734 break;
7736 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
7737 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7738 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
7739 break;
7741 case WINED3DTSS_TCI_SPHEREMAP:
7742 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7743 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7744 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7745 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
7746 break;
7748 default:
7749 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
7750 break;
7752 if (output_legacy_texcoord)
7753 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
7756 switch (settings->fog_mode)
7758 case WINED3D_FFP_VS_FOG_OFF:
7759 output_legacy_fogcoord = FALSE;
7760 break;
7762 case WINED3D_FFP_VS_FOG_FOGCOORD:
7763 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7764 break;
7766 case WINED3D_FFP_VS_FOG_RANGE:
7767 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7768 break;
7770 case WINED3D_FFP_VS_FOG_DEPTH:
7771 if (settings->ortho_fog)
7773 if (gl_info->supported[ARB_CLIP_CONTROL])
7774 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
7775 else
7776 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7777 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7779 else if (settings->transformed)
7781 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
7783 else
7785 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7787 break;
7789 default:
7790 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
7791 break;
7793 if (output_legacy_fogcoord)
7794 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7796 if (settings->point_size)
7798 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7799 " + ffp_point.l_att * length(ec_pos.xyz)"
7800 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7801 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
7802 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7805 shader_addline(buffer, "}\n");
7807 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7808 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
7810 return shader_obj;
7813 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
7814 DWORD argnum, unsigned int stage, DWORD arg)
7816 const char *ret;
7818 if (arg == ARG_UNUSED)
7819 return "<unused arg>";
7821 switch (arg & WINED3DTA_SELECTMASK)
7823 case WINED3DTA_DIFFUSE:
7824 ret = "ffp_varying_diffuse";
7825 break;
7827 case WINED3DTA_CURRENT:
7828 ret = "ret";
7829 break;
7831 case WINED3DTA_TEXTURE:
7832 switch (stage)
7834 case 0: ret = "tex0"; break;
7835 case 1: ret = "tex1"; break;
7836 case 2: ret = "tex2"; break;
7837 case 3: ret = "tex3"; break;
7838 case 4: ret = "tex4"; break;
7839 case 5: ret = "tex5"; break;
7840 case 6: ret = "tex6"; break;
7841 case 7: ret = "tex7"; break;
7842 default:
7843 ret = "<invalid texture>";
7844 break;
7846 break;
7848 case WINED3DTA_TFACTOR:
7849 ret = "tex_factor";
7850 break;
7852 case WINED3DTA_SPECULAR:
7853 ret = "ffp_varying_specular";
7854 break;
7856 case WINED3DTA_TEMP:
7857 ret = "temp_reg";
7858 break;
7860 case WINED3DTA_CONSTANT:
7861 switch (stage)
7863 case 0: ret = "tss_const0"; break;
7864 case 1: ret = "tss_const1"; break;
7865 case 2: ret = "tss_const2"; break;
7866 case 3: ret = "tss_const3"; break;
7867 case 4: ret = "tss_const4"; break;
7868 case 5: ret = "tss_const5"; break;
7869 case 6: ret = "tss_const6"; break;
7870 case 7: ret = "tss_const7"; break;
7871 default:
7872 ret = "<invalid constant>";
7873 break;
7875 break;
7877 default:
7878 return "<unhandled arg>";
7881 if (arg & WINED3DTA_COMPLEMENT)
7883 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
7884 if (argnum == 0)
7885 ret = "arg0";
7886 else if (argnum == 1)
7887 ret = "arg1";
7888 else if (argnum == 2)
7889 ret = "arg2";
7892 if (arg & WINED3DTA_ALPHAREPLICATE)
7894 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
7895 if (argnum == 0)
7896 ret = "arg0";
7897 else if (argnum == 1)
7898 ret = "arg1";
7899 else if (argnum == 2)
7900 ret = "arg2";
7903 return ret;
7906 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
7907 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
7909 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
7911 if (color && alpha)
7912 dstmask = "";
7913 else if (color)
7914 dstmask = ".xyz";
7915 else
7916 dstmask = ".w";
7918 if (dst == tempreg)
7919 dstreg = "temp_reg";
7920 else
7921 dstreg = "ret";
7923 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
7924 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
7925 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
7927 switch (op)
7929 case WINED3D_TOP_DISABLE:
7930 break;
7932 case WINED3D_TOP_SELECT_ARG1:
7933 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
7934 break;
7936 case WINED3D_TOP_SELECT_ARG2:
7937 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
7938 break;
7940 case WINED3D_TOP_MODULATE:
7941 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7942 break;
7944 case WINED3D_TOP_MODULATE_4X:
7945 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7946 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7947 break;
7949 case WINED3D_TOP_MODULATE_2X:
7950 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7951 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7952 break;
7954 case WINED3D_TOP_ADD:
7955 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7956 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7957 break;
7959 case WINED3D_TOP_ADD_SIGNED:
7960 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7961 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7962 break;
7964 case WINED3D_TOP_ADD_SIGNED_2X:
7965 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7966 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7967 break;
7969 case WINED3D_TOP_SUBTRACT:
7970 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7971 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7972 break;
7974 case WINED3D_TOP_ADD_SMOOTH:
7975 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7976 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
7977 break;
7979 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
7980 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
7981 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7982 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7983 break;
7985 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7986 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7987 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7988 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7989 break;
7991 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7992 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
7993 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7994 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7995 break;
7997 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7998 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7999 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8000 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
8001 break;
8003 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
8004 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
8005 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
8006 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
8007 break;
8009 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
8010 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
8011 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
8012 break;
8014 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
8015 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
8016 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
8017 break;
8019 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
8020 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
8021 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
8022 break;
8023 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
8024 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
8025 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
8026 break;
8028 case WINED3D_TOP_BUMPENVMAP:
8029 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
8030 /* These are handled in the first pass, nothing to do. */
8031 break;
8033 case WINED3D_TOP_DOTPRODUCT3:
8034 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
8035 dstreg, dstmask, arg1, arg2, dstmask);
8036 break;
8038 case WINED3D_TOP_MULTIPLY_ADD:
8039 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
8040 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
8041 break;
8043 case WINED3D_TOP_LERP:
8044 /* MSDN isn't quite right here. */
8045 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
8046 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
8047 break;
8049 default:
8050 FIXME("Unhandled operation %#x.\n", op);
8051 break;
8055 /* Context activation is done by the caller. */
8056 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
8057 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
8059 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
8060 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
8061 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8062 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8063 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
8064 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
8065 UINT lowest_disabled_stage;
8066 GLuint shader_id;
8067 DWORD arg0, arg1, arg2;
8068 unsigned int stage;
8070 string_buffer_clear(buffer);
8072 /* Find out which textures are read */
8073 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8075 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8076 break;
8078 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
8079 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
8080 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
8082 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
8083 || (stage == 0 && settings->color_key_enabled))
8084 tex_map |= 1u << stage;
8085 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
8086 tfactor_used = TRUE;
8087 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
8088 tempreg_used = TRUE;
8089 if (settings->op[stage].dst == tempreg)
8090 tempreg_used = TRUE;
8091 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
8092 tss_const_map |= 1u << stage;
8094 switch (settings->op[stage].cop)
8096 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
8097 lum_map |= 1u << stage;
8098 /* fall through */
8099 case WINED3D_TOP_BUMPENVMAP:
8100 bump_map |= 1u << stage;
8101 /* fall through */
8102 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
8103 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
8104 tex_map |= 1u << stage;
8105 break;
8107 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
8108 tfactor_used = TRUE;
8109 break;
8111 default:
8112 break;
8115 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8116 continue;
8118 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
8119 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
8120 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
8122 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
8123 tex_map |= 1u << stage;
8124 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
8125 tfactor_used = TRUE;
8126 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
8127 tempreg_used = TRUE;
8128 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
8129 tss_const_map |= 1u << stage;
8131 lowest_disabled_stage = stage;
8133 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
8135 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
8136 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
8138 if (!needs_legacy_glsl_syntax(gl_info))
8139 shader_addline(buffer, "out vec4 ps_out[1];\n");
8141 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
8142 shader_addline(buffer, "vec4 ret;\n");
8143 if (tempreg_used || settings->sRGB_write)
8144 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
8145 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
8147 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8149 if (tss_const_map & (1u << stage))
8150 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
8152 if (!(tex_map & (1u << stage)))
8153 continue;
8155 switch (settings->op[stage].tex_type)
8157 case WINED3D_GL_RES_TYPE_TEX_1D:
8158 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
8159 break;
8160 case WINED3D_GL_RES_TYPE_TEX_2D:
8161 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
8162 break;
8163 case WINED3D_GL_RES_TYPE_TEX_3D:
8164 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
8165 break;
8166 case WINED3D_GL_RES_TYPE_TEX_CUBE:
8167 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
8168 break;
8169 case WINED3D_GL_RES_TYPE_TEX_RECT:
8170 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
8171 break;
8172 default:
8173 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
8174 break;
8177 shader_addline(buffer, "vec4 tex%u;\n", stage);
8179 if (!(bump_map & (1u << stage)))
8180 continue;
8181 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
8183 if (!(lum_map & (1u << stage)))
8184 continue;
8185 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
8186 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
8188 if (tfactor_used)
8189 shader_addline(buffer, "uniform vec4 tex_factor;\n");
8190 if (settings->color_key_enabled)
8191 shader_addline(buffer, "uniform vec4 color_key[2];\n");
8192 shader_addline(buffer, "uniform vec4 specular_enable;\n");
8194 if (settings->sRGB_write)
8196 shader_addline(buffer, "const vec4 srgb_const0 = ");
8197 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
8198 shader_addline(buffer, ";\n");
8199 shader_addline(buffer, "const vec4 srgb_const1 = ");
8200 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
8201 shader_addline(buffer, ";\n");
8204 shader_addline(buffer, "uniform struct\n{\n");
8205 shader_addline(buffer, " vec4 color;\n");
8206 shader_addline(buffer, " float density;\n");
8207 shader_addline(buffer, " float end;\n");
8208 shader_addline(buffer, " float scale;\n");
8209 shader_addline(buffer, "} ffp_fog;\n");
8211 if (alpha_test_func != WINED3D_CMP_ALWAYS)
8212 shader_addline(buffer, "uniform float alpha_test_ref;\n");
8214 if (legacy_context)
8216 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8217 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8218 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8219 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
8220 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8222 else
8224 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8225 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8226 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8227 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
8228 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8231 shader_addline(buffer, "void main()\n{\n");
8233 if (legacy_context)
8235 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
8236 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
8239 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8241 if (tex_map & (1u << stage))
8243 if (settings->pointsprite)
8244 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
8245 else if (settings->texcoords_initialized & (1u << stage))
8246 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
8247 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
8248 else
8249 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
8253 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
8254 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
8256 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
8257 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
8259 /* Generate texture sampling instructions */
8260 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
8262 const char *texture_function, *coord_mask;
8263 BOOL proj;
8265 if (!(tex_map & (1u << stage)))
8266 continue;
8268 if (settings->op[stage].projected == proj_none)
8270 proj = FALSE;
8272 else if (settings->op[stage].projected == proj_count4
8273 || settings->op[stage].projected == proj_count3)
8275 proj = TRUE;
8277 else
8279 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
8280 proj = TRUE;
8283 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
8284 proj = FALSE;
8286 switch (settings->op[stage].tex_type)
8288 case WINED3D_GL_RES_TYPE_TEX_1D:
8289 if (proj)
8291 texture_function = "texture1DProj";
8292 coord_mask = "xw";
8294 else
8296 texture_function = "texture1D";
8297 coord_mask = "x";
8299 break;
8300 case WINED3D_GL_RES_TYPE_TEX_2D:
8301 if (proj)
8303 texture_function = "texture2DProj";
8304 coord_mask = "xyw";
8306 else
8308 texture_function = "texture2D";
8309 coord_mask = "xy";
8311 break;
8312 case WINED3D_GL_RES_TYPE_TEX_3D:
8313 if (proj)
8315 texture_function = "texture3DProj";
8316 coord_mask = "xyzw";
8318 else
8320 texture_function = "texture3D";
8321 coord_mask = "xyz";
8323 break;
8324 case WINED3D_GL_RES_TYPE_TEX_CUBE:
8325 texture_function = "textureCube";
8326 coord_mask = "xyz";
8327 break;
8328 case WINED3D_GL_RES_TYPE_TEX_RECT:
8329 if (proj)
8331 texture_function = "texture2DRectProj";
8332 coord_mask = "xyw";
8334 else
8336 texture_function = "texture2DRect";
8337 coord_mask = "xy";
8339 break;
8340 default:
8341 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
8342 texture_function = "";
8343 coord_mask = "xyzw";
8344 break;
8346 if (!needs_legacy_glsl_syntax(gl_info))
8347 texture_function = proj ? "textureProj" : "texture";
8349 if (stage > 0
8350 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
8351 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
8353 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
8355 /* With projective textures, texbem only divides the static
8356 * texture coord, not the displacement, so multiply the
8357 * displacement with the dividing parameter before passing it to
8358 * TXP. */
8359 if (settings->op[stage].projected != proj_none)
8361 if (settings->op[stage].projected == proj_count4)
8363 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
8364 stage, stage);
8365 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
8367 else
8369 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
8370 stage, stage);
8371 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
8374 else
8376 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
8379 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
8380 stage, texture_function, stage, coord_mask);
8382 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8383 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
8384 stage, stage - 1, stage - 1, stage - 1);
8386 else if (settings->op[stage].projected == proj_count3)
8388 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
8389 stage, texture_function, stage, stage);
8391 else
8393 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
8394 stage, texture_function, stage, stage, coord_mask);
8397 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
8398 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
8399 settings->op[stage].color_fixup);
8402 if (settings->color_key_enabled)
8404 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
8405 shader_addline(buffer, " discard;\n");
8408 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
8410 /* Generate the main shader */
8411 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8413 BOOL op_equal;
8415 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8416 break;
8418 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8419 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8420 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
8421 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8422 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8423 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
8424 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8425 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8426 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
8427 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8428 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8429 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
8430 else
8431 op_equal = settings->op[stage].aop == settings->op[stage].cop
8432 && settings->op[stage].carg0 == settings->op[stage].aarg0
8433 && settings->op[stage].carg1 == settings->op[stage].aarg1
8434 && settings->op[stage].carg2 == settings->op[stage].aarg2;
8436 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8438 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8439 settings->op[stage].cop, settings->op[stage].carg0,
8440 settings->op[stage].carg1, settings->op[stage].carg2);
8442 else if (op_equal)
8444 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
8445 settings->op[stage].cop, settings->op[stage].carg0,
8446 settings->op[stage].carg1, settings->op[stage].carg2);
8448 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
8449 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8451 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8452 settings->op[stage].cop, settings->op[stage].carg0,
8453 settings->op[stage].carg1, settings->op[stage].carg2);
8454 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
8455 settings->op[stage].aop, settings->op[stage].aarg0,
8456 settings->op[stage].aarg1, settings->op[stage].aarg2);
8460 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
8461 get_fragment_output(gl_info));
8463 if (settings->sRGB_write)
8464 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
8466 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
8468 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
8470 shader_addline(buffer, "}\n");
8472 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8473 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8475 string_buffer_release(&priv->string_buffers, tex_reg_name);
8476 return shader_id;
8479 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
8480 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
8482 struct glsl_ffp_vertex_shader *shader;
8483 const struct wine_rb_entry *entry;
8485 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
8486 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
8488 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
8489 return NULL;
8491 shader->desc.settings = *settings;
8492 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
8493 list_init(&shader->linked_programs);
8494 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
8495 ERR("Failed to insert ffp vertex shader.\n");
8497 return shader;
8500 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
8501 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
8503 struct glsl_ffp_fragment_shader *glsl_desc;
8504 const struct ffp_frag_desc *desc;
8506 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
8507 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
8509 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
8510 return NULL;
8512 glsl_desc->entry.settings = *args;
8513 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
8514 list_init(&glsl_desc->linked_programs);
8515 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
8517 return glsl_desc;
8521 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
8522 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
8524 unsigned int i;
8525 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8527 for (i = 0; i < vs_c_count; ++i)
8529 string_buffer_sprintf(name, "vs_c[%u]", i);
8530 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8532 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
8534 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8536 string_buffer_sprintf(name, "vs_i[%u]", i);
8537 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8540 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8542 string_buffer_sprintf(name, "vs_b[%u]", i);
8543 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8546 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8548 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
8550 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
8551 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8553 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
8554 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
8555 for (i = 0; i < MAX_TEXTURES; ++i)
8557 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
8558 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8560 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
8561 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
8562 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
8563 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
8564 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
8565 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
8566 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
8568 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
8569 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8570 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
8571 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8572 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
8573 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8574 string_buffer_sprintf(name, "ffp_light[%u].position", i);
8575 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8576 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
8577 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8578 string_buffer_sprintf(name, "ffp_light[%u].range", i);
8579 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8580 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
8581 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8582 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
8583 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8584 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
8585 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8586 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
8587 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8588 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
8589 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8590 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
8591 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8593 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
8594 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
8595 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
8596 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
8597 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
8598 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
8599 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
8601 string_buffer_release(&priv->string_buffers, name);
8604 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
8605 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
8607 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8610 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
8611 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
8613 unsigned int i;
8614 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8616 for (i = 0; i < ps_c_count; ++i)
8618 string_buffer_sprintf(name, "ps_c[%u]", i);
8619 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8621 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
8623 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8625 string_buffer_sprintf(name, "ps_i[%u]", i);
8626 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8629 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8631 string_buffer_sprintf(name, "ps_b[%u]", i);
8632 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8635 for (i = 0; i < MAX_TEXTURES; ++i)
8637 string_buffer_sprintf(name, "bumpenv_mat%u", i);
8638 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8639 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
8640 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8641 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
8642 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8643 string_buffer_sprintf(name, "tss_const%u", i);
8644 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8647 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
8648 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
8650 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
8651 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
8652 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
8653 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
8655 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
8657 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
8658 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
8659 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
8661 string_buffer_release(&priv->string_buffers, name);
8664 /* Context activation is done by the caller. */
8665 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
8666 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8668 const struct wined3d_gl_info *gl_info = context->gl_info;
8669 struct glsl_shader_prog_link *entry = NULL;
8670 struct wined3d_shader *shader;
8671 struct glsl_program_key key;
8672 GLuint program_id, cs_id;
8674 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
8675 return;
8677 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
8679 WARN("Compute shader is NULL.\n");
8680 ctx_data->glsl_program = NULL;
8681 return;
8684 cs_id = find_glsl_compute_shader(context, &priv->shader_buffer, &priv->string_buffers, shader);
8685 memset(&key, 0, sizeof(key));
8686 key.cs_id = cs_id;
8687 if ((entry = get_glsl_program_entry(priv, &key)))
8689 ctx_data->glsl_program = entry;
8690 return;
8693 program_id = GL_EXTCALL(glCreateProgram());
8694 TRACE("Created new GLSL shader program %u.\n", program_id);
8696 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
8698 ERR("Out of memory.\n");
8699 return;
8701 entry->id = program_id;
8702 entry->vs.id = 0;
8703 entry->gs.id = 0;
8704 entry->ps.id = 0;
8705 entry->cs.id = cs_id;
8706 entry->constant_version = 0;
8707 entry->ps.np2_fixup_info = NULL;
8708 add_glsl_program_entry(priv, entry);
8710 ctx_data->glsl_program = entry;
8712 TRACE("Attaching GLSL shader object %u to program %u.\n", cs_id, program_id);
8713 GL_EXTCALL(glAttachShader(program_id, cs_id));
8714 checkGLcall("glAttachShader");
8716 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
8718 TRACE("Linking GLSL shader program %u.\n", program_id);
8719 GL_EXTCALL(glLinkProgram(program_id));
8720 shader_glsl_validate_link(gl_info, program_id);
8722 GL_EXTCALL(glUseProgram(program_id));
8723 checkGLcall("glUseProgram");
8724 shader_glsl_load_program_resources(context, priv, program_id, shader);
8725 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
8727 entry->constant_update_mask = 0;
8730 /* Context activation is done by the caller. */
8731 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
8732 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8734 const struct wined3d_gl_info *gl_info = context->gl_info;
8735 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
8736 const struct ps_np2fixup_info *np2fixup_info = NULL;
8737 struct glsl_shader_prog_link *entry = NULL;
8738 struct wined3d_shader *vshader = NULL;
8739 struct wined3d_shader *gshader = NULL;
8740 struct wined3d_shader *pshader = NULL;
8741 GLuint reorder_shader_id = 0;
8742 struct glsl_program_key key;
8743 GLuint program_id;
8744 unsigned int i;
8745 GLuint vs_id = 0;
8746 GLuint gs_id = 0;
8747 GLuint ps_id = 0;
8748 struct list *ps_list, *vs_list;
8749 WORD attribs_map;
8750 struct wined3d_string_buffer *tmp_name;
8752 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
8754 vs_id = ctx_data->glsl_program->vs.id;
8755 vs_list = &ctx_data->glsl_program->vs.shader_entry;
8757 if (use_vs(state))
8759 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8760 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8762 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8763 && ctx_data->glsl_program->gs.id)
8765 gs_id = ctx_data->glsl_program->gs.id;
8767 else if (gshader)
8769 struct gs_compile_args args;
8771 find_gs_compile_args(state, gshader, &args);
8772 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
8776 else if (use_vs(state))
8778 struct vs_compile_args vs_compile_args;
8780 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8781 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8783 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
8784 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
8785 vs_list = &vshader->linked_programs;
8787 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8788 && ctx_data->glsl_program->gs.id)
8790 gs_id = ctx_data->glsl_program->gs.id;
8792 else if (gshader)
8794 struct gs_compile_args gs_compile_args;
8796 find_gs_compile_args(state, gshader, &gs_compile_args);
8797 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
8800 else if (priv->vertex_pipe == &glsl_vertex_pipe)
8802 struct glsl_ffp_vertex_shader *ffp_shader;
8803 struct wined3d_ffp_vs_settings settings;
8805 wined3d_ffp_get_vs_settings(context, state, &settings);
8806 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
8807 vs_id = ffp_shader->id;
8808 vs_list = &ffp_shader->linked_programs;
8811 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
8813 ps_id = ctx_data->glsl_program->ps.id;
8814 ps_list = &ctx_data->glsl_program->ps.shader_entry;
8816 if (use_ps(state))
8817 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8819 else if (use_ps(state))
8821 struct ps_compile_args ps_compile_args;
8822 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8823 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
8824 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
8825 pshader, &ps_compile_args, &np2fixup_info);
8826 ps_list = &pshader->linked_programs;
8828 else if (priv->fragment_pipe == &glsl_fragment_pipe)
8830 struct glsl_ffp_fragment_shader *ffp_shader;
8831 struct ffp_frag_settings settings;
8833 gen_ffp_frag_op(context, state, &settings, FALSE);
8834 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
8835 ps_id = ffp_shader->id;
8836 ps_list = &ffp_shader->linked_programs;
8839 key.vs_id = vs_id;
8840 key.gs_id = gs_id;
8841 key.ps_id = ps_id;
8842 key.cs_id = 0;
8843 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
8845 ctx_data->glsl_program = entry;
8846 return;
8849 /* If we get to this point, then no matching program exists, so we create one */
8850 program_id = GL_EXTCALL(glCreateProgram());
8851 TRACE("Created new GLSL shader program %u.\n", program_id);
8853 /* Create the entry */
8854 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
8855 entry->id = program_id;
8856 entry->vs.id = vs_id;
8857 entry->gs.id = gs_id;
8858 entry->ps.id = ps_id;
8859 entry->cs.id = 0;
8860 entry->constant_version = 0;
8861 entry->ps.np2_fixup_info = np2fixup_info;
8862 /* Add the hash table entry */
8863 add_glsl_program_entry(priv, entry);
8865 /* Set the current program */
8866 ctx_data->glsl_program = entry;
8868 /* Attach GLSL vshader */
8869 if (vs_id)
8871 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
8872 GL_EXTCALL(glAttachShader(program_id, vs_id));
8873 checkGLcall("glAttachShader");
8875 list_add_head(vs_list, &entry->vs.shader_entry);
8878 if (vshader)
8880 attribs_map = vshader->reg_maps.input_registers;
8881 if (vshader->reg_maps.shader_version.major < 4)
8883 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
8884 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
8885 d3d_info->emulated_flatshading
8886 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
8887 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
8888 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
8889 checkGLcall("glAttachShader");
8890 /* Flag the reorder function for deletion, it will be freed
8891 * automatically when the program is destroyed. */
8892 GL_EXTCALL(glDeleteShader(reorder_shader_id));
8895 else
8897 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
8900 if (!shader_glsl_use_explicit_attrib_location(gl_info))
8902 /* Bind vertex attributes to a corresponding index number to match
8903 * the same index numbers as ARB_vertex_programs (makes loading
8904 * vertex attributes simpler). With this method, we can use the
8905 * exact same code to load the attributes later for both ARB and
8906 * GLSL shaders.
8908 * We have to do this here because we need to know the Program ID
8909 * in order to make the bindings work, and it has to be done prior
8910 * to linking the GLSL program. */
8911 tmp_name = string_buffer_get(&priv->string_buffers);
8912 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
8914 if (!(attribs_map & 1))
8915 continue;
8917 string_buffer_sprintf(tmp_name, "vs_in%u", i);
8918 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8919 if (vshader && vshader->reg_maps.shader_version.major >= 4)
8921 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
8922 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8923 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
8924 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8927 checkGLcall("glBindAttribLocation");
8928 string_buffer_release(&priv->string_buffers, tmp_name);
8931 if (gshader)
8933 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
8934 GL_EXTCALL(glAttachShader(program_id, gs_id));
8935 checkGLcall("glAttachShader");
8937 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8939 TRACE("input type %s, output type %s, vertices out %u.\n",
8940 debug_d3dprimitivetype(gshader->u.gs.input_type),
8941 debug_d3dprimitivetype(gshader->u.gs.output_type),
8942 gshader->u.gs.vertices_out);
8943 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
8944 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
8945 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
8946 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
8947 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
8948 gshader->u.gs.vertices_out));
8949 checkGLcall("glProgramParameteriARB");
8952 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
8955 /* Attach GLSL pshader */
8956 if (ps_id)
8958 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
8959 GL_EXTCALL(glAttachShader(program_id, ps_id));
8960 checkGLcall("glAttachShader");
8962 list_add_head(ps_list, &entry->ps.shader_entry);
8965 /* Link the program */
8966 TRACE("Linking GLSL shader program %u.\n", program_id);
8967 GL_EXTCALL(glLinkProgram(program_id));
8968 shader_glsl_validate_link(gl_info, program_id);
8970 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
8971 vshader ? vshader->limits->constant_float : 0);
8972 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
8973 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
8974 pshader ? pshader->limits->constant_float : 0);
8975 checkGLcall("Find glsl program uniform locations");
8977 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8979 if (pshader && pshader->reg_maps.shader_version.major >= 3
8980 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
8982 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
8983 entry->vs.vertex_color_clamp = GL_FALSE;
8985 else
8987 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8990 else
8992 /* With core profile we never change vertex_color_clamp from
8993 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8994 * glClampColorARB(). */
8995 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8998 /* Set the shader to allow uniform loading on it */
8999 GL_EXTCALL(glUseProgram(program_id));
9000 checkGLcall("glUseProgram");
9002 entry->constant_update_mask = 0;
9003 if (vshader)
9005 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
9006 if (vshader->reg_maps.integer_constants)
9007 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
9008 if (vshader->reg_maps.boolean_constants)
9009 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
9010 if (entry->vs.pos_fixup_location != -1)
9011 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9013 shader_glsl_load_program_resources(context, priv, program_id, vshader);
9015 else
9017 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9018 | WINED3D_SHADER_CONST_FFP_PROJ;
9020 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
9022 if (entry->vs.modelview_matrix_location[i] != -1)
9024 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9025 break;
9029 for (i = 0; i < MAX_TEXTURES; ++i)
9031 if (entry->vs.texture_matrix_location[i] != -1)
9033 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9034 break;
9037 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
9038 || entry->vs.material_specular_location != -1
9039 || entry->vs.material_emissive_location != -1
9040 || entry->vs.material_shininess_location != -1)
9041 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9042 if (entry->vs.light_ambient_location != -1)
9043 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9045 if (entry->vs.clip_planes_location != -1)
9046 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9047 if (entry->vs.pointsize_min_location != -1)
9048 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9050 if (gshader)
9052 if (entry->gs.pos_fixup_location != -1)
9053 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9055 shader_glsl_load_program_resources(context, priv, program_id, gshader);
9058 if (ps_id)
9060 if (pshader)
9062 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
9063 if (pshader->reg_maps.integer_constants)
9064 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
9065 if (pshader->reg_maps.boolean_constants)
9066 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
9067 if (entry->ps.ycorrection_location != -1)
9068 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
9070 shader_glsl_load_program_resources(context, priv, program_id, pshader);
9071 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
9073 else
9075 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9077 shader_glsl_load_samplers_range(gl_info, priv, program_id,
9078 shader_glsl_get_prefix(WINED3D_SHADER_TYPE_PIXEL),
9079 0, MAX_TEXTURES, context->tex_unit_map);
9082 for (i = 0; i < MAX_TEXTURES; ++i)
9084 if (entry->ps.bumpenv_mat_location[i] != -1)
9086 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
9087 break;
9091 if (entry->ps.fog_color_location != -1)
9092 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9093 if (entry->ps.alpha_test_ref_location != -1)
9094 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9095 if (entry->ps.np2_fixup_location != -1)
9096 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
9097 if (entry->ps.color_key_location != -1)
9098 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9102 /* Context activation is done by the caller. */
9103 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
9104 const struct wined3d_state *state)
9106 struct glsl_context_data *ctx_data = context->shader_backend_data;
9107 const struct wined3d_gl_info *gl_info = context->gl_info;
9108 struct shader_glsl_priv *priv = shader_priv;
9109 GLenum current_vertex_color_clamp;
9110 GLuint program_id, prev_id;
9112 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
9113 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
9115 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9117 set_glsl_shader_program(context, state, priv, ctx_data);
9119 if (ctx_data->glsl_program)
9121 program_id = ctx_data->glsl_program->id;
9122 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
9124 else
9126 program_id = 0;
9127 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
9130 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
9132 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
9133 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
9135 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
9136 checkGLcall("glClampColorARB");
9138 else
9140 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
9144 TRACE("Using GLSL program %u.\n", program_id);
9146 if (prev_id != program_id)
9148 GL_EXTCALL(glUseProgram(program_id));
9149 checkGLcall("glUseProgram");
9151 if (program_id)
9152 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
9155 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
9158 /* Context activation is done by the caller. */
9159 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
9160 const struct wined3d_state *state)
9162 struct glsl_context_data *ctx_data = context->shader_backend_data;
9163 const struct wined3d_gl_info *gl_info = context->gl_info;
9164 struct shader_glsl_priv *priv = shader_priv;
9165 GLuint program_id, prev_id;
9167 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9168 set_glsl_compute_shader_program(context, state, priv, ctx_data);
9169 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9171 TRACE("Using GLSL program %u.\n", program_id);
9173 if (prev_id != program_id)
9175 GL_EXTCALL(glUseProgram(program_id));
9176 checkGLcall("glUseProgram");
9179 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
9180 | (1u << WINED3D_SHADER_TYPE_VERTEX)
9181 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
9182 | (1u << WINED3D_SHADER_TYPE_HULL)
9183 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
9186 /* "context" is not necessarily the currently active context. */
9187 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
9189 struct glsl_context_data *ctx_data = context->shader_backend_data;
9191 ctx_data->glsl_program = NULL;
9192 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
9193 | (1u << WINED3D_SHADER_TYPE_VERTEX)
9194 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
9195 | (1u << WINED3D_SHADER_TYPE_HULL)
9196 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
9197 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
9200 /* Context activation is done by the caller. */
9201 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
9203 struct glsl_context_data *ctx_data = context->shader_backend_data;
9204 const struct wined3d_gl_info *gl_info = context->gl_info;
9205 struct shader_glsl_priv *priv = shader_priv;
9207 shader_glsl_invalidate_current_program(context);
9208 GL_EXTCALL(glUseProgram(0));
9209 checkGLcall("glUseProgram");
9211 priv->vertex_pipe->vp_enable(gl_info, FALSE);
9212 priv->fragment_pipe->enable_extension(gl_info, FALSE);
9214 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
9216 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9217 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
9218 checkGLcall("glClampColorARB");
9222 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
9223 const struct glsl_shader_prog_link *program)
9225 const struct glsl_context_data *ctx_data;
9226 struct wined3d_context *context;
9227 unsigned int i;
9229 for (i = 0; i < device->context_count; ++i)
9231 context = device->contexts[i];
9232 ctx_data = context->shader_backend_data;
9234 if (ctx_data->glsl_program == program)
9235 shader_glsl_invalidate_current_program(context);
9239 static void shader_glsl_destroy(struct wined3d_shader *shader)
9241 struct glsl_shader_private *shader_data = shader->backend_data;
9242 struct wined3d_device *device = shader->device;
9243 struct shader_glsl_priv *priv = device->shader_priv;
9244 const struct wined3d_gl_info *gl_info;
9245 const struct list *linked_programs;
9246 struct wined3d_context *context;
9248 if (!shader_data || !shader_data->num_gl_shaders)
9250 HeapFree(GetProcessHeap(), 0, shader_data);
9251 shader->backend_data = NULL;
9252 return;
9255 context = context_acquire(device, NULL, 0);
9256 gl_info = context->gl_info;
9258 TRACE("Deleting linked programs.\n");
9259 linked_programs = &shader->linked_programs;
9260 if (linked_programs->next)
9262 struct glsl_shader_prog_link *entry, *entry2;
9263 UINT i;
9265 switch (shader->reg_maps.shader_version.type)
9267 case WINED3D_SHADER_TYPE_PIXEL:
9269 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
9271 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9273 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
9274 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9275 checkGLcall("glDeleteShader");
9277 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
9279 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9280 struct glsl_shader_prog_link, ps.shader_entry)
9282 shader_glsl_invalidate_contexts_program(device, entry);
9283 delete_glsl_program_entry(priv, gl_info, entry);
9286 break;
9289 case WINED3D_SHADER_TYPE_VERTEX:
9291 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
9293 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9295 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
9296 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9297 checkGLcall("glDeleteShader");
9299 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
9301 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9302 struct glsl_shader_prog_link, vs.shader_entry)
9304 shader_glsl_invalidate_contexts_program(device, entry);
9305 delete_glsl_program_entry(priv, gl_info, entry);
9308 break;
9311 case WINED3D_SHADER_TYPE_GEOMETRY:
9313 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
9315 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9317 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
9318 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9319 checkGLcall("glDeleteShader");
9321 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
9323 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9324 struct glsl_shader_prog_link, gs.shader_entry)
9326 shader_glsl_invalidate_contexts_program(device, entry);
9327 delete_glsl_program_entry(priv, gl_info, entry);
9330 break;
9333 case WINED3D_SHADER_TYPE_COMPUTE:
9335 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
9337 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9339 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
9340 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9341 checkGLcall("glDeleteShader");
9343 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
9345 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9346 struct glsl_shader_prog_link, cs.shader_entry)
9348 shader_glsl_invalidate_contexts_program(device, entry);
9349 delete_glsl_program_entry(priv, gl_info, entry);
9352 break;
9355 default:
9356 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
9357 break;
9361 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9362 shader->backend_data = NULL;
9364 context_release(context);
9367 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
9369 const struct glsl_program_key *k = key;
9370 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
9371 const struct glsl_shader_prog_link, program_lookup_entry);
9373 if (k->vs_id > prog->vs.id) return 1;
9374 else if (k->vs_id < prog->vs.id) return -1;
9376 if (k->gs_id > prog->gs.id) return 1;
9377 else if (k->gs_id < prog->gs.id) return -1;
9379 if (k->ps_id > prog->ps.id) return 1;
9380 else if (k->ps_id < prog->ps.id) return -1;
9382 if (k->cs_id > prog->cs.id) return 1;
9383 else if (k->cs_id < prog->cs.id) return -1;
9385 return 0;
9388 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
9390 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
9391 + constant_count * sizeof(*heap->contained)
9392 + constant_count * sizeof(*heap->positions);
9393 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
9395 if (!mem)
9397 ERR("Failed to allocate memory\n");
9398 return FALSE;
9401 heap->entries = mem;
9402 heap->entries[1].version = 0;
9403 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
9404 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
9405 heap->positions = (unsigned int *)(heap->contained + constant_count);
9406 heap->size = 1;
9408 return TRUE;
9411 static void constant_heap_free(struct constant_heap *heap)
9413 HeapFree(GetProcessHeap(), 0, heap->entries);
9416 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
9417 const struct fragment_pipeline *fragment_pipe)
9419 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
9420 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
9421 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
9422 struct fragment_caps fragment_caps;
9423 void *vertex_priv, *fragment_priv;
9425 string_buffer_list_init(&priv->string_buffers);
9427 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
9429 ERR("Failed to initialize vertex pipe.\n");
9430 HeapFree(GetProcessHeap(), 0, priv);
9431 return E_FAIL;
9434 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
9436 ERR("Failed to initialize fragment pipe.\n");
9437 vertex_pipe->vp_free(device);
9438 HeapFree(GetProcessHeap(), 0, priv);
9439 return E_FAIL;
9442 if (!string_buffer_init(&priv->shader_buffer))
9444 ERR("Failed to initialize shader buffer.\n");
9445 goto fail;
9448 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
9450 ERR("Failed to allocate memory.\n");
9451 goto fail;
9454 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
9456 ERR("Failed to initialize vertex shader constant heap\n");
9457 goto fail;
9460 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
9462 ERR("Failed to initialize pixel shader constant heap\n");
9463 goto fail;
9466 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
9468 priv->next_constant_version = 1;
9469 priv->vertex_pipe = vertex_pipe;
9470 priv->fragment_pipe = fragment_pipe;
9471 fragment_pipe->get_caps(gl_info, &fragment_caps);
9472 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
9473 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
9475 device->vertex_priv = vertex_priv;
9476 device->fragment_priv = fragment_priv;
9477 device->shader_priv = priv;
9479 return WINED3D_OK;
9481 fail:
9482 constant_heap_free(&priv->pconst_heap);
9483 constant_heap_free(&priv->vconst_heap);
9484 HeapFree(GetProcessHeap(), 0, priv->stack);
9485 string_buffer_free(&priv->shader_buffer);
9486 fragment_pipe->free_private(device);
9487 vertex_pipe->vp_free(device);
9488 HeapFree(GetProcessHeap(), 0, priv);
9489 return E_OUTOFMEMORY;
9492 /* Context activation is done by the caller. */
9493 static void shader_glsl_free(struct wined3d_device *device)
9495 struct shader_glsl_priv *priv = device->shader_priv;
9497 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
9498 constant_heap_free(&priv->pconst_heap);
9499 constant_heap_free(&priv->vconst_heap);
9500 HeapFree(GetProcessHeap(), 0, priv->stack);
9501 string_buffer_list_cleanup(&priv->string_buffers);
9502 string_buffer_free(&priv->shader_buffer);
9503 priv->fragment_pipe->free_private(device);
9504 priv->vertex_pipe->vp_free(device);
9506 HeapFree(GetProcessHeap(), 0, device->shader_priv);
9507 device->shader_priv = NULL;
9510 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
9512 struct glsl_context_data *ctx_data;
9513 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
9514 return FALSE;
9515 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9516 context->shader_backend_data = ctx_data;
9517 return TRUE;
9520 static void shader_glsl_free_context_data(struct wined3d_context *context)
9522 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
9525 static void shader_glsl_init_context_state(struct wined3d_context *context)
9527 const struct wined3d_gl_info *gl_info = context->gl_info;
9529 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
9530 checkGLcall("GL_PROGRAM_POINT_SIZE");
9533 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
9535 UINT shader_model;
9537 /* FIXME: Check for the specific extensions required for SM5 support
9538 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
9539 * soon as we introduce them, adjusting the GL / GLSL version checks
9540 * accordingly. */
9541 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
9542 && gl_info->supported[ARB_COMPUTE_SHADER]
9543 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
9544 && gl_info->supported[ARB_GPU_SHADER5]
9545 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
9546 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
9547 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
9548 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
9549 shader_model = 5;
9550 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
9551 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
9552 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
9553 shader_model = 4;
9554 /* Support for texldd and texldl instructions in pixel shaders is required
9555 * for SM3. */
9556 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
9557 shader_model = 3;
9558 else
9559 shader_model = 2;
9560 TRACE("Shader model %u.\n", shader_model);
9562 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
9563 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
9564 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
9565 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
9566 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
9567 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
9569 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
9570 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
9572 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
9573 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
9574 caps->varying_count = gl_info->limits.glsl_varyings;
9576 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
9577 * Direct3D minimum requirement.
9579 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
9580 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
9582 * The problem is that the refrast clamps temporary results in the shader to
9583 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
9584 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
9585 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
9586 * offer a way to query this.
9588 if (shader_model >= 4)
9589 caps->ps_1x_max_value = FLT_MAX;
9590 else
9591 caps->ps_1x_max_value = 1024.0f;
9593 /* Ideally we'd only set caps like sRGB writes here if supported by both
9594 * the shader backend and the fragment pipe, but we can get called before
9595 * shader_glsl_alloc(). */
9596 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
9597 | WINED3D_SHADER_CAP_SRGB_WRITE;
9600 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
9602 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
9604 TRACE("Checking support for fixup:\n");
9605 dump_color_fixup_desc(fixup);
9608 /* We support everything except YUV conversions. */
9609 if (!is_complex_fixup(fixup))
9611 TRACE("[OK]\n");
9612 return TRUE;
9615 TRACE("[FAILED]\n");
9616 return FALSE;
9619 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
9621 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
9622 /* WINED3DSIH_ADD */ shader_glsl_binop,
9623 /* WINED3DSIH_AND */ shader_glsl_binop,
9624 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
9625 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
9626 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
9627 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
9628 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
9629 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
9630 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
9631 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
9632 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
9633 /* WINED3DSIH_BEM */ shader_glsl_bem,
9634 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
9635 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
9636 /* WINED3DSIH_BREAK */ shader_glsl_break,
9637 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
9638 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
9639 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
9640 /* WINED3DSIH_CALL */ shader_glsl_call,
9641 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
9642 /* WINED3DSIH_CASE */ shader_glsl_case,
9643 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
9644 /* WINED3DSIH_CND */ shader_glsl_cnd,
9645 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
9646 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
9647 /* WINED3DSIH_CRS */ shader_glsl_cross,
9648 /* WINED3DSIH_CUT */ shader_glsl_cut,
9649 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
9650 /* WINED3DSIH_DCL */ shader_glsl_nop,
9651 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
9652 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
9653 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
9654 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
9655 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
9656 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ NULL,
9657 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
9658 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
9659 /* WINED3DSIH_DCL_INDEX_RANGE */ NULL,
9660 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
9661 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
9662 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
9663 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
9664 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
9665 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
9666 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
9667 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
9668 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
9669 /* WINED3DSIH_DCL_INTERFACE */ NULL,
9670 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
9671 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
9672 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
9673 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
9674 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
9675 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
9676 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
9677 /* WINED3DSIH_DCL_STREAM */ NULL,
9678 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
9679 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
9680 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
9681 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
9682 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
9683 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
9684 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
9685 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
9686 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
9687 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
9688 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
9689 /* WINED3DSIH_DEF */ shader_glsl_nop,
9690 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
9691 /* WINED3DSIH_DEFB */ shader_glsl_nop,
9692 /* WINED3DSIH_DEFI */ shader_glsl_nop,
9693 /* WINED3DSIH_DIV */ shader_glsl_binop,
9694 /* WINED3DSIH_DP2 */ shader_glsl_dot,
9695 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
9696 /* WINED3DSIH_DP3 */ shader_glsl_dot,
9697 /* WINED3DSIH_DP4 */ shader_glsl_dot,
9698 /* WINED3DSIH_DST */ shader_glsl_dst,
9699 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
9700 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
9701 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
9702 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
9703 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
9704 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
9705 /* WINED3DSIH_ELSE */ shader_glsl_else,
9706 /* WINED3DSIH_EMIT */ shader_glsl_emit,
9707 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
9708 /* WINED3DSIH_ENDIF */ shader_glsl_end,
9709 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
9710 /* WINED3DSIH_ENDREP */ shader_glsl_end,
9711 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
9712 /* WINED3DSIH_EQ */ shader_glsl_relop,
9713 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
9714 /* WINED3DSIH_EXPP */ shader_glsl_expp,
9715 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
9716 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
9717 /* WINED3DSIH_FCALL */ NULL,
9718 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
9719 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
9720 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
9721 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
9722 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
9723 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
9724 /* WINED3DSIH_GATHER4 */ NULL,
9725 /* WINED3DSIH_GATHER4_C */ NULL,
9726 /* WINED3DSIH_GE */ shader_glsl_relop,
9727 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
9728 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
9729 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
9730 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
9731 /* WINED3DSIH_IADD */ shader_glsl_binop,
9732 /* WINED3DSIH_IEQ */ shader_glsl_relop,
9733 /* WINED3DSIH_IF */ shader_glsl_if,
9734 /* WINED3DSIH_IFC */ shader_glsl_ifc,
9735 /* WINED3DSIH_IGE */ shader_glsl_relop,
9736 /* WINED3DSIH_ILT */ shader_glsl_relop,
9737 /* WINED3DSIH_IMAD */ shader_glsl_mad,
9738 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
9739 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
9740 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
9741 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
9742 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
9743 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
9744 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
9745 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
9746 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
9747 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
9748 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
9749 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
9750 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
9751 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
9752 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
9753 /* WINED3DSIH_INE */ shader_glsl_relop,
9754 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
9755 /* WINED3DSIH_ISHL */ shader_glsl_binop,
9756 /* WINED3DSIH_ISHR */ shader_glsl_binop,
9757 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
9758 /* WINED3DSIH_LABEL */ shader_glsl_label,
9759 /* WINED3DSIH_LD */ shader_glsl_ld,
9760 /* WINED3DSIH_LD2DMS */ NULL,
9761 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
9762 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
9763 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
9764 /* WINED3DSIH_LIT */ shader_glsl_lit,
9765 /* WINED3DSIH_LOD */ NULL,
9766 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
9767 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
9768 /* WINED3DSIH_LOOP */ shader_glsl_loop,
9769 /* WINED3DSIH_LRP */ shader_glsl_lrp,
9770 /* WINED3DSIH_LT */ shader_glsl_relop,
9771 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
9772 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
9773 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
9774 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
9775 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
9776 /* WINED3DSIH_MAD */ shader_glsl_mad,
9777 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
9778 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
9779 /* WINED3DSIH_MOV */ shader_glsl_mov,
9780 /* WINED3DSIH_MOVA */ shader_glsl_mov,
9781 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
9782 /* WINED3DSIH_MUL */ shader_glsl_binop,
9783 /* WINED3DSIH_NE */ shader_glsl_relop,
9784 /* WINED3DSIH_NOP */ shader_glsl_nop,
9785 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
9786 /* WINED3DSIH_NRM */ shader_glsl_nrm,
9787 /* WINED3DSIH_OR */ shader_glsl_binop,
9788 /* WINED3DSIH_PHASE */ shader_glsl_nop,
9789 /* WINED3DSIH_POW */ shader_glsl_pow,
9790 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
9791 /* WINED3DSIH_REP */ shader_glsl_rep,
9792 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
9793 /* WINED3DSIH_RET */ shader_glsl_ret,
9794 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
9795 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
9796 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
9797 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
9798 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
9799 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
9800 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
9801 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
9802 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
9803 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
9804 /* WINED3DSIH_SAMPLE_INFO */ NULL,
9805 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
9806 /* WINED3DSIH_SAMPLE_POS */ NULL,
9807 /* WINED3DSIH_SETP */ NULL,
9808 /* WINED3DSIH_SGE */ shader_glsl_compare,
9809 /* WINED3DSIH_SGN */ shader_glsl_sgn,
9810 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
9811 /* WINED3DSIH_SLT */ shader_glsl_compare,
9812 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
9813 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
9814 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
9815 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
9816 /* WINED3DSIH_SUB */ shader_glsl_binop,
9817 /* WINED3DSIH_SWAPC */ NULL,
9818 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
9819 /* WINED3DSIH_SYNC */ shader_glsl_sync,
9820 /* WINED3DSIH_TEX */ shader_glsl_tex,
9821 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
9822 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
9823 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
9824 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
9825 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
9826 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
9827 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
9828 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
9829 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
9830 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
9831 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
9832 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
9833 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
9834 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
9835 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
9836 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
9837 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
9838 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
9839 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
9840 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
9841 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
9842 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
9843 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
9844 /* WINED3DSIH_UGE */ shader_glsl_relop,
9845 /* WINED3DSIH_ULT */ shader_glsl_relop,
9846 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
9847 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
9848 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
9849 /* WINED3DSIH_USHR */ shader_glsl_binop,
9850 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
9851 /* WINED3DSIH_XOR */ shader_glsl_binop,
9854 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
9855 SHADER_HANDLER hw_fct;
9857 /* Select handler */
9858 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
9860 /* Unhandled opcode */
9861 if (!hw_fct)
9863 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
9864 return;
9866 hw_fct(ins);
9868 shader_glsl_add_instruction_modifiers(ins);
9871 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
9873 struct shader_glsl_priv *priv = shader_priv;
9875 return priv->ffp_proj_control;
9878 const struct wined3d_shader_backend_ops glsl_shader_backend =
9880 shader_glsl_handle_instruction,
9881 shader_glsl_select,
9882 shader_glsl_select_compute,
9883 shader_glsl_disable,
9884 shader_glsl_update_float_vertex_constants,
9885 shader_glsl_update_float_pixel_constants,
9886 shader_glsl_load_constants,
9887 shader_glsl_destroy,
9888 shader_glsl_alloc,
9889 shader_glsl_free,
9890 shader_glsl_allocate_context_data,
9891 shader_glsl_free_context_data,
9892 shader_glsl_init_context_state,
9893 shader_glsl_get_caps,
9894 shader_glsl_color_fixup_supported,
9895 shader_glsl_has_ffp_proj_control,
9898 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
9900 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
9902 caps->xyzrhw = TRUE;
9903 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9904 caps->ffp_generic_attributes = TRUE;
9905 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
9906 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
9907 caps->max_vertex_blend_matrix_index = 0;
9908 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
9909 | WINED3DVTXPCAPS_MATERIALSOURCE7
9910 | WINED3DVTXPCAPS_VERTEXFOG
9911 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9912 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9913 | WINED3DVTXPCAPS_LOCALVIEWER
9914 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
9915 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
9916 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
9917 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
9920 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
9922 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9923 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9924 return 0;
9927 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9929 struct shader_glsl_priv *priv;
9931 if (shader_backend == &glsl_shader_backend)
9933 priv = shader_priv;
9934 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
9935 return priv;
9938 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9940 return NULL;
9943 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
9945 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9946 struct glsl_ffp_vertex_shader, desc.entry);
9947 struct glsl_shader_prog_link *program, *program2;
9948 struct glsl_ffp_destroy_ctx *ctx = context;
9950 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9951 struct glsl_shader_prog_link, vs.shader_entry)
9953 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9955 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9956 HeapFree(GetProcessHeap(), 0, shader);
9959 /* Context activation is done by the caller. */
9960 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
9962 struct shader_glsl_priv *priv = device->vertex_priv;
9963 struct glsl_ffp_destroy_ctx ctx;
9965 ctx.priv = priv;
9966 ctx.gl_info = &device->adapter->gl_info;
9967 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9970 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9971 const struct wined3d_state *state, DWORD state_id) {}
9973 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9974 const struct wined3d_state *state, DWORD state_id)
9976 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9979 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
9980 const struct wined3d_state *state, DWORD state_id)
9982 const struct wined3d_gl_info *gl_info = context->gl_info;
9983 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
9984 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9985 BOOL transformed = context->stream_info.position_transformed;
9986 BOOL wasrhw = context->last_was_rhw;
9987 unsigned int i;
9989 context->last_was_rhw = transformed;
9991 /* If the vertex declaration contains a transformed position attribute,
9992 * the draw uses the fixed function vertex pipeline regardless of any
9993 * vertex shader set by the application. */
9994 if (transformed != wasrhw
9995 || context->stream_info.swizzle_map != context->last_swizzle_map)
9996 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9998 context->last_swizzle_map = context->stream_info.swizzle_map;
10000 if (!use_vs(state))
10002 if (context->last_was_vshader)
10004 if (legacy_context)
10005 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
10006 clipplane(context, state, STATE_CLIPPLANE(i));
10007 else
10008 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10011 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10013 /* Because of settings->texcoords, we have to regenerate the vertex
10014 * shader on a vdecl change if there aren't enough varyings to just
10015 * always output all the texture coordinates. */
10016 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
10017 || normal != context->last_was_normal)
10018 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10020 if (use_ps(state)
10021 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
10022 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
10023 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10025 else
10027 if (!context->last_was_vshader)
10029 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
10030 if (legacy_context)
10031 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
10032 clipplane(context, state, STATE_CLIPPLANE(i));
10033 else
10034 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10038 context->last_was_vshader = use_vs(state);
10039 context->last_was_normal = normal;
10042 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
10043 const struct wined3d_state *state, DWORD state_id)
10045 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10046 /* Different vertex shaders potentially require a different vertex attributes setup. */
10047 if (!isStateDirty(context, STATE_VDECL))
10048 context_apply_state(context, state, STATE_VDECL);
10051 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
10052 const struct wined3d_state *state, DWORD state_id)
10054 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
10055 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
10056 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10059 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
10060 const struct wined3d_state *state, DWORD state_id)
10062 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
10063 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
10064 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
10065 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
10066 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10069 static void glsl_vertex_pipe_world(struct wined3d_context *context,
10070 const struct wined3d_state *state, DWORD state_id)
10072 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
10075 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
10076 const struct wined3d_state *state, DWORD state_id)
10078 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10081 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
10083 const struct wined3d_gl_info *gl_info = context->gl_info;
10084 unsigned int k;
10086 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10087 | WINED3D_SHADER_CONST_FFP_LIGHTS
10088 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10090 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10092 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
10094 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
10095 clipplane(context, state, STATE_CLIPPLANE(k));
10098 else
10100 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10104 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
10105 const struct wined3d_state *state, DWORD state_id)
10107 /* Table fog behavior depends on the projection matrix. */
10108 if (state->render_states[WINED3D_RS_FOGENABLE]
10109 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
10110 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10111 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
10114 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
10115 const struct wined3d_state *state, DWORD state_id)
10117 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
10118 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
10119 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
10120 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
10121 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10122 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10125 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
10126 const struct wined3d_state *state, DWORD state_id)
10128 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10131 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
10132 const struct wined3d_state *state, DWORD state_id)
10134 DWORD sampler = state_id - STATE_SAMPLER(0);
10135 const struct wined3d_texture *texture = state->textures[sampler];
10136 BOOL np2;
10138 if (!texture)
10139 return;
10141 if (sampler >= MAX_TEXTURES)
10142 return;
10144 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
10145 || context->lastWasPow2Texture & (1u << sampler))
10147 if (np2)
10148 context->lastWasPow2Texture |= 1u << sampler;
10149 else
10150 context->lastWasPow2Texture &= ~(1u << sampler);
10152 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10156 static void glsl_vertex_pipe_material(struct wined3d_context *context,
10157 const struct wined3d_state *state, DWORD state_id)
10159 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10162 static void glsl_vertex_pipe_light(struct wined3d_context *context,
10163 const struct wined3d_state *state, DWORD state_id)
10165 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10168 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
10169 const struct wined3d_state *state, DWORD state_id)
10171 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10174 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
10175 const struct wined3d_state *state, DWORD state_id)
10177 if (!use_vs(state))
10178 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10181 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
10182 const struct wined3d_state *state, DWORD state_id)
10184 static unsigned int once;
10186 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
10187 FIXME("Non-point sprite points not supported in core profile.\n");
10190 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
10191 const struct wined3d_state *state, DWORD state_id)
10193 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10196 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
10197 const struct wined3d_state *state, DWORD state_id)
10199 const struct wined3d_gl_info *gl_info = context->gl_info;
10200 UINT index = state_id - STATE_CLIPPLANE(0);
10202 if (index >= gl_info->limits.user_clip_distances)
10203 return;
10205 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10208 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
10210 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10211 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
10212 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
10213 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
10214 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
10215 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
10216 /* Clip planes */
10217 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10218 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10219 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10220 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10221 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10222 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10223 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10224 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10225 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10226 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10227 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10228 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10229 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10230 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10231 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10232 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10233 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10234 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10235 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10236 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10237 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10238 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10239 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10240 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10241 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10242 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10243 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10244 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10245 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10246 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10247 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10248 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10249 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10250 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10251 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10252 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10253 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10254 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10255 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10256 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10257 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10258 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10259 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10260 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10261 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10262 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10263 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10264 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10265 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10266 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10267 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10268 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10269 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10270 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10271 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10272 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10273 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10274 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10275 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10276 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10277 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10278 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10279 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10280 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10281 /* Lights */
10282 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10283 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10284 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10285 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10286 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10287 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10288 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10289 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10290 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10291 /* Viewport */
10292 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
10293 /* Transform states */
10294 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
10295 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
10296 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10297 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10298 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10299 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10300 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10301 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10302 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10303 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10304 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
10305 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10306 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10307 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10308 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10309 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10310 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10311 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10312 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10313 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10314 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10315 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10316 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10317 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10318 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10319 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10320 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10321 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10322 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10323 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10324 /* Fog */
10325 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10326 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10327 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10328 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10329 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
10330 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
10331 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10332 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10333 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10334 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10335 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10336 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10337 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10338 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10339 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10340 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10341 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10342 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
10343 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
10344 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
10345 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
10346 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
10347 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10348 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10349 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10350 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10351 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10352 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10353 /* NP2 texture matrix fixups. They are not needed if
10354 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
10355 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
10356 * matrix. */
10357 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10358 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10359 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10360 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10361 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10362 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10363 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10364 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10365 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10366 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10367 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10368 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10369 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10370 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10371 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10372 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10373 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10374 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10375 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10376 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10377 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10378 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10379 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10380 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10381 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10382 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
10383 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
10384 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
10387 /* TODO:
10388 * - Implement vertex tweening. */
10389 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
10391 glsl_vertex_pipe_vp_enable,
10392 glsl_vertex_pipe_vp_get_caps,
10393 glsl_vertex_pipe_vp_get_emul_mask,
10394 glsl_vertex_pipe_vp_alloc,
10395 glsl_vertex_pipe_vp_free,
10396 glsl_vertex_pipe_vp_states,
10399 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
10401 /* Nothing to do. */
10404 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
10406 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
10407 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
10408 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
10409 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
10410 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
10411 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
10412 | WINED3DTEXOPCAPS_SELECTARG1
10413 | WINED3DTEXOPCAPS_SELECTARG2
10414 | WINED3DTEXOPCAPS_MODULATE4X
10415 | WINED3DTEXOPCAPS_MODULATE2X
10416 | WINED3DTEXOPCAPS_MODULATE
10417 | WINED3DTEXOPCAPS_ADDSIGNED2X
10418 | WINED3DTEXOPCAPS_ADDSIGNED
10419 | WINED3DTEXOPCAPS_ADD
10420 | WINED3DTEXOPCAPS_SUBTRACT
10421 | WINED3DTEXOPCAPS_ADDSMOOTH
10422 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
10423 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
10424 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
10425 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
10426 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
10427 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
10428 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
10429 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
10430 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
10431 | WINED3DTEXOPCAPS_DOTPRODUCT3
10432 | WINED3DTEXOPCAPS_MULTIPLYADD
10433 | WINED3DTEXOPCAPS_LERP
10434 | WINED3DTEXOPCAPS_BUMPENVMAP
10435 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
10436 caps->MaxTextureBlendStages = MAX_TEXTURES;
10437 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
10440 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
10442 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10443 return GL_EXT_EMUL_ARB_MULTITEXTURE;
10444 return 0;
10447 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
10449 struct shader_glsl_priv *priv;
10451 if (shader_backend == &glsl_shader_backend)
10453 priv = shader_priv;
10454 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
10455 return priv;
10458 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
10460 return NULL;
10463 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
10465 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
10466 struct glsl_ffp_fragment_shader, entry.entry);
10467 struct glsl_shader_prog_link *program, *program2;
10468 struct glsl_ffp_destroy_ctx *ctx = context;
10470 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
10471 struct glsl_shader_prog_link, ps.shader_entry)
10473 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
10475 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
10476 HeapFree(GetProcessHeap(), 0, shader);
10479 /* Context activation is done by the caller. */
10480 static void glsl_fragment_pipe_free(struct wined3d_device *device)
10482 struct shader_glsl_priv *priv = device->fragment_priv;
10483 struct glsl_ffp_destroy_ctx ctx;
10485 ctx.priv = priv;
10486 ctx.gl_info = &device->adapter->gl_info;
10487 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
10490 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
10491 const struct wined3d_state *state, DWORD state_id)
10493 context->last_was_pshader = use_ps(state);
10495 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10498 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
10499 const struct wined3d_state *state, DWORD state_id)
10501 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10504 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
10505 const struct wined3d_state *state, DWORD state_id)
10507 BOOL use_vshader = use_vs(state);
10508 enum fogsource new_source;
10509 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
10510 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
10512 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10514 if (!state->render_states[WINED3D_RS_FOGENABLE])
10515 return;
10517 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
10519 if (use_vshader)
10520 new_source = FOGSOURCE_VS;
10521 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
10522 new_source = FOGSOURCE_COORD;
10523 else
10524 new_source = FOGSOURCE_FFP;
10526 else
10528 new_source = FOGSOURCE_FFP;
10531 if (new_source != context->fog_source || fogstart == fogend)
10533 context->fog_source = new_source;
10534 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10538 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
10539 const struct wined3d_state *state, DWORD state_id)
10541 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10542 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10543 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10545 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
10546 glsl_fragment_pipe_fog(context, state, state_id);
10549 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
10550 const struct wined3d_state *state, DWORD state_id)
10552 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10553 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10554 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10557 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
10558 const struct wined3d_state *state, DWORD state_id)
10560 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10563 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
10564 const struct wined3d_state *state, DWORD state_id)
10566 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10569 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
10570 const struct wined3d_state *state, DWORD state_id)
10572 const struct wined3d_gl_info *gl_info = context->gl_info;
10573 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
10574 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
10576 if (func)
10578 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
10579 checkGLcall("glAlphaFunc");
10583 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
10584 const struct wined3d_state *state, DWORD state_id)
10586 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10589 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
10590 const struct wined3d_state *state, DWORD state_id)
10592 const struct wined3d_gl_info *gl_info = context->gl_info;
10594 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
10596 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
10597 checkGLcall("glEnable(GL_ALPHA_TEST)");
10599 else
10601 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
10602 checkGLcall("glDisable(GL_ALPHA_TEST)");
10606 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
10607 const struct wined3d_state *state, DWORD state_id)
10609 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10612 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
10613 const struct wined3d_state *state, DWORD state_id)
10615 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10618 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
10619 const struct wined3d_state *state, DWORD state_id)
10621 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10624 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
10626 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10627 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
10628 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10629 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10630 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10631 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10632 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10633 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10634 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10635 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10636 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10637 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10638 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10639 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10640 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10641 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10642 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10643 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10644 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10645 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10646 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10647 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10648 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10649 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10650 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10651 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10652 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10653 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10654 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10655 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10656 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10657 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10658 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10659 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10660 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10661 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10662 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10663 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10664 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10665 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10666 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10667 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10668 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10669 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10670 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10671 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10672 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10673 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10674 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10675 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10676 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10677 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10678 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10679 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10680 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10681 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10682 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10683 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10684 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10685 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10686 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10687 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10688 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10689 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10690 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10691 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10692 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10693 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10694 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10695 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10696 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10697 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10698 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10699 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10700 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10701 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10702 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
10703 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
10704 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
10705 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
10706 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
10707 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
10708 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10709 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
10710 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
10711 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10712 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10713 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10714 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
10715 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
10716 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10717 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10718 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10719 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
10720 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
10721 {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 },
10722 {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 },
10723 {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 },
10724 {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 },
10725 {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 },
10726 {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 },
10727 {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 },
10728 {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 },
10729 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10730 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10731 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10732 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10733 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10734 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10735 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10736 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10737 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10738 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10739 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
10740 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
10741 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
10744 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
10746 return TRUE;
10749 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
10753 const struct fragment_pipeline glsl_fragment_pipe =
10755 glsl_fragment_pipe_enable,
10756 glsl_fragment_pipe_get_caps,
10757 glsl_fragment_pipe_get_emul_mask,
10758 glsl_fragment_pipe_alloc,
10759 glsl_fragment_pipe_free,
10760 glsl_fragment_pipe_alloc_context_data,
10761 glsl_fragment_pipe_free_context_data,
10762 shader_glsl_color_fixup_supported,
10763 glsl_fragment_pipe_state_template,