wined3d: Move legacy texture image unit range allocation to wined3d_gl_limits_get_tex...
[wine.git] / dlls / wined3d / glsl_shader.c
blobf82d861bccc96520b4e329c71a7e515204c0ab9a
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 /* Context activation is done by the caller. */
562 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
563 struct shader_glsl_priv *priv, const char *prefix, unsigned int base_idx,
564 unsigned int count, const DWORD *tex_unit_map, GLuint program_id)
566 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
567 unsigned int mapped_unit, i;
568 GLint name_loc;
570 for (i = 0; i < count; ++i)
572 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
573 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
574 if (name_loc == -1)
575 continue;
577 mapped_unit = tex_unit_map ? tex_unit_map[base_idx + i] : base_idx + i;
578 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
580 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
581 continue;
584 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
585 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
587 checkGLcall("Load sampler bindings");
588 string_buffer_release(&priv->string_buffers, sampler_name);
591 /* Context activation is done by the caller. */
592 static void shader_glsl_load_graphics_samplers(const struct wined3d_gl_info *gl_info,
593 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
595 unsigned int i, base_idx, count;
597 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
599 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, i, &base_idx, &count);
600 shader_glsl_load_samplers(gl_info, priv, shader_glsl_get_prefix(i),
601 base_idx, count, tex_unit_map, program_id);
605 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
606 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
608 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
610 if (icb)
612 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
613 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
614 GLint icb_location;
616 string_buffer_sprintf(icb_name, "%s_icb", prefix);
617 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
618 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
619 checkGLcall("Load immediate constant buffer");
621 string_buffer_release(&priv->string_buffers, icb_name);
625 /* Context activation is done by the caller. */
626 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
627 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
629 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
630 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
631 GLint location;
632 unsigned int i;
634 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
636 if (!reg_maps->uav_resource_info[i].type)
637 continue;
639 string_buffer_sprintf(name, "%s_image%u", prefix, i);
640 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
641 if (location == -1)
642 continue;
644 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
645 GL_EXTCALL(glUniform1i(location, i));
647 checkGLcall("Load image bindings");
648 string_buffer_release(&priv->string_buffers, name);
651 /* Context activation is done by the caller. */
652 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
653 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
655 unsigned int start = ~0U, end = 0;
656 int stack_idx = 0;
657 unsigned int heap_idx = 1;
658 unsigned int idx;
660 if (heap->entries[heap_idx].version <= version) return;
662 idx = heap->entries[heap_idx].idx;
663 if (constant_locations[idx] != -1)
664 start = end = idx;
665 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
667 while (stack_idx >= 0)
669 /* Note that we fall through to the next case statement. */
670 switch(stack[stack_idx])
672 case HEAP_NODE_TRAVERSE_LEFT:
674 unsigned int left_idx = heap_idx << 1;
675 if (left_idx < heap->size && heap->entries[left_idx].version > version)
677 heap_idx = left_idx;
678 idx = heap->entries[heap_idx].idx;
679 if (constant_locations[idx] != -1)
681 if (start > idx)
682 start = idx;
683 if (end < idx)
684 end = idx;
687 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
688 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
689 break;
693 case HEAP_NODE_TRAVERSE_RIGHT:
695 unsigned int right_idx = (heap_idx << 1) + 1;
696 if (right_idx < heap->size && heap->entries[right_idx].version > version)
698 heap_idx = right_idx;
699 idx = heap->entries[heap_idx].idx;
700 if (constant_locations[idx] != -1)
702 if (start > idx)
703 start = idx;
704 if (end < idx)
705 end = idx;
708 stack[stack_idx++] = HEAP_NODE_POP;
709 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
710 break;
714 case HEAP_NODE_POP:
715 heap_idx >>= 1;
716 --stack_idx;
717 break;
720 if (start <= end)
721 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
722 checkGLcall("walk_constant_heap()");
725 /* Context activation is done by the caller. */
726 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
727 GLint location, const struct wined3d_vec4 *data)
729 GLfloat clamped_constant[4];
731 if (location == -1) return;
733 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
734 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
735 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
736 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
738 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
741 /* Context activation is done by the caller. */
742 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
743 const struct wined3d_vec4 *constants, const GLint *constant_locations,
744 const struct constant_heap *heap, unsigned char *stack, DWORD version)
746 int stack_idx = 0;
747 unsigned int heap_idx = 1;
748 unsigned int idx;
750 if (heap->entries[heap_idx].version <= version) return;
752 idx = heap->entries[heap_idx].idx;
753 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
754 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
756 while (stack_idx >= 0)
758 /* Note that we fall through to the next case statement. */
759 switch(stack[stack_idx])
761 case HEAP_NODE_TRAVERSE_LEFT:
763 unsigned int left_idx = heap_idx << 1;
764 if (left_idx < heap->size && heap->entries[left_idx].version > version)
766 heap_idx = left_idx;
767 idx = heap->entries[heap_idx].idx;
768 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
770 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
771 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
772 break;
776 case HEAP_NODE_TRAVERSE_RIGHT:
778 unsigned int right_idx = (heap_idx << 1) + 1;
779 if (right_idx < heap->size && heap->entries[right_idx].version > version)
781 heap_idx = right_idx;
782 idx = heap->entries[heap_idx].idx;
783 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
785 stack[stack_idx++] = HEAP_NODE_POP;
786 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
787 break;
791 case HEAP_NODE_POP:
792 heap_idx >>= 1;
793 --stack_idx;
794 break;
797 checkGLcall("walk_constant_heap_clamped()");
800 /* Context activation is done by the caller. */
801 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
802 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
803 unsigned char *stack, unsigned int version)
805 const struct wined3d_shader_lconst *lconst;
807 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
808 if (shader->reg_maps.shader_version.major == 1
809 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
810 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
811 else
812 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
814 if (!shader->load_local_constsF)
816 TRACE("No need to load local float constants for this shader.\n");
817 return;
820 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
821 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
823 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
825 checkGLcall("glUniform4fv()");
828 /* Context activation is done by the caller. */
829 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
830 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
832 unsigned int i;
833 struct list* ptr;
835 for (i = 0; constants_set; constants_set >>= 1, ++i)
837 if (!(constants_set & 1)) continue;
839 /* We found this uniform name in the program - go ahead and send the data */
840 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
843 /* Load immediate constants */
844 ptr = list_head(&shader->constantsI);
845 while (ptr)
847 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
848 unsigned int idx = lconst->idx;
849 const GLint *values = (const GLint *)lconst->value;
851 /* We found this uniform name in the program - go ahead and send the data */
852 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
853 ptr = list_next(&shader->constantsI, ptr);
855 checkGLcall("glUniform4iv()");
858 /* Context activation is done by the caller. */
859 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
860 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
862 unsigned int i;
863 struct list* ptr;
865 for (i = 0; constants_set; constants_set >>= 1, ++i)
867 if (!(constants_set & 1)) continue;
869 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
872 /* Load immediate constants */
873 ptr = list_head(&shader->constantsB);
874 while (ptr)
876 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
877 unsigned int idx = lconst->idx;
878 const GLint *values = (const GLint *)lconst->value;
880 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
881 ptr = list_next(&shader->constantsB, ptr);
883 checkGLcall("glUniform1iv()");
886 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
888 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
891 /* Context activation is done by the caller (state handler). */
892 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
893 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
895 struct
897 float sx, sy;
899 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
900 UINT fixup = ps->np2_fixup_info->active;
901 UINT i;
903 for (i = 0; fixup; fixup >>= 1, ++i)
905 const struct wined3d_texture *tex = state->textures[i];
906 unsigned char idx = ps->np2_fixup_info->idx[i];
908 if (!tex)
910 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
911 continue;
914 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
915 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
918 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
921 /* Taken and adapted from Mesa. */
922 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
924 float pos, neg, t, det;
925 struct wined3d_matrix temp;
927 /* Calculate the determinant of upper left 3x3 submatrix and
928 * determine if the matrix is singular. */
929 pos = neg = 0.0f;
930 t = in->_11 * in->_22 * in->_33;
931 if (t >= 0.0f)
932 pos += t;
933 else
934 neg += t;
936 t = in->_21 * in->_32 * in->_13;
937 if (t >= 0.0f)
938 pos += t;
939 else
940 neg += t;
941 t = in->_31 * in->_12 * in->_23;
942 if (t >= 0.0f)
943 pos += t;
944 else
945 neg += t;
947 t = -in->_31 * in->_22 * in->_13;
948 if (t >= 0.0f)
949 pos += t;
950 else
951 neg += t;
952 t = -in->_21 * in->_12 * in->_33;
953 if (t >= 0.0f)
954 pos += t;
955 else
956 neg += t;
958 t = -in->_11 * in->_32 * in->_23;
959 if (t >= 0.0f)
960 pos += t;
961 else
962 neg += t;
964 det = pos + neg;
966 if (fabsf(det) < 1e-25f)
967 return FALSE;
969 det = 1.0f / det;
970 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
971 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
972 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
973 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
974 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
975 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
976 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
977 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
978 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
980 *out = temp;
981 return TRUE;
984 static void swap_rows(float **a, float **b)
986 float *tmp = *a;
988 *a = *b;
989 *b = tmp;
992 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
994 float wtmp[4][8];
995 float m0, m1, m2, m3, s;
996 float *r0, *r1, *r2, *r3;
998 r0 = wtmp[0];
999 r1 = wtmp[1];
1000 r2 = wtmp[2];
1001 r3 = wtmp[3];
1003 r0[0] = m->_11;
1004 r0[1] = m->_12;
1005 r0[2] = m->_13;
1006 r0[3] = m->_14;
1007 r0[4] = 1.0f;
1008 r0[5] = r0[6] = r0[7] = 0.0f;
1010 r1[0] = m->_21;
1011 r1[1] = m->_22;
1012 r1[2] = m->_23;
1013 r1[3] = m->_24;
1014 r1[5] = 1.0f;
1015 r1[4] = r1[6] = r1[7] = 0.0f;
1017 r2[0] = m->_31;
1018 r2[1] = m->_32;
1019 r2[2] = m->_33;
1020 r2[3] = m->_34;
1021 r2[6] = 1.0f;
1022 r2[4] = r2[5] = r2[7] = 0.0f;
1024 r3[0] = m->_41;
1025 r3[1] = m->_42;
1026 r3[2] = m->_43;
1027 r3[3] = m->_44;
1028 r3[7] = 1.0f;
1029 r3[4] = r3[5] = r3[6] = 0.0f;
1031 /* Choose pivot - or die. */
1032 if (fabsf(r3[0]) > fabsf(r2[0]))
1033 swap_rows(&r3, &r2);
1034 if (fabsf(r2[0]) > fabsf(r1[0]))
1035 swap_rows(&r2, &r1);
1036 if (fabsf(r1[0]) > fabsf(r0[0]))
1037 swap_rows(&r1, &r0);
1038 if (r0[0] == 0.0f)
1039 return FALSE;
1041 /* Eliminate first variable. */
1042 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1043 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1044 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1045 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1046 s = r0[4];
1047 if (s != 0.0f)
1049 r1[4] -= m1 * s;
1050 r2[4] -= m2 * s;
1051 r3[4] -= m3 * s;
1053 s = r0[5];
1054 if (s != 0.0f)
1056 r1[5] -= m1 * s;
1057 r2[5] -= m2 * s;
1058 r3[5] -= m3 * s;
1060 s = r0[6];
1061 if (s != 0.0f)
1063 r1[6] -= m1 * s;
1064 r2[6] -= m2 * s;
1065 r3[6] -= m3 * s;
1067 s = r0[7];
1068 if (s != 0.0f)
1070 r1[7] -= m1 * s;
1071 r2[7] -= m2 * s;
1072 r3[7] -= m3 * s;
1075 /* Choose pivot - or die. */
1076 if (fabsf(r3[1]) > fabsf(r2[1]))
1077 swap_rows(&r3, &r2);
1078 if (fabsf(r2[1]) > fabsf(r1[1]))
1079 swap_rows(&r2, &r1);
1080 if (r1[1] == 0.0f)
1081 return FALSE;
1083 /* Eliminate second variable. */
1084 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1085 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1086 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1087 s = r1[4];
1088 if (s != 0.0f)
1090 r2[4] -= m2 * s;
1091 r3[4] -= m3 * s;
1093 s = r1[5];
1094 if (s != 0.0f)
1096 r2[5] -= m2 * s;
1097 r3[5] -= m3 * s;
1099 s = r1[6];
1100 if (s != 0.0f)
1102 r2[6] -= m2 * s;
1103 r3[6] -= m3 * s;
1105 s = r1[7];
1106 if (s != 0.0f)
1108 r2[7] -= m2 * s;
1109 r3[7] -= m3 * s;
1112 /* Choose pivot - or die. */
1113 if (fabsf(r3[2]) > fabsf(r2[2]))
1114 swap_rows(&r3, &r2);
1115 if (r2[2] == 0.0f)
1116 return FALSE;
1118 /* Eliminate third variable. */
1119 m3 = r3[2] / r2[2];
1120 r3[3] -= m3 * r2[3];
1121 r3[4] -= m3 * r2[4];
1122 r3[5] -= m3 * r2[5];
1123 r3[6] -= m3 * r2[6];
1124 r3[7] -= m3 * r2[7];
1126 /* Last check. */
1127 if (r3[3] == 0.0f)
1128 return FALSE;
1130 /* Back substitute row 3. */
1131 s = 1.0f / r3[3];
1132 r3[4] *= s;
1133 r3[5] *= s;
1134 r3[6] *= s;
1135 r3[7] *= s;
1137 /* Back substitute row 2. */
1138 m2 = r2[3];
1139 s = 1.0f / r2[2];
1140 r2[4] = s * (r2[4] - r3[4] * m2);
1141 r2[5] = s * (r2[5] - r3[5] * m2);
1142 r2[6] = s * (r2[6] - r3[6] * m2);
1143 r2[7] = s * (r2[7] - r3[7] * m2);
1144 m1 = r1[3];
1145 r1[4] -= r3[4] * m1;
1146 r1[5] -= r3[5] * m1;
1147 r1[6] -= r3[6] * m1;
1148 r1[7] -= r3[7] * m1;
1149 m0 = r0[3];
1150 r0[4] -= r3[4] * m0;
1151 r0[5] -= r3[5] * m0;
1152 r0[6] -= r3[6] * m0;
1153 r0[7] -= r3[7] * m0;
1155 /* Back substitute row 1. */
1156 m1 = r1[2];
1157 s = 1.0f / r1[1];
1158 r1[4] = s * (r1[4] - r2[4] * m1);
1159 r1[5] = s * (r1[5] - r2[5] * m1);
1160 r1[6] = s * (r1[6] - r2[6] * m1);
1161 r1[7] = s * (r1[7] - r2[7] * m1);
1162 m0 = r0[2];
1163 r0[4] -= r2[4] * m0;
1164 r0[5] -= r2[5] * m0;
1165 r0[6] -= r2[6] * m0;
1166 r0[7] -= r2[7] * m0;
1168 /* Back substitute row 0. */
1169 m0 = r0[1];
1170 s = 1.0f / r0[0];
1171 r0[4] = s * (r0[4] - r1[4] * m0);
1172 r0[5] = s * (r0[5] - r1[5] * m0);
1173 r0[6] = s * (r0[6] - r1[6] * m0);
1174 r0[7] = s * (r0[7] - r1[7] * m0);
1176 out->_11 = r0[4];
1177 out->_12 = r0[5];
1178 out->_13 = r0[6];
1179 out->_14 = r0[7];
1180 out->_21 = r1[4];
1181 out->_22 = r1[5];
1182 out->_23 = r1[6];
1183 out->_24 = r1[7];
1184 out->_31 = r2[4];
1185 out->_32 = r2[5];
1186 out->_33 = r2[6];
1187 out->_34 = r2[7];
1188 out->_41 = r3[4];
1189 out->_42 = r3[5];
1190 out->_43 = r3[6];
1191 out->_44 = r3[7];
1193 return TRUE;
1196 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1197 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1199 const struct wined3d_gl_info *gl_info = context->gl_info;
1200 float mat[3 * 3];
1201 struct wined3d_matrix mv;
1202 unsigned int i, j;
1204 if (prog->vs.normal_matrix_location == -1)
1205 return;
1207 get_modelview_matrix(context, state, 0, &mv);
1208 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1209 invert_matrix_3d(&mv, &mv);
1210 else
1211 invert_matrix(&mv, &mv);
1212 /* Tests show that singular modelview matrices are used unchanged as normal
1213 * matrices on D3D3 and older. There seems to be no clearly consistent
1214 * behavior on newer D3D versions so always follow older ddraw behavior. */
1215 for (i = 0; i < 3; ++i)
1216 for (j = 0; j < 3; ++j)
1217 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1219 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1220 checkGLcall("glUniformMatrix3fv");
1223 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1224 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1226 const struct wined3d_gl_info *gl_info = context->gl_info;
1227 struct wined3d_matrix mat;
1229 if (tex >= MAX_TEXTURES)
1230 return;
1231 if (prog->vs.texture_matrix_location[tex] == -1)
1232 return;
1234 get_texture_matrix(context, state, tex, &mat);
1235 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1236 checkGLcall("glUniformMatrix4fv");
1239 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1240 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1242 const struct wined3d_gl_info *gl_info = context->gl_info;
1244 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1246 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1247 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1249 else
1251 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1253 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1255 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1256 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1257 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1258 checkGLcall("setting FFP material uniforms");
1261 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1262 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1264 const struct wined3d_gl_info *gl_info = context->gl_info;
1265 struct wined3d_color color;
1267 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1268 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1269 checkGLcall("glUniform3fv");
1272 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1273 const struct wined3d_matrix *src2)
1275 struct wined3d_vec4 temp;
1277 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1278 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1279 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1280 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1282 *dest = temp;
1285 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1286 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1287 struct glsl_shader_prog_link *prog)
1289 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1290 const struct wined3d_gl_info *gl_info = context->gl_info;
1291 struct wined3d_vec4 vec4;
1293 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1294 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1295 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1297 switch (light_info->OriginalParms.type)
1299 case WINED3D_LIGHT_POINT:
1300 multiply_vector_matrix(&vec4, &light_info->position, view);
1301 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1302 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1303 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1304 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1305 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1306 break;
1308 case WINED3D_LIGHT_SPOT:
1309 multiply_vector_matrix(&vec4, &light_info->position, view);
1310 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1312 multiply_vector_matrix(&vec4, &light_info->direction, view);
1313 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1315 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1316 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1317 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1318 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1319 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1320 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1321 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1322 break;
1324 case WINED3D_LIGHT_DIRECTIONAL:
1325 multiply_vector_matrix(&vec4, &light_info->direction, view);
1326 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1327 break;
1329 case WINED3D_LIGHT_PARALLELPOINT:
1330 multiply_vector_matrix(&vec4, &light_info->position, view);
1331 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1332 break;
1334 default:
1335 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1337 checkGLcall("setting FFP lights uniforms");
1340 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1341 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1343 const struct wined3d_gl_info *gl_info = context->gl_info;
1344 float min, max;
1345 float size, att[3];
1347 get_pointsize_minmax(context, state, &min, &max);
1349 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1350 checkGLcall("glUniform1f");
1351 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1352 checkGLcall("glUniform1f");
1354 get_pointsize(context, state, &size, att);
1356 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1357 checkGLcall("glUniform1f");
1358 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1359 checkGLcall("glUniform1f");
1360 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1361 checkGLcall("glUniform1f");
1362 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1363 checkGLcall("glUniform1f");
1366 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1367 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1369 const struct wined3d_gl_info *gl_info = context->gl_info;
1370 struct wined3d_color color;
1371 float start, end, scale;
1372 union
1374 DWORD d;
1375 float f;
1376 } tmpvalue;
1378 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1379 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1380 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1381 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1382 get_fog_start_end(context, state, &start, &end);
1383 scale = 1.0f / (end - start);
1384 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1385 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1386 checkGLcall("fog emulation uniforms");
1389 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1390 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1392 const struct wined3d_gl_info *gl_info = context->gl_info;
1393 struct wined3d_vec4 plane;
1395 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1396 if (!use_vs(state))
1397 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1398 else
1399 plane = state->clip_planes[index];
1401 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1404 /* Context activation is done by the caller (state handler). */
1405 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1406 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1408 struct wined3d_color float_key[2];
1409 const struct wined3d_texture *texture = state->textures[0];
1411 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1412 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1415 /* Context activation is done by the caller (state handler). */
1416 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1417 const struct wined3d_state *state)
1419 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1420 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1421 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1422 const struct wined3d_gl_info *gl_info = context->gl_info;
1423 struct shader_glsl_priv *priv = shader_priv;
1424 float position_fixup[4];
1425 DWORD update_mask;
1427 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1428 UINT constant_version;
1429 int i;
1431 if (!prog) {
1432 /* No GLSL program set - nothing to do. */
1433 return;
1435 constant_version = prog->constant_version;
1436 update_mask = context->constant_update_mask & prog->constant_update_mask;
1438 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1439 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1440 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1442 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1443 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1444 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1446 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1447 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1448 vshader->reg_maps.boolean_constants);
1450 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1452 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1453 shader_glsl_clip_plane_uniform(context, state, i, prog);
1456 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1457 shader_glsl_pointsize_uniform(context, state, prog);
1459 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1461 shader_get_position_fixup(context, state, position_fixup);
1462 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1463 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1464 else
1465 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1466 checkGLcall("glUniform4fv");
1469 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1471 struct wined3d_matrix mat;
1473 get_modelview_matrix(context, state, 0, &mat);
1474 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1475 checkGLcall("glUniformMatrix4fv");
1477 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1480 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1482 struct wined3d_matrix mat;
1484 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1486 if (prog->vs.modelview_matrix_location[i] == -1)
1487 break;
1489 get_modelview_matrix(context, state, i, &mat);
1490 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1491 checkGLcall("glUniformMatrix4fv");
1495 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1497 struct wined3d_matrix projection;
1499 get_projection_matrix(context, state, &projection);
1500 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1501 checkGLcall("glUniformMatrix4fv");
1504 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1506 for (i = 0; i < MAX_TEXTURES; ++i)
1507 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1510 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1511 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1513 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1515 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1516 DWORD point_count = 0;
1517 DWORD spot_count = 0;
1518 DWORD directional_count = 0;
1519 DWORD parallel_point_count = 0;
1521 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1523 if (!state->lights[i])
1524 continue;
1526 switch (state->lights[i]->OriginalParms.type)
1528 case WINED3D_LIGHT_POINT:
1529 ++point_count;
1530 break;
1531 case WINED3D_LIGHT_SPOT:
1532 ++spot_count;
1533 break;
1534 case WINED3D_LIGHT_DIRECTIONAL:
1535 ++directional_count;
1536 break;
1537 case WINED3D_LIGHT_PARALLELPOINT:
1538 ++parallel_point_count;
1539 break;
1540 default:
1541 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1542 break;
1545 point_idx = 0;
1546 spot_idx = point_idx + point_count;
1547 directional_idx = spot_idx + spot_count;
1548 parallel_point_idx = directional_idx + directional_count;
1550 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1551 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1553 const struct wined3d_light_info *light_info = state->lights[i];
1554 unsigned int idx;
1556 if (!light_info)
1557 continue;
1559 switch (light_info->OriginalParms.type)
1561 case WINED3D_LIGHT_POINT:
1562 idx = point_idx++;
1563 break;
1564 case WINED3D_LIGHT_SPOT:
1565 idx = spot_idx++;
1566 break;
1567 case WINED3D_LIGHT_DIRECTIONAL:
1568 idx = directional_idx++;
1569 break;
1570 case WINED3D_LIGHT_PARALLELPOINT:
1571 idx = parallel_point_idx++;
1572 break;
1573 default:
1574 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1575 continue;
1577 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1581 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1582 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1583 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1585 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1586 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1587 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1589 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1590 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1591 pshader->reg_maps.boolean_constants);
1593 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1595 for (i = 0; i < MAX_TEXTURES; ++i)
1597 if (prog->ps.bumpenv_mat_location[i] == -1)
1598 continue;
1600 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1601 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1603 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1605 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1606 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1607 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1608 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1612 checkGLcall("bump env uniforms");
1615 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1617 const struct wined3d_vec4 correction_params =
1619 /* Position is relative to the framebuffer, not the viewport. */
1620 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1621 context->render_offscreen ? 1.0f : -1.0f,
1622 0.0f,
1623 0.0f,
1626 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1629 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1630 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1631 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1632 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1634 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1636 struct wined3d_color color;
1638 if (prog->ps.tex_factor_location != -1)
1640 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1641 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1644 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1645 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1646 else
1647 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1649 for (i = 0; i < MAX_TEXTURES; ++i)
1651 if (prog->ps.tss_constant_location[i] == -1)
1652 continue;
1654 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1655 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1658 checkGLcall("fixed function uniforms");
1661 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1662 shader_glsl_load_fog_uniform(context, state, prog);
1664 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1666 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1668 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1669 checkGLcall("alpha test emulation uniform");
1672 if (priv->next_constant_version == UINT_MAX)
1674 TRACE("Max constant version reached, resetting to 0.\n");
1675 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1676 priv->next_constant_version = 1;
1678 else
1680 prog->constant_version = priv->next_constant_version++;
1684 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1686 struct constant_entry *entries = heap->entries;
1687 unsigned int *positions = heap->positions;
1688 unsigned int heap_idx, parent_idx;
1690 if (!heap->contained[idx])
1692 heap_idx = heap->size++;
1693 heap->contained[idx] = TRUE;
1695 else
1697 heap_idx = positions[idx];
1700 while (heap_idx > 1)
1702 parent_idx = heap_idx >> 1;
1704 if (new_version <= entries[parent_idx].version) break;
1706 entries[heap_idx] = entries[parent_idx];
1707 positions[entries[parent_idx].idx] = heap_idx;
1708 heap_idx = parent_idx;
1711 entries[heap_idx].version = new_version;
1712 entries[heap_idx].idx = idx;
1713 positions[idx] = heap_idx;
1716 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1718 struct shader_glsl_priv *priv = device->shader_priv;
1719 struct constant_heap *heap = &priv->vconst_heap;
1720 UINT i;
1722 for (i = start; i < count + start; ++i)
1724 update_heap_entry(heap, i, priv->next_constant_version);
1728 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1730 struct shader_glsl_priv *priv = device->shader_priv;
1731 struct constant_heap *heap = &priv->pconst_heap;
1732 UINT i;
1734 for (i = start; i < count + start; ++i)
1736 update_heap_entry(heap, i, priv->next_constant_version);
1740 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1742 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1743 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1744 if(shader_major > 3) return ret;
1746 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1747 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1748 return ret;
1751 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1753 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1756 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1758 return !needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION];
1761 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1763 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1766 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1767 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1769 va_list args;
1770 int ret;
1772 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1773 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1774 for (;;)
1776 va_start(args, format);
1777 ret = shader_vaddline(buffer, format, args);
1778 va_end(args);
1779 if (!ret)
1780 return;
1781 if (!string_buffer_resize(buffer, ret))
1782 return;
1786 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1787 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1789 va_list args;
1790 int ret;
1792 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1793 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1794 for (;;)
1796 va_start(args, format);
1797 ret = shader_vaddline(buffer, format, args);
1798 va_end(args);
1799 if (!ret)
1800 return;
1801 if (!string_buffer_resize(buffer, ret))
1802 return;
1806 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1808 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1811 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1813 switch (primitive_type)
1815 case WINED3D_PT_POINTLIST:
1816 return "points";
1818 case WINED3D_PT_LINELIST:
1819 return "lines";
1821 case WINED3D_PT_LINESTRIP:
1822 return "line_strip";
1824 case WINED3D_PT_TRIANGLELIST:
1825 return "triangles";
1827 case WINED3D_PT_TRIANGLESTRIP:
1828 return "triangle_strip";
1830 case WINED3D_PT_LINELIST_ADJ:
1831 return "lines_adjacency";
1833 case WINED3D_PT_TRIANGLELIST_ADJ:
1834 return "triangles_adjacency";
1836 default:
1837 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1838 return "";
1842 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1844 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1845 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1846 DWORD input_reg_used = shader->u.ps.input_reg_used;
1847 unsigned int i;
1849 if (reg_maps->shader_version.major < 3)
1850 return input_reg_used & (1u << idx);
1852 for (i = 0; i < input_signature->element_count; ++i)
1854 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1856 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1857 continue;
1859 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1860 && input->semantic_idx == idx)
1861 return input_reg_used & (1u << input->register_idx);
1863 return FALSE;
1866 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1867 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1869 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1871 if (version->major >= 4)
1872 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1873 else
1874 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1877 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1878 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1879 unsigned int index)
1881 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1882 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1883 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1884 index, scalar_type, scalar_type, index);
1887 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1888 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1890 unsigned int index = e->register_idx;
1892 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1894 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1895 index);
1896 return;
1898 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1900 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1901 index);
1902 return;
1904 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1905 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1907 if (shader_glsl_use_explicit_attrib_location(gl_info))
1908 shader_addline(buffer, "layout(location = %u) ", index);
1910 switch (e->component_type)
1912 case WINED3D_TYPE_UINT:
1913 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1914 break;
1915 case WINED3D_TYPE_INT:
1916 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1917 break;
1919 default:
1920 FIXME("Unhandled type %#x.\n", e->component_type);
1921 /* Fall through. */
1922 case WINED3D_TYPE_UNKNOWN:
1923 case WINED3D_TYPE_FLOAT:
1924 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1925 break;
1929 /** Generate the variable & register declarations for the GLSL output target */
1930 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1931 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1932 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1934 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1935 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1936 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1937 const struct wined3d_gl_info *gl_info = context->gl_info;
1938 const struct wined3d_shader_indexable_temp *idx_temp_reg;
1939 unsigned int i, extra_constants_needed = 0;
1940 const struct wined3d_shader_lconst *lconst;
1941 const char *prefix;
1942 DWORD map;
1944 prefix = shader_glsl_get_prefix(version->type);
1946 /* Prototype the subroutines */
1947 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1949 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1952 /* Declare the constants (aka uniforms) */
1953 if (shader->limits->constant_float > 0)
1955 unsigned max_constantsF;
1957 /* Unless the shader uses indirect addressing, always declare the
1958 * maximum array size and ignore that we need some uniforms privately.
1959 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1960 * and immediate values, still declare VC[256]. If the shader needs
1961 * more uniforms than we have it won't work in any case. If it uses
1962 * less, the compiler will figure out which uniforms are really used
1963 * and strip them out. This allows a shader to use c255 on a dx9 card,
1964 * as long as it doesn't also use all the other constants.
1966 * If the shader uses indirect addressing the compiler must assume
1967 * that all declared uniforms are used. In this case, declare only the
1968 * amount that we're assured to have.
1970 * Thus we run into problems in these two cases:
1971 * 1) The shader really uses more uniforms than supported.
1972 * 2) The shader uses indirect addressing, less constants than
1973 * supported, but uses a constant index > #supported consts. */
1974 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1976 /* No indirect addressing here. */
1977 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1979 else
1981 if (reg_maps->usesrelconstF)
1983 /* Subtract the other potential uniforms from the max
1984 * available (bools, ints, and 1 row of projection matrix).
1985 * Subtract another uniform for immediate values, which have
1986 * to be loaded via uniform by the driver as well. The shader
1987 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1988 * shader code, so one vec4 should be enough. (Unfortunately
1989 * the Nvidia driver doesn't store 128 and -128 in one float).
1991 * Writing gl_ClipVertex requires one uniform for each
1992 * clipplane as well. */
1993 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1994 if (vs_args->clip_enabled)
1995 max_constantsF -= gl_info->limits.user_clip_distances;
1996 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1997 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1998 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1999 * for now take this into account when calculating the number of available constants
2001 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2002 /* Set by driver quirks in directx.c */
2003 max_constantsF -= gl_info->reserved_glsl_constants;
2005 if (max_constantsF < shader->limits->constant_float)
2007 static unsigned int once;
2009 if (!once++)
2010 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2011 " it may not render correctly.\n");
2012 else
2013 WARN("The hardware does not support enough uniform components to run this shader.\n");
2016 else
2018 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2021 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2022 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2025 /* Always declare the full set of constants, the compiler can remove the
2026 * unused ones because d3d doesn't (yet) support indirect int and bool
2027 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2028 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2029 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2031 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2032 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2034 /* Declare immediate constant buffer */
2035 if (reg_maps->icb)
2036 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2038 /* Declare constant buffers */
2039 for (i = 0; i < WINED3D_MAX_CBS; ++i)
2041 if (reg_maps->cb_sizes[i])
2042 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2043 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2046 /* Declare texture samplers */
2047 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2049 struct wined3d_shader_sampler_map_entry *entry;
2050 const char *sampler_type_prefix, *sampler_type;
2051 BOOL shadow_sampler, tex_rect;
2053 entry = &reg_maps->sampler_map.entries[i];
2055 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2057 ERR("Invalid resource index %u.\n", entry->resource_idx);
2058 continue;
2061 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2063 case WINED3D_DATA_FLOAT:
2064 case WINED3D_DATA_UNORM:
2065 case WINED3D_DATA_SNORM:
2066 sampler_type_prefix = "";
2067 break;
2069 case WINED3D_DATA_INT:
2070 sampler_type_prefix = "i";
2071 break;
2073 case WINED3D_DATA_UINT:
2074 sampler_type_prefix = "u";
2075 break;
2077 default:
2078 sampler_type_prefix = "";
2079 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2080 break;
2083 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2084 switch (reg_maps->resource_info[entry->resource_idx].type)
2086 case WINED3D_SHADER_RESOURCE_BUFFER:
2087 sampler_type = "samplerBuffer";
2088 break;
2090 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2091 if (shadow_sampler)
2092 sampler_type = "sampler1DShadow";
2093 else
2094 sampler_type = "sampler1D";
2095 break;
2097 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2098 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2099 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2100 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2101 if (shadow_sampler)
2103 if (tex_rect)
2104 sampler_type = "sampler2DRectShadow";
2105 else
2106 sampler_type = "sampler2DShadow";
2108 else
2110 if (tex_rect)
2111 sampler_type = "sampler2DRect";
2112 else
2113 sampler_type = "sampler2D";
2115 break;
2117 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2118 if (shadow_sampler)
2119 FIXME("Unsupported 3D shadow sampler.\n");
2120 sampler_type = "sampler3D";
2121 break;
2123 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2124 if (shadow_sampler)
2125 FIXME("Unsupported Cube shadow sampler.\n");
2126 sampler_type = "samplerCube";
2127 break;
2129 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2130 if (shadow_sampler)
2131 sampler_type = "sampler2DArrayShadow";
2132 else
2133 sampler_type = "sampler2DArray";
2134 break;
2136 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2137 if (shadow_sampler)
2138 FIXME("Unsupported Cube array shadow sampler.\n");
2139 sampler_type = "samplerCubeArray";
2140 break;
2142 default:
2143 sampler_type = "unsupported_sampler";
2144 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2145 break;
2147 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2148 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2151 /* Declare images */
2152 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2154 const char *image_type_prefix, *image_type, *read_format;
2156 if (!reg_maps->uav_resource_info[i].type)
2157 continue;
2159 switch (reg_maps->uav_resource_info[i].data_type)
2161 case WINED3D_DATA_FLOAT:
2162 case WINED3D_DATA_UNORM:
2163 case WINED3D_DATA_SNORM:
2164 image_type_prefix = "";
2165 read_format = "r32f";
2166 break;
2168 case WINED3D_DATA_INT:
2169 image_type_prefix = "i";
2170 read_format = "r32i";
2171 break;
2173 case WINED3D_DATA_UINT:
2174 image_type_prefix = "u";
2175 read_format = "r32ui";
2176 break;
2178 default:
2179 image_type_prefix = "";
2180 read_format = "";
2181 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2182 break;
2185 switch (reg_maps->uav_resource_info[i].type)
2187 case WINED3D_SHADER_RESOURCE_BUFFER:
2188 image_type = "imageBuffer";
2189 break;
2191 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2192 image_type = "image2D";
2193 break;
2195 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2196 image_type = "image3D";
2197 break;
2199 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2200 image_type = "image2DArray";
2201 break;
2203 default:
2204 image_type = "unsupported_image";
2205 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2206 break;
2209 if (reg_maps->uav_read_mask & (1u << i))
2210 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2211 read_format, image_type_prefix, image_type, prefix, i);
2212 else
2213 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2214 image_type_prefix, image_type, prefix, i);
2216 if (reg_maps->uav_counter_mask & (1u << i))
2217 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2218 i, prefix, i);
2221 /* Declare uniforms for NP2 texcoord fixup:
2222 * This is NOT done inside the loop that declares the texture samplers
2223 * since the NP2 fixup code is currently only used for the GeforceFX
2224 * series and when forcing the ARB_npot extension off. Modern cards just
2225 * skip the code anyway, so put it inside a separate loop. */
2226 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2228 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2229 UINT cur = 0;
2231 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2232 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2233 * samplerNP2Fixup stores texture dimensions and is updated through
2234 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2236 for (i = 0; i < shader->limits->sampler; ++i)
2238 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2239 continue;
2241 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2243 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2244 continue;
2247 fixup->idx[i] = cur++;
2250 fixup->num_consts = (cur + 1) >> 1;
2251 fixup->active = ps_args->np2_fixup;
2252 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2255 /* Declare address variables */
2256 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2258 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2261 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2263 for (i = 0; i < shader->input_signature.element_count; ++i)
2264 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2266 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2268 shader_addline(buffer, "uniform struct\n{\n");
2269 shader_addline(buffer, " float size;\n");
2270 shader_addline(buffer, " float size_min;\n");
2271 shader_addline(buffer, " float size_max;\n");
2272 shader_addline(buffer, "} ffp_point;\n");
2275 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2277 if (vs_args->clip_enabled)
2278 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
2280 if (version->major < 3)
2282 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2283 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2284 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2285 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2289 if (version->major < 4)
2290 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2292 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2294 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2296 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2298 else
2300 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2301 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2302 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2303 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2306 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2308 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2310 shader_addline(buffer, "uniform struct\n{\n");
2311 shader_addline(buffer, " vec4 color;\n");
2312 shader_addline(buffer, " float density;\n");
2313 shader_addline(buffer, " float end;\n");
2314 shader_addline(buffer, " float scale;\n");
2315 shader_addline(buffer, "} ffp_fog;\n");
2317 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2319 if (glsl_is_color_reg_read(shader, 0))
2320 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2321 if (glsl_is_color_reg_read(shader, 1))
2322 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2323 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2324 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2326 else
2328 if (glsl_is_color_reg_read(shader, 0))
2329 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2330 if (glsl_is_color_reg_read(shader, 1))
2331 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2332 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2333 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2334 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2338 if (version->major >= 3)
2340 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2342 if (ps_args->vp_mode == vertexshader)
2343 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2344 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2347 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2349 if (!(map & 1))
2350 continue;
2352 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2354 if (reg_maps->luminanceparams & (1u << i))
2356 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2357 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2358 extra_constants_needed++;
2361 extra_constants_needed++;
2364 if (ps_args->srgb_correction)
2366 shader_addline(buffer, "const vec4 srgb_const0 = ");
2367 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2368 shader_addline(buffer, ";\n");
2369 shader_addline(buffer, "const vec4 srgb_const1 = ");
2370 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2371 shader_addline(buffer, ";\n");
2373 if (reg_maps->vpos || reg_maps->usesdsy)
2375 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2377 ++extra_constants_needed;
2378 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2380 if (reg_maps->vpos)
2382 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2384 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
2385 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2386 ps_args->render_offscreen ? "" : "origin_upper_left, ");
2387 else if (!ps_args->render_offscreen)
2388 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2390 shader_addline(buffer, "vec4 vpos;\n");
2394 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2395 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2397 if (!needs_legacy_glsl_syntax(gl_info))
2398 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2400 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2401 FIXME("Insufficient uniforms to run this shader.\n");
2404 /* Declare output register temporaries */
2405 if (shader->limits->packed_output)
2406 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2408 /* Declare temporary variables */
2409 if (reg_maps->temporary_count)
2411 for (i = 0; i < reg_maps->temporary_count; ++i)
2412 shader_addline(buffer, "vec4 R%u;\n", i);
2414 else
2416 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2418 if (map & 1)
2419 shader_addline(buffer, "vec4 R%u;\n", i);
2423 /* Declare indexable temporary variables */
2424 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2426 if (idx_temp_reg->component_count != 4)
2427 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2428 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2431 /* Declare loop registers aLx */
2432 if (version->major < 4)
2434 for (i = 0; i < reg_maps->loop_depth; ++i)
2436 shader_addline(buffer, "int aL%u;\n", i);
2437 shader_addline(buffer, "int tmpInt%u;\n", i);
2441 /* Temporary variables for matrix operations */
2442 shader_addline(buffer, "vec4 tmp0;\n");
2443 shader_addline(buffer, "vec4 tmp1;\n");
2445 if (!shader->load_local_constsF)
2447 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2449 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2450 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2451 shader_addline(buffer, ";\n");
2456 /*****************************************************************************
2457 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2459 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2460 ****************************************************************************/
2462 /* Prototypes */
2463 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2464 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2466 /** Used for opcode modifiers - They multiply the result by the specified amount */
2467 static const char * const shift_glsl_tab[] = {
2468 "", /* 0 (none) */
2469 "2.0 * ", /* 1 (x2) */
2470 "4.0 * ", /* 2 (x4) */
2471 "8.0 * ", /* 3 (x8) */
2472 "16.0 * ", /* 4 (x16) */
2473 "32.0 * ", /* 5 (x32) */
2474 "", /* 6 (x64) */
2475 "", /* 7 (x128) */
2476 "", /* 8 (d256) */
2477 "", /* 9 (d128) */
2478 "", /* 10 (d64) */
2479 "", /* 11 (d32) */
2480 "0.0625 * ", /* 12 (d16) */
2481 "0.125 * ", /* 13 (d8) */
2482 "0.25 * ", /* 14 (d4) */
2483 "0.5 * " /* 15 (d2) */
2486 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2487 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2488 const char *in_reg, const char *in_regswizzle, char *out_str)
2490 switch (src_modifier)
2492 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2493 case WINED3DSPSM_DW:
2494 case WINED3DSPSM_NONE:
2495 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2496 break;
2497 case WINED3DSPSM_NEG:
2498 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2499 break;
2500 case WINED3DSPSM_NOT:
2501 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2502 break;
2503 case WINED3DSPSM_BIAS:
2504 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2505 break;
2506 case WINED3DSPSM_BIASNEG:
2507 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2508 break;
2509 case WINED3DSPSM_SIGN:
2510 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2511 break;
2512 case WINED3DSPSM_SIGNNEG:
2513 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2514 break;
2515 case WINED3DSPSM_COMP:
2516 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2517 break;
2518 case WINED3DSPSM_X2:
2519 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2520 break;
2521 case WINED3DSPSM_X2NEG:
2522 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2523 break;
2524 case WINED3DSPSM_ABS:
2525 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2526 break;
2527 case WINED3DSPSM_ABSNEG:
2528 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2529 break;
2530 default:
2531 FIXME("Unhandled modifier %u\n", src_modifier);
2532 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2536 /** Writes the GLSL variable name that corresponds to the register that the
2537 * DX opcode parameter is trying to access */
2538 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2539 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2541 /* oPos, oFog and oPts in D3D */
2542 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2544 const struct wined3d_shader *shader = ins->ctx->shader;
2545 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2546 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2547 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2548 const char *prefix = shader_glsl_get_prefix(version->type);
2549 struct glsl_src_param rel_param0, rel_param1;
2550 char imm_str[4][17];
2552 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2553 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2554 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2555 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2556 *is_color = FALSE;
2558 switch (reg->type)
2560 case WINED3DSPR_TEMP:
2561 sprintf(register_name, "R%u", reg->idx[0].offset);
2562 break;
2564 case WINED3DSPR_INPUT:
2565 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2567 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2569 if (reg->idx[0].rel_addr)
2570 FIXME("VS3+ input registers relative addressing.\n");
2571 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2572 *is_color = TRUE;
2573 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2574 break;
2577 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2579 if (reg->idx[0].rel_addr)
2581 if (reg->idx[1].rel_addr)
2582 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2583 rel_param0.param_str, reg->idx[0].offset,
2584 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2585 rel_param1.param_str, reg->idx[1].offset);
2586 else
2587 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2588 rel_param0.param_str, reg->idx[0].offset,
2589 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2590 reg->idx[1].offset);
2592 else if (reg->idx[1].rel_addr)
2593 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2594 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2595 rel_param1.param_str, reg->idx[1].offset);
2596 else
2597 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2598 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2599 reg->idx[1].offset);
2600 break;
2603 /* pixel shaders >= 3.0 */
2604 if (version->major >= 3)
2606 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2607 unsigned int in_count = vec4_varyings(version->major, gl_info);
2609 if (reg->idx[0].rel_addr)
2611 /* Removing a + 0 would be an obvious optimization, but
2612 * OS X doesn't see the NOP operation there. */
2613 if (idx)
2615 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2616 && shader->u.ps.declared_in_count > in_count)
2618 sprintf(register_name,
2619 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2620 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2621 prefix, rel_param0.param_str, idx);
2623 else
2625 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2628 else
2630 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2631 && shader->u.ps.declared_in_count > in_count)
2633 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2634 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2635 prefix, rel_param0.param_str);
2637 else
2639 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2643 else
2645 if (idx == in_count) sprintf(register_name, "gl_Color");
2646 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2647 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2650 else
2652 if (!reg->idx[0].offset)
2653 strcpy(register_name, "ffp_varying_diffuse");
2654 else
2655 strcpy(register_name, "ffp_varying_specular");
2656 break;
2658 break;
2660 case WINED3DSPR_CONST:
2662 /* Relative addressing */
2663 if (reg->idx[0].rel_addr)
2665 if (wined3d_settings.check_float_constants)
2666 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2667 rel_param0.param_str, reg->idx[0].offset,
2668 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2669 prefix, rel_param0.param_str, reg->idx[0].offset);
2670 else if (reg->idx[0].offset)
2671 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2672 else
2673 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2675 else
2677 if (shader_constant_is_local(shader, reg->idx[0].offset))
2678 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2679 else
2680 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2683 break;
2685 case WINED3DSPR_CONSTINT:
2686 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2687 break;
2689 case WINED3DSPR_CONSTBOOL:
2690 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2691 break;
2693 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2694 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2695 sprintf(register_name, "T%u", reg->idx[0].offset);
2696 else
2697 sprintf(register_name, "A%u", reg->idx[0].offset);
2698 break;
2700 case WINED3DSPR_LOOP:
2701 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2702 break;
2704 case WINED3DSPR_SAMPLER:
2705 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2706 break;
2708 case WINED3DSPR_COLOROUT:
2709 if (reg->idx[0].offset >= gl_info->limits.buffers)
2710 WARN("Write to render target %u, only %d supported.\n",
2711 reg->idx[0].offset, gl_info->limits.buffers);
2713 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2714 break;
2716 case WINED3DSPR_RASTOUT:
2717 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2718 break;
2720 case WINED3DSPR_DEPTHOUT:
2721 sprintf(register_name, "gl_FragDepth");
2722 break;
2724 case WINED3DSPR_ATTROUT:
2725 if (!reg->idx[0].offset)
2726 sprintf(register_name, "%s_out[8]", prefix);
2727 else
2728 sprintf(register_name, "%s_out[9]", prefix);
2729 break;
2731 case WINED3DSPR_TEXCRDOUT:
2732 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2733 if (reg->idx[0].rel_addr)
2734 FIXME("VS3 output registers relative addressing.\n");
2735 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2736 break;
2738 case WINED3DSPR_MISCTYPE:
2739 if (!reg->idx[0].offset)
2741 /* vPos */
2742 sprintf(register_name, "vpos");
2744 else if (reg->idx[0].offset == 1)
2746 /* Note that gl_FrontFacing is a bool, while vFace is
2747 * a float for which the sign determines front/back */
2748 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2750 else
2752 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2753 sprintf(register_name, "unrecognized_register");
2755 break;
2757 case WINED3DSPR_IMMCONST:
2758 switch (reg->immconst_type)
2760 case WINED3D_IMMCONST_SCALAR:
2761 switch (reg->data_type)
2763 case WINED3D_DATA_FLOAT:
2764 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2765 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
2766 else
2767 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
2768 break;
2769 case WINED3D_DATA_INT:
2770 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2771 break;
2772 case WINED3D_DATA_RESOURCE:
2773 case WINED3D_DATA_SAMPLER:
2774 case WINED3D_DATA_UINT:
2775 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2776 break;
2777 default:
2778 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2779 break;
2781 break;
2783 case WINED3D_IMMCONST_VEC4:
2784 switch (reg->data_type)
2786 case WINED3D_DATA_FLOAT:
2787 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2789 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2790 reg->u.immconst_data[0], reg->u.immconst_data[1],
2791 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2793 else
2795 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
2796 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
2797 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
2798 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
2799 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2800 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2802 break;
2803 case WINED3D_DATA_INT:
2804 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2805 reg->u.immconst_data[0], reg->u.immconst_data[1],
2806 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2807 break;
2808 case WINED3D_DATA_RESOURCE:
2809 case WINED3D_DATA_SAMPLER:
2810 case WINED3D_DATA_UINT:
2811 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2812 reg->u.immconst_data[0], reg->u.immconst_data[1],
2813 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2814 break;
2815 default:
2816 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2817 break;
2819 break;
2821 default:
2822 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2823 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2825 break;
2827 case WINED3DSPR_CONSTBUFFER:
2828 if (reg->idx[1].rel_addr)
2829 sprintf(register_name, "%s_cb%u[%s + %u]",
2830 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2831 else
2832 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2833 break;
2835 case WINED3DSPR_IMMCONSTBUFFER:
2836 if (reg->idx[0].rel_addr)
2837 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2838 else
2839 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2840 break;
2842 case WINED3DSPR_PRIMID:
2843 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2844 break;
2846 case WINED3DSPR_IDXTEMP:
2847 if (reg->idx[1].rel_addr)
2848 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2849 else
2850 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2851 break;
2853 case WINED3DSPR_LOCALTHREADINDEX:
2854 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2855 sprintf(register_name, "int(gl_LocalInvocationIndex)");
2856 else
2857 sprintf(register_name, "ivec2(gl_LocalInvocationIndex, 0)");
2858 break;
2860 case WINED3DSPR_THREADID:
2861 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2862 break;
2864 case WINED3DSPR_THREADGROUPID:
2865 sprintf(register_name, "ivec3(gl_WorkGroupID)");
2866 break;
2868 case WINED3DSPR_LOCALTHREADID:
2869 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
2870 break;
2872 default:
2873 FIXME("Unhandled register type %#x.\n", reg->type);
2874 sprintf(register_name, "unrecognized_register");
2875 break;
2879 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2881 *str++ = '.';
2882 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2883 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2884 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2885 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2886 *str = '\0';
2889 /* Get the GLSL write mask for the destination register */
2890 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2892 DWORD mask = param->write_mask;
2894 if (shader_is_scalar(&param->reg))
2896 mask = WINED3DSP_WRITEMASK_0;
2897 *write_mask = '\0';
2899 else
2901 shader_glsl_write_mask_to_str(mask, write_mask);
2904 return mask;
2907 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
2909 unsigned int size = 0;
2911 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2912 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2913 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2914 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2916 return size;
2919 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
2920 unsigned int component_idx)
2922 /* swizzle bits fields: wwzzyyxx */
2923 return (swizzle >> (2 * component_idx)) & 0x3;
2926 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2928 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2929 * but addressed as "rgba". To fix this we need to swap the register's x
2930 * and z components. */
2931 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2932 unsigned int i;
2934 *str++ = '.';
2935 for (i = 0; i < 4; ++i)
2937 if (mask & (WINED3DSP_WRITEMASK_0 << i))
2938 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
2940 *str = '\0';
2943 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2944 BOOL fixup, DWORD mask, char *swizzle_str)
2946 if (shader_is_scalar(&param->reg))
2947 *swizzle_str = '\0';
2948 else
2949 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2952 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
2953 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
2955 if (dst_data_type == src_data_type)
2957 string_buffer_sprintf(dst_param, "%s", src_param);
2958 return;
2961 if (src_data_type == WINED3D_DATA_FLOAT)
2963 switch (dst_data_type)
2965 case WINED3D_DATA_INT:
2966 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
2967 return;
2968 case WINED3D_DATA_RESOURCE:
2969 case WINED3D_DATA_SAMPLER:
2970 case WINED3D_DATA_UINT:
2971 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
2972 return;
2973 default:
2974 break;
2978 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
2980 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
2981 return;
2984 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
2986 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
2987 return;
2990 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
2991 string_buffer_sprintf(dst_param, "%s", src_param);
2994 /* From a given parameter token, generate the corresponding GLSL string.
2995 * Also, return the actual register name and swizzle in case the
2996 * caller needs this information as well. */
2997 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
2998 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2999 enum wined3d_data_type data_type)
3001 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3002 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3003 enum wined3d_data_type param_data_type;
3004 BOOL is_color = FALSE;
3005 char swizzle_str[6];
3007 glsl_src->reg_name[0] = '\0';
3008 glsl_src->param_str[0] = '\0';
3009 swizzle_str[0] = '\0';
3011 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
3012 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3014 switch (wined3d_src->reg.type)
3016 case WINED3DSPR_IMMCONST:
3017 param_data_type = data_type;
3018 break;
3019 case WINED3DSPR_PRIMID:
3020 param_data_type = WINED3D_DATA_UINT;
3021 break;
3022 case WINED3DSPR_LOCALTHREADINDEX:
3023 case WINED3DSPR_THREADID:
3024 case WINED3DSPR_THREADGROUPID:
3025 case WINED3DSPR_LOCALTHREADID:
3026 param_data_type = WINED3D_DATA_INT;
3027 break;
3028 default:
3029 param_data_type = WINED3D_DATA_FLOAT;
3030 break;
3033 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3034 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3036 string_buffer_release(priv->string_buffers, reg_name);
3039 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3040 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3042 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3045 /* From a given parameter token, generate the corresponding GLSL string.
3046 * Also, return the actual register name and swizzle in case the
3047 * caller needs this information as well. */
3048 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3049 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3051 BOOL is_color = FALSE;
3053 glsl_dst->mask_str[0] = '\0';
3054 glsl_dst->reg_name[0] = '\0';
3056 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
3057 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3060 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3061 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3062 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3063 enum wined3d_data_type data_type)
3065 struct glsl_dst_param glsl_dst;
3066 DWORD mask;
3068 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3070 switch (data_type)
3072 case WINED3D_DATA_FLOAT:
3073 shader_addline(buffer, "%s%s = %s(",
3074 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3075 break;
3076 case WINED3D_DATA_INT:
3077 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3078 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3079 break;
3080 case WINED3D_DATA_RESOURCE:
3081 case WINED3D_DATA_SAMPLER:
3082 case WINED3D_DATA_UINT:
3083 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3084 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3085 break;
3086 default:
3087 FIXME("Unhandled data type %#x.\n", data_type);
3088 shader_addline(buffer, "%s%s = %s(",
3089 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3090 break;
3094 return mask;
3097 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3098 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3100 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3103 /** Process GLSL instruction modifiers */
3104 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3106 struct glsl_dst_param dst_param;
3107 DWORD modifiers;
3109 if (!ins->dst_count) return;
3111 modifiers = ins->dst[0].modifiers;
3112 if (!modifiers) return;
3114 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3116 if (modifiers & WINED3DSPDM_SATURATE)
3118 /* _SAT means to clamp the value of the register to between 0 and 1 */
3119 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3120 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3123 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3125 FIXME("_centroid modifier not handled\n");
3128 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3130 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3134 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3136 switch (op)
3138 case WINED3D_SHADER_REL_OP_GT: return ">";
3139 case WINED3D_SHADER_REL_OP_EQ: return "==";
3140 case WINED3D_SHADER_REL_OP_GE: return ">=";
3141 case WINED3D_SHADER_REL_OP_LT: return "<";
3142 case WINED3D_SHADER_REL_OP_NE: return "!=";
3143 case WINED3D_SHADER_REL_OP_LE: return "<=";
3144 default:
3145 FIXME("Unrecognized operator %#x.\n", op);
3146 return "(\?\?)";
3150 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info,
3151 const struct wined3d_shader_version *version)
3153 return shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3156 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3157 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3159 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3160 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3161 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3162 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3163 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3164 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3165 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3166 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3167 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3168 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3169 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3170 const char *base = "texture", *type_part = "", *suffix = "";
3171 unsigned int coord_size, deriv_size;
3172 BOOL array;
3174 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3176 if (resource_type >= ARRAY_SIZE(resource_type_info))
3178 ERR("Unexpected resource type %#x.\n", resource_type);
3179 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3181 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3182 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3184 /* Note that there's no such thing as a projected cube texture. */
3185 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3186 projected = FALSE;
3188 if (needs_legacy_glsl_syntax(gl_info))
3190 if (shadow)
3191 base = "shadow";
3193 type_part = resource_type_info[resource_type].type_part;
3194 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3195 type_part = "2DRect";
3196 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3197 FIXME("Unhandled resource type %#x.\n", resource_type);
3199 if (!lod && grad && !shader_glsl_has_core_grad(gl_info, &ctx->shader->reg_maps.shader_version))
3201 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3202 suffix = "ARB";
3203 else
3204 FIXME("Unsupported grad function.\n");
3208 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3210 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3211 if (flags & ~texel_fetch_flags)
3212 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3214 base = "texelFetch";
3215 type_part = "";
3218 sample_function->name = string_buffer_get(priv->string_buffers);
3219 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3220 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3222 coord_size = resource_type_info[resource_type].coord_size;
3223 deriv_size = coord_size;
3224 if (shadow)
3225 ++coord_size;
3226 if (array)
3227 --deriv_size;
3228 sample_function->offset_size = offset ? deriv_size : 0;
3229 sample_function->coord_mask = (1u << coord_size) - 1;
3230 sample_function->deriv_mask = (1u << deriv_size) - 1;
3231 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3234 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3235 struct glsl_sample_function *sample_function)
3237 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3239 string_buffer_release(priv->string_buffers, sample_function->name);
3242 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3243 BOOL sign_fixup, enum fixup_channel_source channel_source)
3245 switch(channel_source)
3247 case CHANNEL_SOURCE_ZERO:
3248 strcat(arguments, "0.0");
3249 break;
3251 case CHANNEL_SOURCE_ONE:
3252 strcat(arguments, "1.0");
3253 break;
3255 case CHANNEL_SOURCE_X:
3256 strcat(arguments, reg_name);
3257 strcat(arguments, ".x");
3258 break;
3260 case CHANNEL_SOURCE_Y:
3261 strcat(arguments, reg_name);
3262 strcat(arguments, ".y");
3263 break;
3265 case CHANNEL_SOURCE_Z:
3266 strcat(arguments, reg_name);
3267 strcat(arguments, ".z");
3268 break;
3270 case CHANNEL_SOURCE_W:
3271 strcat(arguments, reg_name);
3272 strcat(arguments, ".w");
3273 break;
3275 default:
3276 FIXME("Unhandled channel source %#x\n", channel_source);
3277 strcat(arguments, "undefined");
3278 break;
3281 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3284 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3285 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3287 unsigned int mask_size, remaining;
3288 DWORD fixup_mask = 0;
3289 char arguments[256];
3290 char mask_str[6];
3292 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3293 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3294 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3295 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3296 if (!(mask &= fixup_mask))
3297 return;
3299 if (is_complex_fixup(fixup))
3301 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3302 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3303 return;
3306 shader_glsl_write_mask_to_str(mask, mask_str);
3307 mask_size = shader_glsl_get_write_mask_size(mask);
3309 arguments[0] = '\0';
3310 remaining = mask_size;
3311 if (mask & WINED3DSP_WRITEMASK_0)
3313 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3314 if (--remaining) strcat(arguments, ", ");
3316 if (mask & WINED3DSP_WRITEMASK_1)
3318 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3319 if (--remaining) strcat(arguments, ", ");
3321 if (mask & WINED3DSP_WRITEMASK_2)
3323 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3324 if (--remaining) strcat(arguments, ", ");
3326 if (mask & WINED3DSP_WRITEMASK_3)
3328 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3329 if (--remaining) strcat(arguments, ", ");
3332 if (mask_size > 1)
3333 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3334 else
3335 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3338 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3340 char reg_name[256];
3341 BOOL is_color;
3343 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3344 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3347 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3348 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3349 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3350 const char *coord_reg_fmt, ...)
3352 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3353 char dst_swizzle[6];
3354 struct color_fixup_desc fixup;
3355 BOOL np2_fixup = FALSE;
3356 va_list args;
3357 int ret;
3359 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3361 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3362 * We actually rely on it for vertex shaders and SM4+. */
3363 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3365 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3366 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3368 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3369 np2_fixup = TRUE;
3371 else
3373 fixup = COLOR_FIXUP_IDENTITY;
3376 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3378 if (sample_function->output_single_component)
3379 shader_addline(ins->ctx->buffer, "vec4(");
3381 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3382 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3384 for (;;)
3386 va_start(args, coord_reg_fmt);
3387 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3388 va_end(args);
3389 if (!ret)
3390 break;
3391 if (!string_buffer_resize(ins->ctx->buffer, ret))
3392 break;
3395 if (np2_fixup)
3397 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3398 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3400 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3402 case 1:
3403 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3404 idx >> 1, (idx % 2) ? "z" : "x");
3405 break;
3406 case 2:
3407 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3408 idx >> 1, (idx % 2) ? "zw" : "xy");
3409 break;
3410 case 3:
3411 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3412 idx >> 1, (idx % 2) ? "zw" : "xy");
3413 break;
3414 case 4:
3415 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3416 idx >> 1, (idx % 2) ? "zw" : "xy");
3417 break;
3420 if (dx && dy)
3421 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3422 else if (bias)
3423 shader_addline(ins->ctx->buffer, ", %s", bias);
3424 if (sample_function->offset_size)
3426 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3427 shader_addline(ins->ctx->buffer, ", ");
3428 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3430 shader_addline(ins->ctx->buffer, ")");
3432 if (sample_function->output_single_component)
3433 shader_addline(ins->ctx->buffer, ")");
3435 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3437 if (!is_identity_fixup(fixup))
3438 shader_glsl_color_correction(ins, fixup);
3441 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3443 /* Write the final position.
3445 * OpenGL coordinates specify the center of the pixel while D3D coords
3446 * specify the corner. The offsets are stored in z and w in
3447 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3448 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3449 * a MAD. */
3450 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3451 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3453 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3454 * in utils.c
3456 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3457 * shaders are run before the homogeneous divide, so we have to take the w
3458 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3459 * z = z * 2 - w. */
3460 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3463 /*****************************************************************************
3464 * Begin processing individual instruction opcodes
3465 ****************************************************************************/
3467 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3469 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3470 struct glsl_src_param src0_param;
3471 struct glsl_src_param src1_param;
3472 DWORD write_mask;
3473 const char *op;
3475 /* Determine the GLSL operator to use based on the opcode */
3476 switch (ins->handler_idx)
3478 case WINED3DSIH_ADD: op = "+"; break;
3479 case WINED3DSIH_AND: op = "&"; break;
3480 case WINED3DSIH_DIV: op = "/"; break;
3481 case WINED3DSIH_IADD: op = "+"; break;
3482 case WINED3DSIH_ISHL: op = "<<"; break;
3483 case WINED3DSIH_ISHR: op = ">>"; break;
3484 case WINED3DSIH_MUL: op = "*"; break;
3485 case WINED3DSIH_OR: op = "|"; break;
3486 case WINED3DSIH_SUB: op = "-"; break;
3487 case WINED3DSIH_USHR: op = ">>"; break;
3488 case WINED3DSIH_XOR: op = "^"; break;
3489 default:
3490 op = "<unhandled operator>";
3491 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3492 break;
3495 write_mask = shader_glsl_append_dst(buffer, ins);
3496 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3497 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3498 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3501 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3503 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3504 struct glsl_src_param src0_param;
3505 struct glsl_src_param src1_param;
3506 unsigned int mask_size;
3507 DWORD write_mask;
3508 const char *op;
3510 write_mask = shader_glsl_append_dst(buffer, ins);
3511 mask_size = shader_glsl_get_write_mask_size(write_mask);
3512 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3513 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3515 if (mask_size > 1)
3517 switch (ins->handler_idx)
3519 case WINED3DSIH_EQ: op = "equal"; break;
3520 case WINED3DSIH_IEQ: op = "equal"; break;
3521 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3522 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3523 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3524 case WINED3DSIH_LT: op = "lessThan"; break;
3525 case WINED3DSIH_ILT: op = "lessThan"; break;
3526 case WINED3DSIH_ULT: op = "lessThan"; break;
3527 case WINED3DSIH_NE: op = "notEqual"; break;
3528 case WINED3DSIH_INE: op = "notEqual"; break;
3529 default:
3530 op = "<unhandled operator>";
3531 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3532 break;
3535 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3536 mask_size, op, src0_param.param_str, src1_param.param_str);
3538 else
3540 switch (ins->handler_idx)
3542 case WINED3DSIH_EQ: op = "=="; break;
3543 case WINED3DSIH_IEQ: op = "=="; break;
3544 case WINED3DSIH_GE: op = ">="; break;
3545 case WINED3DSIH_IGE: op = ">="; break;
3546 case WINED3DSIH_UGE: op = ">="; break;
3547 case WINED3DSIH_LT: op = "<"; break;
3548 case WINED3DSIH_ILT: op = "<"; break;
3549 case WINED3DSIH_ULT: op = "<"; break;
3550 case WINED3DSIH_NE: op = "!="; break;
3551 case WINED3DSIH_INE: op = "!="; break;
3552 default:
3553 op = "<unhandled operator>";
3554 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3555 break;
3558 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3559 src0_param.param_str, op, src1_param.param_str);
3563 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3565 struct glsl_src_param src_param;
3566 DWORD write_mask;
3567 const char *op;
3569 switch (ins->handler_idx)
3571 case WINED3DSIH_INEG: op = "-"; break;
3572 case WINED3DSIH_NOT: op = "~"; break;
3573 default:
3574 op = "<unhandled operator>";
3575 ERR("Unhandled opcode %s.\n",
3576 debug_d3dshaderinstructionhandler(ins->handler_idx));
3577 break;
3580 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3581 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3582 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3585 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3587 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3588 struct glsl_src_param src0_param;
3589 struct glsl_src_param src1_param;
3590 DWORD write_mask;
3592 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3593 * If not, we can emulate it. */
3594 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3595 FIXME("64-bit integer multiplies not implemented.\n");
3597 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3599 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3600 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3601 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3603 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3604 src0_param.param_str, src1_param.param_str);
3608 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3610 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3611 struct glsl_src_param src0_param, src1_param;
3612 DWORD write_mask;
3614 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3616 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3618 char dst_mask[6];
3620 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3621 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3622 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3623 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3624 dst_mask, src0_param.param_str, src1_param.param_str);
3626 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3627 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3628 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3629 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3631 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3632 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3634 else
3636 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3637 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3638 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3639 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3642 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3644 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3645 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3646 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3647 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3651 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3652 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3654 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3655 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3656 struct glsl_src_param src0_param;
3657 DWORD write_mask;
3659 write_mask = shader_glsl_append_dst(buffer, ins);
3660 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3662 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3663 * shader versions WINED3DSIO_MOVA is used for this. */
3664 if (ins->ctx->reg_maps->shader_version.major == 1
3665 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3666 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3668 /* This is a simple floor() */
3669 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3670 if (mask_size > 1) {
3671 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3672 } else {
3673 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3676 else if (ins->handler_idx == WINED3DSIH_MOVA)
3678 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
3679 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3681 if (shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3683 if (mask_size > 1)
3684 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3685 else
3686 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3688 else
3690 if (mask_size > 1)
3691 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3692 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3693 else
3694 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3695 src0_param.param_str, src0_param.param_str);
3698 else
3700 shader_addline(buffer, "%s);\n", src0_param.param_str);
3704 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3705 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3707 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3708 struct glsl_src_param src0_param;
3709 struct glsl_src_param src1_param;
3710 DWORD dst_write_mask, src_write_mask;
3711 unsigned int dst_size;
3713 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3714 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3716 /* dp4 works on vec4, dp3 on vec3, etc. */
3717 if (ins->handler_idx == WINED3DSIH_DP4)
3718 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3719 else if (ins->handler_idx == WINED3DSIH_DP3)
3720 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3721 else
3722 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3724 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3725 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3727 if (dst_size > 1) {
3728 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3729 } else {
3730 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3734 /* Note that this instruction has some restrictions. The destination write mask
3735 * can't contain the w component, and the source swizzles have to be .xyzw */
3736 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3738 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3739 struct glsl_src_param src0_param;
3740 struct glsl_src_param src1_param;
3741 char dst_mask[6];
3743 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3744 shader_glsl_append_dst(ins->ctx->buffer, ins);
3745 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3746 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3747 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3750 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3752 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3754 if (!stream)
3755 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3756 else
3757 FIXME("Unhandled primitive stream %u.\n", stream);
3760 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3761 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3762 * GLSL uses the value as-is. */
3763 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3765 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3766 struct glsl_src_param src0_param;
3767 struct glsl_src_param src1_param;
3768 DWORD dst_write_mask;
3769 unsigned int dst_size;
3771 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3772 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3774 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3775 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3777 if (dst_size > 1)
3779 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3780 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3782 else
3784 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3785 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3789 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3790 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3792 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3793 struct glsl_src_param src_param;
3794 const char *instruction;
3795 DWORD write_mask;
3796 unsigned i;
3798 /* Determine the GLSL function to use based on the opcode */
3799 /* TODO: Possibly make this a table for faster lookups */
3800 switch (ins->handler_idx)
3802 case WINED3DSIH_ABS: instruction = "abs"; break;
3803 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
3804 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
3805 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3806 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3807 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3808 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3809 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3810 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3811 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
3812 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
3813 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
3814 case WINED3DSIH_FRC: instruction = "fract"; break;
3815 case WINED3DSIH_IMAX: instruction = "max"; break;
3816 case WINED3DSIH_IMIN: instruction = "min"; break;
3817 case WINED3DSIH_MAX: instruction = "max"; break;
3818 case WINED3DSIH_MIN: instruction = "min"; break;
3819 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3820 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3821 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3822 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3823 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3824 case WINED3DSIH_UMAX: instruction = "max"; break;
3825 case WINED3DSIH_UMIN: instruction = "min"; break;
3826 default: instruction = "";
3827 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3828 break;
3831 write_mask = shader_glsl_append_dst(buffer, ins);
3833 /* In D3D bits are numbered from the most significant bit. */
3834 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
3835 shader_addline(buffer, "31 - ");
3836 shader_addline(buffer, "%s(", instruction);
3838 if (ins->src_count)
3840 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3841 shader_addline(buffer, "%s", src_param.param_str);
3842 for (i = 1; i < ins->src_count; ++i)
3844 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3845 shader_addline(buffer, ", %s", src_param.param_str);
3849 shader_addline(buffer, "));\n");
3852 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
3854 struct wined3d_shader_dst_param dst;
3855 struct glsl_src_param src;
3856 DWORD write_mask;
3857 const char *fmt;
3858 unsigned int i;
3860 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
3861 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3863 dst = ins->dst[0];
3864 for (i = 0; i < 4; ++i)
3866 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3867 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3868 &dst, dst.reg.data_type)))
3869 continue;
3871 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
3872 shader_addline(ins->ctx->buffer, fmt, src.param_str);
3876 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
3878 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3879 struct wined3d_shader_dst_param dst;
3880 struct glsl_src_param src[4];
3881 const char *instruction;
3882 unsigned int i, j;
3883 DWORD write_mask;
3885 switch (ins->handler_idx)
3887 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
3888 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
3889 default:
3890 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3891 return;
3894 dst = ins->dst[0];
3895 for (i = 0; i < 4; ++i)
3897 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3898 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3899 &dst, dst.reg.data_type)))
3900 continue;
3902 for (j = 0; j < ins->src_count; ++j)
3903 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
3904 shader_addline(buffer, "%s(", instruction);
3905 for (j = 0; j < ins->src_count - 2; ++j)
3906 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
3907 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
3911 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3913 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3915 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3916 struct glsl_src_param src_param;
3917 unsigned int mask_size;
3918 DWORD write_mask;
3919 char dst_mask[6];
3921 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3922 mask_size = shader_glsl_get_write_mask_size(write_mask);
3923 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3925 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3926 src_param.param_str, src_param.param_str);
3927 shader_glsl_append_dst(buffer, ins);
3929 if (mask_size > 1)
3931 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3932 mask_size, src_param.param_str);
3934 else
3936 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3937 src_param.param_str);
3941 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3943 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3944 ins->ctx->reg_maps->shader_version.minor);
3945 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3946 struct glsl_src_param src0_param;
3947 const char *prefix, *suffix;
3948 unsigned int dst_size;
3949 DWORD dst_write_mask;
3951 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3952 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3954 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3955 dst_write_mask = WINED3DSP_WRITEMASK_3;
3957 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3959 switch (ins->handler_idx)
3961 case WINED3DSIH_EXP:
3962 case WINED3DSIH_EXPP:
3963 prefix = "exp2(";
3964 suffix = ")";
3965 break;
3967 case WINED3DSIH_LOG:
3968 case WINED3DSIH_LOGP:
3969 prefix = "log2(abs(";
3970 suffix = "))";
3971 break;
3973 case WINED3DSIH_RCP:
3974 prefix = "1.0 / ";
3975 suffix = "";
3976 break;
3978 case WINED3DSIH_RSQ:
3979 prefix = "inversesqrt(abs(";
3980 suffix = "))";
3981 break;
3983 default:
3984 prefix = "";
3985 suffix = "";
3986 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3987 break;
3990 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3991 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3992 else
3993 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3996 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3997 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3998 * dst.x = 2^(floor(src))
3999 * dst.y = src - floor(src)
4000 * dst.z = 2^src (partial precision is allowed, but optional)
4001 * dst.w = 1.0;
4002 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4003 * dst = 2^src; (partial precision is allowed, but optional)
4005 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4007 if (ins->ctx->reg_maps->shader_version.major < 2)
4009 struct glsl_src_param src_param;
4010 char dst_mask[6];
4012 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4014 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4015 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4016 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4017 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4019 shader_glsl_append_dst(ins->ctx->buffer, ins);
4020 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4021 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4022 return;
4025 shader_glsl_scalar_op(ins);
4028 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4029 const char *vector_constructor, const char *scalar_constructor)
4031 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4032 struct glsl_src_param src_param;
4033 unsigned int mask_size;
4034 DWORD write_mask;
4036 write_mask = shader_glsl_append_dst(buffer, ins);
4037 mask_size = shader_glsl_get_write_mask_size(write_mask);
4038 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4040 if (mask_size > 1)
4041 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4042 else
4043 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4046 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4048 shader_glsl_cast(ins, "ivec", "int");
4051 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4053 shader_glsl_cast(ins, "uvec", "uint");
4056 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4058 shader_glsl_cast(ins, "vec", "float");
4061 /** Process signed comparison opcodes in GLSL. */
4062 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4064 struct glsl_src_param src0_param;
4065 struct glsl_src_param src1_param;
4066 DWORD write_mask;
4067 unsigned int mask_size;
4069 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4070 mask_size = shader_glsl_get_write_mask_size(write_mask);
4071 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4072 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4074 if (mask_size > 1) {
4075 const char *compare;
4077 switch(ins->handler_idx)
4079 case WINED3DSIH_SLT: compare = "lessThan"; break;
4080 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4081 default: compare = "";
4082 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4085 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4086 src0_param.param_str, src1_param.param_str);
4087 } else {
4088 switch(ins->handler_idx)
4090 case WINED3DSIH_SLT:
4091 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4092 * to return 0.0 but step returns 1.0 because step is not < x
4093 * An alternative is a bvec compare padded with an unused second component.
4094 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4095 * issue. Playing with not() is not possible either because not() does not accept
4096 * a scalar.
4098 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4099 src0_param.param_str, src1_param.param_str);
4100 break;
4101 case WINED3DSIH_SGE:
4102 /* Here we can use the step() function and safe a conditional */
4103 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4104 break;
4105 default:
4106 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4112 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4114 const char *condition_prefix, *condition_suffix;
4115 struct wined3d_shader_dst_param dst;
4116 struct glsl_src_param src0_param;
4117 struct glsl_src_param src1_param;
4118 struct glsl_src_param src2_param;
4119 BOOL temp_destination = FALSE;
4120 DWORD cmp_channel = 0;
4121 unsigned int i, j;
4122 char mask_char[6];
4123 DWORD write_mask;
4125 switch (ins->handler_idx)
4127 case WINED3DSIH_CMP:
4128 condition_prefix = "";
4129 condition_suffix = " >= 0.0";
4130 break;
4132 case WINED3DSIH_CND:
4133 condition_prefix = "";
4134 condition_suffix = " > 0.5";
4135 break;
4137 case WINED3DSIH_MOVC:
4138 condition_prefix = "bool(";
4139 condition_suffix = ")";
4140 break;
4142 default:
4143 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4144 condition_prefix = "<unhandled prefix>";
4145 condition_suffix = "<unhandled suffix>";
4146 break;
4149 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4151 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4152 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4153 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4154 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4156 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4157 condition_prefix, src0_param.param_str, condition_suffix,
4158 src1_param.param_str, src2_param.param_str);
4159 return;
4162 dst = ins->dst[0];
4164 /* Splitting the instruction up in multiple lines imposes a problem:
4165 * The first lines may overwrite source parameters of the following lines.
4166 * Deal with that by using a temporary destination register if needed. */
4167 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4168 && ins->src[0].reg.type == dst.reg.type)
4169 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4170 && ins->src[1].reg.type == dst.reg.type)
4171 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4172 && ins->src[2].reg.type == dst.reg.type))
4173 temp_destination = TRUE;
4175 /* Cycle through all source0 channels. */
4176 for (i = 0; i < 4; ++i)
4178 write_mask = 0;
4179 /* Find the destination channels which use the current source0 channel. */
4180 for (j = 0; j < 4; ++j)
4182 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4184 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4185 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4188 dst.write_mask = ins->dst[0].write_mask & write_mask;
4190 if (temp_destination)
4192 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4193 continue;
4194 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4196 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4197 continue;
4199 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4200 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4201 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4203 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4204 condition_prefix, src0_param.param_str, condition_suffix,
4205 src1_param.param_str, src2_param.param_str);
4208 if (temp_destination)
4210 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4211 shader_glsl_append_dst(ins->ctx->buffer, ins);
4212 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4216 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4217 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4218 * the compare is done per component of src0. */
4219 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4221 struct glsl_src_param src0_param;
4222 struct glsl_src_param src1_param;
4223 struct glsl_src_param src2_param;
4224 DWORD write_mask;
4225 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4226 ins->ctx->reg_maps->shader_version.minor);
4228 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4230 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4231 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4232 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4233 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4235 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4236 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4237 else
4238 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4239 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4240 return;
4243 shader_glsl_conditional_move(ins);
4246 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4247 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4249 struct glsl_src_param src0_param;
4250 struct glsl_src_param src1_param;
4251 struct glsl_src_param src2_param;
4252 DWORD write_mask;
4254 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4255 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4256 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4257 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4258 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4259 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4262 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4263 Vertex shaders to GLSL codes */
4264 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4266 int i;
4267 int nComponents = 0;
4268 struct wined3d_shader_dst_param tmp_dst = {{0}};
4269 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4270 struct wined3d_shader_instruction tmp_ins;
4272 memset(&tmp_ins, 0, sizeof(tmp_ins));
4274 /* Set constants for the temporary argument */
4275 tmp_ins.ctx = ins->ctx;
4276 tmp_ins.dst_count = 1;
4277 tmp_ins.dst = &tmp_dst;
4278 tmp_ins.src_count = 2;
4279 tmp_ins.src = tmp_src;
4281 switch(ins->handler_idx)
4283 case WINED3DSIH_M4x4:
4284 nComponents = 4;
4285 tmp_ins.handler_idx = WINED3DSIH_DP4;
4286 break;
4287 case WINED3DSIH_M4x3:
4288 nComponents = 3;
4289 tmp_ins.handler_idx = WINED3DSIH_DP4;
4290 break;
4291 case WINED3DSIH_M3x4:
4292 nComponents = 4;
4293 tmp_ins.handler_idx = WINED3DSIH_DP3;
4294 break;
4295 case WINED3DSIH_M3x3:
4296 nComponents = 3;
4297 tmp_ins.handler_idx = WINED3DSIH_DP3;
4298 break;
4299 case WINED3DSIH_M3x2:
4300 nComponents = 2;
4301 tmp_ins.handler_idx = WINED3DSIH_DP3;
4302 break;
4303 default:
4304 break;
4307 tmp_dst = ins->dst[0];
4308 tmp_src[0] = ins->src[0];
4309 tmp_src[1] = ins->src[1];
4310 for (i = 0; i < nComponents; ++i)
4312 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4313 shader_glsl_dot(&tmp_ins);
4314 ++tmp_src[1].reg.idx[0].offset;
4319 The LRP instruction performs a component-wise linear interpolation
4320 between the second and third operands using the first operand as the
4321 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4322 This is equivalent to mix(src2, src1, src0);
4324 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4326 struct glsl_src_param src0_param;
4327 struct glsl_src_param src1_param;
4328 struct glsl_src_param src2_param;
4329 DWORD write_mask;
4331 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4333 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4334 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4335 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4337 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4338 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4341 /** Process the WINED3DSIO_LIT instruction in GLSL:
4342 * dst.x = dst.w = 1.0
4343 * dst.y = (src0.x > 0) ? src0.x
4344 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4345 * where src.w is clamped at +- 128
4347 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4349 struct glsl_src_param src0_param;
4350 struct glsl_src_param src1_param;
4351 struct glsl_src_param src3_param;
4352 char dst_mask[6];
4354 shader_glsl_append_dst(ins->ctx->buffer, ins);
4355 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4357 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4358 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4359 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4361 /* The sdk specifies the instruction like this
4362 * dst.x = 1.0;
4363 * if(src.x > 0.0) dst.y = src.x
4364 * else dst.y = 0.0.
4365 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4366 * else dst.z = 0.0;
4367 * dst.w = 1.0;
4368 * (where power = src.w clamped between -128 and 128)
4370 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4371 * dst.x = 1.0 ... No further explanation needed
4372 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4373 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4374 * dst.w = 1.0. ... Nothing fancy.
4376 * So we still have one conditional in there. So do this:
4377 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4379 * 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),
4380 * 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.
4381 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4383 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4384 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4385 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4387 shader_addline(ins->ctx->buffer,
4388 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4389 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4390 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4391 src0_param.param_str, src3_param.param_str, dst_mask);
4394 /** Process the WINED3DSIO_DST instruction in GLSL:
4395 * dst.x = 1.0
4396 * dst.y = src0.x * src0.y
4397 * dst.z = src0.z
4398 * dst.w = src1.w
4400 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4402 struct glsl_src_param src0y_param;
4403 struct glsl_src_param src0z_param;
4404 struct glsl_src_param src1y_param;
4405 struct glsl_src_param src1w_param;
4406 char dst_mask[6];
4408 shader_glsl_append_dst(ins->ctx->buffer, ins);
4409 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4411 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4412 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4413 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4414 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4416 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4417 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4420 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4421 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4422 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4424 * dst.x = cos(src0.?)
4425 * dst.y = sin(src0.?)
4426 * dst.z = dst.z
4427 * dst.w = dst.w
4429 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4431 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4432 struct glsl_src_param src0_param;
4433 DWORD write_mask;
4435 if (ins->ctx->reg_maps->shader_version.major < 4)
4437 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4439 write_mask = shader_glsl_append_dst(buffer, ins);
4440 switch (write_mask)
4442 case WINED3DSP_WRITEMASK_0:
4443 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4444 break;
4446 case WINED3DSP_WRITEMASK_1:
4447 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4448 break;
4450 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4451 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4452 src0_param.param_str, src0_param.param_str);
4453 break;
4455 default:
4456 ERR("Write mask should be .x, .y or .xy\n");
4457 break;
4460 return;
4463 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4466 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4468 char dst_mask[6];
4470 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4471 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4472 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4474 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4475 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4476 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4478 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4479 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4481 else
4483 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4484 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4485 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4488 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4490 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4491 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4492 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4496 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4497 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4498 * generate invalid code
4500 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4502 struct glsl_src_param src0_param;
4503 DWORD write_mask;
4505 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4506 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4508 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4511 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4512 * Start a for() loop where src1.y is the initial value of aL,
4513 * increment aL by src1.z for a total of src1.x iterations.
4514 * Need to use a temporary variable for this operation.
4516 /* FIXME: I don't think nested loops will work correctly this way. */
4517 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4519 struct wined3d_shader_parser_state *state = ins->ctx->state;
4520 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4521 const struct wined3d_shader *shader = ins->ctx->shader;
4522 const struct wined3d_shader_lconst *constant;
4523 struct glsl_src_param src1_param;
4524 const DWORD *control_values = NULL;
4526 if (ins->ctx->reg_maps->shader_version.major < 4)
4528 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4530 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4531 * class hardware doesn't support real varying indexing, but Microsoft
4532 * designed this feature for Shader model 2.x+. If the loop control is
4533 * known at compile time, the GLSL compiler can unroll the loop, and
4534 * replace indirect addressing with direct addressing. */
4535 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4537 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4539 if (constant->idx == ins->src[1].reg.idx[0].offset)
4541 control_values = constant->value;
4542 break;
4547 if (control_values)
4549 struct wined3d_shader_loop_control loop_control;
4550 loop_control.count = control_values[0];
4551 loop_control.start = control_values[1];
4552 loop_control.step = (int)control_values[2];
4554 if (loop_control.step > 0)
4556 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4557 state->current_loop_depth, loop_control.start,
4558 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4559 state->current_loop_depth, loop_control.step);
4561 else if (loop_control.step < 0)
4563 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4564 state->current_loop_depth, loop_control.start,
4565 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4566 state->current_loop_depth, loop_control.step);
4568 else
4570 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4571 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4572 state->current_loop_depth, loop_control.count,
4573 state->current_loop_depth);
4576 else
4578 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4579 state->current_loop_depth, state->current_loop_reg,
4580 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4581 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4584 ++state->current_loop_reg;
4586 else
4588 shader_addline(buffer, "for (;;)\n{\n");
4591 ++state->current_loop_depth;
4594 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4596 struct wined3d_shader_parser_state *state = ins->ctx->state;
4598 shader_addline(ins->ctx->buffer, "}\n");
4600 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4602 --state->current_loop_depth;
4603 --state->current_loop_reg;
4606 if (ins->handler_idx == WINED3DSIH_ENDREP)
4608 --state->current_loop_depth;
4612 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4614 struct wined3d_shader_parser_state *state = ins->ctx->state;
4615 const struct wined3d_shader *shader = ins->ctx->shader;
4616 const struct wined3d_shader_lconst *constant;
4617 struct glsl_src_param src0_param;
4618 const DWORD *control_values = NULL;
4620 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4621 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4623 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4625 if (constant->idx == ins->src[0].reg.idx[0].offset)
4627 control_values = constant->value;
4628 break;
4633 if (control_values)
4635 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4636 state->current_loop_depth, state->current_loop_depth,
4637 control_values[0], state->current_loop_depth);
4639 else
4641 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4642 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4643 state->current_loop_depth, state->current_loop_depth,
4644 src0_param.param_str, state->current_loop_depth);
4647 ++state->current_loop_depth;
4650 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4652 struct glsl_src_param src0_param;
4654 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4655 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4658 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4660 struct glsl_src_param src0_param;
4662 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4663 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4666 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4668 shader_addline(ins->ctx->buffer, "default:\n");
4671 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4673 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4674 struct glsl_src_param src0_param;
4676 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4677 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4680 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4682 struct glsl_src_param src0_param;
4683 struct glsl_src_param src1_param;
4685 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4686 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4688 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4689 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4692 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4694 shader_addline(ins->ctx->buffer, "} else {\n");
4697 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4699 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4701 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4702 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
4703 shader_glsl_fixup_position(ins->ctx->buffer);
4705 if (!stream)
4706 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4707 else
4708 FIXME("Unhandled primitive stream %u.\n", stream);
4711 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4713 shader_addline(ins->ctx->buffer, "break;\n");
4716 /* FIXME: According to MSDN the compare is done per component. */
4717 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4719 struct glsl_src_param src0_param;
4720 struct glsl_src_param src1_param;
4722 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4723 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4725 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4726 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4729 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4731 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4732 struct glsl_src_param src_param;
4734 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4735 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4738 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4740 shader_addline(ins->ctx->buffer, "continue;\n");
4743 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4745 shader_addline(ins->ctx->buffer, "}\n");
4746 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4748 /* Subroutines appear at the end of the shader. */
4749 ins->ctx->state->in_subroutine = TRUE;
4752 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4754 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4757 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4759 struct glsl_src_param src1_param;
4761 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4762 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4763 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4766 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4768 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
4770 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
4772 shader_glsl_generate_shader_epilogue(ins->ctx);
4773 shader_addline(ins->ctx->buffer, "return;\n");
4777 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4779 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4780 ins->ctx->reg_maps->shader_version.minor);
4781 struct glsl_sample_function sample_function;
4782 DWORD sample_flags = 0;
4783 DWORD resource_idx;
4784 DWORD mask = 0, swizzle;
4785 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4787 /* 1.0-1.4: Use destination register as sampler source.
4788 * 2.0+: Use provided sampler source. */
4789 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4790 resource_idx = ins->dst[0].reg.idx[0].offset;
4791 else
4792 resource_idx = ins->src[1].reg.idx[0].offset;
4794 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4796 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4797 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4798 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4800 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4801 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4803 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4804 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4806 case WINED3D_TTFF_COUNT1:
4807 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4808 break;
4809 case WINED3D_TTFF_COUNT2:
4810 mask = WINED3DSP_WRITEMASK_1;
4811 break;
4812 case WINED3D_TTFF_COUNT3:
4813 mask = WINED3DSP_WRITEMASK_2;
4814 break;
4815 case WINED3D_TTFF_COUNT4:
4816 case WINED3D_TTFF_DISABLE:
4817 mask = WINED3DSP_WRITEMASK_3;
4818 break;
4822 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4824 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4826 if (src_mod == WINED3DSPSM_DZ) {
4827 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4828 mask = WINED3DSP_WRITEMASK_2;
4829 } else if (src_mod == WINED3DSPSM_DW) {
4830 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4831 mask = WINED3DSP_WRITEMASK_3;
4834 else
4836 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4837 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4839 /* ps 2.0 texldp instruction always divides by the fourth component. */
4840 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4841 mask = WINED3DSP_WRITEMASK_3;
4845 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4846 mask |= sample_function.coord_mask;
4847 sample_function.coord_mask = mask;
4849 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4850 else swizzle = ins->src[1].swizzle;
4852 /* 1.0-1.3: Use destination register as coordinate source.
4853 1.4+: Use provided coordinate source register. */
4854 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4856 char coord_mask[6];
4857 shader_glsl_write_mask_to_str(mask, coord_mask);
4858 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4859 "T%u%s", resource_idx, coord_mask);
4861 else
4863 struct glsl_src_param coord_param;
4864 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4865 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4867 struct glsl_src_param bias;
4868 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4869 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4870 NULL, "%s", coord_param.param_str);
4871 } else {
4872 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4873 "%s", coord_param.param_str);
4876 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4879 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4881 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4882 struct glsl_src_param coord_param, dx_param, dy_param;
4883 struct glsl_sample_function sample_function;
4884 DWORD sampler_idx;
4885 DWORD swizzle = ins->src[1].swizzle;
4887 if (!shader_glsl_has_core_grad(gl_info, &ins->ctx->shader->reg_maps.shader_version)
4888 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4890 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4891 shader_glsl_tex(ins);
4892 return;
4895 sampler_idx = ins->src[1].reg.idx[0].offset;
4897 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4898 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4899 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4900 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4902 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4903 NULL, NULL, "%s", coord_param.param_str);
4904 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4907 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4909 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
4910 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4911 struct glsl_src_param coord_param, lod_param;
4912 struct glsl_sample_function sample_function;
4913 DWORD swizzle = ins->src[1].swizzle;
4914 DWORD sampler_idx;
4916 sampler_idx = ins->src[1].reg.idx[0].offset;
4918 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4919 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4921 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4923 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info, shader_version)
4924 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4926 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4927 * However, the NVIDIA drivers allow them in fragment shaders as well,
4928 * even without the appropriate extension. */
4929 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4931 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4932 "%s", coord_param.param_str);
4933 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4936 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4937 unsigned int resource_idx, unsigned int sampler_idx)
4939 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4940 unsigned int i;
4942 for (i = 0; i < sampler_map->count; ++i)
4944 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4945 return entries[i].bind_idx;
4948 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4950 return ~0u;
4953 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
4955 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
4956 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
4957 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4958 const struct wined3d_shader_version *version = &reg_maps->shader_version;
4959 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4960 struct glsl_src_param structure_idx, offset, data, data2;
4961 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4962 enum wined3d_shader_resource_type resource_type;
4963 struct wined3d_string_buffer *address;
4964 enum wined3d_data_type data_type;
4965 unsigned int resource_idx, stride;
4966 const char *op, *resource;
4967 DWORD coord_mask;
4968 BOOL is_tgsm;
4970 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
4971 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
4972 if (is_tgsm)
4974 if (resource_idx >= reg_maps->tgsm_count)
4976 ERR("Invalid TGSM index %u.\n", resource_idx);
4977 return;
4979 resource = "g";
4980 data_type = WINED3D_DATA_UINT;
4981 coord_mask = 1;
4982 stride = reg_maps->tgsm[resource_idx].stride;
4984 else
4986 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
4988 ERR("Invalid UAV index %u.\n", resource_idx);
4989 return;
4991 resource_type = reg_maps->uav_resource_info[resource_idx].type;
4992 if (resource_type >= ARRAY_SIZE(resource_type_info))
4994 ERR("Unexpected resource type %#x.\n", resource_type);
4995 return;
4997 resource = "image";
4998 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
4999 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5000 stride = reg_maps->uav_resource_info[resource_idx].stride;
5003 switch (ins->handler_idx)
5005 case WINED3DSIH_ATOMIC_AND:
5006 case WINED3DSIH_IMM_ATOMIC_AND:
5007 if (is_tgsm)
5008 op = "atomicAnd";
5009 else
5010 op = "imageAtomicAnd";
5011 break;
5012 case WINED3DSIH_ATOMIC_CMP_STORE:
5013 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5014 if (is_tgsm)
5015 op = "atomicCompSwap";
5016 else
5017 op = "imageAtomicCompSwap";
5018 break;
5019 case WINED3DSIH_ATOMIC_IADD:
5020 case WINED3DSIH_IMM_ATOMIC_IADD:
5021 if (is_tgsm)
5022 op = "atomicAdd";
5023 else
5024 op = "imageAtomicAdd";
5025 break;
5026 case WINED3DSIH_ATOMIC_IMAX:
5027 case WINED3DSIH_IMM_ATOMIC_IMAX:
5028 if (is_tgsm)
5029 op = "atomicMax";
5030 else
5031 op = "imageAtomicMax";
5032 if (data_type != WINED3D_DATA_INT)
5034 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5035 return;
5037 break;
5038 case WINED3DSIH_ATOMIC_IMIN:
5039 case WINED3DSIH_IMM_ATOMIC_IMIN:
5040 if (is_tgsm)
5041 op = "atomicMin";
5042 else
5043 op = "imageAtomicMin";
5044 if (data_type != WINED3D_DATA_INT)
5046 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5047 return;
5049 break;
5050 case WINED3DSIH_ATOMIC_OR:
5051 case WINED3DSIH_IMM_ATOMIC_OR:
5052 if (is_tgsm)
5053 op = "atomicOr";
5054 else
5055 op = "imageAtomicOr";
5056 break;
5057 case WINED3DSIH_ATOMIC_UMAX:
5058 case WINED3DSIH_IMM_ATOMIC_UMAX:
5059 if (is_tgsm)
5060 op = "atomicMax";
5061 else
5062 op = "imageAtomicMax";
5063 if (data_type != WINED3D_DATA_UINT)
5065 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5066 return;
5068 break;
5069 case WINED3DSIH_ATOMIC_UMIN:
5070 case WINED3DSIH_IMM_ATOMIC_UMIN:
5071 if (is_tgsm)
5072 op = "atomicMin";
5073 else
5074 op = "imageAtomicMin";
5075 if (data_type != WINED3D_DATA_UINT)
5077 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5078 return;
5080 break;
5081 case WINED3DSIH_ATOMIC_XOR:
5082 case WINED3DSIH_IMM_ATOMIC_XOR:
5083 if (is_tgsm)
5084 op = "atomicXor";
5085 else
5086 op = "imageAtomicXor";
5087 break;
5088 case WINED3DSIH_IMM_ATOMIC_EXCH:
5089 if (is_tgsm)
5090 op = "atomicExchange";
5091 else
5092 op = "imageAtomicExchange";
5093 break;
5094 default:
5095 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5096 return;
5099 address = string_buffer_get(priv->string_buffers);
5100 if (stride)
5102 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5103 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5104 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5106 else
5108 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5109 string_buffer_sprintf(address, "%s", offset.param_str);
5110 if (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW)
5111 shader_addline(address, "/ 4");
5114 if (is_imm_instruction)
5115 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5117 if (is_tgsm)
5118 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5119 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5120 else
5121 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5122 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5124 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5125 shader_addline(buffer, "%s", data.param_str);
5126 if (ins->src_count >= 3)
5128 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5129 shader_addline(buffer, ", %s", data2.param_str);
5132 if (is_imm_instruction)
5133 shader_addline(buffer, ")");
5134 shader_addline(buffer, ");\n");
5136 string_buffer_release(priv->string_buffers, address);
5139 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5141 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5142 const char *op;
5144 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5145 op = "atomicCounterIncrement";
5146 else
5147 op = "atomicCounterDecrement";
5149 shader_glsl_append_dst(ins->ctx->buffer, ins);
5150 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5153 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5155 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5156 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5157 enum wined3d_shader_resource_type resource_type;
5158 struct glsl_src_param image_coord_param;
5159 enum wined3d_data_type data_type;
5160 DWORD coord_mask, write_mask;
5161 unsigned int uav_idx;
5162 char dst_swizzle[6];
5164 uav_idx = ins->src[1].reg.idx[0].offset;
5165 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5167 ERR("Invalid UAV index %u.\n", uav_idx);
5168 return;
5170 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5171 if (resource_type >= ARRAY_SIZE(resource_type_info))
5173 ERR("Unexpected resource type %#x.\n", resource_type);
5174 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5176 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5177 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5179 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5180 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5182 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5183 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5184 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5187 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5189 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5190 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5191 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5192 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5193 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5194 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5195 struct glsl_src_param structure_idx, offset;
5196 struct wined3d_string_buffer *address;
5197 struct wined3d_shader_dst_param dst;
5198 const char *function, *resource;
5200 resource_idx = src->reg.idx[0].offset;
5201 if (src->reg.type == WINED3DSPR_RESOURCE)
5203 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5205 ERR("Invalid resource index %u.\n", resource_idx);
5206 return;
5208 stride = reg_maps->resource_info[resource_idx].stride;
5209 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5210 function = "texelFetch";
5211 resource = "sampler";
5213 else if (src->reg.type == WINED3DSPR_UAV)
5215 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5217 ERR("Invalid UAV index %u.\n", resource_idx);
5218 return;
5220 stride = reg_maps->uav_resource_info[resource_idx].stride;
5221 bind_idx = resource_idx;
5222 function = "imageLoad";
5223 resource = "image";
5225 else
5227 if (resource_idx >= reg_maps->tgsm_count)
5229 ERR("Invalid TGSM index %u.\n", resource_idx);
5230 return;
5232 stride = reg_maps->tgsm[resource_idx].stride;
5233 bind_idx = resource_idx;
5234 function = NULL;
5235 resource = "g";
5238 address = string_buffer_get(priv->string_buffers);
5239 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5241 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5242 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5244 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5245 shader_addline(address, "%s / 4", offset.param_str);
5247 dst = ins->dst[0];
5248 for (i = 0; i < 4; ++i)
5250 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5251 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5252 continue;
5254 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5255 if (function)
5256 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5257 function, prefix, resource, bind_idx, address->buffer, swizzle);
5258 else
5259 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5260 prefix, resource, bind_idx, address->buffer, swizzle);
5263 string_buffer_release(priv->string_buffers, address);
5266 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5268 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5269 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5270 struct glsl_src_param image_coord_param, image_data_param;
5271 enum wined3d_shader_resource_type resource_type;
5272 enum wined3d_data_type data_type;
5273 unsigned int uav_idx;
5274 DWORD coord_mask;
5276 uav_idx = ins->dst[0].reg.idx[0].offset;
5277 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5279 ERR("Invalid UAV index %u.\n", uav_idx);
5280 return;
5282 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5283 if (resource_type >= ARRAY_SIZE(resource_type_info))
5285 ERR("Unexpected resource type %#x.\n", resource_type);
5286 return;
5288 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5289 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5291 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5292 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5293 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5294 shader_glsl_get_prefix(version->type), uav_idx,
5295 image_coord_param.param_str, image_data_param.param_str);
5298 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5300 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5301 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5302 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5303 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5304 struct glsl_src_param structure_idx, offset, data;
5305 unsigned int i, resource_idx, stride, src_idx = 0;
5306 struct wined3d_string_buffer *address;
5307 DWORD write_mask;
5308 BOOL is_tgsm;
5310 resource_idx = ins->dst[0].reg.idx[0].offset;
5311 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5312 if (is_tgsm)
5314 if (resource_idx >= reg_maps->tgsm_count)
5316 ERR("Invalid TGSM index %u.\n", resource_idx);
5317 return;
5319 stride = reg_maps->tgsm[resource_idx].stride;
5321 else
5323 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5325 ERR("Invalid UAV index %u.\n", resource_idx);
5326 return;
5328 stride = reg_maps->uav_resource_info[resource_idx].stride;
5331 address = string_buffer_get(priv->string_buffers);
5332 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5334 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5335 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5337 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5338 shader_addline(address, "%s / 4", offset.param_str);
5340 for (i = 0; i < 4; ++i)
5342 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5343 continue;
5345 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5347 if (is_tgsm)
5348 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5349 prefix, resource_idx, address->buffer, i, data.param_str);
5350 else
5351 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5352 prefix, resource_idx, address->buffer, i, data.param_str);
5355 string_buffer_release(priv->string_buffers, address);
5358 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5360 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5361 unsigned int sync_flags = ins->flags;
5363 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5365 shader_addline(buffer, "barrier();\n");
5366 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5369 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5371 shader_addline(buffer, "memoryBarrierShared();\n");
5372 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5375 if (sync_flags)
5376 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5379 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5380 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5382 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5383 unsigned int idx = reg->idx[0].offset;
5385 if (reg->type == WINED3DSPR_RESOURCE)
5387 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5389 ERR("Invalid resource index %u.\n", idx);
5390 return NULL;
5392 return &reg_maps->resource_info[idx];
5395 if (reg->type == WINED3DSPR_UAV)
5397 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5399 ERR("Invalid UAV index %u.\n", idx);
5400 return NULL;
5402 return &reg_maps->uav_resource_info[idx];
5405 FIXME("Unhandled register type %#x.\n", reg->type);
5406 return NULL;
5409 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5411 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5412 const struct wined3d_shader_resource_info *resource_info;
5413 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5414 unsigned int resource_idx;
5415 char dst_swizzle[6];
5416 DWORD write_mask;
5418 write_mask = shader_glsl_append_dst(buffer, ins);
5419 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5421 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5422 return;
5423 resource_idx = ins->src[0].reg.idx[0].offset;
5425 shader_addline(buffer, "ivec2(");
5426 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5428 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5429 resource_idx, WINED3D_SAMPLER_DEFAULT);
5430 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5432 else
5434 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5436 if (resource_info->stride)
5437 shader_addline(buffer, " / %u", resource_info->stride);
5438 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5439 shader_addline(buffer, " * 4");
5440 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5443 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5445 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5446 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5447 enum wined3d_shader_resource_type resource_type;
5448 enum wined3d_shader_register_type reg_type;
5449 unsigned int resource_idx, bind_idx, i;
5450 enum wined3d_data_type dst_data_type;
5451 struct glsl_src_param lod_param;
5452 char dst_swizzle[6];
5453 DWORD write_mask;
5455 dst_data_type = ins->dst[0].reg.data_type;
5456 if (ins->flags == WINED3DSI_RESINFO_UINT)
5457 dst_data_type = WINED3D_DATA_UINT;
5458 else if (ins->flags)
5459 FIXME("Unhandled flags %#x.\n", ins->flags);
5461 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5462 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5464 reg_type = ins->src[1].reg.type;
5465 resource_idx = ins->src[1].reg.idx[0].offset;
5466 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5467 if (reg_type == WINED3DSPR_RESOURCE)
5469 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5470 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5471 resource_idx, WINED3D_SAMPLER_DEFAULT);
5473 else
5475 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5476 bind_idx = resource_idx;
5479 if (resource_type >= ARRAY_SIZE(resource_type_info))
5481 ERR("Unexpected resource type %#x.\n", resource_type);
5482 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5485 if (dst_data_type == WINED3D_DATA_UINT)
5486 shader_addline(ins->ctx->buffer, "uvec4(");
5487 else
5488 shader_addline(ins->ctx->buffer, "vec4(");
5490 if (reg_type == WINED3DSPR_RESOURCE)
5492 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5493 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5495 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5496 shader_addline(ins->ctx->buffer, "0, ");
5498 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5500 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5501 shader_glsl_get_prefix(version->type), bind_idx);
5503 else
5505 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5506 shader_addline(ins->ctx->buffer, "1");
5509 else
5511 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5512 shader_glsl_get_prefix(version->type), bind_idx);
5514 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5515 shader_addline(ins->ctx->buffer, "0, ");
5517 /* For UAVs the returned miplevel count is always 1. */
5518 shader_addline(ins->ctx->buffer, "1");
5521 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5524 /* FIXME: The current implementation does not handle multisample textures correctly. */
5525 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5527 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5528 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5529 struct glsl_src_param coord_param, lod_param;
5530 struct glsl_sample_function sample_function;
5531 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5532 BOOL has_lod_param;
5534 if (wined3d_shader_instruction_has_texel_offset(ins))
5535 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5537 resource_idx = ins->src[1].reg.idx[0].offset;
5538 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5540 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5542 ERR("Invalid resource index %u.\n", resource_idx);
5543 return;
5545 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5547 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5548 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5549 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5550 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5551 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5552 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5553 "%s", coord_param.param_str);
5554 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5557 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5559 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5560 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5561 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5562 struct glsl_sample_function sample_function;
5563 DWORD flags = 0;
5565 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5566 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5567 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5568 flags |= WINED3D_GLSL_SAMPLE_LOD;
5569 if (wined3d_shader_instruction_has_texel_offset(ins))
5570 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5572 resource_idx = ins->src[1].reg.idx[0].offset;
5573 sampler_idx = ins->src[2].reg.idx[0].offset;
5575 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5576 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5578 switch (ins->handler_idx)
5580 case WINED3DSIH_SAMPLE:
5581 break;
5582 case WINED3DSIH_SAMPLE_B:
5583 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5584 lod_param_str = lod_param.param_str;
5585 break;
5586 case WINED3DSIH_SAMPLE_GRAD:
5587 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5588 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5589 dx_param_str = dx_param.param_str;
5590 dy_param_str = dy_param.param_str;
5591 break;
5592 case WINED3DSIH_SAMPLE_LOD:
5593 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5594 lod_param_str = lod_param.param_str;
5595 break;
5596 default:
5597 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5598 break;
5601 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5602 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5603 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5604 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5607 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5609 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5610 struct glsl_src_param coord_param, compare_param;
5611 struct glsl_sample_function sample_function;
5612 const char *lod_param = NULL;
5613 DWORD flags = 0;
5614 UINT coord_size;
5616 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5618 lod_param = "0";
5619 flags |= WINED3D_GLSL_SAMPLE_LOD;
5622 if (wined3d_shader_instruction_has_texel_offset(ins))
5623 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5625 resource_idx = ins->src[1].reg.idx[0].offset;
5626 sampler_idx = ins->src[2].reg.idx[0].offset;
5628 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5629 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5630 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5631 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5632 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5633 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5634 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5635 coord_size, coord_param.param_str, compare_param.param_str);
5636 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5639 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
5641 /* FIXME: Make this work for more than just 2D textures */
5642 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5643 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5645 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
5647 char dst_mask[6];
5649 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5650 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5651 ins->dst[0].reg.idx[0].offset, dst_mask);
5653 else
5655 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5656 DWORD reg = ins->src[0].reg.idx[0].offset;
5657 char dst_swizzle[6];
5659 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5661 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
5663 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
5664 struct glsl_src_param div_param;
5665 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
5667 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
5669 if (mask_size > 1)
5670 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
5671 else
5672 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
5674 else
5676 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
5681 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5682 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5683 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5684 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
5686 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5687 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5688 struct glsl_sample_function sample_function;
5689 struct glsl_src_param src0_param;
5690 UINT mask_size;
5692 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5694 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5695 * scalar, and projected sampling would require 4.
5697 * It is a dependent read - not valid with conditional NP2 textures
5699 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5700 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5702 switch(mask_size)
5704 case 1:
5705 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5706 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
5707 break;
5709 case 2:
5710 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5711 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
5712 break;
5714 case 3:
5715 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5716 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
5717 break;
5719 default:
5720 FIXME("Unexpected mask size %u\n", mask_size);
5721 break;
5723 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5726 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5727 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5728 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
5730 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5731 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5732 struct glsl_src_param src0_param;
5733 DWORD dst_mask;
5734 unsigned int mask_size;
5736 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5737 mask_size = shader_glsl_get_write_mask_size(dst_mask);
5738 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5740 if (mask_size > 1) {
5741 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
5742 } else {
5743 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
5747 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5748 * Calculate the depth as dst.x / dst.y */
5749 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
5751 struct glsl_dst_param dst_param;
5753 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5755 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5756 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5757 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5758 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5759 * >= 1.0 or < 0.0
5761 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5762 dst_param.reg_name, dst_param.reg_name);
5765 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5766 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5767 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5768 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5770 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
5772 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5773 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5774 struct glsl_src_param src0_param;
5776 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5778 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
5779 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5782 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5783 * Calculate the 1st of a 2-row matrix multiplication. */
5784 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
5786 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5787 DWORD reg = ins->dst[0].reg.idx[0].offset;
5788 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5789 struct glsl_src_param src0_param;
5791 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5792 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5795 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5796 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5797 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
5799 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5800 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5801 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5802 DWORD reg = ins->dst[0].reg.idx[0].offset;
5803 struct glsl_src_param src0_param;
5805 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5806 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
5807 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
5810 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
5812 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5813 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5814 struct glsl_sample_function sample_function;
5815 DWORD reg = ins->dst[0].reg.idx[0].offset;
5816 struct glsl_src_param src0_param;
5818 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5819 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5821 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5823 /* Sample the texture using the calculated coordinates */
5824 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5825 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5828 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5829 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5830 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5832 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5833 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5834 struct glsl_sample_function sample_function;
5835 DWORD reg = ins->dst[0].reg.idx[0].offset;
5836 struct glsl_src_param src0_param;
5838 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5839 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5841 /* Dependent read, not valid with conditional NP2 */
5842 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5844 /* Sample the texture using the calculated coordinates */
5845 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5846 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5848 tex_mx->current_row = 0;
5851 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5852 * Perform the 3rd row of a 3x3 matrix multiply */
5853 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5855 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5856 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5857 DWORD reg = ins->dst[0].reg.idx[0].offset;
5858 struct glsl_src_param src0_param;
5859 char dst_mask[6];
5861 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5863 shader_glsl_append_dst(ins->ctx->buffer, ins);
5864 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5865 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5867 tex_mx->current_row = 0;
5870 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5871 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5872 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5874 struct glsl_src_param src0_param;
5875 struct glsl_src_param src1_param;
5876 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5877 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5878 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5879 struct glsl_sample_function sample_function;
5880 DWORD reg = ins->dst[0].reg.idx[0].offset;
5881 char coord_mask[6];
5883 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5884 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5886 /* Perform the last matrix multiply operation */
5887 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5888 /* Reflection calculation */
5889 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5891 /* Dependent read, not valid with conditional NP2 */
5892 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5893 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5895 /* Sample the texture */
5896 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5897 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5898 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5900 tex_mx->current_row = 0;
5903 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5904 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5905 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5907 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5908 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5909 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5910 struct glsl_sample_function sample_function;
5911 DWORD reg = ins->dst[0].reg.idx[0].offset;
5912 struct glsl_src_param src0_param;
5913 char coord_mask[6];
5915 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5917 /* Perform the last matrix multiply operation */
5918 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5920 /* Construct the eye-ray vector from w coordinates */
5921 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5922 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5923 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5925 /* Dependent read, not valid with conditional NP2 */
5926 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5927 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5929 /* Sample the texture using the calculated coordinates */
5930 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5931 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5932 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5934 tex_mx->current_row = 0;
5937 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5938 * Apply a fake bump map transform.
5939 * texbem is pshader <= 1.3 only, this saves a few version checks
5941 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
5943 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5944 struct glsl_sample_function sample_function;
5945 struct glsl_src_param coord_param;
5946 DWORD sampler_idx;
5947 DWORD mask;
5948 DWORD flags;
5949 char coord_mask[6];
5951 sampler_idx = ins->dst[0].reg.idx[0].offset;
5952 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5953 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5955 /* Dependent read, not valid with conditional NP2 */
5956 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5957 mask = sample_function.coord_mask;
5959 shader_glsl_write_mask_to_str(mask, coord_mask);
5961 /* With projected textures, texbem only divides the static texture coord,
5962 * not the displacement, so we can't let GL handle this. */
5963 if (flags & WINED3D_PSARGS_PROJECTED)
5965 DWORD div_mask=0;
5966 char coord_div_mask[3];
5967 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5969 case WINED3D_TTFF_COUNT1:
5970 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5971 break;
5972 case WINED3D_TTFF_COUNT2:
5973 div_mask = WINED3DSP_WRITEMASK_1;
5974 break;
5975 case WINED3D_TTFF_COUNT3:
5976 div_mask = WINED3DSP_WRITEMASK_2;
5977 break;
5978 case WINED3D_TTFF_COUNT4:
5979 case WINED3D_TTFF_DISABLE:
5980 div_mask = WINED3DSP_WRITEMASK_3;
5981 break;
5983 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
5984 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
5987 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
5989 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5990 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
5991 coord_param.param_str, coord_mask);
5993 if (ins->handler_idx == WINED3DSIH_TEXBEML)
5995 struct glsl_src_param luminance_param;
5996 struct glsl_dst_param dst_param;
5998 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
5999 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6001 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6002 dst_param.reg_name, dst_param.mask_str,
6003 luminance_param.param_str, sampler_idx, sampler_idx);
6005 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6008 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6010 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6011 struct glsl_src_param src0_param, src1_param;
6013 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6014 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6016 shader_glsl_append_dst(ins->ctx->buffer, ins);
6017 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6018 src0_param.param_str, sampler_idx, src1_param.param_str);
6021 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6022 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6023 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6025 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6026 struct glsl_sample_function sample_function;
6027 struct glsl_src_param src0_param;
6029 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6031 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6032 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6033 "%s.wx", src0_param.reg_name);
6034 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6037 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6038 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6039 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6041 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6042 struct glsl_sample_function sample_function;
6043 struct glsl_src_param src0_param;
6045 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
6047 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6048 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6049 "%s.yz", src0_param.reg_name);
6050 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6053 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6054 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6055 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6057 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6058 struct glsl_sample_function sample_function;
6059 struct glsl_src_param src0_param;
6061 /* Dependent read, not valid with conditional NP2 */
6062 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6063 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6065 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6066 "%s", src0_param.param_str);
6067 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6070 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6071 * If any of the first 3 components are < 0, discard this pixel */
6072 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6074 if (ins->ctx->reg_maps->shader_version.major >= 4)
6076 struct glsl_src_param src_param;
6078 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
6079 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
6081 else
6083 struct glsl_dst_param dst_param;
6085 /* The argument is a destination parameter, and no writemasks are allowed */
6086 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6088 /* 2.0 shaders compare all 4 components in texkill. */
6089 if (ins->ctx->reg_maps->shader_version.major >= 2)
6090 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6091 /* 1.x shaders only compare the first 3 components, probably due to
6092 * the nature of the texkill instruction as a tex* instruction, and
6093 * phase, which kills all .w components. Even if all 4 components are
6094 * defined, only the first 3 are used. */
6095 else
6096 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6100 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6101 * dst = dot2(src0, src1) + src2 */
6102 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6104 struct glsl_src_param src0_param;
6105 struct glsl_src_param src1_param;
6106 struct glsl_src_param src2_param;
6107 DWORD write_mask;
6108 unsigned int mask_size;
6110 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6111 mask_size = shader_glsl_get_write_mask_size(write_mask);
6113 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6114 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6115 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6117 if (mask_size > 1) {
6118 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6119 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6120 } else {
6121 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6122 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6126 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6127 const struct wined3d_shader_signature *input_signature,
6128 const struct wined3d_shader_reg_maps *reg_maps,
6129 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
6131 unsigned int i;
6133 for (i = 0; i < input_signature->element_count; ++i)
6135 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6136 const char *semantic_name;
6137 UINT semantic_idx;
6138 char reg_mask[6];
6140 /* Unused */
6141 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6142 continue;
6144 semantic_name = input->semantic_name;
6145 semantic_idx = input->semantic_idx;
6146 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6148 if (args->vp_mode == vertexshader)
6150 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6152 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6153 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6155 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6157 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6159 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6161 shader_addline(buffer, "ps_in[%u] = vec4("
6162 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
6163 input->register_idx);
6165 else
6167 if (input->sysval_semantic)
6168 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6169 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
6170 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6171 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6174 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6176 if (args->pointsprite)
6177 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6178 shader->u.ps.input_reg_map[input->register_idx]);
6179 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
6180 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6181 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6182 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
6183 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6184 else
6185 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6186 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6188 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6190 if (!semantic_idx)
6191 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6192 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6193 else if (semantic_idx == 1)
6194 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6195 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6196 else
6197 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6198 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6200 else
6202 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6203 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6208 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6210 struct glsl_program_key key;
6212 key.vs_id = entry->vs.id;
6213 key.gs_id = entry->gs.id;
6214 key.ps_id = entry->ps.id;
6215 key.cs_id = entry->cs.id;
6217 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6219 ERR("Failed to insert program entry.\n");
6223 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6224 const struct glsl_program_key *key)
6226 struct wine_rb_entry *entry;
6228 entry = wine_rb_get(&priv->program_lookup, key);
6229 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6232 /* Context activation is done by the caller. */
6233 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6234 struct glsl_shader_prog_link *entry)
6236 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6238 GL_EXTCALL(glDeleteProgram(entry->id));
6239 if (entry->vs.id)
6240 list_remove(&entry->vs.shader_entry);
6241 if (entry->gs.id)
6242 list_remove(&entry->gs.shader_entry);
6243 if (entry->ps.id)
6244 list_remove(&entry->ps.shader_entry);
6245 if (entry->cs.id)
6246 list_remove(&entry->cs.shader_entry);
6247 HeapFree(GetProcessHeap(), 0, entry);
6250 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6251 const struct wined3d_gl_info *gl_info, const DWORD *map,
6252 const struct wined3d_shader_signature *input_signature,
6253 const struct wined3d_shader_reg_maps *reg_maps_in,
6254 const struct wined3d_shader_signature *output_signature,
6255 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6257 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6258 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6259 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6260 unsigned int in_count = vec4_varyings(3, gl_info);
6261 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
6262 DWORD in_idx, *set = NULL;
6263 unsigned int i, j;
6264 char reg_mask[6];
6266 set = wined3d_calloc(max_varyings, sizeof(*set));
6268 for (i = 0; i < input_signature->element_count; ++i)
6270 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6272 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6273 continue;
6275 in_idx = map[input->register_idx];
6276 /* Declared, but not read register */
6277 if (in_idx == ~0u)
6278 continue;
6279 if (in_idx >= max_varyings)
6281 FIXME("More input varyings declared than supported, expect issues.\n");
6282 continue;
6285 if (in_idx == in_count)
6286 string_buffer_sprintf(destination, "gl_FrontColor");
6287 else if (in_idx == in_count + 1)
6288 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6289 else
6290 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6292 if (!set[in_idx])
6293 set[in_idx] = ~0u;
6295 for (j = 0; j < output_signature->element_count; ++j)
6297 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6298 DWORD mask;
6300 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6301 || input->semantic_idx != output->semantic_idx
6302 || strcmp(input->semantic_name, output->semantic_name)
6303 || !(mask = input->mask & output->mask))
6304 continue;
6306 if (set[in_idx] == ~0u)
6307 set[in_idx] = 0;
6308 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6309 shader_glsl_write_mask_to_str(mask, reg_mask);
6311 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6312 destination->buffer, reg_mask, output->register_idx, reg_mask);
6316 for (i = 0; i < max_varyings; ++i)
6318 unsigned int size;
6320 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6321 continue;
6323 if (set[i] == ~0u)
6324 set[i] = 0;
6326 size = 0;
6327 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6328 reg_mask[size++] = 'x';
6329 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6330 reg_mask[size++] = 'y';
6331 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6332 reg_mask[size++] = 'z';
6333 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6334 reg_mask[size++] = 'w';
6335 reg_mask[size] = '\0';
6337 if (i == in_count)
6338 string_buffer_sprintf(destination, "gl_FrontColor");
6339 else if (i == in_count + 1)
6340 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6341 else
6342 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6344 if (size == 1)
6345 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6346 else
6347 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6350 HeapFree(GetProcessHeap(), 0, set);
6351 string_buffer_release(&priv->string_buffers, destination);
6354 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6355 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6356 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6358 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6359 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6360 char reg_mask[6];
6361 unsigned int i;
6363 for (i = 0; i < output_signature->element_count; ++i)
6365 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6367 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6368 continue;
6370 if (output->register_idx >= input_count)
6371 continue;
6373 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
6375 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6377 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6378 destination->buffer, reg_mask, output->register_idx, reg_mask);
6381 string_buffer_release(&priv->string_buffers, destination);
6384 /* Context activation is done by the caller. */
6385 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
6386 const struct wined3d_shader *vs, unsigned int input_count,
6387 const struct wined3d_gl_info *gl_info)
6389 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6390 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6392 if (legacy_context)
6393 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
6394 else
6395 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
6396 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6398 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
6399 legacy_context ? "gs_in" : "gs_in.gs_in");
6401 shader_addline(buffer, "}\n");
6404 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6405 const struct wined3d_gl_info *gl_info, const DWORD *map,
6406 const struct wined3d_shader_signature *input_signature,
6407 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6408 const struct wined3d_shader_signature *output_signature,
6409 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6411 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6412 const char *semantic_name;
6413 UINT semantic_idx;
6414 char reg_mask[6];
6415 unsigned int i;
6417 /* First, sort out position and point size system values. */
6418 for (i = 0; i < output_signature->element_count; ++i)
6420 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6422 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6423 continue;
6425 semantic_name = output->semantic_name;
6426 semantic_idx = output->semantic_idx;
6427 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6429 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6431 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6432 reg_mask, output->register_idx, reg_mask);
6434 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6436 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6437 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6439 else if (output->sysval_semantic)
6441 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6445 /* Then, setup the pixel shader input. */
6446 if (reg_maps_out->shader_version.major < 4)
6447 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6448 output_signature, reg_maps_out, "ps_link");
6449 else
6450 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
6453 /* Context activation is done by the caller. */
6454 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6455 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6456 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6458 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6459 GLuint ret;
6460 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6461 unsigned int i;
6462 const char *semantic_name;
6463 UINT semantic_idx;
6464 char reg_mask[6];
6465 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6467 string_buffer_clear(buffer);
6469 shader_glsl_add_version_declaration(buffer, gl_info, &vs->reg_maps.shader_version);
6471 if (per_vertex_point_size)
6473 shader_addline(buffer, "uniform struct\n{\n");
6474 shader_addline(buffer, " float size_min;\n");
6475 shader_addline(buffer, " float size_max;\n");
6476 shader_addline(buffer, "} ffp_point;\n");
6479 if (ps_major < 3)
6481 DWORD colors_written_mask[2] = {0};
6482 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
6484 if (!legacy_context)
6486 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
6487 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
6488 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6489 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6492 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6494 for (i = 0; i < vs->output_signature.element_count; ++i)
6496 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
6497 DWORD write_mask;
6499 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
6500 continue;
6502 semantic_name = output->semantic_name;
6503 semantic_idx = output->semantic_idx;
6504 write_mask = output->mask;
6505 shader_glsl_write_mask_to_str(write_mask, reg_mask);
6507 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
6509 if (legacy_context)
6510 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6511 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
6512 else
6513 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6514 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
6516 colors_written_mask[semantic_idx] = write_mask;
6518 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
6520 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6521 reg_mask, output->register_idx, reg_mask);
6523 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6525 if (semantic_idx < MAX_TEXTURES)
6527 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
6528 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
6529 semantic_idx, reg_mask, output->register_idx, reg_mask);
6530 texcoords_written_mask[semantic_idx] = write_mask;
6533 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6535 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6536 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6538 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6540 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6541 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6542 output->register_idx, reg_mask[1]);
6546 for (i = 0; i < 2; ++i)
6548 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6550 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6551 if (!i)
6552 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6553 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
6554 reg_mask, reg_mask);
6555 else
6556 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6557 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6558 reg_mask, reg_mask);
6561 for (i = 0; i < MAX_TEXTURES; ++i)
6563 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6564 continue;
6566 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6568 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6569 && !texcoords_written_mask[i])
6570 continue;
6572 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6573 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6574 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6578 else
6580 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6582 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
6583 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6584 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6585 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6588 shader_addline(buffer, "}\n");
6590 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6591 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6592 shader_glsl_compile(gl_info, ret, buffer->buffer);
6594 return ret;
6597 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
6598 const struct wined3d_shader *shader, unsigned int input_count,
6599 const struct wined3d_gl_info *gl_info)
6601 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6603 if (input_count)
6604 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
6606 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6607 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
6609 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
6610 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
6612 shader_addline(buffer, "}\n");
6615 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
6616 const struct wined3d_gl_info *gl_info)
6618 const char *output = get_fragment_output(gl_info);
6620 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
6621 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6622 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
6623 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
6624 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
6625 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
6628 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
6629 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
6631 const char *output = get_fragment_output(gl_info);
6633 switch (mode)
6635 case WINED3D_FFP_PS_FOG_OFF:
6636 return;
6638 case WINED3D_FFP_PS_FOG_LINEAR:
6639 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6640 break;
6642 case WINED3D_FFP_PS_FOG_EXP:
6643 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6644 break;
6646 case WINED3D_FFP_PS_FOG_EXP2:
6647 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6648 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6649 break;
6651 default:
6652 ERR("Invalid fog mode %#x.\n", mode);
6653 return;
6656 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6657 output, output);
6660 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
6661 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
6663 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6664 * flipping all the operators here, just negate the comparison below. */
6665 static const char * const comparison_operator[] =
6667 "", /* WINED3D_CMP_NEVER */
6668 "<", /* WINED3D_CMP_LESS */
6669 "==", /* WINED3D_CMP_EQUAL */
6670 "<=", /* WINED3D_CMP_LESSEQUAL */
6671 ">", /* WINED3D_CMP_GREATER */
6672 "!=", /* WINED3D_CMP_NOTEQUAL */
6673 ">=", /* WINED3D_CMP_GREATEREQUAL */
6674 "" /* WINED3D_CMP_ALWAYS */
6677 if (alpha_func == WINED3D_CMP_ALWAYS)
6678 return;
6680 if (alpha_func != WINED3D_CMP_NEVER)
6681 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
6682 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
6683 shader_addline(buffer, " discard;\n");
6686 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
6687 const struct wined3d_gl_info *gl_info)
6689 if (gl_info->supported[ARB_GPU_SHADER5])
6690 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
6691 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
6692 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
6693 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
6694 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
6695 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
6696 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
6697 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
6698 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
6699 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
6700 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
6701 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
6702 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
6703 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
6704 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
6705 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
6706 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
6707 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6708 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
6709 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
6710 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
6711 if (gl_info->supported[EXT_GPU_SHADER4])
6712 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
6713 if (gl_info->supported[EXT_TEXTURE_ARRAY])
6714 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
6717 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
6718 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6719 const struct ps_compile_args *args)
6721 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6723 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6724 if (reg_maps->shader_version.major < 2)
6725 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6727 if (args->srgb_correction)
6728 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6730 /* SM < 3 does not replace the fog stage. */
6731 if (reg_maps->shader_version.major < 3)
6732 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6734 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6737 /* Context activation is done by the caller. */
6738 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
6739 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6740 const struct wined3d_shader *shader,
6741 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
6743 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6744 const struct wined3d_gl_info *gl_info = context->gl_info;
6745 struct shader_glsl_ctx_priv priv_ctx;
6746 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6747 GLuint shader_id;
6749 memset(&priv_ctx, 0, sizeof(priv_ctx));
6750 priv_ctx.cur_ps_args = args;
6751 priv_ctx.cur_np2fixup_info = np2fixup_info;
6752 priv_ctx.string_buffers = string_buffers;
6754 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6756 shader_glsl_enable_extensions(buffer, gl_info);
6757 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
6758 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
6759 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6760 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6761 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
6762 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
6763 /* The spec says that it doesn't have to be explicitly enabled, but the
6764 * nvidia drivers write a warning if we don't do so. */
6765 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6766 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6768 /* Base Declarations */
6769 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6771 shader_addline(buffer, "void main()\n{\n");
6773 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6774 * add approximately 0.5. This causes off-by-one problems as spotted by
6775 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6776 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6777 * causes precision troubles when we just subtract 0.5.
6779 * To deal with that, just floor() the position. This will eliminate the
6780 * fraction on all cards.
6782 * TODO: Test how this behaves with multisampling.
6784 * An advantage of floor is that it works even if the driver doesn't add
6785 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6786 * to return in gl_FragCoord, even though coordinates specify the pixel
6787 * centers instead of the pixel corners. This code will behave correctly
6788 * on drivers that returns integer values. */
6789 if (reg_maps->vpos)
6791 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6792 shader_addline(buffer, "vpos = gl_FragCoord;\n");
6793 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
6794 shader_addline(buffer,
6795 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6796 else
6797 shader_addline(buffer,
6798 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6801 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
6803 unsigned int i;
6804 WORD map = reg_maps->texcoord;
6806 if (legacy_context)
6808 if (glsl_is_color_reg_read(shader, 0))
6809 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6810 if (glsl_is_color_reg_read(shader, 1))
6811 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6814 for (i = 0; map; map >>= 1, ++i)
6816 if (map & 1)
6818 if (args->pointsprite)
6819 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
6820 else if (args->texcoords_initialized & (1u << i))
6821 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
6822 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
6823 else
6824 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
6825 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
6829 if (legacy_context)
6830 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6833 /* Pack 3.0 inputs */
6834 if (reg_maps->shader_version.major >= 3)
6835 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
6837 /* Base Shader Body */
6838 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
6839 return 0;
6841 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6842 if (reg_maps->shader_version.major < 4)
6843 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
6845 shader_addline(buffer, "}\n");
6847 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6848 TRACE("Compiling shader object %u.\n", shader_id);
6849 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6851 return shader_id;
6854 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
6855 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6856 const struct vs_compile_args *args)
6858 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6859 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6860 unsigned int i;
6862 /* Unpack outputs. */
6863 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6865 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6866 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6867 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6868 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6870 if (reg_maps->shader_version.major < 3)
6872 if (args->fog_src == VS_FOG_Z)
6873 shader_addline(buffer, "%s = gl_Position.z;\n",
6874 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6875 else if (!reg_maps->fog)
6876 shader_addline(buffer, "%s = 0.0;\n",
6877 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6880 /* We always store the clipplanes without y inversion. */
6881 if (args->clip_enabled)
6883 if (legacy_context)
6884 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6885 else
6886 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6887 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6890 if (args->point_size && !args->per_vertex_point_size)
6891 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6893 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6894 shader_glsl_fixup_position(buffer);
6897 /* Context activation is done by the caller. */
6898 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6899 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6901 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6902 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6903 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6904 const struct wined3d_gl_info *gl_info = context->gl_info;
6905 struct shader_glsl_ctx_priv priv_ctx;
6906 GLuint shader_id;
6908 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6910 shader_glsl_enable_extensions(buffer, gl_info);
6911 if (gl_info->supported[ARB_DRAW_INSTANCED])
6912 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6913 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6914 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6916 memset(&priv_ctx, 0, sizeof(priv_ctx));
6917 priv_ctx.cur_vs_args = args;
6918 priv_ctx.string_buffers = string_buffers;
6920 /* Base Declarations */
6921 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6923 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6924 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6926 if (reg_maps->shader_version.major >= 4)
6928 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6929 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6930 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6931 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6934 shader_addline(buffer, "void main()\n{\n");
6936 /* Base Shader Body */
6937 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
6938 return 0;
6940 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6941 if (reg_maps->shader_version.major < 4)
6942 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
6944 shader_addline(buffer, "}\n");
6946 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6947 TRACE("Compiling shader object %u.\n", shader_id);
6948 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6950 return shader_id;
6953 /* Context activation is done by the caller. */
6954 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
6955 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
6957 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6958 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6959 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6960 const struct wined3d_gl_info *gl_info = context->gl_info;
6961 struct shader_glsl_ctx_priv priv_ctx;
6962 GLuint shader_id;
6964 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6966 shader_glsl_enable_extensions(buffer, gl_info);
6967 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
6968 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
6970 memset(&priv_ctx, 0, sizeof(priv_ctx));
6971 priv_ctx.string_buffers = string_buffers;
6972 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6973 if (!gl_info->supported[ARB_CLIP_CONTROL])
6974 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6975 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
6976 shader_addline(buffer, "void main()\n{\n");
6977 if (FAILED(shader_generate_main(shader, buffer, reg_maps, &priv_ctx)))
6978 return 0;
6979 shader_addline(buffer, "}\n");
6981 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
6982 TRACE("Compiling shader object %u.\n", shader_id);
6983 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6985 return shader_id;
6988 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
6990 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
6991 const struct wined3d_gl_info *gl_info = ctx->gl_info;
6992 const struct wined3d_shader *shader = ctx->shader;
6994 switch (shader->reg_maps.shader_version.type)
6996 case WINED3D_SHADER_TYPE_PIXEL:
6997 shader_glsl_generate_ps_epilogue(gl_info, ctx->buffer, shader, priv->cur_ps_args);
6998 break;
6999 case WINED3D_SHADER_TYPE_VERTEX:
7000 shader_glsl_generate_vs_epilogue(gl_info, ctx->buffer, shader, priv->cur_vs_args);
7001 break;
7002 case WINED3D_SHADER_TYPE_GEOMETRY:
7003 case WINED3D_SHADER_TYPE_COMPUTE:
7004 break;
7005 default:
7006 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7007 break;
7011 /* Context activation is done by the caller. */
7012 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
7013 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7014 const struct wined3d_shader *shader)
7016 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
7017 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7018 const struct wined3d_gl_info *gl_info = context->gl_info;
7019 struct shader_glsl_ctx_priv priv_ctx;
7020 GLuint shader_id;
7021 unsigned int i;
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_COMPUTE_SHADER])
7027 shader_addline(buffer, "#extension GL_ARB_compute_shader : 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);
7033 for (i = 0; i < reg_maps->tgsm_count; ++i)
7035 if (reg_maps->tgsm[i].size)
7036 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
7039 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
7040 thread_group_size->x, thread_group_size->y, thread_group_size->z);
7042 shader_addline(buffer, "void main()\n{\n");
7043 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
7044 shader_addline(buffer, "}\n");
7046 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
7047 TRACE("Compiling shader object %u.\n", shader_id);
7048 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7050 return shader_id;
7053 static GLuint find_glsl_pshader(const struct wined3d_context *context,
7054 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7055 struct wined3d_shader *shader,
7056 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
7058 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
7059 struct glsl_shader_private *shader_data;
7060 struct ps_np2fixup_info *np2fixup;
7061 UINT i;
7062 DWORD new_size;
7063 GLuint ret;
7065 if (!shader->backend_data)
7067 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
7068 if (!shader->backend_data)
7070 ERR("Failed to allocate backend data.\n");
7071 return 0;
7074 shader_data = shader->backend_data;
7075 gl_shaders = shader_data->gl_shaders.ps;
7077 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7078 * so a linear search is more performant than a hashmap or a binary search
7079 * (cache coherency etc)
7081 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7083 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
7085 if (args->np2_fixup)
7086 *np2fixup_info = &gl_shaders[i].np2fixup;
7087 return gl_shaders[i].id;
7091 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7092 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
7093 if (shader_data->num_gl_shaders)
7095 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
7096 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
7097 new_size * sizeof(*gl_shaders));
7099 else
7101 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
7102 new_size = 1;
7105 if(!new_array) {
7106 ERR("Out of memory\n");
7107 return 0;
7109 shader_data->gl_shaders.ps = new_array;
7110 shader_data->shader_array_size = new_size;
7111 gl_shaders = new_array;
7114 gl_shaders[shader_data->num_gl_shaders].args = *args;
7116 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
7117 memset(np2fixup, 0, sizeof(*np2fixup));
7118 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
7120 pixelshader_update_resource_types(shader, args->tex_types);
7122 string_buffer_clear(buffer);
7123 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
7124 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7126 return ret;
7129 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
7130 const DWORD use_map)
7132 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
7133 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
7134 if (stored->point_size != new->point_size)
7135 return FALSE;
7136 if (stored->per_vertex_point_size != new->per_vertex_point_size)
7137 return FALSE;
7138 if (stored->flatshading != new->flatshading)
7139 return FALSE;
7140 if (stored->next_shader_type != new->next_shader_type)
7141 return FALSE;
7142 if (stored->next_shader_input_count != new->next_shader_input_count)
7143 return FALSE;
7144 return stored->fog_src == new->fog_src;
7147 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
7148 struct wined3d_shader *shader, const struct vs_compile_args *args)
7150 UINT i;
7151 DWORD new_size;
7152 DWORD use_map = context->stream_info.use_map;
7153 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
7154 struct glsl_shader_private *shader_data;
7155 GLuint ret;
7157 if (!shader->backend_data)
7159 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
7160 if (!shader->backend_data)
7162 ERR("Failed to allocate backend data.\n");
7163 return 0;
7166 shader_data = shader->backend_data;
7167 gl_shaders = shader_data->gl_shaders.vs;
7169 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
7170 * so a linear search is more performant than a hashmap or a binary search
7171 * (cache coherency etc)
7173 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7175 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
7176 return gl_shaders[i].id;
7179 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7181 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
7182 if (shader_data->num_gl_shaders)
7184 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
7185 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
7186 new_size * sizeof(*gl_shaders));
7188 else
7190 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
7191 new_size = 1;
7194 if(!new_array) {
7195 ERR("Out of memory\n");
7196 return 0;
7198 shader_data->gl_shaders.vs = new_array;
7199 shader_data->shader_array_size = new_size;
7200 gl_shaders = new_array;
7203 gl_shaders[shader_data->num_gl_shaders].args = *args;
7205 string_buffer_clear(&priv->shader_buffer);
7206 ret = shader_glsl_generate_vshader(context, priv, shader, args);
7207 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7209 return ret;
7212 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
7213 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
7215 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
7216 struct glsl_shader_private *shader_data;
7217 unsigned int i, new_size;
7218 GLuint ret;
7220 if (!shader->backend_data)
7222 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7224 ERR("Failed to allocate backend data.\n");
7225 return 0;
7228 shader_data = shader->backend_data;
7229 gl_shaders = shader_data->gl_shaders.gs;
7231 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7233 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
7234 return gl_shaders[i].id;
7237 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7239 if (shader_data->num_gl_shaders)
7241 new_size = shader_data->shader_array_size + 1;
7242 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
7243 new_size * sizeof(*new_array));
7245 else
7247 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
7248 new_size = 1;
7251 if (!new_array)
7253 ERR("Failed to allocate GL shaders array.\n");
7254 return 0;
7256 shader_data->gl_shaders.gs = new_array;
7257 shader_data->shader_array_size = new_size;
7258 gl_shaders = new_array;
7260 string_buffer_clear(&priv->shader_buffer);
7261 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
7262 gl_shaders[shader_data->num_gl_shaders].args = *args;
7263 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7265 return ret;
7268 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
7269 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7270 struct wined3d_shader *shader)
7272 struct glsl_cs_compiled_shader *gl_shaders;
7273 struct glsl_shader_private *shader_data;
7274 GLuint ret;
7276 if (!shader->backend_data)
7278 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7280 ERR("Failed to allocate backend data.\n");
7281 return 0;
7284 shader_data = shader->backend_data;
7285 gl_shaders = shader_data->gl_shaders.cs;
7287 /* No shader variants are used for compute shaders. */
7288 if (shader_data->num_gl_shaders)
7289 return gl_shaders[0].id;
7291 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7293 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
7295 ERR("Failed to allocate GL shader array.\n");
7296 return 0;
7298 shader_data->shader_array_size = 1;
7299 gl_shaders = shader_data->gl_shaders.cs;
7301 string_buffer_clear(buffer);
7302 ret = shader_glsl_generate_compute_shader(context, buffer, string_buffers, shader);
7303 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7305 return ret;
7308 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
7310 switch (mcs)
7312 case WINED3D_MCS_MATERIAL:
7313 return material;
7314 case WINED3D_MCS_COLOR1:
7315 return "ffp_attrib_diffuse";
7316 case WINED3D_MCS_COLOR2:
7317 return "ffp_attrib_specular";
7318 default:
7319 ERR("Invalid material color source %#x.\n", mcs);
7320 return "<invalid>";
7324 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
7325 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
7327 const char *diffuse, *specular, *emissive, *ambient;
7328 unsigned int i, idx;
7330 if (!settings->lighting)
7332 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
7333 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
7334 return;
7337 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
7338 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
7339 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
7340 shader_addline(buffer, "vec3 dir, dst;\n");
7341 shader_addline(buffer, "float att, t;\n");
7343 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
7344 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
7345 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
7346 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
7348 idx = 0;
7349 for (i = 0; i < settings->point_light_count; ++i, ++idx)
7351 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7352 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7353 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7354 shader_addline(buffer, "dst.x = 1.0;\n");
7355 if (legacy_lighting)
7357 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7358 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7360 else
7362 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7364 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7365 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
7366 if (!legacy_lighting)
7367 shader_addline(buffer, "att = 1.0 / att;\n");
7368 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7369 if (!settings->normal)
7371 if (!legacy_lighting)
7372 shader_addline(buffer, "}\n");
7373 continue;
7375 shader_addline(buffer, "dir = normalize(dir);\n");
7376 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7377 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7378 if (settings->localviewer)
7379 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7380 else
7381 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7382 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7383 " * ffp_light[%u].specular) * att;\n", idx);
7384 if (!legacy_lighting)
7385 shader_addline(buffer, "}\n");
7388 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
7390 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7391 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7392 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7393 shader_addline(buffer, "dst.x = 1.0;\n");
7394 if (legacy_lighting)
7396 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7397 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7399 else
7401 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7403 shader_addline(buffer, "dir = normalize(dir);\n");
7404 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
7405 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
7406 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
7407 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
7408 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
7409 idx, idx, idx, idx);
7410 if (legacy_lighting)
7411 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7412 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7413 idx, idx, idx);
7414 else
7415 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7416 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7417 idx, idx, idx);
7418 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7419 if (!settings->normal)
7421 if (!legacy_lighting)
7422 shader_addline(buffer, "}\n");
7423 continue;
7425 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7426 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7427 if (settings->localviewer)
7428 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7429 else
7430 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7431 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7432 " * ffp_light[%u].specular) * att;\n", idx);
7433 if (!legacy_lighting)
7434 shader_addline(buffer, "}\n");
7437 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
7439 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7440 if (!settings->normal)
7441 continue;
7442 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
7443 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7444 " * ffp_light[%u].diffuse.xyz;\n", idx);
7445 /* TODO: In the non-local viewer case the halfvector is constant
7446 * and could be precomputed and stored in a uniform. */
7447 if (settings->localviewer)
7448 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7449 else
7450 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7451 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7452 " * ffp_light[%u].specular;\n", idx);
7455 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
7457 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7458 if (!settings->normal)
7459 continue;
7460 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
7461 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7462 " * ffp_light[%u].diffuse.xyz;\n", idx);
7463 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7464 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7465 " * ffp_light[%u].specular;\n", idx);
7468 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
7469 ambient, diffuse, emissive);
7470 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
7471 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
7474 /* Context activation is done by the caller. */
7475 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
7476 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
7478 static const struct attrib_info
7480 const char type[6];
7481 const char name[24];
7483 attrib_info[] =
7485 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
7486 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
7487 /* TODO: Indexed vertex blending */
7488 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
7489 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
7490 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
7491 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
7492 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
7494 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7495 BOOL legacy_lighting = priv->legacy_lighting;
7496 GLuint shader_obj;
7497 unsigned int i;
7498 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7499 BOOL output_legacy_fogcoord = legacy_context;
7501 string_buffer_clear(buffer);
7503 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
7505 if (shader_glsl_use_explicit_attrib_location(gl_info))
7506 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7508 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
7510 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
7512 if (shader_glsl_use_explicit_attrib_location(gl_info))
7513 shader_addline(buffer, "layout(location = %u) ", i);
7514 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
7516 shader_addline(buffer, "\n");
7518 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
7519 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
7520 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
7521 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
7523 shader_addline(buffer, "uniform struct\n{\n");
7524 shader_addline(buffer, " vec4 emissive;\n");
7525 shader_addline(buffer, " vec4 ambient;\n");
7526 shader_addline(buffer, " vec4 diffuse;\n");
7527 shader_addline(buffer, " vec4 specular;\n");
7528 shader_addline(buffer, " float shininess;\n");
7529 shader_addline(buffer, "} ffp_material;\n");
7531 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
7532 shader_addline(buffer, "uniform struct\n{\n");
7533 shader_addline(buffer, " vec4 diffuse;\n");
7534 shader_addline(buffer, " vec4 specular;\n");
7535 shader_addline(buffer, " vec4 ambient;\n");
7536 shader_addline(buffer, " vec4 position;\n");
7537 shader_addline(buffer, " vec3 direction;\n");
7538 shader_addline(buffer, " float range;\n");
7539 shader_addline(buffer, " float falloff;\n");
7540 shader_addline(buffer, " float c_att;\n");
7541 shader_addline(buffer, " float l_att;\n");
7542 shader_addline(buffer, " float q_att;\n");
7543 shader_addline(buffer, " float cos_htheta;\n");
7544 shader_addline(buffer, " float cos_hphi;\n");
7545 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
7547 if (settings->point_size)
7549 shader_addline(buffer, "uniform struct\n{\n");
7550 shader_addline(buffer, " float size;\n");
7551 shader_addline(buffer, " float size_min;\n");
7552 shader_addline(buffer, " float size_max;\n");
7553 shader_addline(buffer, " float c_att;\n");
7554 shader_addline(buffer, " float l_att;\n");
7555 shader_addline(buffer, " float q_att;\n");
7556 shader_addline(buffer, "} ffp_point;\n");
7559 if (legacy_context)
7561 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7562 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7563 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7564 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7566 else
7568 if (settings->clipping)
7569 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7571 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7572 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7573 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7574 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7577 shader_addline(buffer, "\nvoid main()\n{\n");
7578 shader_addline(buffer, "float m;\n");
7579 shader_addline(buffer, "vec3 r;\n");
7581 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
7583 if (attrib_info[i].name[0])
7584 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
7585 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
7587 for (i = 0; i < MAX_TEXTURES; ++i)
7589 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
7590 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7591 && settings->texcoords & (1u << i))
7592 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
7595 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
7597 if (settings->transformed)
7599 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7600 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7601 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7603 else
7605 for (i = 0; i < settings->vertexblends; ++i)
7606 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
7608 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
7609 for (i = 0; i < settings->vertexblends + 1; ++i)
7610 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
7612 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7613 if (settings->clipping)
7615 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7616 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
7617 else
7618 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7619 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
7621 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
7624 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
7625 if (settings->normal)
7627 if (!settings->vertexblends)
7629 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7631 else
7633 for (i = 0; i < settings->vertexblends + 1; ++i)
7634 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
7637 if (settings->normalize)
7638 shader_addline(buffer, "normal = normalize(normal);\n");
7641 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
7642 if (legacy_context)
7644 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
7645 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7647 else
7649 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7650 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7653 for (i = 0; i < MAX_TEXTURES; ++i)
7655 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7657 switch (settings->texgen[i] & 0xffff0000)
7659 case WINED3DTSS_TCI_PASSTHRU:
7660 if (settings->texcoords & (1u << i))
7661 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7662 i, i, i);
7663 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
7664 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
7665 else
7666 output_legacy_texcoord = FALSE;
7667 break;
7669 case WINED3DTSS_TCI_CAMERASPACENORMAL:
7670 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
7671 break;
7673 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
7674 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
7675 break;
7677 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
7678 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7679 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
7680 break;
7682 case WINED3DTSS_TCI_SPHEREMAP:
7683 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7684 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7685 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7686 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
7687 break;
7689 default:
7690 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
7691 break;
7693 if (output_legacy_texcoord)
7694 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
7697 switch (settings->fog_mode)
7699 case WINED3D_FFP_VS_FOG_OFF:
7700 output_legacy_fogcoord = FALSE;
7701 break;
7703 case WINED3D_FFP_VS_FOG_FOGCOORD:
7704 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7705 break;
7707 case WINED3D_FFP_VS_FOG_RANGE:
7708 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7709 break;
7711 case WINED3D_FFP_VS_FOG_DEPTH:
7712 if (settings->ortho_fog)
7714 if (gl_info->supported[ARB_CLIP_CONTROL])
7715 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
7716 else
7717 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7718 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7720 else if (settings->transformed)
7722 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
7724 else
7726 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7728 break;
7730 default:
7731 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
7732 break;
7734 if (output_legacy_fogcoord)
7735 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7737 if (settings->point_size)
7739 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7740 " + ffp_point.l_att * length(ec_pos.xyz)"
7741 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7742 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
7743 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7746 shader_addline(buffer, "}\n");
7748 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7749 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
7751 return shader_obj;
7754 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
7755 DWORD argnum, unsigned int stage, DWORD arg)
7757 const char *ret;
7759 if (arg == ARG_UNUSED)
7760 return "<unused arg>";
7762 switch (arg & WINED3DTA_SELECTMASK)
7764 case WINED3DTA_DIFFUSE:
7765 ret = "ffp_varying_diffuse";
7766 break;
7768 case WINED3DTA_CURRENT:
7769 ret = "ret";
7770 break;
7772 case WINED3DTA_TEXTURE:
7773 switch (stage)
7775 case 0: ret = "tex0"; break;
7776 case 1: ret = "tex1"; break;
7777 case 2: ret = "tex2"; break;
7778 case 3: ret = "tex3"; break;
7779 case 4: ret = "tex4"; break;
7780 case 5: ret = "tex5"; break;
7781 case 6: ret = "tex6"; break;
7782 case 7: ret = "tex7"; break;
7783 default:
7784 ret = "<invalid texture>";
7785 break;
7787 break;
7789 case WINED3DTA_TFACTOR:
7790 ret = "tex_factor";
7791 break;
7793 case WINED3DTA_SPECULAR:
7794 ret = "ffp_varying_specular";
7795 break;
7797 case WINED3DTA_TEMP:
7798 ret = "temp_reg";
7799 break;
7801 case WINED3DTA_CONSTANT:
7802 switch (stage)
7804 case 0: ret = "tss_const0"; break;
7805 case 1: ret = "tss_const1"; break;
7806 case 2: ret = "tss_const2"; break;
7807 case 3: ret = "tss_const3"; break;
7808 case 4: ret = "tss_const4"; break;
7809 case 5: ret = "tss_const5"; break;
7810 case 6: ret = "tss_const6"; break;
7811 case 7: ret = "tss_const7"; break;
7812 default:
7813 ret = "<invalid constant>";
7814 break;
7816 break;
7818 default:
7819 return "<unhandled arg>";
7822 if (arg & WINED3DTA_COMPLEMENT)
7824 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
7825 if (argnum == 0)
7826 ret = "arg0";
7827 else if (argnum == 1)
7828 ret = "arg1";
7829 else if (argnum == 2)
7830 ret = "arg2";
7833 if (arg & WINED3DTA_ALPHAREPLICATE)
7835 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
7836 if (argnum == 0)
7837 ret = "arg0";
7838 else if (argnum == 1)
7839 ret = "arg1";
7840 else if (argnum == 2)
7841 ret = "arg2";
7844 return ret;
7847 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
7848 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
7850 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
7852 if (color && alpha)
7853 dstmask = "";
7854 else if (color)
7855 dstmask = ".xyz";
7856 else
7857 dstmask = ".w";
7859 if (dst == tempreg)
7860 dstreg = "temp_reg";
7861 else
7862 dstreg = "ret";
7864 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
7865 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
7866 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
7868 switch (op)
7870 case WINED3D_TOP_DISABLE:
7871 break;
7873 case WINED3D_TOP_SELECT_ARG1:
7874 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
7875 break;
7877 case WINED3D_TOP_SELECT_ARG2:
7878 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
7879 break;
7881 case WINED3D_TOP_MODULATE:
7882 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7883 break;
7885 case WINED3D_TOP_MODULATE_4X:
7886 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7887 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7888 break;
7890 case WINED3D_TOP_MODULATE_2X:
7891 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7892 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7893 break;
7895 case WINED3D_TOP_ADD:
7896 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7897 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7898 break;
7900 case WINED3D_TOP_ADD_SIGNED:
7901 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7902 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7903 break;
7905 case WINED3D_TOP_ADD_SIGNED_2X:
7906 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7907 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7908 break;
7910 case WINED3D_TOP_SUBTRACT:
7911 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7912 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7913 break;
7915 case WINED3D_TOP_ADD_SMOOTH:
7916 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7917 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
7918 break;
7920 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
7921 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
7922 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7923 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7924 break;
7926 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7927 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7928 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7929 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7930 break;
7932 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7933 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
7934 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7935 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7936 break;
7938 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7939 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7940 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7941 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
7942 break;
7944 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
7945 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
7946 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7947 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7948 break;
7950 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
7951 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7952 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7953 break;
7955 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
7956 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7957 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7958 break;
7960 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
7961 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7962 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7963 break;
7964 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
7965 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7966 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7967 break;
7969 case WINED3D_TOP_BUMPENVMAP:
7970 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7971 /* These are handled in the first pass, nothing to do. */
7972 break;
7974 case WINED3D_TOP_DOTPRODUCT3:
7975 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7976 dstreg, dstmask, arg1, arg2, dstmask);
7977 break;
7979 case WINED3D_TOP_MULTIPLY_ADD:
7980 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7981 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
7982 break;
7984 case WINED3D_TOP_LERP:
7985 /* MSDN isn't quite right here. */
7986 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7987 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
7988 break;
7990 default:
7991 FIXME("Unhandled operation %#x.\n", op);
7992 break;
7996 /* Context activation is done by the caller. */
7997 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
7998 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
8000 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
8001 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
8002 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8003 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8004 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
8005 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
8006 UINT lowest_disabled_stage;
8007 GLuint shader_id;
8008 DWORD arg0, arg1, arg2;
8009 unsigned int stage;
8011 string_buffer_clear(buffer);
8013 /* Find out which textures are read */
8014 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8016 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8017 break;
8019 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
8020 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
8021 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
8023 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
8024 || (stage == 0 && settings->color_key_enabled))
8025 tex_map |= 1u << stage;
8026 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
8027 tfactor_used = TRUE;
8028 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
8029 tempreg_used = TRUE;
8030 if (settings->op[stage].dst == tempreg)
8031 tempreg_used = TRUE;
8032 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
8033 tss_const_map |= 1u << stage;
8035 switch (settings->op[stage].cop)
8037 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
8038 lum_map |= 1u << stage;
8039 /* fall through */
8040 case WINED3D_TOP_BUMPENVMAP:
8041 bump_map |= 1u << stage;
8042 /* fall through */
8043 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
8044 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
8045 tex_map |= 1u << stage;
8046 break;
8048 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
8049 tfactor_used = TRUE;
8050 break;
8052 default:
8053 break;
8056 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8057 continue;
8059 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
8060 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
8061 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
8063 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
8064 tex_map |= 1u << stage;
8065 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
8066 tfactor_used = TRUE;
8067 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
8068 tempreg_used = TRUE;
8069 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
8070 tss_const_map |= 1u << stage;
8072 lowest_disabled_stage = stage;
8074 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
8076 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
8077 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
8079 if (!needs_legacy_glsl_syntax(gl_info))
8080 shader_addline(buffer, "out vec4 ps_out[1];\n");
8082 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
8083 shader_addline(buffer, "vec4 ret;\n");
8084 if (tempreg_used || settings->sRGB_write)
8085 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
8086 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
8088 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8090 if (tss_const_map & (1u << stage))
8091 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
8093 if (!(tex_map & (1u << stage)))
8094 continue;
8096 switch (settings->op[stage].tex_type)
8098 case WINED3D_GL_RES_TYPE_TEX_1D:
8099 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
8100 break;
8101 case WINED3D_GL_RES_TYPE_TEX_2D:
8102 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
8103 break;
8104 case WINED3D_GL_RES_TYPE_TEX_3D:
8105 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
8106 break;
8107 case WINED3D_GL_RES_TYPE_TEX_CUBE:
8108 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
8109 break;
8110 case WINED3D_GL_RES_TYPE_TEX_RECT:
8111 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
8112 break;
8113 default:
8114 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
8115 break;
8118 shader_addline(buffer, "vec4 tex%u;\n", stage);
8120 if (!(bump_map & (1u << stage)))
8121 continue;
8122 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
8124 if (!(lum_map & (1u << stage)))
8125 continue;
8126 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
8127 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
8129 if (tfactor_used)
8130 shader_addline(buffer, "uniform vec4 tex_factor;\n");
8131 if (settings->color_key_enabled)
8132 shader_addline(buffer, "uniform vec4 color_key[2];\n");
8133 shader_addline(buffer, "uniform vec4 specular_enable;\n");
8135 if (settings->sRGB_write)
8137 shader_addline(buffer, "const vec4 srgb_const0 = ");
8138 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
8139 shader_addline(buffer, ";\n");
8140 shader_addline(buffer, "const vec4 srgb_const1 = ");
8141 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
8142 shader_addline(buffer, ";\n");
8145 shader_addline(buffer, "uniform struct\n{\n");
8146 shader_addline(buffer, " vec4 color;\n");
8147 shader_addline(buffer, " float density;\n");
8148 shader_addline(buffer, " float end;\n");
8149 shader_addline(buffer, " float scale;\n");
8150 shader_addline(buffer, "} ffp_fog;\n");
8152 if (alpha_test_func != WINED3D_CMP_ALWAYS)
8153 shader_addline(buffer, "uniform float alpha_test_ref;\n");
8155 if (legacy_context)
8157 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
8158 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
8159 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8160 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
8161 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
8163 else
8165 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
8166 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
8167 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
8168 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
8169 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8172 shader_addline(buffer, "void main()\n{\n");
8174 if (legacy_context)
8176 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
8177 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
8180 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8182 if (tex_map & (1u << stage))
8184 if (settings->pointsprite)
8185 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
8186 else if (settings->texcoords_initialized & (1u << stage))
8187 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
8188 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
8189 else
8190 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
8194 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
8195 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
8197 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
8198 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
8200 /* Generate texture sampling instructions */
8201 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
8203 const char *texture_function, *coord_mask;
8204 BOOL proj;
8206 if (!(tex_map & (1u << stage)))
8207 continue;
8209 if (settings->op[stage].projected == proj_none)
8211 proj = FALSE;
8213 else if (settings->op[stage].projected == proj_count4
8214 || settings->op[stage].projected == proj_count3)
8216 proj = TRUE;
8218 else
8220 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
8221 proj = TRUE;
8224 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
8225 proj = FALSE;
8227 switch (settings->op[stage].tex_type)
8229 case WINED3D_GL_RES_TYPE_TEX_1D:
8230 if (proj)
8232 texture_function = "texture1DProj";
8233 coord_mask = "xw";
8235 else
8237 texture_function = "texture1D";
8238 coord_mask = "x";
8240 break;
8241 case WINED3D_GL_RES_TYPE_TEX_2D:
8242 if (proj)
8244 texture_function = "texture2DProj";
8245 coord_mask = "xyw";
8247 else
8249 texture_function = "texture2D";
8250 coord_mask = "xy";
8252 break;
8253 case WINED3D_GL_RES_TYPE_TEX_3D:
8254 if (proj)
8256 texture_function = "texture3DProj";
8257 coord_mask = "xyzw";
8259 else
8261 texture_function = "texture3D";
8262 coord_mask = "xyz";
8264 break;
8265 case WINED3D_GL_RES_TYPE_TEX_CUBE:
8266 texture_function = "textureCube";
8267 coord_mask = "xyz";
8268 break;
8269 case WINED3D_GL_RES_TYPE_TEX_RECT:
8270 if (proj)
8272 texture_function = "texture2DRectProj";
8273 coord_mask = "xyw";
8275 else
8277 texture_function = "texture2DRect";
8278 coord_mask = "xy";
8280 break;
8281 default:
8282 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
8283 texture_function = "";
8284 coord_mask = "xyzw";
8285 break;
8287 if (!needs_legacy_glsl_syntax(gl_info))
8288 texture_function = proj ? "textureProj" : "texture";
8290 if (stage > 0
8291 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
8292 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
8294 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
8296 /* With projective textures, texbem only divides the static
8297 * texture coord, not the displacement, so multiply the
8298 * displacement with the dividing parameter before passing it to
8299 * TXP. */
8300 if (settings->op[stage].projected != proj_none)
8302 if (settings->op[stage].projected == proj_count4)
8304 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
8305 stage, stage);
8306 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
8308 else
8310 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
8311 stage, stage);
8312 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
8315 else
8317 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
8320 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
8321 stage, texture_function, stage, coord_mask);
8323 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8324 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
8325 stage, stage - 1, stage - 1, stage - 1);
8327 else if (settings->op[stage].projected == proj_count3)
8329 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
8330 stage, texture_function, stage, stage);
8332 else
8334 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
8335 stage, texture_function, stage, stage, coord_mask);
8338 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
8339 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
8340 settings->op[stage].color_fixup);
8343 if (settings->color_key_enabled)
8345 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
8346 shader_addline(buffer, " discard;\n");
8349 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
8351 /* Generate the main shader */
8352 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8354 BOOL op_equal;
8356 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8357 break;
8359 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8360 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8361 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
8362 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8363 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8364 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
8365 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8366 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8367 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
8368 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8369 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8370 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
8371 else
8372 op_equal = settings->op[stage].aop == settings->op[stage].cop
8373 && settings->op[stage].carg0 == settings->op[stage].aarg0
8374 && settings->op[stage].carg1 == settings->op[stage].aarg1
8375 && settings->op[stage].carg2 == settings->op[stage].aarg2;
8377 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8379 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8380 settings->op[stage].cop, settings->op[stage].carg0,
8381 settings->op[stage].carg1, settings->op[stage].carg2);
8383 else if (op_equal)
8385 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
8386 settings->op[stage].cop, settings->op[stage].carg0,
8387 settings->op[stage].carg1, settings->op[stage].carg2);
8389 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
8390 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8392 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8393 settings->op[stage].cop, settings->op[stage].carg0,
8394 settings->op[stage].carg1, settings->op[stage].carg2);
8395 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
8396 settings->op[stage].aop, settings->op[stage].aarg0,
8397 settings->op[stage].aarg1, settings->op[stage].aarg2);
8401 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
8402 get_fragment_output(gl_info));
8404 if (settings->sRGB_write)
8405 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
8407 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
8409 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
8411 shader_addline(buffer, "}\n");
8413 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8414 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8416 string_buffer_release(&priv->string_buffers, tex_reg_name);
8417 return shader_id;
8420 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
8421 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
8423 struct glsl_ffp_vertex_shader *shader;
8424 const struct wine_rb_entry *entry;
8426 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
8427 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
8429 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
8430 return NULL;
8432 shader->desc.settings = *settings;
8433 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
8434 list_init(&shader->linked_programs);
8435 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
8436 ERR("Failed to insert ffp vertex shader.\n");
8438 return shader;
8441 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
8442 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
8444 struct glsl_ffp_fragment_shader *glsl_desc;
8445 const struct ffp_frag_desc *desc;
8447 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
8448 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
8450 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
8451 return NULL;
8453 glsl_desc->entry.settings = *args;
8454 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
8455 list_init(&glsl_desc->linked_programs);
8456 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
8458 return glsl_desc;
8462 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
8463 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
8465 unsigned int i;
8466 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8468 for (i = 0; i < vs_c_count; ++i)
8470 string_buffer_sprintf(name, "vs_c[%u]", i);
8471 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8473 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
8475 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8477 string_buffer_sprintf(name, "vs_i[%u]", i);
8478 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8481 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8483 string_buffer_sprintf(name, "vs_b[%u]", i);
8484 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8487 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8489 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
8491 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
8492 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8494 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
8495 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
8496 for (i = 0; i < MAX_TEXTURES; ++i)
8498 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
8499 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8501 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
8502 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
8503 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
8504 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
8505 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
8506 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
8507 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
8509 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
8510 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8511 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
8512 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8513 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
8514 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8515 string_buffer_sprintf(name, "ffp_light[%u].position", i);
8516 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8517 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
8518 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8519 string_buffer_sprintf(name, "ffp_light[%u].range", i);
8520 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8521 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
8522 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8523 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
8524 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8525 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
8526 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8527 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
8528 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8529 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
8530 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8531 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
8532 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8534 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
8535 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
8536 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
8537 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
8538 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
8539 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
8540 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
8542 string_buffer_release(&priv->string_buffers, name);
8545 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
8546 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
8548 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8551 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
8552 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
8554 unsigned int i;
8555 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8557 for (i = 0; i < ps_c_count; ++i)
8559 string_buffer_sprintf(name, "ps_c[%u]", i);
8560 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8562 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
8564 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8566 string_buffer_sprintf(name, "ps_i[%u]", i);
8567 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8570 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8572 string_buffer_sprintf(name, "ps_b[%u]", i);
8573 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8576 for (i = 0; i < MAX_TEXTURES; ++i)
8578 string_buffer_sprintf(name, "bumpenv_mat%u", i);
8579 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8580 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
8581 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8582 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
8583 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8584 string_buffer_sprintf(name, "tss_const%u", i);
8585 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8588 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
8589 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
8591 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
8592 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
8593 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
8594 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
8596 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
8598 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
8599 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
8600 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
8602 string_buffer_release(&priv->string_buffers, name);
8605 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
8606 struct shader_glsl_priv *priv, GLuint program_id,
8607 const struct wined3d_shader_reg_maps *reg_maps)
8609 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8610 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
8611 unsigned int i, base, count;
8612 GLuint block_idx;
8614 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
8615 for (i = 0; i < count; ++i)
8617 if (!reg_maps->cb_sizes[i])
8618 continue;
8620 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
8621 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
8622 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
8624 checkGLcall("glUniformBlockBinding");
8625 string_buffer_release(&priv->string_buffers, name);
8628 /* Context activation is done by the caller. */
8629 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
8630 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8632 const struct wined3d_gl_info *gl_info = context->gl_info;
8633 unsigned int base_sampler_idx, sampler_count;
8634 struct glsl_shader_prog_link *entry = NULL;
8635 struct wined3d_shader *shader;
8636 struct glsl_program_key key;
8637 GLuint program_id, cs_id;
8639 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
8640 return;
8642 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
8644 WARN("Compute shader is NULL.\n");
8645 ctx_data->glsl_program = NULL;
8646 return;
8649 cs_id = find_glsl_compute_shader(context, &priv->shader_buffer, &priv->string_buffers, shader);
8650 memset(&key, 0, sizeof(key));
8651 key.cs_id = cs_id;
8652 if ((entry = get_glsl_program_entry(priv, &key)))
8654 ctx_data->glsl_program = entry;
8655 return;
8658 program_id = GL_EXTCALL(glCreateProgram());
8659 TRACE("Created new GLSL shader program %u.\n", program_id);
8661 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
8663 ERR("Out of memory.\n");
8664 return;
8666 entry->id = program_id;
8667 entry->vs.id = 0;
8668 entry->gs.id = 0;
8669 entry->ps.id = 0;
8670 entry->cs.id = cs_id;
8671 entry->constant_version = 0;
8672 entry->ps.np2_fixup_info = NULL;
8673 add_glsl_program_entry(priv, entry);
8675 ctx_data->glsl_program = entry;
8677 TRACE("Attaching GLSL shader object %u to program %u.\n", cs_id, program_id);
8678 GL_EXTCALL(glAttachShader(program_id, cs_id));
8679 checkGLcall("glAttachShader");
8681 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
8683 TRACE("Linking GLSL shader program %u.\n", program_id);
8684 GL_EXTCALL(glLinkProgram(program_id));
8685 shader_glsl_validate_link(gl_info, program_id);
8687 GL_EXTCALL(glUseProgram(program_id));
8688 checkGLcall("glUseProgram");
8689 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &shader->reg_maps);
8690 shader_glsl_load_icb(gl_info, priv, program_id, &shader->reg_maps);
8691 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
8692 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_COMPUTE,
8693 &base_sampler_idx, &sampler_count);
8694 shader_glsl_load_samplers(gl_info, priv, shader_glsl_get_prefix(WINED3D_SHADER_TYPE_COMPUTE),
8695 base_sampler_idx, sampler_count, NULL, program_id);
8697 entry->constant_update_mask = 0;
8700 /* Context activation is done by the caller. */
8701 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
8702 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8704 const struct wined3d_gl_info *gl_info = context->gl_info;
8705 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
8706 const struct ps_np2fixup_info *np2fixup_info = NULL;
8707 struct glsl_shader_prog_link *entry = NULL;
8708 struct wined3d_shader *vshader = NULL;
8709 struct wined3d_shader *gshader = NULL;
8710 struct wined3d_shader *pshader = NULL;
8711 GLuint reorder_shader_id = 0;
8712 struct glsl_program_key key;
8713 GLuint program_id;
8714 unsigned int i;
8715 GLuint vs_id = 0;
8716 GLuint gs_id = 0;
8717 GLuint ps_id = 0;
8718 struct list *ps_list, *vs_list;
8719 WORD attribs_map;
8720 struct wined3d_string_buffer *tmp_name;
8722 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
8724 vs_id = ctx_data->glsl_program->vs.id;
8725 vs_list = &ctx_data->glsl_program->vs.shader_entry;
8727 if (use_vs(state))
8729 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8730 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8732 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8733 && ctx_data->glsl_program->gs.id)
8735 gs_id = ctx_data->glsl_program->gs.id;
8737 else if (gshader)
8739 struct gs_compile_args args;
8741 find_gs_compile_args(state, gshader, &args);
8742 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
8746 else if (use_vs(state))
8748 struct vs_compile_args vs_compile_args;
8750 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8751 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8753 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
8754 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
8755 vs_list = &vshader->linked_programs;
8757 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8758 && ctx_data->glsl_program->gs.id)
8760 gs_id = ctx_data->glsl_program->gs.id;
8762 else if (gshader)
8764 struct gs_compile_args gs_compile_args;
8766 find_gs_compile_args(state, gshader, &gs_compile_args);
8767 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
8770 else if (priv->vertex_pipe == &glsl_vertex_pipe)
8772 struct glsl_ffp_vertex_shader *ffp_shader;
8773 struct wined3d_ffp_vs_settings settings;
8775 wined3d_ffp_get_vs_settings(context, state, &settings);
8776 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
8777 vs_id = ffp_shader->id;
8778 vs_list = &ffp_shader->linked_programs;
8781 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
8783 ps_id = ctx_data->glsl_program->ps.id;
8784 ps_list = &ctx_data->glsl_program->ps.shader_entry;
8786 if (use_ps(state))
8787 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8789 else if (use_ps(state))
8791 struct ps_compile_args ps_compile_args;
8792 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8793 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
8794 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
8795 pshader, &ps_compile_args, &np2fixup_info);
8796 ps_list = &pshader->linked_programs;
8798 else if (priv->fragment_pipe == &glsl_fragment_pipe)
8800 struct glsl_ffp_fragment_shader *ffp_shader;
8801 struct ffp_frag_settings settings;
8803 gen_ffp_frag_op(context, state, &settings, FALSE);
8804 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
8805 ps_id = ffp_shader->id;
8806 ps_list = &ffp_shader->linked_programs;
8809 key.vs_id = vs_id;
8810 key.gs_id = gs_id;
8811 key.ps_id = ps_id;
8812 key.cs_id = 0;
8813 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
8815 ctx_data->glsl_program = entry;
8816 return;
8819 /* If we get to this point, then no matching program exists, so we create one */
8820 program_id = GL_EXTCALL(glCreateProgram());
8821 TRACE("Created new GLSL shader program %u.\n", program_id);
8823 /* Create the entry */
8824 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
8825 entry->id = program_id;
8826 entry->vs.id = vs_id;
8827 entry->gs.id = gs_id;
8828 entry->ps.id = ps_id;
8829 entry->cs.id = 0;
8830 entry->constant_version = 0;
8831 entry->ps.np2_fixup_info = np2fixup_info;
8832 /* Add the hash table entry */
8833 add_glsl_program_entry(priv, entry);
8835 /* Set the current program */
8836 ctx_data->glsl_program = entry;
8838 /* Attach GLSL vshader */
8839 if (vs_id)
8841 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
8842 GL_EXTCALL(glAttachShader(program_id, vs_id));
8843 checkGLcall("glAttachShader");
8845 list_add_head(vs_list, &entry->vs.shader_entry);
8848 if (vshader)
8850 attribs_map = vshader->reg_maps.input_registers;
8851 if (vshader->reg_maps.shader_version.major < 4)
8853 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
8854 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
8855 d3d_info->emulated_flatshading
8856 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
8857 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
8858 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
8859 checkGLcall("glAttachShader");
8860 /* Flag the reorder function for deletion, it will be freed
8861 * automatically when the program is destroyed. */
8862 GL_EXTCALL(glDeleteShader(reorder_shader_id));
8865 else
8867 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
8870 if (!shader_glsl_use_explicit_attrib_location(gl_info))
8872 /* Bind vertex attributes to a corresponding index number to match
8873 * the same index numbers as ARB_vertex_programs (makes loading
8874 * vertex attributes simpler). With this method, we can use the
8875 * exact same code to load the attributes later for both ARB and
8876 * GLSL shaders.
8878 * We have to do this here because we need to know the Program ID
8879 * in order to make the bindings work, and it has to be done prior
8880 * to linking the GLSL program. */
8881 tmp_name = string_buffer_get(&priv->string_buffers);
8882 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
8884 if (!(attribs_map & 1))
8885 continue;
8887 string_buffer_sprintf(tmp_name, "vs_in%u", i);
8888 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8889 if (vshader && vshader->reg_maps.shader_version.major >= 4)
8891 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
8892 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8893 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
8894 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8897 checkGLcall("glBindAttribLocation");
8898 string_buffer_release(&priv->string_buffers, tmp_name);
8901 if (gshader)
8903 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
8904 GL_EXTCALL(glAttachShader(program_id, gs_id));
8905 checkGLcall("glAttachShader");
8907 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8909 TRACE("input type %s, output type %s, vertices out %u.\n",
8910 debug_d3dprimitivetype(gshader->u.gs.input_type),
8911 debug_d3dprimitivetype(gshader->u.gs.output_type),
8912 gshader->u.gs.vertices_out);
8913 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
8914 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
8915 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
8916 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
8917 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
8918 gshader->u.gs.vertices_out));
8919 checkGLcall("glProgramParameteriARB");
8922 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
8925 /* Attach GLSL pshader */
8926 if (ps_id)
8928 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
8929 GL_EXTCALL(glAttachShader(program_id, ps_id));
8930 checkGLcall("glAttachShader");
8932 list_add_head(ps_list, &entry->ps.shader_entry);
8935 /* Link the program */
8936 TRACE("Linking GLSL shader program %u.\n", program_id);
8937 GL_EXTCALL(glLinkProgram(program_id));
8938 shader_glsl_validate_link(gl_info, program_id);
8940 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
8941 vshader ? vshader->limits->constant_float : 0);
8942 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
8943 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
8944 pshader ? pshader->limits->constant_float : 0);
8945 checkGLcall("Find glsl program uniform locations");
8947 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8949 if (pshader && pshader->reg_maps.shader_version.major >= 3
8950 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
8952 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
8953 entry->vs.vertex_color_clamp = GL_FALSE;
8955 else
8957 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8960 else
8962 /* With core profile we never change vertex_color_clamp from
8963 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8964 * glClampColorARB(). */
8965 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8968 /* Set the shader to allow uniform loading on it */
8969 GL_EXTCALL(glUseProgram(program_id));
8970 checkGLcall("glUseProgram");
8972 /* Texture unit mapping is set up to be the same each time the shader
8973 * program is used so we can hardcode the sampler uniform values. */
8974 shader_glsl_load_graphics_samplers(gl_info, priv, context->tex_unit_map, program_id);
8976 entry->constant_update_mask = 0;
8977 if (vshader)
8979 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
8980 if (vshader->reg_maps.integer_constants)
8981 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
8982 if (vshader->reg_maps.boolean_constants)
8983 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
8984 if (entry->vs.pos_fixup_location != -1)
8985 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8987 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps);
8988 shader_glsl_load_icb(gl_info, priv, program_id, &vshader->reg_maps);
8990 else
8992 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8993 | WINED3D_SHADER_CONST_FFP_PROJ;
8995 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
8997 if (entry->vs.modelview_matrix_location[i] != -1)
8999 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9000 break;
9004 for (i = 0; i < MAX_TEXTURES; ++i)
9006 if (entry->vs.texture_matrix_location[i] != -1)
9008 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9009 break;
9012 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
9013 || entry->vs.material_specular_location != -1
9014 || entry->vs.material_emissive_location != -1
9015 || entry->vs.material_shininess_location != -1)
9016 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9017 if (entry->vs.light_ambient_location != -1)
9018 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9020 if (entry->vs.clip_planes_location != -1)
9021 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9022 if (entry->vs.pointsize_min_location != -1)
9023 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9025 if (gshader)
9027 if (entry->gs.pos_fixup_location != -1)
9028 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9029 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps);
9030 shader_glsl_load_icb(gl_info, priv, program_id, &gshader->reg_maps);
9033 if (ps_id)
9035 if (pshader)
9037 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
9038 if (pshader->reg_maps.integer_constants)
9039 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
9040 if (pshader->reg_maps.boolean_constants)
9041 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
9042 if (entry->ps.ycorrection_location != -1)
9043 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
9045 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps);
9046 shader_glsl_load_icb(gl_info, priv, program_id, &pshader->reg_maps);
9047 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
9049 else
9051 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9054 for (i = 0; i < MAX_TEXTURES; ++i)
9056 if (entry->ps.bumpenv_mat_location[i] != -1)
9058 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
9059 break;
9063 if (entry->ps.fog_color_location != -1)
9064 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9065 if (entry->ps.alpha_test_ref_location != -1)
9066 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9067 if (entry->ps.np2_fixup_location != -1)
9068 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
9069 if (entry->ps.color_key_location != -1)
9070 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9074 /* Context activation is done by the caller. */
9075 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
9076 const struct wined3d_state *state)
9078 struct glsl_context_data *ctx_data = context->shader_backend_data;
9079 const struct wined3d_gl_info *gl_info = context->gl_info;
9080 struct shader_glsl_priv *priv = shader_priv;
9081 GLenum current_vertex_color_clamp;
9082 GLuint program_id, prev_id;
9084 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
9085 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
9087 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9089 set_glsl_shader_program(context, state, priv, ctx_data);
9091 if (ctx_data->glsl_program)
9093 program_id = ctx_data->glsl_program->id;
9094 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
9096 else
9098 program_id = 0;
9099 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
9102 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
9104 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
9105 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
9107 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
9108 checkGLcall("glClampColorARB");
9110 else
9112 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
9116 TRACE("Using GLSL program %u.\n", program_id);
9118 if (prev_id != program_id)
9120 GL_EXTCALL(glUseProgram(program_id));
9121 checkGLcall("glUseProgram");
9123 if (program_id)
9124 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
9127 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
9130 /* Context activation is done by the caller. */
9131 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
9132 const struct wined3d_state *state)
9134 struct glsl_context_data *ctx_data = context->shader_backend_data;
9135 const struct wined3d_gl_info *gl_info = context->gl_info;
9136 struct shader_glsl_priv *priv = shader_priv;
9137 GLuint program_id, prev_id;
9139 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9140 set_glsl_compute_shader_program(context, state, priv, ctx_data);
9141 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
9143 TRACE("Using GLSL program %u.\n", program_id);
9145 if (prev_id != program_id)
9147 GL_EXTCALL(glUseProgram(program_id));
9148 checkGLcall("glUseProgram");
9151 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
9152 | (1u << WINED3D_SHADER_TYPE_VERTEX)
9153 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
9154 | (1u << WINED3D_SHADER_TYPE_HULL)
9155 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
9158 /* "context" is not necessarily the currently active context. */
9159 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
9161 struct glsl_context_data *ctx_data = context->shader_backend_data;
9163 ctx_data->glsl_program = NULL;
9164 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
9165 | (1u << WINED3D_SHADER_TYPE_VERTEX)
9166 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
9167 | (1u << WINED3D_SHADER_TYPE_HULL)
9168 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
9169 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
9172 /* Context activation is done by the caller. */
9173 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
9175 struct glsl_context_data *ctx_data = context->shader_backend_data;
9176 const struct wined3d_gl_info *gl_info = context->gl_info;
9177 struct shader_glsl_priv *priv = shader_priv;
9179 shader_glsl_invalidate_current_program(context);
9180 GL_EXTCALL(glUseProgram(0));
9181 checkGLcall("glUseProgram");
9183 priv->vertex_pipe->vp_enable(gl_info, FALSE);
9184 priv->fragment_pipe->enable_extension(gl_info, FALSE);
9186 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
9188 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9189 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
9190 checkGLcall("glClampColorARB");
9194 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
9195 const struct glsl_shader_prog_link *program)
9197 const struct glsl_context_data *ctx_data;
9198 struct wined3d_context *context;
9199 unsigned int i;
9201 for (i = 0; i < device->context_count; ++i)
9203 context = device->contexts[i];
9204 ctx_data = context->shader_backend_data;
9206 if (ctx_data->glsl_program == program)
9207 shader_glsl_invalidate_current_program(context);
9211 static void shader_glsl_destroy(struct wined3d_shader *shader)
9213 struct glsl_shader_private *shader_data = shader->backend_data;
9214 struct wined3d_device *device = shader->device;
9215 struct shader_glsl_priv *priv = device->shader_priv;
9216 const struct wined3d_gl_info *gl_info;
9217 const struct list *linked_programs;
9218 struct wined3d_context *context;
9220 if (!shader_data || !shader_data->num_gl_shaders)
9222 HeapFree(GetProcessHeap(), 0, shader_data);
9223 shader->backend_data = NULL;
9224 return;
9227 context = context_acquire(device, NULL, 0);
9228 gl_info = context->gl_info;
9230 TRACE("Deleting linked programs.\n");
9231 linked_programs = &shader->linked_programs;
9232 if (linked_programs->next)
9234 struct glsl_shader_prog_link *entry, *entry2;
9235 UINT i;
9237 switch (shader->reg_maps.shader_version.type)
9239 case WINED3D_SHADER_TYPE_PIXEL:
9241 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
9243 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9245 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
9246 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9247 checkGLcall("glDeleteShader");
9249 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
9251 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9252 struct glsl_shader_prog_link, ps.shader_entry)
9254 shader_glsl_invalidate_contexts_program(device, entry);
9255 delete_glsl_program_entry(priv, gl_info, entry);
9258 break;
9261 case WINED3D_SHADER_TYPE_VERTEX:
9263 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
9265 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9267 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
9268 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9269 checkGLcall("glDeleteShader");
9271 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
9273 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9274 struct glsl_shader_prog_link, vs.shader_entry)
9276 shader_glsl_invalidate_contexts_program(device, entry);
9277 delete_glsl_program_entry(priv, gl_info, entry);
9280 break;
9283 case WINED3D_SHADER_TYPE_GEOMETRY:
9285 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
9287 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9289 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
9290 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9291 checkGLcall("glDeleteShader");
9293 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
9295 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9296 struct glsl_shader_prog_link, gs.shader_entry)
9298 shader_glsl_invalidate_contexts_program(device, entry);
9299 delete_glsl_program_entry(priv, gl_info, entry);
9302 break;
9305 case WINED3D_SHADER_TYPE_COMPUTE:
9307 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
9309 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9311 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
9312 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9313 checkGLcall("glDeleteShader");
9315 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
9317 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9318 struct glsl_shader_prog_link, cs.shader_entry)
9320 shader_glsl_invalidate_contexts_program(device, entry);
9321 delete_glsl_program_entry(priv, gl_info, entry);
9324 break;
9327 default:
9328 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
9329 break;
9333 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9334 shader->backend_data = NULL;
9336 context_release(context);
9339 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
9341 const struct glsl_program_key *k = key;
9342 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
9343 const struct glsl_shader_prog_link, program_lookup_entry);
9345 if (k->vs_id > prog->vs.id) return 1;
9346 else if (k->vs_id < prog->vs.id) return -1;
9348 if (k->gs_id > prog->gs.id) return 1;
9349 else if (k->gs_id < prog->gs.id) return -1;
9351 if (k->ps_id > prog->ps.id) return 1;
9352 else if (k->ps_id < prog->ps.id) return -1;
9354 if (k->cs_id > prog->cs.id) return 1;
9355 else if (k->cs_id < prog->cs.id) return -1;
9357 return 0;
9360 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
9362 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
9363 + constant_count * sizeof(*heap->contained)
9364 + constant_count * sizeof(*heap->positions);
9365 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
9367 if (!mem)
9369 ERR("Failed to allocate memory\n");
9370 return FALSE;
9373 heap->entries = mem;
9374 heap->entries[1].version = 0;
9375 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
9376 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
9377 heap->positions = (unsigned int *)(heap->contained + constant_count);
9378 heap->size = 1;
9380 return TRUE;
9383 static void constant_heap_free(struct constant_heap *heap)
9385 HeapFree(GetProcessHeap(), 0, heap->entries);
9388 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
9389 const struct fragment_pipeline *fragment_pipe)
9391 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
9392 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
9393 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
9394 struct fragment_caps fragment_caps;
9395 void *vertex_priv, *fragment_priv;
9397 string_buffer_list_init(&priv->string_buffers);
9399 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
9401 ERR("Failed to initialize vertex pipe.\n");
9402 HeapFree(GetProcessHeap(), 0, priv);
9403 return E_FAIL;
9406 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
9408 ERR("Failed to initialize fragment pipe.\n");
9409 vertex_pipe->vp_free(device);
9410 HeapFree(GetProcessHeap(), 0, priv);
9411 return E_FAIL;
9414 if (!string_buffer_init(&priv->shader_buffer))
9416 ERR("Failed to initialize shader buffer.\n");
9417 goto fail;
9420 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
9422 ERR("Failed to allocate memory.\n");
9423 goto fail;
9426 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
9428 ERR("Failed to initialize vertex shader constant heap\n");
9429 goto fail;
9432 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
9434 ERR("Failed to initialize pixel shader constant heap\n");
9435 goto fail;
9438 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
9440 priv->next_constant_version = 1;
9441 priv->vertex_pipe = vertex_pipe;
9442 priv->fragment_pipe = fragment_pipe;
9443 fragment_pipe->get_caps(gl_info, &fragment_caps);
9444 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
9445 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
9447 device->vertex_priv = vertex_priv;
9448 device->fragment_priv = fragment_priv;
9449 device->shader_priv = priv;
9451 return WINED3D_OK;
9453 fail:
9454 constant_heap_free(&priv->pconst_heap);
9455 constant_heap_free(&priv->vconst_heap);
9456 HeapFree(GetProcessHeap(), 0, priv->stack);
9457 string_buffer_free(&priv->shader_buffer);
9458 fragment_pipe->free_private(device);
9459 vertex_pipe->vp_free(device);
9460 HeapFree(GetProcessHeap(), 0, priv);
9461 return E_OUTOFMEMORY;
9464 /* Context activation is done by the caller. */
9465 static void shader_glsl_free(struct wined3d_device *device)
9467 struct shader_glsl_priv *priv = device->shader_priv;
9469 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
9470 constant_heap_free(&priv->pconst_heap);
9471 constant_heap_free(&priv->vconst_heap);
9472 HeapFree(GetProcessHeap(), 0, priv->stack);
9473 string_buffer_list_cleanup(&priv->string_buffers);
9474 string_buffer_free(&priv->shader_buffer);
9475 priv->fragment_pipe->free_private(device);
9476 priv->vertex_pipe->vp_free(device);
9478 HeapFree(GetProcessHeap(), 0, device->shader_priv);
9479 device->shader_priv = NULL;
9482 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
9484 struct glsl_context_data *ctx_data;
9485 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
9486 return FALSE;
9487 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9488 context->shader_backend_data = ctx_data;
9489 return TRUE;
9492 static void shader_glsl_free_context_data(struct wined3d_context *context)
9494 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
9497 static void shader_glsl_init_context_state(struct wined3d_context *context)
9499 const struct wined3d_gl_info *gl_info = context->gl_info;
9501 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
9502 checkGLcall("GL_PROGRAM_POINT_SIZE");
9505 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
9507 UINT shader_model;
9509 /* FIXME: Check for the specific extensions required for SM5 support
9510 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
9511 * soon as we introduce them, adjusting the GL / GLSL version checks
9512 * accordingly. */
9513 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
9514 && gl_info->supported[ARB_COMPUTE_SHADER]
9515 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
9516 && gl_info->supported[ARB_GPU_SHADER5]
9517 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
9518 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
9519 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
9520 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
9521 shader_model = 5;
9522 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
9523 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
9524 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
9525 shader_model = 4;
9526 /* Support for texldd and texldl instructions in pixel shaders is required
9527 * for SM3. */
9528 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
9529 shader_model = 3;
9530 else
9531 shader_model = 2;
9532 TRACE("Shader model %u.\n", shader_model);
9534 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
9535 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
9536 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
9537 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
9538 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
9539 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
9541 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
9542 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
9544 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
9545 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
9546 caps->varying_count = gl_info->limits.glsl_varyings;
9548 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
9549 * Direct3D minimum requirement.
9551 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
9552 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
9554 * The problem is that the refrast clamps temporary results in the shader to
9555 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
9556 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
9557 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
9558 * offer a way to query this.
9560 if (shader_model >= 4)
9561 caps->ps_1x_max_value = FLT_MAX;
9562 else
9563 caps->ps_1x_max_value = 1024.0f;
9565 /* Ideally we'd only set caps like sRGB writes here if supported by both
9566 * the shader backend and the fragment pipe, but we can get called before
9567 * shader_glsl_alloc(). */
9568 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
9569 | WINED3D_SHADER_CAP_SRGB_WRITE;
9572 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
9574 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
9576 TRACE("Checking support for fixup:\n");
9577 dump_color_fixup_desc(fixup);
9580 /* We support everything except YUV conversions. */
9581 if (!is_complex_fixup(fixup))
9583 TRACE("[OK]\n");
9584 return TRUE;
9587 TRACE("[FAILED]\n");
9588 return FALSE;
9591 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
9593 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
9594 /* WINED3DSIH_ADD */ shader_glsl_binop,
9595 /* WINED3DSIH_AND */ shader_glsl_binop,
9596 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
9597 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
9598 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
9599 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
9600 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
9601 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
9602 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
9603 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
9604 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
9605 /* WINED3DSIH_BEM */ shader_glsl_bem,
9606 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
9607 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
9608 /* WINED3DSIH_BREAK */ shader_glsl_break,
9609 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
9610 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
9611 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
9612 /* WINED3DSIH_CALL */ shader_glsl_call,
9613 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
9614 /* WINED3DSIH_CASE */ shader_glsl_case,
9615 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
9616 /* WINED3DSIH_CND */ shader_glsl_cnd,
9617 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
9618 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
9619 /* WINED3DSIH_CRS */ shader_glsl_cross,
9620 /* WINED3DSIH_CUT */ shader_glsl_cut,
9621 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
9622 /* WINED3DSIH_DCL */ shader_glsl_nop,
9623 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
9624 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
9625 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
9626 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
9627 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
9628 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ NULL,
9629 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
9630 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
9631 /* WINED3DSIH_DCL_INDEX_RANGE */ NULL,
9632 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
9633 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
9634 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
9635 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
9636 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
9637 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
9638 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
9639 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
9640 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
9641 /* WINED3DSIH_DCL_INTERFACE */ NULL,
9642 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
9643 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
9644 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
9645 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
9646 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
9647 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
9648 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
9649 /* WINED3DSIH_DCL_STREAM */ NULL,
9650 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
9651 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
9652 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
9653 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
9654 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
9655 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
9656 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
9657 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
9658 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
9659 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
9660 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
9661 /* WINED3DSIH_DEF */ shader_glsl_nop,
9662 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
9663 /* WINED3DSIH_DEFB */ shader_glsl_nop,
9664 /* WINED3DSIH_DEFI */ shader_glsl_nop,
9665 /* WINED3DSIH_DIV */ shader_glsl_binop,
9666 /* WINED3DSIH_DP2 */ shader_glsl_dot,
9667 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
9668 /* WINED3DSIH_DP3 */ shader_glsl_dot,
9669 /* WINED3DSIH_DP4 */ shader_glsl_dot,
9670 /* WINED3DSIH_DST */ shader_glsl_dst,
9671 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
9672 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
9673 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
9674 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
9675 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
9676 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
9677 /* WINED3DSIH_ELSE */ shader_glsl_else,
9678 /* WINED3DSIH_EMIT */ shader_glsl_emit,
9679 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
9680 /* WINED3DSIH_ENDIF */ shader_glsl_end,
9681 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
9682 /* WINED3DSIH_ENDREP */ shader_glsl_end,
9683 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
9684 /* WINED3DSIH_EQ */ shader_glsl_relop,
9685 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
9686 /* WINED3DSIH_EXPP */ shader_glsl_expp,
9687 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
9688 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
9689 /* WINED3DSIH_FCALL */ NULL,
9690 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
9691 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
9692 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
9693 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
9694 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
9695 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
9696 /* WINED3DSIH_GATHER4 */ NULL,
9697 /* WINED3DSIH_GATHER4_C */ NULL,
9698 /* WINED3DSIH_GE */ shader_glsl_relop,
9699 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
9700 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
9701 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
9702 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
9703 /* WINED3DSIH_IADD */ shader_glsl_binop,
9704 /* WINED3DSIH_IEQ */ shader_glsl_relop,
9705 /* WINED3DSIH_IF */ shader_glsl_if,
9706 /* WINED3DSIH_IFC */ shader_glsl_ifc,
9707 /* WINED3DSIH_IGE */ shader_glsl_relop,
9708 /* WINED3DSIH_ILT */ shader_glsl_relop,
9709 /* WINED3DSIH_IMAD */ shader_glsl_mad,
9710 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
9711 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
9712 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
9713 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
9714 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
9715 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
9716 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
9717 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
9718 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
9719 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
9720 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
9721 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
9722 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
9723 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
9724 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
9725 /* WINED3DSIH_INE */ shader_glsl_relop,
9726 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
9727 /* WINED3DSIH_ISHL */ shader_glsl_binop,
9728 /* WINED3DSIH_ISHR */ shader_glsl_binop,
9729 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
9730 /* WINED3DSIH_LABEL */ shader_glsl_label,
9731 /* WINED3DSIH_LD */ shader_glsl_ld,
9732 /* WINED3DSIH_LD2DMS */ NULL,
9733 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
9734 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
9735 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
9736 /* WINED3DSIH_LIT */ shader_glsl_lit,
9737 /* WINED3DSIH_LOD */ NULL,
9738 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
9739 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
9740 /* WINED3DSIH_LOOP */ shader_glsl_loop,
9741 /* WINED3DSIH_LRP */ shader_glsl_lrp,
9742 /* WINED3DSIH_LT */ shader_glsl_relop,
9743 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
9744 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
9745 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
9746 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
9747 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
9748 /* WINED3DSIH_MAD */ shader_glsl_mad,
9749 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
9750 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
9751 /* WINED3DSIH_MOV */ shader_glsl_mov,
9752 /* WINED3DSIH_MOVA */ shader_glsl_mov,
9753 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
9754 /* WINED3DSIH_MUL */ shader_glsl_binop,
9755 /* WINED3DSIH_NE */ shader_glsl_relop,
9756 /* WINED3DSIH_NOP */ shader_glsl_nop,
9757 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
9758 /* WINED3DSIH_NRM */ shader_glsl_nrm,
9759 /* WINED3DSIH_OR */ shader_glsl_binop,
9760 /* WINED3DSIH_PHASE */ shader_glsl_nop,
9761 /* WINED3DSIH_POW */ shader_glsl_pow,
9762 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
9763 /* WINED3DSIH_REP */ shader_glsl_rep,
9764 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
9765 /* WINED3DSIH_RET */ shader_glsl_ret,
9766 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
9767 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
9768 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
9769 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
9770 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
9771 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
9772 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
9773 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
9774 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
9775 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
9776 /* WINED3DSIH_SAMPLE_INFO */ NULL,
9777 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
9778 /* WINED3DSIH_SAMPLE_POS */ NULL,
9779 /* WINED3DSIH_SETP */ NULL,
9780 /* WINED3DSIH_SGE */ shader_glsl_compare,
9781 /* WINED3DSIH_SGN */ shader_glsl_sgn,
9782 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
9783 /* WINED3DSIH_SLT */ shader_glsl_compare,
9784 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
9785 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
9786 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
9787 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
9788 /* WINED3DSIH_SUB */ shader_glsl_binop,
9789 /* WINED3DSIH_SWAPC */ NULL,
9790 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
9791 /* WINED3DSIH_SYNC */ shader_glsl_sync,
9792 /* WINED3DSIH_TEX */ shader_glsl_tex,
9793 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
9794 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
9795 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
9796 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
9797 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
9798 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
9799 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
9800 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
9801 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
9802 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
9803 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
9804 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
9805 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
9806 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
9807 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
9808 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
9809 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
9810 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
9811 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
9812 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
9813 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
9814 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
9815 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
9816 /* WINED3DSIH_UGE */ shader_glsl_relop,
9817 /* WINED3DSIH_ULT */ shader_glsl_relop,
9818 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
9819 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
9820 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
9821 /* WINED3DSIH_USHR */ shader_glsl_binop,
9822 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
9823 /* WINED3DSIH_XOR */ shader_glsl_binop,
9826 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
9827 SHADER_HANDLER hw_fct;
9829 /* Select handler */
9830 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
9832 /* Unhandled opcode */
9833 if (!hw_fct)
9835 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
9836 return;
9838 hw_fct(ins);
9840 shader_glsl_add_instruction_modifiers(ins);
9843 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
9845 struct shader_glsl_priv *priv = shader_priv;
9847 return priv->ffp_proj_control;
9850 const struct wined3d_shader_backend_ops glsl_shader_backend =
9852 shader_glsl_handle_instruction,
9853 shader_glsl_select,
9854 shader_glsl_select_compute,
9855 shader_glsl_disable,
9856 shader_glsl_update_float_vertex_constants,
9857 shader_glsl_update_float_pixel_constants,
9858 shader_glsl_load_constants,
9859 shader_glsl_destroy,
9860 shader_glsl_alloc,
9861 shader_glsl_free,
9862 shader_glsl_allocate_context_data,
9863 shader_glsl_free_context_data,
9864 shader_glsl_init_context_state,
9865 shader_glsl_get_caps,
9866 shader_glsl_color_fixup_supported,
9867 shader_glsl_has_ffp_proj_control,
9870 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
9872 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
9874 caps->xyzrhw = TRUE;
9875 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9876 caps->ffp_generic_attributes = TRUE;
9877 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
9878 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
9879 caps->max_vertex_blend_matrix_index = 0;
9880 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
9881 | WINED3DVTXPCAPS_MATERIALSOURCE7
9882 | WINED3DVTXPCAPS_VERTEXFOG
9883 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9884 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9885 | WINED3DVTXPCAPS_LOCALVIEWER
9886 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
9887 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
9888 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
9889 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
9892 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
9894 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9895 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9896 return 0;
9899 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9901 struct shader_glsl_priv *priv;
9903 if (shader_backend == &glsl_shader_backend)
9905 priv = shader_priv;
9906 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
9907 return priv;
9910 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9912 return NULL;
9915 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
9917 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9918 struct glsl_ffp_vertex_shader, desc.entry);
9919 struct glsl_shader_prog_link *program, *program2;
9920 struct glsl_ffp_destroy_ctx *ctx = context;
9922 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9923 struct glsl_shader_prog_link, vs.shader_entry)
9925 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9927 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9928 HeapFree(GetProcessHeap(), 0, shader);
9931 /* Context activation is done by the caller. */
9932 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
9934 struct shader_glsl_priv *priv = device->vertex_priv;
9935 struct glsl_ffp_destroy_ctx ctx;
9937 ctx.priv = priv;
9938 ctx.gl_info = &device->adapter->gl_info;
9939 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9942 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9943 const struct wined3d_state *state, DWORD state_id) {}
9945 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9946 const struct wined3d_state *state, DWORD state_id)
9948 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9951 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
9952 const struct wined3d_state *state, DWORD state_id)
9954 const struct wined3d_gl_info *gl_info = context->gl_info;
9955 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
9956 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9957 BOOL transformed = context->stream_info.position_transformed;
9958 BOOL wasrhw = context->last_was_rhw;
9959 unsigned int i;
9961 context->last_was_rhw = transformed;
9963 /* If the vertex declaration contains a transformed position attribute,
9964 * the draw uses the fixed function vertex pipeline regardless of any
9965 * vertex shader set by the application. */
9966 if (transformed != wasrhw
9967 || context->stream_info.swizzle_map != context->last_swizzle_map)
9968 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9970 context->last_swizzle_map = context->stream_info.swizzle_map;
9972 if (!use_vs(state))
9974 if (context->last_was_vshader)
9976 if (legacy_context)
9977 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9978 clipplane(context, state, STATE_CLIPPLANE(i));
9979 else
9980 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9983 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9985 /* Because of settings->texcoords, we have to regenerate the vertex
9986 * shader on a vdecl change if there aren't enough varyings to just
9987 * always output all the texture coordinates. */
9988 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
9989 || normal != context->last_was_normal)
9990 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9992 if (use_ps(state)
9993 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
9994 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
9995 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9997 else
9999 if (!context->last_was_vshader)
10001 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
10002 if (legacy_context)
10003 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
10004 clipplane(context, state, STATE_CLIPPLANE(i));
10005 else
10006 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10010 context->last_was_vshader = use_vs(state);
10011 context->last_was_normal = normal;
10014 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
10015 const struct wined3d_state *state, DWORD state_id)
10017 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10018 /* Different vertex shaders potentially require a different vertex attributes setup. */
10019 if (!isStateDirty(context, STATE_VDECL))
10020 context_apply_state(context, state, STATE_VDECL);
10023 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
10024 const struct wined3d_state *state, DWORD state_id)
10026 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
10027 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
10028 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10031 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
10032 const struct wined3d_state *state, DWORD state_id)
10034 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
10035 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
10036 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
10037 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
10038 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10041 static void glsl_vertex_pipe_world(struct wined3d_context *context,
10042 const struct wined3d_state *state, DWORD state_id)
10044 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
10047 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
10048 const struct wined3d_state *state, DWORD state_id)
10050 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10053 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
10055 const struct wined3d_gl_info *gl_info = context->gl_info;
10056 unsigned int k;
10058 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10059 | WINED3D_SHADER_CONST_FFP_LIGHTS
10060 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10062 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10064 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
10066 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
10067 clipplane(context, state, STATE_CLIPPLANE(k));
10070 else
10072 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10076 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
10077 const struct wined3d_state *state, DWORD state_id)
10079 /* Table fog behavior depends on the projection matrix. */
10080 if (state->render_states[WINED3D_RS_FOGENABLE]
10081 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
10082 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10083 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
10086 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
10087 const struct wined3d_state *state, DWORD state_id)
10089 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
10090 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
10091 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
10092 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
10093 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10094 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10097 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
10098 const struct wined3d_state *state, DWORD state_id)
10100 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10103 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
10104 const struct wined3d_state *state, DWORD state_id)
10106 DWORD sampler = state_id - STATE_SAMPLER(0);
10107 const struct wined3d_texture *texture = state->textures[sampler];
10108 BOOL np2;
10110 if (!texture)
10111 return;
10113 if (sampler >= MAX_TEXTURES)
10114 return;
10116 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
10117 || context->lastWasPow2Texture & (1u << sampler))
10119 if (np2)
10120 context->lastWasPow2Texture |= 1u << sampler;
10121 else
10122 context->lastWasPow2Texture &= ~(1u << sampler);
10124 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10128 static void glsl_vertex_pipe_material(struct wined3d_context *context,
10129 const struct wined3d_state *state, DWORD state_id)
10131 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10134 static void glsl_vertex_pipe_light(struct wined3d_context *context,
10135 const struct wined3d_state *state, DWORD state_id)
10137 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10140 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
10141 const struct wined3d_state *state, DWORD state_id)
10143 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10146 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
10147 const struct wined3d_state *state, DWORD state_id)
10149 if (!use_vs(state))
10150 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10153 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
10154 const struct wined3d_state *state, DWORD state_id)
10156 static unsigned int once;
10158 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
10159 FIXME("Non-point sprite points not supported in core profile.\n");
10162 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
10163 const struct wined3d_state *state, DWORD state_id)
10165 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
10168 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
10169 const struct wined3d_state *state, DWORD state_id)
10171 const struct wined3d_gl_info *gl_info = context->gl_info;
10172 UINT index = state_id - STATE_CLIPPLANE(0);
10174 if (index >= gl_info->limits.user_clip_distances)
10175 return;
10177 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10180 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
10182 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10183 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
10184 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
10185 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
10186 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
10187 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
10188 /* Clip planes */
10189 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10190 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10191 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10192 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10193 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10194 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10195 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10196 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10197 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10198 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10199 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10200 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10201 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10202 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10203 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10204 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10205 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10206 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10207 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10208 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10209 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10210 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10211 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10212 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10213 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10214 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10215 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10216 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10217 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10218 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10219 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10220 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10221 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10222 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10223 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10224 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10225 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10226 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10227 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10228 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10229 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10230 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10231 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10232 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10233 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10234 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10235 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10236 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10237 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10238 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10239 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10240 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10241 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10242 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10243 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10244 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10245 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10246 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10247 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10248 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10249 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10250 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10251 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10252 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10253 /* Lights */
10254 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10255 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10256 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10257 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10258 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10259 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10260 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10261 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10262 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10263 /* Viewport */
10264 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
10265 /* Transform states */
10266 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
10267 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
10268 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10269 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10270 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10271 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10272 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10273 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10274 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10275 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10276 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
10277 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10278 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10279 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10280 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10281 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10282 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10283 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10284 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10285 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10286 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10287 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10288 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10289 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10290 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10291 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10292 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10293 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10294 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10295 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10296 /* Fog */
10297 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10298 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10299 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10300 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10301 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
10302 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
10303 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10304 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10305 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10306 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10307 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10308 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10309 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10310 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10311 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10312 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10313 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10314 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
10315 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
10316 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
10317 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
10318 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
10319 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10320 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10321 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10322 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10323 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10324 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10325 /* NP2 texture matrix fixups. They are not needed if
10326 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
10327 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
10328 * matrix. */
10329 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10330 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10331 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10332 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10333 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10334 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10335 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10336 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10337 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10338 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10339 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10340 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10341 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10342 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10343 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10344 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10345 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10346 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10347 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10348 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10349 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10350 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10351 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10352 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10353 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10354 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
10355 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
10356 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
10359 /* TODO:
10360 * - Implement vertex tweening. */
10361 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
10363 glsl_vertex_pipe_vp_enable,
10364 glsl_vertex_pipe_vp_get_caps,
10365 glsl_vertex_pipe_vp_get_emul_mask,
10366 glsl_vertex_pipe_vp_alloc,
10367 glsl_vertex_pipe_vp_free,
10368 glsl_vertex_pipe_vp_states,
10371 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
10373 /* Nothing to do. */
10376 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
10378 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
10379 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
10380 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
10381 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
10382 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
10383 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
10384 | WINED3DTEXOPCAPS_SELECTARG1
10385 | WINED3DTEXOPCAPS_SELECTARG2
10386 | WINED3DTEXOPCAPS_MODULATE4X
10387 | WINED3DTEXOPCAPS_MODULATE2X
10388 | WINED3DTEXOPCAPS_MODULATE
10389 | WINED3DTEXOPCAPS_ADDSIGNED2X
10390 | WINED3DTEXOPCAPS_ADDSIGNED
10391 | WINED3DTEXOPCAPS_ADD
10392 | WINED3DTEXOPCAPS_SUBTRACT
10393 | WINED3DTEXOPCAPS_ADDSMOOTH
10394 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
10395 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
10396 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
10397 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
10398 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
10399 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
10400 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
10401 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
10402 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
10403 | WINED3DTEXOPCAPS_DOTPRODUCT3
10404 | WINED3DTEXOPCAPS_MULTIPLYADD
10405 | WINED3DTEXOPCAPS_LERP
10406 | WINED3DTEXOPCAPS_BUMPENVMAP
10407 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
10408 caps->MaxTextureBlendStages = MAX_TEXTURES;
10409 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
10412 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
10414 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10415 return GL_EXT_EMUL_ARB_MULTITEXTURE;
10416 return 0;
10419 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
10421 struct shader_glsl_priv *priv;
10423 if (shader_backend == &glsl_shader_backend)
10425 priv = shader_priv;
10426 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
10427 return priv;
10430 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
10432 return NULL;
10435 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
10437 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
10438 struct glsl_ffp_fragment_shader, entry.entry);
10439 struct glsl_shader_prog_link *program, *program2;
10440 struct glsl_ffp_destroy_ctx *ctx = context;
10442 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
10443 struct glsl_shader_prog_link, ps.shader_entry)
10445 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
10447 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
10448 HeapFree(GetProcessHeap(), 0, shader);
10451 /* Context activation is done by the caller. */
10452 static void glsl_fragment_pipe_free(struct wined3d_device *device)
10454 struct shader_glsl_priv *priv = device->fragment_priv;
10455 struct glsl_ffp_destroy_ctx ctx;
10457 ctx.priv = priv;
10458 ctx.gl_info = &device->adapter->gl_info;
10459 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
10462 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
10463 const struct wined3d_state *state, DWORD state_id)
10465 context->last_was_pshader = use_ps(state);
10467 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10470 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
10471 const struct wined3d_state *state, DWORD state_id)
10473 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10476 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
10477 const struct wined3d_state *state, DWORD state_id)
10479 BOOL use_vshader = use_vs(state);
10480 enum fogsource new_source;
10481 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
10482 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
10484 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10486 if (!state->render_states[WINED3D_RS_FOGENABLE])
10487 return;
10489 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
10491 if (use_vshader)
10492 new_source = FOGSOURCE_VS;
10493 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
10494 new_source = FOGSOURCE_COORD;
10495 else
10496 new_source = FOGSOURCE_FFP;
10498 else
10500 new_source = FOGSOURCE_FFP;
10503 if (new_source != context->fog_source || fogstart == fogend)
10505 context->fog_source = new_source;
10506 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10510 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
10511 const struct wined3d_state *state, DWORD state_id)
10513 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10514 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10515 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10517 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
10518 glsl_fragment_pipe_fog(context, state, state_id);
10521 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
10522 const struct wined3d_state *state, DWORD state_id)
10524 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10525 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10526 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10529 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
10530 const struct wined3d_state *state, DWORD state_id)
10532 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10535 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
10536 const struct wined3d_state *state, DWORD state_id)
10538 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10541 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
10542 const struct wined3d_state *state, DWORD state_id)
10544 const struct wined3d_gl_info *gl_info = context->gl_info;
10545 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
10546 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
10548 if (func)
10550 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
10551 checkGLcall("glAlphaFunc");
10555 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
10556 const struct wined3d_state *state, DWORD state_id)
10558 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10561 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
10562 const struct wined3d_state *state, DWORD state_id)
10564 const struct wined3d_gl_info *gl_info = context->gl_info;
10566 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
10568 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
10569 checkGLcall("glEnable(GL_ALPHA_TEST)");
10571 else
10573 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
10574 checkGLcall("glDisable(GL_ALPHA_TEST)");
10578 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
10579 const struct wined3d_state *state, DWORD state_id)
10581 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10584 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
10585 const struct wined3d_state *state, DWORD state_id)
10587 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10590 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
10591 const struct wined3d_state *state, DWORD state_id)
10593 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10596 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
10598 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10599 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
10600 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10601 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10602 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10603 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10604 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10605 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10606 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10607 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10608 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10609 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10610 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10611 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10612 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10613 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10614 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10615 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10616 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10617 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10618 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10619 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10620 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10621 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10622 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10623 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10624 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10625 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10626 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10627 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10628 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10629 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10630 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10631 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10632 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10633 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10634 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10635 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10636 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10637 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10638 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10639 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10640 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10641 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10642 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10643 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10644 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10645 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10646 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10647 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10648 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10649 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10650 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10651 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10652 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10653 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10654 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10655 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10656 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10657 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10658 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10659 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10660 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10661 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10662 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10663 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10664 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10665 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10666 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10667 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10668 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10669 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10670 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10671 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10672 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10673 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10674 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
10675 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
10676 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
10677 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
10678 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
10679 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
10680 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10681 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
10682 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
10683 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10684 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10685 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10686 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
10687 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
10688 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10689 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10690 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10691 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
10692 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
10693 {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 },
10694 {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 },
10695 {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 },
10696 {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 },
10697 {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 },
10698 {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 },
10699 {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 },
10700 {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 },
10701 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10702 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10703 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10704 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10705 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10706 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10707 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10708 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10709 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10710 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10711 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
10712 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
10713 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
10716 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
10718 return TRUE;
10721 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
10725 const struct fragment_pipeline glsl_fragment_pipe =
10727 glsl_fragment_pipe_enable,
10728 glsl_fragment_pipe_get_caps,
10729 glsl_fragment_pipe_get_emul_mask,
10730 glsl_fragment_pipe_alloc,
10731 glsl_fragment_pipe_free,
10732 glsl_fragment_pipe_alloc_context_data,
10733 glsl_fragment_pipe_free_context_data,
10734 shader_glsl_color_fixup_supported,
10735 glsl_fragment_pipe_state_template,